Diffstat (limited to 'frontend/gamma/tests/tests/Clipperz/Async.test.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/tests/tests/Clipperz/Async.test.js | 687 |
1 files changed, 687 insertions, 0 deletions
diff --git a/frontend/gamma/tests/tests/Clipperz/Async.test.js b/frontend/gamma/tests/tests/Clipperz/Async.test.js new file mode 100644 index 0000000..fa6daca --- a/dev/null +++ b/frontend/gamma/tests/tests/Clipperz/Async.test.js | |||
@@ -0,0 +1,687 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | testObject = function (aLabel) { | ||
30 | this._label = aLabel; | ||
31 | this._sibling = null; | ||
32 | } | ||
33 | |||
34 | testObject.prototype = { | ||
35 | 'label': function () { | ||
36 | return MochiKit.Async.succeed(this._label); | ||
37 | }, | ||
38 | |||
39 | 'shouldHit': function () { | ||
40 | var filterRegExp; | ||
41 | |||
42 | filterRegExp = new RegExp("hit", "i"); | ||
43 | |||
44 | return Clipperz.Async.callbacks("testObject.shouldHit", [ | ||
45 | MochiKit.Base.method(this, 'label'), | ||
46 | MochiKit.Base.method(filterRegExp, 'test') | ||
47 | ]); | ||
48 | }, | ||
49 | |||
50 | 'sibling': function () { | ||
51 | return this._sibling; | ||
52 | }, | ||
53 | |||
54 | 'setSibling': function (aSibling) { | ||
55 | this._sibling = aSibling; | ||
56 | } | ||
57 | |||
58 | } | ||
59 | |||
60 | var tests = { | ||
61 | |||
62 | //------------------------------------------------------------------------- | ||
63 | |||
64 | 'succeedingForkedDeferrer_test': function (someTestArgs) { | ||
65 | var deferredResult; | ||
66 | |||
67 | deferredResult = new Clipperz.Async.Deferred('Async.test succeedingForkedDeferred', someTestArgs); | ||
68 | deferredResult.addCallback(Clipperz.Async.forkAndJoin("Async.test succeedingForkedDeferred", | ||
69 | [ | ||
70 | MochiKit.Base.partial(MochiKit.Async.succeed, 3), | ||
71 | MochiKit.Base.partial(MochiKit.Async.succeed, 2) | ||
72 | ], someTestArgs | ||
73 | )); | ||
74 | deferredResult.addCallback(function (anAsyncResult) { | ||
75 | var sum; | ||
76 | |||
77 | sum = MochiKit.Iter.reduce(MochiKit.Base.operator.add, anAsyncResult); | ||
78 | SimpleTest.is(sum, 5, "the sum of all the returned results should be 5"); | ||
79 | }); | ||
80 | deferredResult.addErrback(function() { | ||
81 | SimpleTest.ok(false, "forkAndJoin should succeed and execution path should NOT go through here (1)") | ||
82 | }) | ||
83 | deferredResult.callback(); | ||
84 | |||
85 | return deferredResult; | ||
86 | }, | ||
87 | |||
88 | //------------------------------------------------------------------------- | ||
89 | |||
90 | 'succeedingForkedAndWaitDeferrer_test': function (someTestArgs) { | ||
91 | var deferredResult; | ||
92 | |||
93 | deferredResult = new Clipperz.Async.Deferred('succeedingForkedAndWaitDeferrer_test', someTestArgs); | ||
94 | deferredResult.addCallback(Clipperz.Async.forkAndJoin("Async.test succeedingForkedAndWaitDeferrer", | ||
95 | [ | ||
96 | MochiKit.Base.partial(MochiKit.Async.callLater, 2, MochiKit.Async.succeed, 3), | ||
97 | MochiKit.Base.partial(MochiKit.Async.callLater, 1, MochiKit.Async.succeed, 2), | ||
98 | MochiKit.Base.partial(MochiKit.Async.callLater, 3, MochiKit.Async.succeed, 7) | ||
99 | ], someTestArgs | ||
100 | )); | ||
101 | deferredResult.addCallback(function (anAsyncResult) { | ||
102 | var sum; | ||
103 | |||
104 | sum = MochiKit.Iter.reduce(MochiKit.Base.operator.add, anAsyncResult); | ||
105 | SimpleTest.is(sum, 12, "the sum of all the returned results should be 12"); | ||
106 | }); | ||
107 | deferredResult.addErrback(function() { | ||
108 | SimpleTest.ok(false, "forkAndJoin should succeed and execution path should NOT go through here (2)") | ||
109 | }) | ||
110 | deferredResult.callback(); | ||
111 | |||
112 | return deferredResult; | ||
113 | }, | ||
114 | |||
115 | //------------------------------------------------------------------------- | ||
116 | |||
117 | 'failingForkedDeferrer_test': function (someTestArgs) { | ||
118 | var deferredResult; | ||
119 | |||
120 | deferredResult = new Clipperz.Async.Deferred('Async.test failingForkedDeferred', someTestArgs); | ||
121 | deferredResult.addCallback(Clipperz.Async.forkAndJoin("Async.test failingForkedDeferred", | ||
122 | [ | ||
123 | MochiKit.Async.succeed, | ||
124 | MochiKit.Async.fail | ||
125 | ], someTestArgs | ||
126 | )); | ||
127 | deferredResult.addCallback(function () { | ||
128 | SimpleTest.ok(false, "forkAndJoin should fail, and execution path should NOT go through here"); | ||
129 | }); | ||
130 | deferredResult.addErrback(function() { | ||
131 | SimpleTest.ok(true, "forkAndJoin should fail and execution path should go through here") | ||
132 | }) | ||
133 | deferredResult.callback(); | ||
134 | |||
135 | return deferredResult; | ||
136 | }, | ||
137 | |||
138 | //------------------------------------------------------------------------- | ||
139 | |||
140 | 'succeedingCollectResultsDeferrer_test': function (someTestArgs) { | ||
141 | var deferredResult; | ||
142 | |||
143 | deferredResult = new Clipperz.Async.Deferred('Async.test succeedingCollectResultsDeferrer', someTestArgs); | ||
144 | deferredResult.addCallback(Clipperz.Async.collectResults('Async.test succeedingCollectResultsDeferrer', { | ||
145 | 'first':MochiKit.Base.partial(MochiKit.Async.callLater, 2, MochiKit.Async.succeed, 3), | ||
146 | 'second':MochiKit.Base.partial(MochiKit.Async.callLater, 1, MochiKit.Async.succeed, 2), | ||
147 | 'third':MochiKit.Base.partial(MochiKit.Async.callLater, 3, MochiKit.Async.succeed, 7) | ||
148 | }, someTestArgs)); | ||
149 | deferredResult.addCallback(function (anAsyncResult) { | ||
150 | var sum; | ||
151 | |||
152 | sum = MochiKit.Iter.reduce(MochiKit.Base.operator.add, MochiKit.Base.values(anAsyncResult)); | ||
153 | SimpleTest.is(sum, 12, "the sum of all the returned results should be 12"); | ||
154 | SimpleTest.is(anAsyncResult['first'], 3, "the result of the 'first' item is 3"); | ||
155 | SimpleTest.is(anAsyncResult['second'], 2, "the result of the 'second' item is 2"); | ||
156 | SimpleTest.is(anAsyncResult['third'], 7, "the result of the 'third' item is 7"); | ||
157 | SimpleTest.is(MochiKit.Base.keys(anAsyncResult).length, 3, "the result has exactly 3 values"); | ||
158 | }); | ||
159 | deferredResult.addErrback(function() { | ||
160 | SimpleTest.ok(false, "collectResults should succeed and execution path should NOT go through here"); | ||
161 | }); | ||
162 | deferredResult.callback(); | ||
163 | |||
164 | return deferredResult; | ||
165 | }, | ||
166 | |||
167 | //------------------------------------------------------------------------- | ||
168 | |||
169 | 'collectResultsWithParameter_test': function (someTestArgs) { | ||
170 | var deferredResult; | ||
171 | |||
172 | deferredResult = new Clipperz.Async.Deferred('collectResultsWithParameter_test', someTestArgs); | ||
173 | deferredResult.collectResults({ | ||
174 | 'add_3':MochiKit.Base.partial(MochiKit.Base.operator.add, 3), | ||
175 | 'sub_8':MochiKit.Base.partial(MochiKit.Base.operator.sub, 8), | ||
176 | 'mul_4':MochiKit.Base.partial(MochiKit.Base.operator.mul, 4) | ||
177 | }); | ||
178 | deferredResult.addCallback(function (anAsyncResult) { | ||
179 | SimpleTest.is(anAsyncResult['add_3'], 8, "adding 3 to the passed value (5) returns 8"); | ||
180 | SimpleTest.is(anAsyncResult['sub_8'], 3, "subtracting the passed value (5) to 8 returns 3"); | ||
181 | SimpleTest.is(anAsyncResult['mul_4'], 20, "multiplying the passed value (5) by 4 returns 20"); | ||
182 | SimpleTest.is(MochiKit.Base.keys(anAsyncResult).length, 3, "the result has exactly 3 values"); | ||
183 | }); | ||
184 | deferredResult.callback(5); | ||
185 | |||
186 | return deferredResult; | ||
187 | }, | ||
188 | |||
189 | //------------------------------------------------------------------------- | ||
190 | |||
191 | 'succeedingCollectResultsDeferrer_alternative_syntax_test': function (someTestArgs) { | ||
192 | var deferredResult; | ||
193 | |||
194 | deferredResult = new Clipperz.Async.Deferred('succeedingCollectResultsDeferrer_alternative_syntax_test', someTestArgs); | ||
195 | deferredResult.collectResults({ | ||
196 | 'first':MochiKit.Base.partial(MochiKit.Async.callLater, 2, MochiKit.Async.succeed, 3), | ||
197 | 'second':MochiKit.Base.partial(MochiKit.Async.callLater, 1, MochiKit.Async.succeed, 2), | ||
198 | 'third':MochiKit.Base.partial(MochiKit.Async.callLater, 3, MochiKit.Async.succeed, 7) | ||
199 | }); | ||
200 | deferredResult.addCallback(function (anAsyncResult) { | ||
201 | var sum; | ||
202 | |||
203 | sum = MochiKit.Iter.reduce(MochiKit.Base.operator.add, MochiKit.Base.values(anAsyncResult)); | ||
204 | SimpleTest.is(sum, 12, "the sum of all the returned results should be 12"); | ||
205 | SimpleTest.is(anAsyncResult['first'], 3, "the result of the 'first' item is 3"); | ||
206 | SimpleTest.is(anAsyncResult['second'], 2, "the result of the 'second' item is 2"); | ||
207 | SimpleTest.is(anAsyncResult['third'], 7, "the result of the 'third' item is 7"); | ||
208 | SimpleTest.is(MochiKit.Base.keys(anAsyncResult).length, 3, "the result has exactly 3 values"); | ||
209 | }); | ||
210 | deferredResult.addErrback(function() { | ||
211 | SimpleTest.ok(false, "collectResults should succeed and execution path should NOT go through here"); | ||
212 | }); | ||
213 | deferredResult.callback(); | ||
214 | |||
215 | return deferredResult; | ||
216 | }, | ||
217 | |||
218 | //------------------------------------------------------------------------- | ||
219 | |||
220 | 'failingCollectResultsDeferrer_test': function (someTestArgs) { | ||
221 | var deferredResult; | ||
222 | |||
223 | deferredResult = new Clipperz.Async.Deferred('Async.test failingCollectResultsDeferrer', someTestArgs); | ||
224 | deferredResult.addCallback(Clipperz.Async.collectResults('Async.test failingCollectResultsDeferrer', { | ||
225 | 'succeed':MochiKit.Base.partial(MochiKit.Async.succeed, 3), | ||
226 | 'fail': MochiKit.Base.partial(MochiKit.Async.fail, 2) | ||
227 | }, someTestArgs)); | ||
228 | deferredResult.addCallback(function() { | ||
229 | SimpleTest.ok(false, "collectResults should fail and execution path should NOT go through here"); | ||
230 | }); | ||
231 | deferredResult.addErrback(function (anAsyncResult) { | ||
232 | var result; | ||
233 | |||
234 | result = anAsyncResult['message']; | ||
235 | SimpleTest.is(MochiKit.Base.keys(result).length, 2, "the result has exactly 2 values"); | ||
236 | //console.log("anAsyncResult", anAsyncResult); | ||
237 | SimpleTest.ok(!(result['succeed'] instanceof Error), "the successful value is actually successful"); | ||
238 | SimpleTest.ok(result['fail'] instanceof Error, "the failed value is actually failed"); | ||
239 | }); | ||
240 | deferredResult.callback(); | ||
241 | |||
242 | return deferredResult; | ||
243 | }, | ||
244 | |||
245 | //------------------------------------------------------------------------- | ||
246 | |||
247 | 'collectResults_withSimpleArrayListOfCalls_test': function (someTestArgs) { | ||
248 | var deferredResult; | ||
249 | |||
250 | deferredResult = new Clipperz.Async.Deferred('collectResults_withSimpleArrayListOfCalls_test', someTestArgs); | ||
251 | deferredResult.addCallback(Clipperz.Async.collectResults('collectResults_withSimpleArrayListOfCalls_test - collectResults', { | ||
252 | 'path1': [ | ||
253 | MochiKit.Base.partial(MochiKit.Async.succeed, 3) | ||
254 | ], | ||
255 | 'path2': MochiKit.Base.partial(MochiKit.Async.succeed, 2) | ||
256 | }, someTestArgs)); | ||
257 | deferredResult.addCallback(function (anAsyncResult) { | ||
258 | SimpleTest.is(anAsyncResult['path1'], 3, "the result of the first path is 3"); | ||
259 | SimpleTest.is(anAsyncResult['path2'], 2, "the result of the second path is 2"); | ||
260 | }); | ||
261 | deferredResult.callback(); | ||
262 | |||
263 | return deferredResult; | ||
264 | }, | ||
265 | |||
266 | //------------------------------------------------------------------------- | ||
267 | |||
268 | 'collectResults_withArrayListOfCalls_test': function (someTestArgs) { | ||
269 | var deferredResult; | ||
270 | |||
271 | deferredResult = new Clipperz.Async.Deferred('collectResults_withArrayListOfCalls_test', someTestArgs); | ||
272 | deferredResult.addCallback(Clipperz.Async.collectResults('collectResults_withArrayListOfCalls_test - collectResults', { | ||
273 | 'path1': [ | ||
274 | MochiKit.Base.partial(MochiKit.Async.succeed, {key1:'value1', key2:'value2'}), | ||
275 | Clipperz.Base.serializeJSON | ||
276 | ], | ||
277 | 'path2': [ | ||
278 | MochiKit.Base.partial(MochiKit.Async.succeed, {key3:'value3', key4:'value4'}), | ||
279 | Clipperz.Base.serializeJSON | ||
280 | ] | ||
281 | }, someTestArgs)); | ||
282 | deferredResult.addCallback(function (anAsyncResult) { | ||
283 | SimpleTest.is(anAsyncResult['path1'], "{\"key1\":\"value1\",\"key2\":\"value2\"}", "the result of the first path is correct"); | ||
284 | SimpleTest.is(anAsyncResult['path2'], "{\"key3\":\"value3\",\"key4\":\"value4\"}", "the result of the second path is correct"); | ||
285 | }); | ||
286 | deferredResult.callback(); | ||
287 | |||
288 | return deferredResult; | ||
289 | }, | ||
290 | |||
291 | //------------------------------------------------------------------------- | ||
292 | |||
293 | 'deferredSort_test': function (someTestArgs) { | ||
294 | var deferredResult; | ||
295 | vartestArray; | ||
296 | |||
297 | testArray = [ | ||
298 | {key:'key2', label:function () { return MochiKit.Async.succeed('label2')} }, | ||
299 | {key:'key3', label:function () { return MochiKit.Async.succeed('label3')} }, | ||
300 | {key:'key7', label:function () { return MochiKit.Async.succeed('label7')} }, | ||
301 | {key:'key1', label:function () { return MochiKit.Async.succeed('label1')} }, | ||
302 | {key:'key5', label:function () { return MochiKit.Async.succeed('label5')} }, | ||
303 | {key:'key4', label:function () { return MochiKit.Async.succeed('label4')} }, | ||
304 | {key:'key9', label:function () { return MochiKit.Async.succeed('label9')} }, | ||
305 | {key:'key8', label:function () { return MochiKit.Async.succeed('label8')} }, | ||
306 | {key:'key6', label:function () { return MochiKit.Async.succeed('label6')} } | ||
307 | ] | ||
308 | |||
309 | deferredResult = new Clipperz.Async.Deferred('deferredSort_test', someTestArgs); | ||
310 | deferredResult.addCallback(Clipperz.Async.deferredSort, function (aObject, bObject) { | ||
311 | var result; | ||
312 | |||
313 | result = Clipperz.Async.deferredCompare(MochiKit.Base.compare, aObject.label(), bObject.label()); | ||
314 | |||
315 | return result; | ||
316 | }); | ||
317 | deferredResult.addCallback(function (aSortedArray) { | ||
318 | SimpleTest.is(aSortedArray.length, testArray.length, "The sorted array should have the same number of objects as the original one"); | ||
319 | SimpleTest.is(aSortedArray[0]['key'], 'key1', "[0] -> key1"); | ||
320 | SimpleTest.is(aSortedArray[1]['key'], 'key2', "[1] -> key2"); | ||
321 | SimpleTest.is(aSortedArray[2]['key'], 'key3', "[2] -> key3"); | ||
322 | SimpleTest.is(aSortedArray[3]['key'], 'key4', "[3] -> key4"); | ||
323 | SimpleTest.is(aSortedArray[4]['key'], 'key5', "[4] -> key5"); | ||
324 | SimpleTest.is(aSortedArray[5]['key'], 'key6', "[5] -> key6"); | ||
325 | SimpleTest.is(aSortedArray[6]['key'], 'key7', "[6] -> key7"); | ||
326 | SimpleTest.is(aSortedArray[7]['key'], 'key8', "[7] -> key8"); | ||
327 | SimpleTest.is(aSortedArray[8]['key'], 'key9', "[8] -> key9"); | ||
328 | }); | ||
329 | deferredResult.callback(testArray); | ||
330 | |||
331 | return deferredResult; | ||
332 | }, | ||
333 | |||
334 | //------------------------------------------------------------------------- | ||
335 | |||
336 | 'callbacks_test': function (someTestArgs) { | ||
337 | return Clipperz.Async.callbacks("callbacks_test", [ | ||
338 | MochiKit.Base.partial(MochiKit.Base.operator.add, 5), | ||
339 | Clipperz.Async.Test.is(15, "Clipperz.Async.callbacks seems to work") | ||
340 | ], someTestArgs, 10); | ||
341 | }, | ||
342 | |||
343 | //------------------------------------------------------------------------- | ||
344 | |||
345 | 'deferredAcquireLockTest': function (someTestArgs) { | ||
346 | var deferredLock; | ||
347 | var deferredResult; | ||
348 | var anotherDeferred; | ||
349 | var deferredResults; | ||
350 | |||
351 | deferredLock = new MochiKit.Async.DeferredLock(); | ||
352 | deferredResult = new Clipperz.Async.Deferred("acquireLockTest", {trace:false}); | ||
353 | deferredResult.acquireLock(deferredLock); | ||
354 | deferredResult.addCallback(function () { | ||
355 | SimpleTest.is(true, deferredLock.locked, 'DeferredLock is locked after Clipperz.Async.Deferred.acquireLock'); | ||
356 | }); | ||
357 | |||
358 | anotherDeferred = new Clipperz.Async.Deferred("acquireLockLockedTest", {trace:false}); | ||
359 | anotherDeferred.acquireLock(deferredLock); | ||
360 | anotherDeferred.addCallback(function () { | ||
361 | SimpleTest.ok(false, 'Did not wait on a locked DeferredLock') | ||
362 | }); | ||
363 | anotherDeferred.addErrback(function (anError) { | ||
364 | SimpleTest.ok(true, 'Did wait on a locked DeferredLock'); | ||
365 | }); | ||
366 | |||
367 | deferredResults = new MochiKit.Async.DeferredList([deferredResult, anotherDeferred], false, false, false); | ||
368 | MochiKit.Async.callLater(1, function () { | ||
369 | anotherDeferred.cancel(); | ||
370 | }); | ||
371 | |||
372 | deferredResult.callback(); | ||
373 | anotherDeferred.callback(); | ||
374 | return deferredResults; | ||
375 | }, | ||
376 | |||
377 | //------------------------------------------------------------------------- | ||
378 | |||
379 | 'deferredLockAcquireTest': function (someTestArgs) { | ||
380 | var deferredLock; | ||
381 | var defferedResult; | ||
382 | var anotherDeferred; | ||
383 | var deferredResults; | ||
384 | |||
385 | deferredLock = new MochiKit.Async.DeferredLock(); | ||
386 | deferredResult = new Clipperz.Async.Deferred("acquireLockTest", {trace:false}); | ||
387 | deferredResult.addMethod(deferredLock, 'acquire'); | ||
388 | deferredResult.addCallback(function () { | ||
389 | SimpleTest.is(true, deferredLock.locked, 'DeferredLock is locked after Clipperz.Async.Deferred.acquireLock'); | ||
390 | }); | ||
391 | |||
392 | anotherDeferred = new Clipperz.Async.Deferred("acquireLockLockedTest", {trace:false}); | ||
393 | anotherDeferred.addMethod(deferredLock, 'acquire'); | ||
394 | anotherDeferred.addCallback(function () { | ||
395 | SimpleTest.ok(false, 'Did not wait on a locked DeferredLock') | ||
396 | }); | ||
397 | anotherDeferred.addErrback(function (anError) { | ||
398 | SimpleTest.ok(true, 'Did wait on a locked DeferredLock'); | ||
399 | }); | ||
400 | |||
401 | |||
402 | deferredResults = new MochiKit.Async.DeferredList([deferredResult, anotherDeferred], false, false, false); | ||
403 | MochiKit.Async.callLater(1, function () { | ||
404 | anotherDeferred.cancel(); | ||
405 | }); | ||
406 | |||
407 | deferredResult.callback(); | ||
408 | anotherDeferred.callback() | ||
409 | |||
410 | return deferredResults; | ||
411 | }, | ||
412 | |||
413 | //------------------------------------------------------------------------- | ||
414 | |||
415 | 'deferredFilter_test': function (someTestArgs) { | ||
416 | var deferredResult; | ||
417 | vartestObjects; | ||
418 | var filterRegExp; | ||
419 | var deferredFilterFunction; | ||
420 | |||
421 | testObjects = [ | ||
422 | {key: '1', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'skip 1') }, | ||
423 | {key: '2', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'hit 2') }, | ||
424 | {key: '3', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'skip 3') }, | ||
425 | {key: '4', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'hit 4') }, | ||
426 | {key: '5', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'skip 5') }, | ||
427 | {key: '6', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'skip 6') }, | ||
428 | {key: '7', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'hit 7') }, | ||
429 | {key: '8', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'skip 8') }, | ||
430 | {key: '9', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'skip 9') }, | ||
431 | {key:'10', label: MochiKit.Base.partial(MochiKit.Async.succeed, 'hit 10') } | ||
432 | ] | ||
433 | |||
434 | filterRegExp = new RegExp("hit", "i"); | ||
435 | |||
436 | deferredFilterFunction = function (aRegExp, anObject) { | ||
437 | return Clipperz.Async.callbacks("deferredFilterFunction", [ | ||
438 | MochiKit.Base.method(anObject, 'label'), | ||
439 | MochiKit.Base.method(aRegExp, 'test'), | ||
440 | function (doesItMatch) { | ||
441 | var result; | ||
442 | |||
443 | if (doesItMatch) { | ||
444 | result = MochiKit.Async.succeed('match'); | ||
445 | } else { | ||
446 | result = MochiKit.Async.fail('miss'); | ||
447 | } | ||
448 | |||
449 | return result; | ||
450 | } | ||
451 | ], someTestArgs); | ||
452 | }; | ||
453 | |||
454 | deferredResult = new Clipperz.Async.Deferred("deferredFilter_test", someTestArgs); | ||
455 | deferredResult.addCallback(Clipperz.Async.deferredFilter, MochiKit.Base.partial(deferredFilterFunction, filterRegExp), testObjects); | ||
456 | deferredResult.addCallback(function (aResult) { | ||
457 | SimpleTest.is(aResult.length, 4, "There are 4 items matching the 'hit' regexp"); | ||
458 | SimpleTest.is(aResult[0]['key'], '2', "The first item to match is the one with key:2"); | ||
459 | SimpleTest.is(aResult[1]['key'], '4', "The first item to match is the one with key:4"); | ||
460 | SimpleTest.is(aResult[2]['key'], '7', "The first item to match is the one with key:7"); | ||
461 | SimpleTest.is(aResult[3]['key'], '10', "The first item to match is the one with key:10"); | ||
462 | }) | ||
463 | deferredResult.callback(); | ||
464 | |||
465 | return deferredResult; | ||
466 | }, | ||
467 | |||
468 | //------------------------------------------------------------------------- | ||
469 | |||
470 | 'deferredFilter_methodcaller_test': function (someTestArgs) { | ||
471 | var deferredResult; | ||
472 | vartestObjects; | ||
473 | var filterRegExp; | ||
474 | var deferredFilterFunction; | ||
475 | |||
476 | testObjects = [ | ||
477 | new testObject('skip 1'), | ||
478 | new testObject('hit 2'), | ||
479 | new testObject('skip 3'), | ||
480 | new testObject('hit 4'), | ||
481 | new testObject('skip 5'), | ||
482 | new testObject('skip 6'), | ||
483 | new testObject('hit 7'), | ||
484 | new testObject('skip 8'), | ||
485 | new testObject('skip 9'), | ||
486 | new testObject('hit 10') | ||
487 | ]; | ||
488 | |||
489 | deferredResult = new Clipperz.Async.Deferred("deferredFilter_methodcaller_test", someTestArgs); | ||
490 | // deferredResult.addCallback(function () { return testObjects[0]; }); | ||
491 | deferredResult.addCallback(MochiKit.Async.succeed, testObjects[0]); | ||
492 | deferredResult.addCallback(MochiKit.Base.methodcaller('shouldHit')); | ||
493 | deferredResult.addTest(false, "the first element shoud return 'false' to the 'shouldHit' method"); | ||
494 | |||
495 | deferredResult.addCallback(MochiKit.Async.succeed, testObjects[1]); | ||
496 | deferredResult.addCallback(MochiKit.Base.methodcaller('shouldHit')); | ||
497 | deferredResult.addTest(true, "the second element shoud return 'true' to the 'shouldHit' method"); | ||
498 | |||
499 | deferredResult.addCallback(MochiKit.Async.succeed, testObjects); | ||
500 | deferredResult.addCallback(Clipperz.Async.deferredFilter, MochiKit.Base.methodcaller('shouldHit')); | ||
501 | deferredResult.addCallback(function (aResult) { | ||
502 | SimpleTest.is(aResult.length, 4, "There are 4 items matching the 'hit' regexp"); | ||
503 | SimpleTest.is(aResult[0]._label, 'hit 2', "The first item to match is the one with key:2"); | ||
504 | SimpleTest.is(aResult[1]._label, 'hit 4', "The first item to match is the one with key:4"); | ||
505 | SimpleTest.is(aResult[2]._label, 'hit 7', "The first item to match is the one with key:7"); | ||
506 | SimpleTest.is(aResult[3]._label, 'hit 10', "The first item to match is the one with key:10"); | ||
507 | }) | ||
508 | deferredResult.callback(); | ||
509 | |||
510 | return deferredResult; | ||
511 | }, | ||
512 | |||
513 | //------------------------------------------------------------------------- | ||
514 | |||
515 | 'setItem_test': function (someTestArgs) { | ||
516 | var deferredResult; | ||
517 | var result; | ||
518 | |||
519 | result = {}; | ||
520 | deferredResult = new Clipperz.Async.Deferred("setItem_test", someTestArgs); | ||
521 | deferredResult.addCallback(MochiKit.Async.succeed, "Value 1"); | ||
522 | deferredResult.addCallback(Clipperz.Async.setItem, result, 'key1'); | ||
523 | deferredResult.addCallback(MochiKit.Async.succeed, "Value 2"); | ||
524 | deferredResult.addCallback(Clipperz.Async.setItem, result, 'key2'); | ||
525 | |||
526 | deferredResult.addCallback(MochiKit.Base.itemgetter('key1')); | ||
527 | deferredResult.addTest("Value 1", "the value for the 'key1' does match"); | ||
528 | |||
529 | deferredResult.addCallback(MochiKit.Async.succeed, result); | ||
530 | deferredResult.addCallback(MochiKit.Base.itemgetter('key2')); | ||
531 | deferredResult.addTest("Value 2", "the value for the 'key2' does match"); | ||
532 | deferredResult.callback(); | ||
533 | |||
534 | return deferredResult; | ||
535 | }, | ||
536 | |||
537 | //------------------------------------------------------------------------- | ||
538 | |||
539 | 'setItemOnObject_test': function (someTestArgs) { | ||
540 | var deferredResult; | ||
541 | var result; | ||
542 | |||
543 | result = {}; | ||
544 | |||
545 | deferredResult = new Clipperz.Async.Deferred("setItemOnObject_test", someTestArgs); | ||
546 | deferredResult.addCallback(MochiKit.Async.succeed, result); | ||
547 | deferredResult.addCallback(Clipperz.Async.setItemOnObject, 'key1', "Value 1"); | ||
548 | deferredResult.addCallback(Clipperz.Async.setItemOnObject, 'key2', "Value 2"); | ||
549 | |||
550 | deferredResult.addCallback(MochiKit.Base.itemgetter('key1')); | ||
551 | deferredResult.addTest("Value 1", "the value for the 'key1' does match"); | ||
552 | |||
553 | deferredResult.addCallback(MochiKit.Async.succeed, result); | ||
554 | deferredResult.addCallback(MochiKit.Base.itemgetter('key2')); | ||
555 | deferredResult.addTest("Value 2", "the value for the 'key2' does match"); | ||
556 | deferredResult.callback(); | ||
557 | |||
558 | return deferredResult; | ||
559 | }, | ||
560 | |||
561 | //------------------------------------------------------------------------- | ||
562 | |||
563 | 'setDeferredItemOnObject_test': function (someTestArgs) { | ||
564 | var deferredResult; | ||
565 | var result; | ||
566 | |||
567 | result = {}; | ||
568 | |||
569 | deferredResult = new Clipperz.Async.Deferred("setDeferredItemOnObject_test", someTestArgs); | ||
570 | deferredResult.addCallback(MochiKit.Async.succeed, result); | ||
571 | deferredResult.addCallback(Clipperz.Async.setDeferredItemOnObject, 'key1', MochiKit.Base.partial(MochiKit.Async.succeed, "Value 1")); | ||
572 | deferredResult.addCallback(Clipperz.Async.setDeferredItemOnObject, 'key2', MochiKit.Base.partial(MochiKit.Async.succeed, "Value 2")); | ||
573 | |||
574 | deferredResult.addCallback(MochiKit.Base.itemgetter('key1')); | ||
575 | deferredResult.addTest("Value 1", "the value for the 'key1' does match"); | ||
576 | |||
577 | deferredResult.addCallback(MochiKit.Async.succeed, result); | ||
578 | deferredResult.addCallback(MochiKit.Base.itemgetter('key2')); | ||
579 | deferredResult.addTest("Value 2", "the value for the 'key2' does match"); | ||
580 | deferredResult.callback(); | ||
581 | |||
582 | return deferredResult; | ||
583 | }, | ||
584 | |||
585 | //------------------------------------------------------------------------- | ||
586 | |||
587 | 'addIf_test': function (someTestArgs) { | ||
588 | var deferredResult; | ||
589 | |||
590 | deferredResult = new Clipperz.Async.Deferred("addIf_test", someTestArgs); | ||
591 | |||
592 | deferredResult.addCallback(MochiKit.Async.succeed, true); | ||
593 | deferredResult.addIf([ | ||
594 | MochiKit.Base.partial(SimpleTest.ok, true, "when a true value is passed, the 'then' branch is executed") | ||
595 | ], [ | ||
596 | MochiKit.Base.partial(SimpleTest.ok, false, "when a true value is passed, the 'else' branch should NOT be executed") | ||
597 | ]); | ||
598 | |||
599 | deferredResult.addCallback(MochiKit.Async.succeed, false); | ||
600 | deferredResult.addIf([ | ||
601 | MochiKit.Base.partial(SimpleTest.ok, false, "when a false value is passed, the 'then' branch should NOT be executed") | ||
602 | ], [ | ||
603 | MochiKit.Base.partial(SimpleTest.ok, true, "when a false value is passed, the 'else' branch is executed") | ||
604 | ]); | ||
605 | |||
606 | deferredResult.callback(); | ||
607 | |||
608 | return deferredResult; | ||
609 | }, | ||
610 | |||
611 | //------------------------------------------------------------------------- | ||
612 | |||
613 | 'addMethod_test': function (someTestArgs) { | ||
614 | var deferredResult; | ||
615 | var testObjectInstance; | ||
616 | var testObjectInstance_2; | ||
617 | var label; | ||
618 | vartestObjectInstanceGetter; | ||
619 | |||
620 | label = "Test - Label"; | ||
621 | testObjectInstance = new testObject(label); | ||
622 | testObjectInstance_2 = new testObject(label + label); | ||
623 | testObjectInstance.setSibling(testObjectInstance_2); | ||
624 | |||
625 | testObjectInstanceGetter = function () { | ||
626 | return testObjectInstance; | ||
627 | } | ||
628 | |||
629 | deferredResult = new Clipperz.Async.Deferred("addMethod_test", someTestArgs); | ||
630 | deferredResult.addMethod(testObjectInstance, 'label'); | ||
631 | deferredResult.addTest(label, "the addMethod seems to work"); | ||
632 | |||
633 | deferredResult.addMethod(testObjectInstanceGetter(), 'label'); | ||
634 | deferredResult.addTest(label, "the addMethod seems to work"); | ||
635 | |||
636 | deferredResult.addMethod(testObjectInstance.sibling(), 'label'); | ||
637 | deferredResult.addTest(label+label, "the addMethod seems to work"); | ||
638 | deferredResult.callback(); | ||
639 | |||
640 | return deferredResult; | ||
641 | }, | ||
642 | |||
643 | //------------------------------------------------------------------------- | ||
644 | |||
645 | 'callbacksWithErrors_test': function (someTestArgs) { | ||
646 | var deferredResult; | ||
647 | |||
648 | deferredResult = new Clipperz.Async.Deferred("callbacksWithErrors_test", someTestArgs); | ||
649 | deferredResult.addCallback(MochiKit.Base.partial(Clipperz.Async.callbacks, "callbacksWithErrors_test - callbacks", [ | ||
650 | function () { return 10; }, | ||
651 | function (aValue) { pippo = pluto + aValue; }, | ||
652 | function (aValue) { SimpleTest.ok(false, "this code should never be executed"); return aValue; } | ||
653 | ], {trace:someTestArgs})); | ||
654 | deferredResult.addCallback(SimpleTest.ok, false, "the inner code should raise an exception and exit through the error chain"); | ||
655 | deferredResult.addErrback(SimpleTest.ok, true, "the inner code should raise an exception and exit through the error chain"); | ||
656 | |||
657 | deferredResult.callback(); | ||
658 | |||
659 | return deferredResult; | ||
660 | }, | ||
661 | |||
662 | //------------------------------------------------------------------------- | ||
663 | |||
664 | 'deferredVars_test': function (someTestArgs) { | ||
665 | var deferredResult; | ||
666 | |||
667 | deferredResult = new Clipperz.Async.Deferred("deferredVars_test", someTestArgs); | ||
668 | deferredResult.addCallback(MochiKit.Async.succeed, "test string"); | ||
669 | deferredResult.setValue('testKey'); | ||
670 | deferredResult.addCallback(MochiKit.Async.succeed, "another string"); | ||
671 | deferredResult.getValue('testKey'); | ||
672 | deferredResult.addTest("test string", "The right string has been fetched"); | ||
673 | |||
674 | deferredResult.callback(); | ||
675 | |||
676 | return deferredResult; | ||
677 | }, | ||
678 | |||
679 | //------------------------------------------------------------------------- | ||
680 | 'syntaxFix': MochiKit.Base.noop | ||
681 | }; | ||
682 | |||
683 | |||
684 | |||
685 | //############################################################################# | ||
686 | |||
687 | SimpleTest.runDeferredTests("Clipperz.Async", tests, {trace:false}); | ||