2019-04-17 02:21:51 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
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) {
|
2019-10-12 13:34:15 +00:00
|
|
|
var blaziken = Pokemon('Blaziken', PokemonType.fire);
|
2021-03-18 00:21:42 +00:00
|
|
|
late Container container;
|
2018-08-11 19:07:35 +00:00
|
|
|
|
|
|
|
setUp(() {
|
2019-10-12 13:34:15 +00:00
|
|
|
container = Container(reflector);
|
2018-08-20 04:40:30 +00:00
|
|
|
container.registerSingleton(blaziken);
|
2019-04-17 02:21:51 +00:00
|
|
|
container.registerFactory<Future<int>>((_) async => 46);
|
2018-08-11 19:07:35 +00:00
|
|
|
});
|
|
|
|
|
2018-08-21 13:36:52 +00:00
|
|
|
test('get field', () {
|
2021-03-18 00:21:42 +00:00
|
|
|
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', () {
|
2021-03-18 00:21:42 +00:00
|
|
|
expect(mirror!.returnType, reflector.reflectType(dynamic));
|
2018-08-21 13:36:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('counts parameters', () {
|
2021-03-18 00:21:42 +00:00
|
|
|
expect(mirror!.parameters, hasLength(1));
|
2018-08-21 13:36:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('counts types parameters', () {
|
2021-03-18 00:21:42 +00:00
|
|
|
expect(mirror!.typeParameters, isEmpty);
|
2018-08-21 13:36:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('correctly reflects parameter types', () {
|
2021-03-18 00:21:42 +00:00
|
|
|
var p = mirror!.parameters[0];
|
2018-08-21 13:36:52 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2019-04-17 02:21:51 +00:00
|
|
|
test('make async returns async object', () async {
|
|
|
|
expect(container.makeAsync<int>(), completion(46));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('make async returns sync object', () async {
|
|
|
|
expect(container.makeAsync<Pokemon>(), completion(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', () {
|
2021-03-18 00:21:42 +00:00
|
|
|
var lower = container.make<LowerPokemon>()!;
|
2018-08-11 19:07:35 +00:00
|
|
|
expect(lower.lowercaseName, blaziken.name.toLowerCase());
|
|
|
|
});
|
|
|
|
|
|
|
|
test('newInstance works', () {
|
2021-03-18 00:21:42 +00:00
|
|
|
var type = container.reflector.reflectType(Pokemon)!;
|
2018-08-11 19:07:35 +00:00
|
|
|
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);
|
2021-03-18 00:21:42 +00:00
|
|
|
var kantoPokemonType = container.reflector.reflectType(KantoPokemon)!;
|
2018-08-21 14:53:37 +00:00
|
|
|
|
|
|
|
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) {
|
2019-10-12 13:34:15 +00:00
|
|
|
return Pokemon(name, other.type);
|
2018-08-11 19:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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 }
|