1 module tests.container;
2 
3 import std.stdio;
4 import unit_threaded;
5 import core.exception;
6 
7 import endovena;
8 import tests.utils;
9 import tests.cut;
10 
11 //10
12 @UnitTest
13 void get_service_should_return_registered_implementation() {
14    Container container = new Container;
15    container.register!(IService, Service);
16 
17    auto service = container.get!IService;
18    service.instanceof!Service.shouldBeTrue;
19 }
20 
21 //21
22 @UnitTest
23 void given_named_and_default_registerations_Get_without_name_returns_default() {
24    Container container = new Container;
25    container.register!(IService, Service);
26    container.register!(IService, AnotherService)("another");
27 
28    auto service = container.get!IService();
29    service.instanceof!Service.shouldBeTrue;
30 }
31 
32 //31
33 @UnitTest
34 void given_named_and_default_registerations_Get_with_name_should_return_correspondingly_named_service() {
35    Container container = new Container;
36    container.register!(IService, Service);
37    container.register!(IService, AnotherService)("another");
38    auto service = container.get!IService("another");
39    service.instanceof!AnotherService.shouldBeTrue;
40 }
41 
42 //45
43 @UnitTest
44 void given_two_named_registerations_Get_without_name_should_throw() {
45    Container container = new Container;
46    container.register!(IService, Service)("some");
47    container.register!(IService, Service)("another");
48    (container.get!IService()).shouldThrow!ResolveException;
49 }
50 
51 //56
52 @UnitTest
53 void resolving_singleton_twice_should_return_same_instances() {
54    Container container = new Container;
55    container.register!(IService, Service, Singleton);
56    auto one = container.get!IService;
57    auto another = container.get!IService;
58    shouldBeTrue(one is another);
59    shouldBeTrue(one == another);
60 }
61 
62 //68
63 @UnitTest
64 void get_non_registered_service_should_throw() {
65    Container container = new Container;
66    (container.get!IService()).shouldThrow!ResolveException;
67 }
68 
69 //77  void Registering_with_interface_for_service_implementation_should_throw()
70 
71 // 86
72 @UnitTest
73 void Given_no_constructor_selector_specified_in_registration_Get_implementation_with_multiple_constructors_should_throw() {
74    Container container = new Container;
75    container.register!(ServiceWithMultipleCostructors);
76    container.get!ServiceWithMultipleCostructors().shouldThrow!ResolveException;
77 }
78 
79 //96
80 @UnitTest
81 void given_registered_service_Injecting_it_as_dependency_should_work() {
82    Container container = new Container;
83    container.register!(IDependency, Dependency);
84    container.register!(ServiceWithDependency);
85    auto service = container.get!ServiceWithDependency;
86    service.shouldNotBeNull;
87 }
88 
89 //108
90 @UnitTest
91 void resolving_service_with_NON_registered_dependency_should_throw() {
92    Container container = new Container;
93    container.register!(ServiceWithDependency);
94    (container.get!ServiceWithDependency()).shouldThrow!ResolveException;
95 }
96 
97 //118 void Resolving_service_with_recursive_dependency_should_throw()
98 
99 //129
100 @UnitTest
101 void given_two_resolved_service_instances_Injected_singleton_dependency_should_be_the_same_in_both() {
102    Container container = new Container;
103    container.register!(IDependency, Dependency, Singleton);
104    container.register!(ServiceWithDependency);
105    auto one = container.get!ServiceWithDependency;
106    auto another = container.get!ServiceWithDependency;
107    shouldBeTrue(one.dependency is another.dependency);
108 }
109 
110 //155:
111 void when_resolving_service_with_two_dependencies_dependent_on_singleton_Then_same_singleton_instance_should_be_used() {
112    Container container = new Container;
113    container.register!(ServiceWithTwoParametersBothDependentOnSameService);
114    container.register!(ServiceWithDependency);
115    container.register!(AnotherServiceWithDependency);
116    container.register!(IDependency, Dependency, Singleton);
117 
118    auto service = container.get!(ServiceWithTwoParametersBothDependentOnSameService);
119    shouldBeTrue(service.one.dependency is service.another.dependency);
120 }
121 
122 //215
123 @UnitTest
124 void registering_second_default_implementation_should_throw() {
125    Container container = new Container;
126    container.register!(IService, Service);
127    container.register!(IService, AnotherService)
128       .shouldThrow!RegistrationException;
129 }
130 
131 //225
132 @UnitTest
133 void registering_service_with_duplicate_name_should_throw() {
134    Container container = new Container;
135    container.register!(IService, Service)("blah");
136    container.register!(IService, Service)("blah")
137       .shouldThrow!RegistrationException;
138 }
139 
140 // 236 not applicab
141 
142 // Not compilable
143 //@UnitTest
144 //void resolving_service_without_public_constructor_should_throw() {
145 //Container container = new Container;
146 //container.register!(ServiceWithoutPublicConstructor).shouldThrow!RegistrationException;;
147 //}
148 
149 @UnitTest
150 void given_only_named_registrations_Get_without_name_should_throw() {
151    Container container = new Container;
152    container.register!(IService, Service)("one");
153    container.register!(IService, AnotherService)("another");
154 
155    container.get!IService().shouldThrow!ResolveException;
156 }