source: Dev/branches/rest-dojo-ui/client/dojo/tests/aspect.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 2.4 KB
Line 
1dojo.provide("dojo.tests.aspect");
2
3var aspect = dojo.require("dojo.aspect");
4
5doh.register("tests.aspect",
6        [
7                function before(t){
8                        var order = [];
9                        var obj = {
10                                method: function(a){
11                                        order.push(a);
12                                }
13                        };
14                        var signal = aspect.before(obj, "method", function(a){
15                                order.push(a);
16                                return [a+1];
17                        });
18                        obj.method(0);
19                        obj.method(2);
20                        var signal2 = aspect.before(obj, "method", function(a){
21                                order.push(a);
22                                return [a+1];
23                        });
24                        obj.method(4);
25                        signal.remove();
26                        obj.method(7);
27                        signal2.remove();
28                        obj.method(9);
29                        t.is(order, [0,1,2,3,4,5,6,7,8,9]);
30                },
31
32                function after(t){
33                        var order = [];
34                        var obj = {
35                                method: function(a){
36                                        order.push(a);
37                                        return a+1;
38                                }
39                        };
40                        var signal = aspect.after(obj, "method", function(a){
41                                order.push(0);
42                                return a+1;
43                        });
44                        obj.method(0);
45                        var signal2 = aspect.after(obj, "method", function(a){
46                                order.push(a);
47                        });
48                        obj.method(3);
49                        var signal3 = aspect.after(obj, "method", function(a){
50                                order.push(3);
51                        }, true);
52                        obj.method(3);
53                        signal2.remove();
54                        obj.method(6);
55                        signal3.remove();
56                        var signal4 = aspect.after(obj, "method", function(a){
57                                order.push(4);
58                        }, true);
59                        signal.remove();
60                        obj.method(7);
61                        t.is(order, [0, 0, 3, 0, 5, 3, 0, 5, 3, 6, 0, 3, 7, 4]);
62                },
63                function around(t){
64                        var order = [];
65                        var obj = {
66                                method: function(a){
67                                        order.push(a);
68                                        return a+1;
69                                }
70                        };
71                        var beforeSignal = aspect.before(obj, "method", function(a){
72                                order.push(a);
73                        });
74                        var signal = aspect.around(obj, "method", function(original){
75                                return function(a){
76                                        a= a + 1;
77                                        a = original(a);
78                                        order.push(a);
79                                        return a+1;
80                                }
81                        });
82                        order.push(obj.method(0));
83                        obj.method(4);
84                        t.is(order, [0,1,2,3,4,5,6]);
85                },
86                function delegation(t){
87                        var order = [];
88                        var proto = {
89                                foo: function(x){
90                                        order.push(x);
91                                        return x;
92                                },
93                                bar: function(){
94                                }
95                        };
96                        aspect.after(proto, "foo", function(x){
97                                order.push(x + 1);
98                                return x;
99                        });
100                        aspect.after(proto, "bar", function(x){
101                                t.t(this.isInstance);
102                        });
103                        proto.foo(0);
104                        function Class(){
105                        }
106                        Class.prototype = proto;
107                        var instance = new Class();
108                        instance.isInstance = true;
109                        aspect.after(instance, "foo", function(x){
110                                order.push(x + 2);
111                                return x;
112                        });
113                        instance.bar();
114                        instance.foo(2);
115                        proto.foo(5);
116                        t.is(order, [0,1,2,3,4,5,6]);
117                }
118        ]
119);
Note: See TracBrowser for help on using the repository browser.