1 | /*! |
---|
2 | * Ext JS Library 3.4.0 |
---|
3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
4 | * licensing@sencha.com |
---|
5 | * http://www.sencha.com/license |
---|
6 | */ |
---|
7 | Ext.test.session.addTest('Function', { |
---|
8 | |
---|
9 | name: 'Global Function Decorators', |
---|
10 | |
---|
11 | planned: 71, |
---|
12 | |
---|
13 | // 3 |
---|
14 | test_createCallback: function(){ |
---|
15 | var fn = function(a, b){ |
---|
16 | Y.Assert.areEqual('a', a, 'Test passed param'); |
---|
17 | Y.Assert.areEqual('b', b, 'Test passed param'); |
---|
18 | return 'x'; |
---|
19 | }; |
---|
20 | |
---|
21 | var cb = fn.createCallback('a', 'b'), |
---|
22 | rt = cb(); // does not accept params |
---|
23 | Y.Assert.areEqual('x', rt, 'Test return value of callback'); |
---|
24 | }, |
---|
25 | |
---|
26 | // 24 |
---|
27 | test_createDelegate: function(){ |
---|
28 | var scope = { |
---|
29 | foo: 'bar' |
---|
30 | }; |
---|
31 | |
---|
32 | var fn = function(a, b, c){ |
---|
33 | Y.Assert.areEqual(scope, this, 'Test if "this" is correct'); |
---|
34 | Y.Assert.areEqual('bar', this.foo, 'Test if property matches'); |
---|
35 | Y.Assert.areEqual('a', a, 'Test passed param'); |
---|
36 | Y.Assert.areEqual('b', b, 'Test passed param'); |
---|
37 | Y.Assert.areEqual('c', c, 'Test passed param'); |
---|
38 | return 'x'; |
---|
39 | }; |
---|
40 | |
---|
41 | var cb = fn.createDelegate(scope, ['b', 'c'], true /* appendArgs: true appends these to passed params */), |
---|
42 | rt = cb('a'); // a b c |
---|
43 | Y.Assert.areEqual('x', rt, 'Test return value'); |
---|
44 | |
---|
45 | |
---|
46 | var cbTwo = fn.createDelegate(scope, ['a', 'b'], 0); |
---|
47 | rt = cbTwo('c'); // a b c |
---|
48 | Y.Assert.areEqual('x', rt, 'Test return value'); |
---|
49 | |
---|
50 | |
---|
51 | var cbThree = fn.createDelegate(scope, ['b'], 1 /* this replaces at pos 1 */); |
---|
52 | rt = cbThree('a', 'c'); // a b c |
---|
53 | Y.Assert.areEqual('x', rt, 'Test return value'); |
---|
54 | |
---|
55 | var cbFour = fn.createDelegate(scope, ['a', 'b', 'c']); |
---|
56 | rt = cbFour('x', 'y'); // overridden with a b c |
---|
57 | Y.Assert.areEqual('x', rt, 'Test return value'); |
---|
58 | }, |
---|
59 | |
---|
60 | // 19 |
---|
61 | test_createInterceptor: function(){ |
---|
62 | var scope = { |
---|
63 | foo: 'bar', |
---|
64 | n: 0 |
---|
65 | }; |
---|
66 | |
---|
67 | var fn = function(a, b){ |
---|
68 | Y.Assert.areEqual(scope, this, 'Test if "this" is correct'); |
---|
69 | Y.Assert.areEqual('bar', this.foo, 'Test if property matches'); |
---|
70 | Y.Assert.areEqual('a', a, 'Test passed param'); |
---|
71 | Y.Assert.areEqual('b', b, 'Test passed param'); |
---|
72 | this.n++; |
---|
73 | return 'x'; |
---|
74 | }; |
---|
75 | |
---|
76 | // normal |
---|
77 | var rt = fn.call(scope, 'a', 'b'); // n 1 |
---|
78 | Y.Assert.areEqual('x', rt, 'Test the return value'); |
---|
79 | |
---|
80 | var cb = fn.createDelegate(scope).createInterceptor(function(a, b, z){ |
---|
81 | Y.Assert.areEqual(scope, this, 'Test if "this" is correct'); |
---|
82 | Y.Assert.areEqual('bar', this.foo, 'Test if property matches'); |
---|
83 | Y.Assert.areEqual('a', a, 'Test passed param'); |
---|
84 | Y.Assert.areEqual('b', b, 'Test passed param'); |
---|
85 | return z === undefined; |
---|
86 | }, scope); |
---|
87 | // intercepted, but allowed to continue |
---|
88 | rt = cb('a', 'b'); // n 2 |
---|
89 | Y.Assert.areEqual('x', rt, 'Test the return value'); |
---|
90 | |
---|
91 | // intercepted, and cancelled |
---|
92 | cb('a', 'b', 'z'); |
---|
93 | Y.Assert.areEqual(2, scope.n, 'Test the interceptor call count'); |
---|
94 | }, |
---|
95 | |
---|
96 | // 16 |
---|
97 | test_createSequence: function(){ |
---|
98 | var scope = { |
---|
99 | foo: 'bar', |
---|
100 | seq: 0 |
---|
101 | }; |
---|
102 | |
---|
103 | var fn = function(a, b){ |
---|
104 | Y.Assert.areEqual(scope, this, 'Test if "this" is correct'); |
---|
105 | Y.Assert.areEqual('bar', this.foo, 'Test if property matches'); |
---|
106 | Y.Assert.areEqual('a', a, 'Test passed param'); |
---|
107 | Y.Assert.areEqual('b', b, 'Test passed param'); |
---|
108 | this.seq++; |
---|
109 | return 'x'; |
---|
110 | }; |
---|
111 | |
---|
112 | var rt = fn.call(scope, 'a', 'b'); // seq 1 |
---|
113 | Y.Assert.areEqual('x', rt, 'Test the return value'); |
---|
114 | Y.Assert.areEqual(1, scope.seq, 'Test the counter'); |
---|
115 | |
---|
116 | var cb = fn.createDelegate(scope).createSequence(fn, scope); |
---|
117 | rt = cb('a', 'b'); // seq 2, 3 |
---|
118 | Y.Assert.areEqual('x', rt, 'Test the return value'); |
---|
119 | Y.Assert.areEqual(3, scope.seq, 'Test the number of times the sequence was called'); |
---|
120 | }, |
---|
121 | |
---|
122 | // 9 |
---|
123 | test_defer: function(){ |
---|
124 | var scope = { |
---|
125 | foo: 'bar', |
---|
126 | n: 0 |
---|
127 | }; |
---|
128 | |
---|
129 | var fn = function(a, b){ |
---|
130 | Y.Assert.areEqual(scope, this, 'Test if "this" is correct'); |
---|
131 | Y.Assert.areEqual('bar', this.foo, 'Test if property matches'); |
---|
132 | Y.Assert.areEqual('a', a, 'Test passed param'); |
---|
133 | Y.Assert.areEqual('b', b, 'Test passed param'); |
---|
134 | this.n++; |
---|
135 | }; |
---|
136 | |
---|
137 | fn.defer(1, scope, ['a', 'b']); |
---|
138 | fn.defer(2, scope, ['a', 'b']); |
---|
139 | |
---|
140 | setTimeout(function(){ |
---|
141 | Y.Assert.areEqual(2, scope.n, 'Test if the counter matches the call timer count'); |
---|
142 | }, 4); |
---|
143 | } |
---|
144 | |
---|
145 | }); |
---|