1 | define(["dojo/aspect"], function(aspect){ |
---|
2 | |
---|
3 | doh.register("tests.aspect", |
---|
4 | [ |
---|
5 | function before(t){ |
---|
6 | var order = []; |
---|
7 | var obj = { |
---|
8 | method: function(a){ |
---|
9 | order.push(a); |
---|
10 | } |
---|
11 | }; |
---|
12 | var signal = aspect.before(obj, "method", function(a){ |
---|
13 | order.push(a); |
---|
14 | return [a+1]; |
---|
15 | }); |
---|
16 | obj.method(0); |
---|
17 | obj.method(2); |
---|
18 | var signal2 = aspect.before(obj, "method", function(a){ |
---|
19 | order.push(a); |
---|
20 | return [a+1]; |
---|
21 | }); |
---|
22 | obj.method(4); |
---|
23 | signal.remove(); |
---|
24 | obj.method(7); |
---|
25 | signal2.remove(); |
---|
26 | obj.method(9); |
---|
27 | t.is(order, [0,1,2,3,4,5,6,7,8,9]); |
---|
28 | }, |
---|
29 | |
---|
30 | function after(t){ |
---|
31 | var order = []; |
---|
32 | var obj = { |
---|
33 | method: function(a){ |
---|
34 | order.push(a); |
---|
35 | return a+1; |
---|
36 | } |
---|
37 | }; |
---|
38 | var signal = aspect.after(obj, "method", function(a){ |
---|
39 | order.push(0); |
---|
40 | return a+1; |
---|
41 | }); |
---|
42 | obj.method(0); // 0, 0 |
---|
43 | var signal2 = aspect.after(obj, "method", function(a){ |
---|
44 | order.push(a); |
---|
45 | }); |
---|
46 | obj.method(3); // 3, 0, 5 |
---|
47 | var signal3 = aspect.after(obj, "method", function(a){ |
---|
48 | order.push(3); |
---|
49 | }, true); |
---|
50 | obj.method(3); // 3, 0, 5, 3 |
---|
51 | signal2.remove(); |
---|
52 | obj.method(6); // 6, 0, 3 |
---|
53 | signal3.remove(); |
---|
54 | var signal4 = aspect.after(obj, "method", function(a){ |
---|
55 | order.push(4); |
---|
56 | }, true); |
---|
57 | signal.remove(); |
---|
58 | obj.method(7); // 7, 4 |
---|
59 | signal4.remove(); |
---|
60 | var signal5 = aspect.after(obj, "method", function(a){ |
---|
61 | order.push(a); |
---|
62 | aspect.after(obj, "method", function(a){ |
---|
63 | order.push(a); |
---|
64 | }); |
---|
65 | aspect.after(obj, "method", function(a){ |
---|
66 | order.push(a); |
---|
67 | }).remove(); |
---|
68 | return a+1; |
---|
69 | }); |
---|
70 | var signal6 = aspect.after(obj, "method", function(a){ |
---|
71 | order.push(a); |
---|
72 | return a+2; |
---|
73 | }); |
---|
74 | obj.method(8); // 8, 9, 10 |
---|
75 | obj.method(8); // 8, 9, 10, 12 |
---|
76 | t.is([0, 0, 3, 0, 5, 3, 0, 5, 3, 6, 0, 3, 7, 4, 8, 9, 10, 8, 9, 10, 12], order); |
---|
77 | obj = {method: function(){}}; |
---|
78 | aspect.after(obj, "method", function(){ |
---|
79 | return false; |
---|
80 | }, true); |
---|
81 | t.t(obj.method() === false); |
---|
82 | }, |
---|
83 | function afterMultiple(t){ |
---|
84 | var order = []; |
---|
85 | obj = { |
---|
86 | foo: function(){} |
---|
87 | }; |
---|
88 | aspect.after(obj, "foo", function(){order.push(1)}); |
---|
89 | aspect.after(obj, "foo", function(){order.push(2)}); |
---|
90 | aspect.after(obj, "foo", function(){order.push(3)}); |
---|
91 | obj.foo(); |
---|
92 | t.is([1,2,3], order); |
---|
93 | }, |
---|
94 | function around(t){ |
---|
95 | var order = []; |
---|
96 | var obj = { |
---|
97 | method: function(a){ |
---|
98 | order.push(a); |
---|
99 | return a+1; |
---|
100 | } |
---|
101 | }; |
---|
102 | var beforeSignal = aspect.before(obj, "method", function(a){ |
---|
103 | order.push(a); |
---|
104 | }); |
---|
105 | var signal = aspect.around(obj, "method", function(original){ |
---|
106 | return function(a){ |
---|
107 | a= a + 1; |
---|
108 | a = original(a); |
---|
109 | order.push(a); |
---|
110 | return a+1; |
---|
111 | }; |
---|
112 | }); |
---|
113 | order.push(obj.method(0)); |
---|
114 | obj.method(4); |
---|
115 | t.is(order, [0,1,2,3,4,5,6]); |
---|
116 | }, |
---|
117 | function around2(t){ |
---|
118 | var order = []; |
---|
119 | var obj = { |
---|
120 | method: function(a){ |
---|
121 | order.push(1); |
---|
122 | } |
---|
123 | }; |
---|
124 | var signal1 = aspect.around(obj, "method", function(original){ |
---|
125 | return function(a){ |
---|
126 | original(); |
---|
127 | order.push(2); |
---|
128 | }; |
---|
129 | }); |
---|
130 | var signal2 = aspect.around(obj, "method", function(original){ |
---|
131 | return function(a){ |
---|
132 | original(); |
---|
133 | order.push(3); |
---|
134 | }; |
---|
135 | }); |
---|
136 | var signal3 = aspect.around(obj, "method", function(original){ |
---|
137 | return function(a){ |
---|
138 | original(); |
---|
139 | order.push(4); |
---|
140 | }; |
---|
141 | }); |
---|
142 | signal2.remove(); |
---|
143 | obj.method(); |
---|
144 | t.is(order, [1,2,4]); |
---|
145 | }, |
---|
146 | function multipleRemove(t){ |
---|
147 | var foo = {bar: function(){}}; |
---|
148 | var order = []; |
---|
149 | var signal1 = aspect.after(foo, "bar", function() { |
---|
150 | order.push(1); |
---|
151 | }); |
---|
152 | |
---|
153 | var signal2 = aspect.after(foo, "bar", function() { |
---|
154 | order.push(2); |
---|
155 | }); |
---|
156 | |
---|
157 | var signal3 = aspect.after(foo, "bar", function() { |
---|
158 | order.push(3); |
---|
159 | }); |
---|
160 | |
---|
161 | // This should execute all 3 callbacks |
---|
162 | foo.bar(); |
---|
163 | |
---|
164 | signal2.remove(); |
---|
165 | signal3.remove(); |
---|
166 | |
---|
167 | // Ideally signal2 should not be removed again, but can happen if the app |
---|
168 | // fails to clear its state. |
---|
169 | signal2.remove(); |
---|
170 | |
---|
171 | // This should execute only the first callback, but notice that the third callback |
---|
172 | // is executed as well |
---|
173 | foo.bar(); |
---|
174 | t.is(order, [1,2,3,1]); |
---|
175 | }, |
---|
176 | function delegation(t){ |
---|
177 | var order = []; |
---|
178 | var proto = { |
---|
179 | foo: function(x){ |
---|
180 | order.push(x); |
---|
181 | return x; |
---|
182 | }, |
---|
183 | bar: function(){ |
---|
184 | } |
---|
185 | }; |
---|
186 | aspect.after(proto, "foo", function(x){ |
---|
187 | order.push(x + 1); |
---|
188 | return x; |
---|
189 | }); |
---|
190 | aspect.after(proto, "bar", function(x){ |
---|
191 | t.t(this.isInstance); |
---|
192 | }); |
---|
193 | proto.foo(0); |
---|
194 | function Class(){ |
---|
195 | } |
---|
196 | Class.prototype = proto; |
---|
197 | var instance = new Class(); |
---|
198 | instance.isInstance = true; |
---|
199 | aspect.after(instance, "foo", function(x){ |
---|
200 | order.push(x + 2); |
---|
201 | return x; |
---|
202 | }); |
---|
203 | instance.bar(); |
---|
204 | instance.foo(2); |
---|
205 | proto.foo(5); |
---|
206 | t.is(order, [0,1,2,3,4,5,6]); |
---|
207 | } |
---|
208 | ] |
---|
209 | ); |
---|
210 | |
---|
211 | }); |
---|