Add getField
to ReflectedInstance
.
This commit is contained in:
parent
6c2cbb34f1
commit
bcee87949a
5 changed files with 53 additions and 2 deletions
|
@ -1,3 +1,6 @@
|
|||
# 1.0.0-alpha.6
|
||||
* Add `getField` to `ReflectedInstance`.
|
||||
|
||||
# 1.0.0-alpha.5
|
||||
* Remove concrete type from `ReflectedTypeParameter`.
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ class MirrorsReflector implements Reflector {
|
|||
|
||||
@override
|
||||
ReflectedInstance reflectInstance(Object object) {
|
||||
return new _ReflectedInstanceMirror(object);
|
||||
return new _ReflectedInstanceMirror(dart.reflect(object));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,6 +145,13 @@ class _ReflectedClassMirror extends ReflectedClass {
|
|||
.newInstance(new Symbol(constructorName), positionalArguments)
|
||||
.reflectee as T;
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(other) {
|
||||
return other is _ReflectedClassMirror && other.mirror == mirror;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class _ReflectedDeclarationMirror extends ReflectedDeclaration {
|
||||
|
@ -172,6 +179,11 @@ class _ReflectedInstanceMirror extends ReflectedInstance {
|
|||
T invoke<T>(Invocation invocation) {
|
||||
return mirror.delegate(invocation) as T;
|
||||
}
|
||||
|
||||
@override
|
||||
T getField<T>(String name) {
|
||||
return mirror.getField(new Symbol(name)).reflectee as T;
|
||||
}
|
||||
}
|
||||
|
||||
class _ReflectedMethodMirror extends ReflectedFunction {
|
||||
|
|
|
@ -28,6 +28,8 @@ abstract class ReflectedInstance {
|
|||
other is ReflectedInstance && other.type == type && other.clazz == clazz;
|
||||
|
||||
T invoke<T>(Invocation invocation);
|
||||
|
||||
T getField<T>(String name);
|
||||
}
|
||||
|
||||
abstract class ReflectedType {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: angel_container
|
||||
version: 1.0.0-alpha.5
|
||||
version: 1.0.0-alpha.6
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
description: "A better IoC container and dependency injector for Angel, ultimately allowing Angel to be used without dart:mirrors."
|
||||
homepage: https://github.com/angel-dart/container.git
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
import 'package:angel_container/angel_container.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void returnVoidFromAFunction(int x) {
|
||||
|
||||
}
|
||||
|
||||
void testReflector(Reflector reflector) {
|
||||
var blaziken = new Pokemon('Blaziken', PokemonType.fire);
|
||||
Container container;
|
||||
|
@ -10,6 +14,36 @@ void testReflector(Reflector reflector) {
|
|||
container.registerSingleton(blaziken);
|
||||
});
|
||||
|
||||
test('get field', () {
|
||||
var blazikenMirror = reflector.reflectInstance(blaziken);
|
||||
expect(blazikenMirror.getField<PokemonType>('type'), blaziken.type);
|
||||
});
|
||||
|
||||
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));
|
||||
});
|
||||
});
|
||||
|
||||
test('make on singleton type returns singleton', () {
|
||||
expect(container.make(Pokemon), blaziken);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue