2018-08-11 19:07:35 +00:00
|
|
|
import 'package:angel_container/angel_container.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2018-08-21 13:37:52 +00:00
|
|
|
void returnVoidFromAFunction(int x) {}
|
2018-08-21 13:36:52 +00:00
|
|
|
|
2018-08-11 19:07:35 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-08-21 13:36:52 +00:00
|
|
|
test('get field', () {
|
|
|
|
var blazikenMirror = reflector.reflectInstance(blaziken);
|
2018-08-21 14:18:11 +00:00
|
|
|
expect(blazikenMirror.getField('type').reflectee, blaziken.type);
|
2018-08-21 13:36:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group('reflectFunction', () {
|
|
|
|
var mirror = reflector.reflectFunction(returnVoidFromAFunction);
|
|
|
|
|
|
|
|
test('void return type returns dynamic', () {
|
|
|
|
expect(mirror.returnType, reflector.reflectType(dynamic));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('counts parameters', () {
|
|
|
|
expect(mirror.parameters, hasLength(1));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('counts types parameters', () {
|
|
|
|
expect(mirror.typeParameters, isEmpty);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('correctly reflects parameter types', () {
|
|
|
|
var p = mirror.parameters[0];
|
|
|
|
expect(p.name, 'x');
|
|
|
|
expect(p.isRequired, true);
|
|
|
|
expect(p.isNamed, false);
|
|
|
|
expect(p.annotations, isEmpty);
|
|
|
|
expect(p.type, reflector.reflectType(int));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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 =
|
2018-08-21 14:18:20 +00:00
|
|
|
type.newInstance('changeName', [blaziken, 'Charizard']).reflectee
|
|
|
|
as Pokemon;
|
2018-08-11 19:07:35 +00:00
|
|
|
print(instance);
|
|
|
|
expect(instance.name, 'Charizard');
|
|
|
|
expect(instance.type, PokemonType.fire);
|
|
|
|
});
|
2018-08-21 14:53:37 +00:00
|
|
|
|
|
|
|
test('isAssignableTo', () {
|
|
|
|
var pokemonType = container.reflector.reflectType(Pokemon);
|
|
|
|
var kantoPokemonType = container.reflector.reflectType(KantoPokemon);
|
|
|
|
|
|
|
|
expect(kantoPokemonType.isAssignableTo(pokemonType), true);
|
|
|
|
expect(
|
|
|
|
kantoPokemonType
|
|
|
|
.isAssignableTo(container.reflector.reflectType(String)),
|
|
|
|
false);
|
|
|
|
});
|
2018-08-11 19:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:53:37 +00:00
|
|
|
class KantoPokemon extends Pokemon {
|
|
|
|
KantoPokemon(String name, PokemonType type) : super(name, type);
|
|
|
|
}
|
|
|
|
|
2018-08-11 19:07:35 +00:00
|
|
|
enum PokemonType { water, fire, grass, ice, poison, flying }
|