1 | <html> |
---|
2 | <head> |
---|
3 | <title>OO/decl</title> |
---|
4 | <style type="text/css"> |
---|
5 | @import "../../../dojo/resources/dojo.css"; |
---|
6 | </style> |
---|
7 | <script type="text/javascript" src="../../../dojo/dojo.js" data-dojo-config="isDebug: true"></script> |
---|
8 | <!--<script type="text/javascript" src="../oo/declare.js"></script>--> |
---|
9 | <script type="text/javascript"> |
---|
10 | //dojo.require("dojox.lang.oo.declare"); |
---|
11 | dojo.require("dojox.lang.tests.declare-old"); |
---|
12 | |
---|
13 | var decl = dojo.declare, t, chains = true, |
---|
14 | oldDecl = dojox.lang.tests.declareOld; |
---|
15 | |
---|
16 | var test1 = function(){ |
---|
17 | var A = decl("A", null, { |
---|
18 | constructor: function(){ |
---|
19 | console.log("A.constructor:", arguments); |
---|
20 | }, |
---|
21 | m1: function(){ |
---|
22 | console.log("A.m1:", arguments); |
---|
23 | } |
---|
24 | }); |
---|
25 | var a = new A(1); |
---|
26 | a.m1(2); |
---|
27 | var B = decl("B", A, { |
---|
28 | constructor: function(){ |
---|
29 | console.log("B.constructor:", arguments); |
---|
30 | }, |
---|
31 | m2: function(){ |
---|
32 | console.log("B.m2:", arguments); |
---|
33 | } |
---|
34 | }); |
---|
35 | var b = new B(1); |
---|
36 | b.m1(2); |
---|
37 | b.m2(3); |
---|
38 | var C = decl("C", B, { |
---|
39 | constructor: function(){ |
---|
40 | console.log("C.constructor:", arguments); |
---|
41 | }, |
---|
42 | m2: function(){ |
---|
43 | this.inherited(arguments); |
---|
44 | console.log("C.m2:", arguments); |
---|
45 | this.inherited(arguments); |
---|
46 | } |
---|
47 | }); |
---|
48 | var c = new C(1); |
---|
49 | c.m1(2); |
---|
50 | c.m2(3); |
---|
51 | if(chains){ |
---|
52 | var D = decl("D", C, { |
---|
53 | constructor: function(){ |
---|
54 | console.log("D.constructor:", arguments); |
---|
55 | }, |
---|
56 | "-chains-": {m1: "after"}, |
---|
57 | m1: function(){ |
---|
58 | console.log("D.m1:", arguments); |
---|
59 | }, |
---|
60 | m2: function(){ |
---|
61 | console.log("D.m2:", arguments); |
---|
62 | this.inherited(arguments); |
---|
63 | }, |
---|
64 | m3: function(){ |
---|
65 | console.log("D.m3:", arguments); |
---|
66 | this.m2.apply(this, arguments); |
---|
67 | } |
---|
68 | }); |
---|
69 | var d = new D(1); |
---|
70 | d.m1(2); |
---|
71 | d.m2(3); |
---|
72 | d.m3(4); |
---|
73 | var E = decl("E", C, { |
---|
74 | constructor: function(){ |
---|
75 | console.log("E.constructor:", arguments); |
---|
76 | }, |
---|
77 | "-chains-": {m1: "before"}, |
---|
78 | m1: function(){ |
---|
79 | console.log("E.m1:", arguments); |
---|
80 | }, |
---|
81 | m2: function(){ |
---|
82 | console.log("E.m2:", arguments); |
---|
83 | this.inherited(arguments); |
---|
84 | }, |
---|
85 | m3: function(){ |
---|
86 | console.log("E.m3:", arguments); |
---|
87 | B.prototype.m2.apply(this, arguments); |
---|
88 | this.m2.apply(this, arguments); |
---|
89 | } |
---|
90 | }); |
---|
91 | var e = new E(1); |
---|
92 | e.m1(2); |
---|
93 | e.m2(3); |
---|
94 | e.m3(4); |
---|
95 | } |
---|
96 | var F = function(){ |
---|
97 | console.log("F.constructor:", arguments); |
---|
98 | }; |
---|
99 | dojo.extend(F, { |
---|
100 | m1: function(){ |
---|
101 | console.log("F.m1:", arguments); |
---|
102 | }, |
---|
103 | m2: function(){ |
---|
104 | console.log("F.m2:", arguments); |
---|
105 | }, |
---|
106 | m3: function(){ |
---|
107 | console.log("F.m3:", arguments); |
---|
108 | } |
---|
109 | }); |
---|
110 | var G = decl("G", null, { |
---|
111 | constructor: function(){ |
---|
112 | console.log("G.constructor:", arguments); |
---|
113 | }, |
---|
114 | m1: function(){ |
---|
115 | console.log("G.m1:", arguments); |
---|
116 | this.inherited(arguments); |
---|
117 | }, |
---|
118 | m2: function(){ |
---|
119 | console.log("G.m2:", arguments); |
---|
120 | }, |
---|
121 | m3: function(){ |
---|
122 | console.log("G.m3:", arguments); |
---|
123 | } |
---|
124 | }); |
---|
125 | var H = decl("H", null, { |
---|
126 | constructor: function(){ |
---|
127 | console.log("H.constructor:", arguments); |
---|
128 | }, |
---|
129 | m1: function(){ |
---|
130 | console.log("H.m1:", arguments); |
---|
131 | }, |
---|
132 | m2: function(){ |
---|
133 | console.log("H.m2:", arguments); |
---|
134 | }, |
---|
135 | m3: function(){ |
---|
136 | console.log("H.m3:", arguments); |
---|
137 | } |
---|
138 | }); |
---|
139 | if(chains){ |
---|
140 | var I = decl("I", [H, F, G], { |
---|
141 | constructor: function(){ |
---|
142 | console.log("I.constructor:", arguments); |
---|
143 | }, |
---|
144 | "-chains-": {m3: "before", m2: "after"}, |
---|
145 | m1: function(){ |
---|
146 | console.log("I.m1:", arguments); |
---|
147 | this.inherited(arguments); |
---|
148 | }, |
---|
149 | m2: function(){ |
---|
150 | console.log("I.m2:", arguments); |
---|
151 | }, |
---|
152 | m3: function(){ |
---|
153 | console.log("I.m3:", arguments); |
---|
154 | } |
---|
155 | }); |
---|
156 | var i = new I(1); |
---|
157 | i.m1(2); |
---|
158 | i.m2(3); |
---|
159 | i.m3(4); |
---|
160 | } |
---|
161 | var J = decl("J", C, { |
---|
162 | m2: function(){ |
---|
163 | console.log("J.m2:", arguments); |
---|
164 | this.inherited(arguments); |
---|
165 | } |
---|
166 | }); |
---|
167 | var j = new J(1); |
---|
168 | j.m1(2); |
---|
169 | j.m2(3); |
---|
170 | console.log("the classic diamond test"); |
---|
171 | if(chains){ |
---|
172 | var DA = decl(null, { |
---|
173 | constructor: function(){ |
---|
174 | console.log("DA.constructor"); |
---|
175 | }, |
---|
176 | m1: function(){ |
---|
177 | this.inherited(arguments); |
---|
178 | console.log("DA.m1"); |
---|
179 | }, |
---|
180 | m2: function(){ |
---|
181 | console.log("DA.m2"); |
---|
182 | this.inherited(arguments); |
---|
183 | }, |
---|
184 | toLocaleString: function(){ |
---|
185 | return this.inherited(arguments) + " -DA"; |
---|
186 | } |
---|
187 | }); |
---|
188 | var DB = decl(DA, { |
---|
189 | constructor: function(){ |
---|
190 | console.log("DB.constructor"); |
---|
191 | }, |
---|
192 | m1: function(){ |
---|
193 | this.inherited(arguments); |
---|
194 | console.log("DB.m1"); |
---|
195 | }, |
---|
196 | m2: function(){ |
---|
197 | console.log("DB.m2"); |
---|
198 | this.inherited(arguments); |
---|
199 | }, |
---|
200 | toLocaleString: function(){ |
---|
201 | return this.inherited(arguments) + " -DB"; |
---|
202 | } |
---|
203 | }); |
---|
204 | var DC = decl(DA, { |
---|
205 | constructor: function(){ |
---|
206 | console.log("DC.constructor"); |
---|
207 | }, |
---|
208 | m1: function(){ |
---|
209 | this.inherited(arguments); |
---|
210 | console.log("DC.m1"); |
---|
211 | }, |
---|
212 | m2: function(){ |
---|
213 | console.log("DC.m2"); |
---|
214 | this.inherited(arguments); |
---|
215 | }, |
---|
216 | toLocaleString: function(){ |
---|
217 | return this.inherited(arguments) + " -DC"; |
---|
218 | } |
---|
219 | }); |
---|
220 | var DD = decl([DB, DC], { |
---|
221 | constructor: function(){ |
---|
222 | console.log("DD.constructor"); |
---|
223 | }, |
---|
224 | m1: function(){ |
---|
225 | this.inherited(arguments); |
---|
226 | console.log("DD.m1"); |
---|
227 | }, |
---|
228 | m2: function(){ |
---|
229 | console.log("DD.m2"); |
---|
230 | this.inherited(arguments); |
---|
231 | }, |
---|
232 | toLocaleString: function(){ |
---|
233 | return this.inherited(arguments) + " -DD"; |
---|
234 | } |
---|
235 | }); |
---|
236 | var dd = new DD; |
---|
237 | dd.m1(); |
---|
238 | dd.m2(); |
---|
239 | console.log(dd.toLocaleString()); |
---|
240 | } |
---|
241 | console.log("chained constructor (but no preamble)"); |
---|
242 | if(chains){ |
---|
243 | var A = decl(null, { |
---|
244 | constructor: function(){ |
---|
245 | console.log("A.constructor"); |
---|
246 | }, |
---|
247 | "-chains-": {constructor: "after", destroy: "before"}, |
---|
248 | destroy: function(){ |
---|
249 | console.log("A.destroy"); |
---|
250 | } |
---|
251 | }), |
---|
252 | B = decl(A, { |
---|
253 | constructor: function(){ |
---|
254 | console.log("B.constructor"); |
---|
255 | }, |
---|
256 | destroy: function(){ |
---|
257 | console.log("B.destroy"); |
---|
258 | } |
---|
259 | }), |
---|
260 | C = decl(B, { |
---|
261 | constructor: function(){ |
---|
262 | console.log("C.constructor"); |
---|
263 | }, |
---|
264 | destroy: function(){ |
---|
265 | console.log("C.destroy"); |
---|
266 | } |
---|
267 | }); |
---|
268 | new A().destroy(); |
---|
269 | new B().destroy(); |
---|
270 | new C().destroy(); |
---|
271 | } |
---|
272 | console.log("constructor is not chained"); |
---|
273 | if(chains){ |
---|
274 | var A = decl(null, { |
---|
275 | constructor: function(){ |
---|
276 | console.log("A.constructor"); |
---|
277 | }, |
---|
278 | "-chains-": {constructor: "manual"} |
---|
279 | }), |
---|
280 | B = decl(A, { |
---|
281 | constructor: function(){ |
---|
282 | console.log("B.constructor"); |
---|
283 | this.inherited(arguments); |
---|
284 | } |
---|
285 | }), |
---|
286 | C = decl(B, { |
---|
287 | constructor: function(){ |
---|
288 | console.log("C.constructor"); |
---|
289 | this.inherited(arguments); |
---|
290 | } |
---|
291 | }); |
---|
292 | new A(); |
---|
293 | new B(); |
---|
294 | new C(); |
---|
295 | } |
---|
296 | console.log("preamble() test #1"); |
---|
297 | var A = decl(null, { |
---|
298 | constructor: function(){ |
---|
299 | console.log("A.constructor: ", arguments); |
---|
300 | } |
---|
301 | }), |
---|
302 | B = decl(A, { |
---|
303 | constructor: function(){ |
---|
304 | console.log("B.constructor: ", arguments); |
---|
305 | }, |
---|
306 | preamble: function(a, b){ |
---|
307 | return [2 * a, 2 * b]; |
---|
308 | } |
---|
309 | }), |
---|
310 | C = decl(B, { |
---|
311 | constructor: function(){ |
---|
312 | console.log("C.constructor: ", arguments); |
---|
313 | }, |
---|
314 | preamble: function(a, b){ |
---|
315 | var dict = {a: 1, b: 2}; |
---|
316 | return [dict[a], dict[b]]; |
---|
317 | } |
---|
318 | }); |
---|
319 | new C("a", "b"); |
---|
320 | console.log("preamble() test #2"); |
---|
321 | var A = decl(null, { |
---|
322 | constructor: function(){ |
---|
323 | console.log("A.constructor: ", arguments); |
---|
324 | } |
---|
325 | }), |
---|
326 | B = decl(A, { |
---|
327 | constructor: function(){ |
---|
328 | console.log("B.constructor: ", arguments); |
---|
329 | } |
---|
330 | }), |
---|
331 | C = decl(B, { |
---|
332 | constructor: function(){ |
---|
333 | console.log("C.constructor: ", arguments); |
---|
334 | } |
---|
335 | }), |
---|
336 | P = decl(null, { |
---|
337 | preamble: function(x, a, b){ |
---|
338 | return [x, 2 * a, 2 * b]; |
---|
339 | } |
---|
340 | }); |
---|
341 | new C(new P, 1, 2); |
---|
342 | }; |
---|
343 | |
---|
344 | var superclassRef = function(){ |
---|
345 | decl("tests._base.declare.tmp10", null, { |
---|
346 | foo: "thonk" |
---|
347 | }); |
---|
348 | decl("tests._base.declare.tmp11", tests._base.declare.tmp10, { |
---|
349 | constructor: function(){ |
---|
350 | this.foo = "blah"; |
---|
351 | } |
---|
352 | }); |
---|
353 | var tmp = new tests._base.declare.tmp11(); |
---|
354 | console.log("blah", tmp.foo); |
---|
355 | console.log("thonk", tests._base.declare.tmp11.superclass.foo); |
---|
356 | } |
---|
357 | |
---|
358 | var inheritedExplicitCall = function(){ |
---|
359 | var foo = "xyzzy"; |
---|
360 | decl("tests._base.declare.tmp14", null, { |
---|
361 | foo: "thonk", |
---|
362 | bar: function(arg1, arg2){ |
---|
363 | if(arg1){ |
---|
364 | this.foo = arg1; |
---|
365 | } |
---|
366 | if(arg2){ |
---|
367 | foo = arg2; |
---|
368 | } |
---|
369 | } |
---|
370 | }); |
---|
371 | decl("tests._base.declare.tmp15", tests._base.declare.tmp14, { |
---|
372 | constructor: function(){ |
---|
373 | this.foo = "blah"; |
---|
374 | }, |
---|
375 | bar: function(arg1, arg2){ |
---|
376 | this.inherited("bar", arguments, [arg2, arg1]); |
---|
377 | }, |
---|
378 | baz: function(arg1, arg2){ |
---|
379 | tests._base.declare.tmp15.superclass.bar.apply(this, arguments); |
---|
380 | } |
---|
381 | }); |
---|
382 | var tmp = new tests._base.declare.tmp15(); |
---|
383 | console.log("blah", tmp.foo); |
---|
384 | console.log("xyzzy", foo); |
---|
385 | tmp.baz("zot"); |
---|
386 | console.log("zot", tmp.foo); |
---|
387 | console.log("xyzzy", foo); |
---|
388 | tmp.bar("trousers", "squiggle"); |
---|
389 | console.log("squiggle", tmp.foo); |
---|
390 | console.log("trousers", foo); |
---|
391 | } |
---|
392 | |
---|
393 | var feature9795 = function(){ |
---|
394 | //var decl = dojo.declare; |
---|
395 | decl("ClassA", [], {}); |
---|
396 | decl("ClassB", [ClassA], |
---|
397 | { |
---|
398 | preamble: function() |
---|
399 | { |
---|
400 | console.log("ClassB preamble invoked on " + this.declaredClass); |
---|
401 | return [new ClassC()]; |
---|
402 | } |
---|
403 | }); |
---|
404 | decl("ClassC", null, |
---|
405 | { |
---|
406 | preamble: function() |
---|
407 | { |
---|
408 | console.log("ClassC preamble invoked on " + this.declaredClass); |
---|
409 | } |
---|
410 | }); |
---|
411 | new ClassB; |
---|
412 | }; |
---|
413 | |
---|
414 | var duplicatedBases = function(){ |
---|
415 | console.log("duplicated bases"); |
---|
416 | var A = decl(null, { |
---|
417 | constructor: function(){ |
---|
418 | console.log("A.constructor"); |
---|
419 | } |
---|
420 | }); |
---|
421 | var B = decl([A, A, A], { |
---|
422 | constructor: function(){ |
---|
423 | console.log("B.constructor"); |
---|
424 | } |
---|
425 | }); |
---|
426 | new B; |
---|
427 | }; |
---|
428 | |
---|
429 | var preambleTest = function(){ |
---|
430 | console.log("preamble tests"); |
---|
431 | var newA = decl(null, { |
---|
432 | constructor: function(arg){ |
---|
433 | console.log("newA: expects 1 - " + arg); |
---|
434 | } |
---|
435 | }), |
---|
436 | newB = decl(newA, { |
---|
437 | constructor: function(arg){ |
---|
438 | console.log("newB expects A - " + arg); |
---|
439 | }, |
---|
440 | preamble: function(arg){ |
---|
441 | console.log("newB/preamble expects A - " + arg); |
---|
442 | return [1]; |
---|
443 | } |
---|
444 | }), |
---|
445 | newX = decl(null, { |
---|
446 | constructor: function(arg){ |
---|
447 | console.log("newX expects object - " + (typeof arg)); |
---|
448 | this.obj = arg; |
---|
449 | } |
---|
450 | }), |
---|
451 | newBi = new newB("A"), newXi = new newX(newBi); |
---|
452 | oldDecl("oldA", null, { |
---|
453 | constructor: function(arg){ |
---|
454 | console.log("oldA: expects 1 - " + arg); |
---|
455 | } |
---|
456 | }); |
---|
457 | oldDecl("oldB", oldA, { |
---|
458 | constructor: function(arg){ |
---|
459 | console.log("oldB expects A - " + arg); |
---|
460 | }, |
---|
461 | preamble: function(arg){ |
---|
462 | console.log("oldB/preamble expects A - " + arg); |
---|
463 | return [1]; |
---|
464 | } |
---|
465 | }); |
---|
466 | oldDecl("oldX", null, { |
---|
467 | constructor: function(arg){ |
---|
468 | console.log("oldX expects object - " + (typeof arg)); |
---|
469 | this.obj = arg; |
---|
470 | } |
---|
471 | }); |
---|
472 | var oldBi = new oldB("A"), oldXi = new oldX(oldBi); |
---|
473 | }; |
---|
474 | |
---|
475 | var test = function(){ |
---|
476 | test1(); |
---|
477 | console.log("testing for individual bugs"); |
---|
478 | superclassRef(); |
---|
479 | inheritedExplicitCall(); |
---|
480 | feature9795(); |
---|
481 | duplicatedBases(); |
---|
482 | preambleTest(); |
---|
483 | }; |
---|
484 | |
---|
485 | //dojo.addOnLoad(test1); |
---|
486 | </script> |
---|
487 | </head> |
---|
488 | <body> |
---|
489 | <p>This test is meant to run with Firebug. Open the console to see the output.</p> |
---|
490 | <p><button onclick="test()">Start</button></p> |
---|
491 | </body> |
---|
492 | </html> |
---|