1 module tests.usage; 2 3 import std.stdio; 4 import core.exception; 5 6 import unit_threaded; 7 8 import endovena; 9 import tests.cut; 10 11 class User { 12 string name; 13 this(string name) { 14 this.name = name; 15 } 16 } 17 18 interface IGreeter { 19 string greet(); 20 } 21 22 class Greeter: IGreeter { 23 string greet() { return "Hello"; } 24 } 25 26 class GreeterWithName: IGreeter { 27 private User user; 28 this(User x) { 29 user = x; 30 } 31 string greet() { return "Hello " ~ user.name; } 32 } 33 34 class GreeterWithMsg: IGreeter { 35 private string _msg; 36 this(string msg) { 37 _msg = msg; 38 } 39 string greet() { return _msg ~ "!"; } 40 } 41 42 @UnitTest 43 void register_with_function() { 44 Container container = new Container; 45 46 container.registerFunc!(IGreeter, Singleton)(function () => new Greeter()); 47 auto service = container.get!IGreeter(); 48 service.shouldNotBeNull; 49 service.greet.shouldEqual("Hello"); 50 } 51 52 @UnitTest 53 void register_with_function_lambda() { 54 Container container = new Container; 55 container.registerFunc!(IGreeter, Singleton)(() => new Greeter()); 56 57 auto service = container.get!IGreeter(); 58 service.shouldNotBeNull; 59 service.greet.shouldEqual("Hello"); 60 } 61 62 @UnitTest 63 void register_with_delegate() { 64 Container container = new Container; 65 container.register!(IGreeter, Singleton)(delegate() { return new Greeter(); } ); 66 auto service = container.get!IGreeter(); 67 service.shouldNotBeNull; 68 service.greet.shouldEqual("Hello"); 69 } 70 71 @UnitTest 72 void register_with_delegate_short() { 73 Container container = new Container; 74 container.register!(IGreeter, Singleton)( { return new Greeter(); } ); 75 auto service = container.get!IGreeter(); 76 service.shouldNotBeNull; 77 service.greet.shouldEqual("Hello"); 78 } 79 80 @UnitTest 81 void register_with_delegate_lambda() { 82 Container container = new Container; 83 container.register!(IGreeter, Singleton)(() => new Greeter()); 84 85 auto service = container.get!IGreeter(); 86 service.shouldNotBeNull; 87 service.greet.shouldEqual("Hello"); 88 } 89 90 @UnitTest 91 void register_with_FunctionProvider() { 92 Container container = new Container; 93 auto p = new FunctionProvider( () => new Greeter()); 94 container.register!(IGreeter, Singleton)(p); 95 96 auto service = container.get!IGreeter(); 97 service.shouldNotBeNull; 98 service.greet.shouldEqual("Hello"); 99 } 100 101 @UnitTest 102 void register_instance() { 103 Container container = new Container; 104 container.register!(GreeterWithMsg, Singleton)(new GreeterWithMsg("a")); 105 106 auto service = container.get!GreeterWithMsg(); 107 service.shouldNotBeNull; 108 service.greet.shouldEqual("a!"); 109 } 110 111 @UnitTest 112 void register_with_InstanceProvider() { 113 Container container = new Container; 114 auto p = new InstanceProvider(new Greeter()); 115 container.register!(IGreeter, Singleton)(p); 116 117 auto service = container.get!IGreeter(); 118 service.shouldNotBeNull; 119 service.greet.shouldEqual("Hello"); 120 } 121 122 @UnitTest 123 void given_service_with_ctor_Register_with_InstanceProvider_should_work() { 124 Container container = new Container; 125 auto p = new InstanceProvider(new GreeterWithMsg("a")); 126 container.register!(IGreeter, Singleton)(p); 127 128 auto service = container.get!IGreeter(); 129 service.shouldNotBeNull; 130 service.greet.shouldEqual("a!"); 131 } 132 133 @UnitTest 134 void given_service_with_ctor_Register_instance_should_work() { 135 Container container = new Container; 136 auto g = new GreeterWithMsg("a"); 137 auto p = new InstanceProvider(g); 138 container.register!(IGreeter, Singleton)(p); 139 140 auto service = container.get!IGreeter(); 141 service.shouldNotBeNull; 142 service.greet.shouldEqual("a!"); 143 } 144 145 @UnitTest 146 void given_service_registred_by_instance_Get_interface_should_throw() { 147 Container container = new Container; 148 container.register!Greeter; 149 auto service = container.get!Greeter(); 150 service.shouldNotBeNull; 151 container.get!IGreeter.shouldThrow!ResolveException; 152 } 153 154 @UnitTest 155 void given_services_registred_by_instance_and_interface_Get_should_works() { 156 Container container = new Container; 157 container.register!Greeter; 158 container.register!(IGreeter,Greeter); 159 160 auto service = container.get!Greeter(); 161 service.shouldNotBeNull; 162 auto iservice = container.get!IGreeter(); 163 iservice.shouldNotBeNull; 164 assert(iservice !is service); 165 } 166 167 168 ///mvc 169 170 import std.signals; 171 class Model { 172 private int _value; 173 @property int value() { return _value; } 174 @property void value(int value) { 175 if (_value != value) { 176 _value = value; 177 modelChanged.emit(); 178 } 179 } 180 void inc() { 181 writeln("inc from ", _value); 182 183 this.value = this.value + 1; 184 } 185 186 mixin Signal modelChanged; 187 } 188 189 import std.stdio; 190 class Controller { 191 private Model m; 192 this(Model m) { 193 this.m = m; 194 } 195 196 void addOne() { 197 writefln("model curr value %s", m.value); 198 199 m.inc(); 200 } 201 } 202 203 class View { 204 private Model m; 205 private Controller ctrl; 206 this(Model m) { 207 this.m = m; 208 ctrl = new Controller(m); 209 m.modelChanged.connect(¬ify); 210 } 211 212 void mouseReleasEvent() { 213 ctrl.addOne(); 214 } 215 216 void notify() { 217 _calls++; 218 } 219 220 private int _calls; 221 @property int calls() { return _calls; } 222 } 223 224 @UnitTest 225 void should_create_mvv() { 226 Container container = new Container; 227 container.register!(Model, Singleton); 228 container.register!View; 229 230 auto v = container.get!View(); 231 v.shouldNotBeNull; 232 v.calls.shouldEqual(0); 233 234 auto m = container.get!Model(); 235 m.shouldNotBeNull; 236 m.value = 42; // genera un evenot 237 v.calls.shouldEqual(1); 238 m.value.shouldEqual(42); 239 240 v.mouseReleasEvent(); 241 m.value.shouldEqual(43); 242 v.calls.shouldEqual(2); 243 244 m.inc(); 245 v.calls.shouldEqual(3); 246 }