-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test.hx
198 lines (144 loc) · 4.62 KB
/
Test.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
typedef Person={
name:String,
surname:String
}
class TestClassList{
public var title:String;
public var collection:Array<String>;
public var user:Person;
public var sum:Float->Float->Float;
public var sum10:Float->Float;
public function new() {}
}
class TestPropertyList{
public var x(getX,setX) : Int;
private var my_x : Int;
private function getX():Int {
return my_x;
}
private function setX( v : Int ):Int {
if( v >= 0 )
my_x = v;
return my_x;
}
public function new() {}
}
class TestPropertyAnnotation{
public var x(getX,setX) : Int;
@inject("aNumber")
private var my_x : Int;
private function getX():Int {
return my_x;
}
private function setX( v : Int ):Int {
if( v >= 0 )
my_x = v;
return my_x;
}
public function new(container:syringo.Container) {
syringo.Injector.injectByAnnotation(this,container);
}
}
class TestClassAnnotations {
@inject("title")
public var title:String;
@inject("list")
public var collection:Array<String>;
@inject("person")
public var user:Person;
@inject("sum")
public var sum:Float->Float->Float;
@inject("sum10")
public var sum10:Float->Float;
public function new(container:syringo.Container) {
syringo.Injector.injectByAnnotation(this,container);
}
}
class Test extends haxe.unit.TestCase {
var container: syringo.Container;
public function checkObjectTest(object:Dynamic) {
assertEquals(object.title,"titolo");
assertEquals(object.user.name,"Mario");
assertEquals(object.collection.length,3);
assertEquals(object.sum(1,1),2);
assertEquals(object.sum10(100),110);
}
override public function setup() {
container=new syringo.Container();
container.setObject("title", "titolo");
container.set("list",function(cont) {
var list=new Array();
list.push(cont.get("title"));
list.push(cont.get("title"));
list.push(cont.get("title"));
return list;
});
container.setObject("aNumber",999);
container.set("person",function(cont):Person {
return {
name:"Mario",
surname:"Rossi"
}
});
//define a function in container
container.set("sum",function(cont) {
return function (a,b) {
return a+b;
};
});
//define a function with closure
container.set("sum10",function(cont) {
var accumulator:Int=10;
return function(a) {
return accumulator+a;
};
});
}
public function testContainerValues(){
assertEquals( container.get("title"), "titolo" );
assertEquals( container.get("list").length, 3 );
assertEquals( container.get("person").name, "Mario" );
//inject a function and exec it
assertEquals( container.get("sum")(1,1), 2);
//inject a function with closure and exec it
assertEquals( container.get("sum10")(100), 110);
}
public function testCheckCacheCallOnlyOne() {
var list:Array<String>=container.get("list");
assertEquals( container.get("list").length, 3 );
container.get("list").push("ciao");
assertEquals( container.get("list").length, 4 );
}
public function testWithoutCache() {
var list:Array<String>=container.getWithoutCache("list");
assertEquals( container.getWithoutCache("list").length, 3 );
container.getWithoutCache("list").push("ciao");
assertEquals( container.getWithoutCache("list").length, 3 );
}
public function testAnnotationValues() {
var object=new TestClassAnnotations(container);
checkObjectTest(object);
}
public function testAnnotationPropertyValues() {
var object=new TestPropertyAnnotation(container);
assertEquals(object.x,999);
}
public function testListValues() {
var object=new TestClassList();
syringo.Injector.injectByList(object, container, [
['title','title'],
['collection','list'],
['user','person'],
['sum','sum'],
['sum10','sum10']
]);
checkObjectTest(object);
}
public function testListPropertyValues() {
var object=new TestPropertyList();
syringo.Injector.injectByList(object, container, [
['my_x','aNumber']
]);
assertEquals(object.x,999);
}
}