2021-05-14 07:41:25 +00:00
|
|
|
import 'package:angel3_container/angel3_container.dart';
|
2018-10-22 15:41:59 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
void main() {
|
2021-03-18 00:21:42 +00:00
|
|
|
late Container container;
|
2018-10-22 15:41:59 +00:00
|
|
|
|
|
|
|
setUp(() {
|
2019-10-12 13:34:15 +00:00
|
|
|
container = Container(const EmptyReflector());
|
|
|
|
container.registerNamedSingleton('foo', Foo(bar: 'baz'));
|
2018-10-22 15:41:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('fetch by name', () {
|
2021-03-18 00:21:42 +00:00
|
|
|
expect(container.findByName<Foo>('foo')!.bar, 'baz');
|
2018-10-22 15:41:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('cannot redefine', () {
|
2019-10-12 13:34:15 +00:00
|
|
|
expect(() => container.registerNamedSingleton('foo', Foo(bar: 'quux')),
|
2018-10-22 15:41:59 +00:00
|
|
|
throwsStateError);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throws on unknown name', () {
|
|
|
|
expect(() => container.findByName('bar'), throwsStateError);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throws on incorrect type', () {
|
|
|
|
expect(() => container.findByName<List<String>>('foo'), throwsA(anything));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
2021-03-18 00:21:42 +00:00
|
|
|
final String? bar;
|
2018-10-22 15:41:59 +00:00
|
|
|
|
|
|
|
Foo({this.bar});
|
|
|
|
}
|