1 | define(["doh/main", "dojo/_base/array", "dojo/_base/lang"], function(doh, array, lang){ |
---|
2 | |
---|
3 | doh.register("tests._base.lang", [ |
---|
4 | function mixin(t){ |
---|
5 | t.assertEqual("object", typeof lang.mixin()); |
---|
6 | t.assertEqual("object", typeof lang.mixin(undefined)); |
---|
7 | t.assertEqual("object", typeof lang.mixin(null)); |
---|
8 | var src = { |
---|
9 | foo: function(){ |
---|
10 | t.debug("foo"); |
---|
11 | }, |
---|
12 | bar: "bar" |
---|
13 | }; |
---|
14 | var dest = {}; |
---|
15 | lang.mixin(dest, src); |
---|
16 | t.assertEqual("function", typeof dest["foo"]); |
---|
17 | t.assertEqual("string", typeof dest["bar"]); |
---|
18 | }, |
---|
19 | |
---|
20 | function extend(t){ |
---|
21 | var src = { |
---|
22 | foo: function(){ |
---|
23 | t.debug("foo"); |
---|
24 | }, |
---|
25 | bar: "bar" |
---|
26 | }; |
---|
27 | function dest(){} |
---|
28 | lang.extend(dest, src); |
---|
29 | var test = new dest(); |
---|
30 | t.assertEqual("function", typeof test["foo"]); |
---|
31 | t.assertEqual("string", typeof test["bar"]); |
---|
32 | }, |
---|
33 | |
---|
34 | function isFunction(t){ |
---|
35 | t.assertTrue(lang.isFunction(new Function())); |
---|
36 | t.assertTrue(lang.isFunction(isFunction)); |
---|
37 | if(lang.isBrowser){ // test the Safari workaround for NodeList |
---|
38 | t.assertFalse(lang.isFunction(lang.doc.getElementsByName("html"))); |
---|
39 | t.assertFalse(lang.isFunction(lang.doc.createElement("object"))); |
---|
40 | } |
---|
41 | }, |
---|
42 | |
---|
43 | function isObject(t){ |
---|
44 | t.assertFalse(lang.isObject(true)); |
---|
45 | t.assertFalse(lang.isObject(false)); |
---|
46 | t.assertFalse(lang.isObject("foo")); |
---|
47 | t.assertTrue(lang.isObject(new String("foo"))); |
---|
48 | t.assertTrue(lang.isObject(null)); |
---|
49 | t.assertTrue(lang.isObject({})); |
---|
50 | t.assertTrue(lang.isObject([])); |
---|
51 | t.assertTrue(lang.isObject(new Array())); |
---|
52 | }, |
---|
53 | |
---|
54 | function isArray(t){ |
---|
55 | t.assertTrue(lang.isArray([])); |
---|
56 | t.assertTrue(lang.isArray(new Array())); |
---|
57 | t.assertFalse(lang.isArray({})); |
---|
58 | }, |
---|
59 | |
---|
60 | function isArrayLike(t){ |
---|
61 | t.assertFalse(lang.isArrayLike("thinger")); |
---|
62 | t.assertTrue(lang.isArrayLike(new Array())); |
---|
63 | t.assertFalse(lang.isArrayLike({})); |
---|
64 | t.assertTrue(lang.isArrayLike(arguments)); |
---|
65 | }, |
---|
66 | |
---|
67 | function isString(t){ |
---|
68 | t.assertFalse(lang.isString(true)); |
---|
69 | t.assertFalse(lang.isString(false)); |
---|
70 | t.assertTrue(lang.isString("foo")); |
---|
71 | t.assertTrue(lang.isString(new String("foo"))); |
---|
72 | t.assertFalse(lang.isString(null)); |
---|
73 | t.assertFalse(lang.isString({})); |
---|
74 | t.assertFalse(lang.isString([])); |
---|
75 | }, |
---|
76 | |
---|
77 | function partial(t){ |
---|
78 | var scope = { foo: "bar" }; |
---|
79 | var scope2 = { foo: "baz" }; |
---|
80 | function thinger(arg1, arg2){ |
---|
81 | return [this.foo, arg1, arg2]; |
---|
82 | } |
---|
83 | |
---|
84 | var st1 = lang.partial(thinger); |
---|
85 | t.assertEqual("bar", st1.call(scope)[0]); |
---|
86 | t.assertEqual(undefined, st1()[0]); |
---|
87 | var st2 = lang.partial(thinger, "foo", "bar"); |
---|
88 | t.assertEqual("bar", st2()[2]); |
---|
89 | var st3 = lang.partial(thinger, "foo", "bar"); |
---|
90 | }, |
---|
91 | |
---|
92 | function nestedPartial(t){ |
---|
93 | function thinger(arg1, arg2){ |
---|
94 | return [arg1, arg2]; |
---|
95 | } |
---|
96 | |
---|
97 | var st1 = lang.partial(thinger, "foo"); |
---|
98 | t.assertEqual(undefined, st1()[1]); |
---|
99 | t.assertEqual("bar", st1("bar")[1]); |
---|
100 | |
---|
101 | // partials can accumulate |
---|
102 | var st2 = lang.partial(st1, "thud"); |
---|
103 | t.assertEqual("foo", st2()[0]); |
---|
104 | t.assertEqual("thud", st2()[1]); |
---|
105 | }, |
---|
106 | |
---|
107 | function hitch(t){ |
---|
108 | var scope = { foo: "bar" }; |
---|
109 | var scope2 = { foo: "baz" }; |
---|
110 | function thinger(){ |
---|
111 | return [this.foo, arguments.length]; |
---|
112 | } |
---|
113 | |
---|
114 | var st1 = lang.hitch(scope, thinger); |
---|
115 | t.assertEqual("bar", st1()[0]); |
---|
116 | t.assertEqual(0, st1()[1]); |
---|
117 | |
---|
118 | var st2 = lang.hitch(scope2, thinger); |
---|
119 | t.assertEqual("baz", st2()[0]); |
---|
120 | t.assertEqual(0, st1()[1]); |
---|
121 | t.assertEqual(1, st1("blah")[1]); |
---|
122 | |
---|
123 | // st2 should be "scope proof" |
---|
124 | t.assertEqual("baz", st2.call(scope)[0]); |
---|
125 | }, |
---|
126 | |
---|
127 | function hitchWithArgs(t){ |
---|
128 | var scope = { foo: "bar" }; |
---|
129 | var scope2 = { foo: "baz" }; |
---|
130 | function thinger(){ |
---|
131 | return [this.foo, arguments.length]; |
---|
132 | } |
---|
133 | |
---|
134 | var st1 = lang.hitch(scope, thinger, "foo", "bar"); |
---|
135 | t.assertEqual("bar", st1()[0]); |
---|
136 | t.assertEqual(2, st1()[1]); |
---|
137 | var st2 = lang.hitch(scope2, thinger, "foo", "bar"); |
---|
138 | t.assertEqual("baz", st2()[0]); |
---|
139 | t.assertEqual(2, st2()[1]); |
---|
140 | }, |
---|
141 | |
---|
142 | function hitchAsPartial(t){ |
---|
143 | var scope = { foo: "bar" }; |
---|
144 | var scope2 = { foo: "baz" }; |
---|
145 | function thinger(arg1, arg2){ |
---|
146 | return [this.foo, arg1, arg2]; |
---|
147 | } |
---|
148 | |
---|
149 | var st1 = lang.hitch(null, thinger); |
---|
150 | t.assertEqual("bar", st1.call(scope)[0]); |
---|
151 | t.assertEqual(undefined, st1()[0]); |
---|
152 | var st2 = lang.hitch(null, thinger, "foo", "bar"); |
---|
153 | t.assertEqual("bar", st2()[2]); |
---|
154 | var st3 = lang.hitch(null, thinger, "foo", "bar"); |
---|
155 | }, |
---|
156 | |
---|
157 | function _toArray(t){ |
---|
158 | var obj1 = [ 'foo', 'bar', 'spam', 'ham' ]; |
---|
159 | |
---|
160 | function thinger(){ |
---|
161 | return lang._toArray(arguments); |
---|
162 | } |
---|
163 | var obj2 = thinger.apply(this, obj1); |
---|
164 | t.assertEqual(obj1[0], obj2[0]); |
---|
165 | |
---|
166 | if(lang.isBrowser){ |
---|
167 | //test DomCollection |
---|
168 | var div = document.createElement('div'); |
---|
169 | div.innerHTML="<a href='#'>link</a>text"; |
---|
170 | var r=lang._toArray(div.childNodes); |
---|
171 | t.is(2,r.length); |
---|
172 | } |
---|
173 | }, |
---|
174 | |
---|
175 | function clone(t){ |
---|
176 | var obj1 = { |
---|
177 | foo: 'bar', |
---|
178 | answer: 42, |
---|
179 | jan102007: new Date(2007, 0, 10), |
---|
180 | baz: { |
---|
181 | a: null, |
---|
182 | b: [1, "b", 2.3, true, false], |
---|
183 | c: { |
---|
184 | d: undefined, |
---|
185 | e: 99, |
---|
186 | f: function(){ console.log(42); return 42; }, |
---|
187 | g: /\d+/gm |
---|
188 | } |
---|
189 | }, |
---|
190 | toString: function(){ return "meow"; } |
---|
191 | }; |
---|
192 | var obj2 = lang.clone(obj1); |
---|
193 | t.assertEqual(obj1.foo, obj2.foo); |
---|
194 | t.assertEqual(obj1.answer, obj2.answer); |
---|
195 | t.assertEqual(obj1.jan102007, obj2.jan102007); |
---|
196 | t.assertEqual(obj1.baz.a, obj2.baz.a); |
---|
197 | for(var i = 0; i < obj1.baz.b.length; ++i){ |
---|
198 | t.assertEqual(obj1.baz.b[i], obj2.baz.b[i]); |
---|
199 | } |
---|
200 | t.assertEqual(obj1.baz.c.d, obj2.baz.c.d); |
---|
201 | t.assertEqual(obj1.baz.c.e, obj2.baz.c.e); |
---|
202 | t.assertEqual(obj1.baz.c.f, obj2.baz.c.f); |
---|
203 | t.assertEqual(obj1.baz.c.f(), obj2.baz.c.f()); |
---|
204 | t.assertEqual(obj1.baz.c.g, obj2.baz.c.g); |
---|
205 | t.assertEqual(obj1.toString, obj2.toString); |
---|
206 | t.assertEqual(obj1.toString(), obj2.toString()); |
---|
207 | }, |
---|
208 | |
---|
209 | function delegate(t){ |
---|
210 | var a = { |
---|
211 | x: 1, |
---|
212 | y: function(){ return 2; }, |
---|
213 | z1: 99 |
---|
214 | }; |
---|
215 | var b = { |
---|
216 | x: 11, |
---|
217 | y: function(){ return 12; }, |
---|
218 | z2: 33, |
---|
219 | toString: function(){ return "bark!"; }, |
---|
220 | toLocaleString: function(){ return "le bark-s!"; } |
---|
221 | }; |
---|
222 | t.is(1, a.x); |
---|
223 | t.is(2, a.y()); |
---|
224 | t.is(99, a.z1); |
---|
225 | var c = lang.delegate(a, b); |
---|
226 | t.is(1, a.x); |
---|
227 | t.is(2, a.y()); |
---|
228 | t.is(99, a.z1); |
---|
229 | t.is(11, c.x); |
---|
230 | t.is(12, c.y()); |
---|
231 | t.is("bark!", c.toString()); |
---|
232 | t.is("le bark-s!", c.toLocaleString()); |
---|
233 | t.is(99, c.z1); |
---|
234 | t.is(33, c.z2); |
---|
235 | }, |
---|
236 | |
---|
237 | function replace(t){ |
---|
238 | var s1 = lang.replace("Hello, {name.first} {name.last} AKA {nick}!", |
---|
239 | { |
---|
240 | nick: "Bob", |
---|
241 | name: { |
---|
242 | first: "Robert", |
---|
243 | middle: "X", |
---|
244 | last: "Cringely" |
---|
245 | } |
---|
246 | }); |
---|
247 | t.is("Hello, Robert Cringely AKA Bob!", s1); |
---|
248 | |
---|
249 | var s2 = lang.replace("Hello, {0} {2}!", ["Robert", "X", "Cringely"]); |
---|
250 | t.is("Hello, Robert Cringely!", s2); |
---|
251 | |
---|
252 | function sum(a){ |
---|
253 | var t = 0; |
---|
254 | array.forEach(a, function(x){ t += x; }); |
---|
255 | return t; |
---|
256 | } |
---|
257 | var s3 = lang.replace( |
---|
258 | "{count} payments averaging {avg} USD per payment.", |
---|
259 | lang.hitch( |
---|
260 | { payments: [11, 16, 12] }, |
---|
261 | function(_, key){ |
---|
262 | switch(key){ |
---|
263 | case "count": return this.payments.length; |
---|
264 | case "min": return Math.min.apply(Math, this.payments); |
---|
265 | case "max": return Math.max.apply(Math, this.payments); |
---|
266 | case "sum": return sum(this.payments); |
---|
267 | case "avg": return sum(this.payments) / this.payments.length; |
---|
268 | } |
---|
269 | return ""; |
---|
270 | } |
---|
271 | )); |
---|
272 | t.is("3 payments averaging 13 USD per payment.", s3); |
---|
273 | |
---|
274 | var s4 = lang.replace("Hello, ${0} ${2}!", ["Robert", "X", "Cringely"], /\$\{([^\}]+)\}/g); |
---|
275 | t.is("Hello, Robert Cringely!", s4); |
---|
276 | } |
---|
277 | ] |
---|
278 | ); |
---|
279 | }); |
---|