platform/angel_container/test/common.dart

64 lines
1.6 KiB
Dart
Raw Normal View History

2018-08-11 19:07:35 +00:00
import 'package:angel_container/angel_container.dart';
import 'package:test/test.dart';
void testReflector(Reflector reflector) {
var blaziken = new Pokemon('Blaziken', PokemonType.fire);
Container container;
setUp(() {
container = new Container(reflector);
2018-08-20 04:40:30 +00:00
container.registerSingleton(blaziken);
2018-08-11 19:07:35 +00:00
});
test('make on singleton type returns singleton', () {
expect(container.make(Pokemon), blaziken);
});
2018-08-20 04:40:30 +00:00
test('make with generic returns same as make with explicit type', () {
expect(container.make<Pokemon>(), blaziken);
});
2018-08-11 19:07:35 +00:00
test('make on aliased singleton returns singleton', () {
2018-08-20 04:40:30 +00:00
container.registerSingleton(blaziken, as: StateError);
2018-08-11 19:07:35 +00:00
expect(container.make(StateError), blaziken);
});
test('constructor injects singleton', () {
2018-08-20 04:40:30 +00:00
var lower = container.make<LowerPokemon>();
2018-08-11 19:07:35 +00:00
expect(lower.lowercaseName, blaziken.name.toLowerCase());
});
test('newInstance works', () {
var type = container.reflector.reflectType(Pokemon);
var instance =
type.newInstance('changeName', [blaziken, 'Charizard']) as Pokemon;
print(instance);
expect(instance.name, 'Charizard');
expect(instance.type, PokemonType.fire);
});
}
class LowerPokemon {
final Pokemon pokemon;
LowerPokemon(this.pokemon);
String get lowercaseName => pokemon.name.toLowerCase();
}
class Pokemon {
final String name;
final PokemonType type;
Pokemon(this.name, this.type);
factory Pokemon.changeName(Pokemon other, String name) {
return new Pokemon(name, other.type);
}
@override
String toString() => 'NAME: $name, TYPE: $type';
}
enum PokemonType { water, fire, grass, ice, poison, flying }