| 2150 |
mathias |
1 |
if(!dojo._hasResource["tests._base.lang"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["tests._base.lang"] = true;
|
|
|
3 |
dojo.provide("tests._base.lang");
|
|
|
4 |
|
|
|
5 |
tests.register("tests._base.lang",
|
|
|
6 |
[
|
|
|
7 |
function mixin(t){
|
|
|
8 |
var src = {
|
|
|
9 |
foo: function(){
|
|
|
10 |
t.debug("foo");
|
|
|
11 |
},
|
|
|
12 |
bar: "bar"
|
|
|
13 |
};
|
|
|
14 |
var dest = {};
|
|
|
15 |
dojo.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 |
dojo.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 isObject(t){
|
|
|
35 |
t.assertFalse(dojo.isObject(true));
|
|
|
36 |
t.assertFalse(dojo.isObject(false));
|
|
|
37 |
t.assertFalse(dojo.isObject("foo"));
|
|
|
38 |
t.assertTrue(dojo.isObject(new String("foo")));
|
|
|
39 |
t.assertTrue(dojo.isObject(null));
|
|
|
40 |
t.assertTrue(dojo.isObject({}));
|
|
|
41 |
t.assertTrue(dojo.isObject([]));
|
|
|
42 |
t.assertTrue(dojo.isObject(new Array()));
|
|
|
43 |
},
|
|
|
44 |
|
|
|
45 |
function isArray(t){
|
|
|
46 |
t.assertTrue(dojo.isArray([]));
|
|
|
47 |
t.assertTrue(dojo.isArray(new Array()));
|
|
|
48 |
t.assertFalse(dojo.isArray({}));
|
|
|
49 |
},
|
|
|
50 |
|
|
|
51 |
function isArrayLike(t){
|
|
|
52 |
t.assertFalse(dojo.isArrayLike("thinger"));
|
|
|
53 |
t.assertTrue(dojo.isArrayLike(new Array()));
|
|
|
54 |
t.assertFalse(dojo.isArrayLike({}));
|
|
|
55 |
t.assertTrue(dojo.isArrayLike(arguments));
|
|
|
56 |
},
|
|
|
57 |
|
|
|
58 |
function isString(t){
|
|
|
59 |
t.assertFalse(dojo.isString(true));
|
|
|
60 |
t.assertFalse(dojo.isString(false));
|
|
|
61 |
t.assertTrue(dojo.isString("foo"));
|
|
|
62 |
t.assertTrue(dojo.isString(new String("foo")));
|
|
|
63 |
t.assertFalse(dojo.isString(null));
|
|
|
64 |
t.assertFalse(dojo.isString({}));
|
|
|
65 |
t.assertFalse(dojo.isString([]));
|
|
|
66 |
},
|
|
|
67 |
|
|
|
68 |
function partial(t){
|
|
|
69 |
var scope = { foo: "bar" };
|
|
|
70 |
var scope2 = { foo: "baz" };
|
|
|
71 |
function thinger(arg1, arg2){
|
|
|
72 |
return [this.foo, arg1, arg2];
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
var st1 = dojo.partial(thinger);
|
|
|
76 |
t.assertEqual("bar", st1.call(scope)[0]);
|
|
|
77 |
t.assertEqual(undefined, st1()[0]);
|
|
|
78 |
var st2 = dojo.partial(thinger, "foo", "bar");
|
|
|
79 |
t.assertEqual("bar", st2()[2]);
|
|
|
80 |
var st3 = dojo.partial(thinger, "foo", "bar");
|
|
|
81 |
},
|
|
|
82 |
|
|
|
83 |
function nestedPartial(t){
|
|
|
84 |
function thinger(arg1, arg2){
|
|
|
85 |
return [arg1, arg2];
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
var st1 = dojo.partial(thinger, "foo");
|
|
|
89 |
t.assertEqual(undefined, st1()[1]);
|
|
|
90 |
t.assertEqual("bar", st1("bar")[1]);
|
|
|
91 |
|
|
|
92 |
// partials can accumulate
|
|
|
93 |
var st2 = dojo.partial(st1, "thud");
|
|
|
94 |
t.assertEqual("foo", st2()[0]);
|
|
|
95 |
t.assertEqual("thud", st2()[1]);
|
|
|
96 |
},
|
|
|
97 |
|
|
|
98 |
function hitch(t){
|
|
|
99 |
var scope = { foo: "bar" };
|
|
|
100 |
var scope2 = { foo: "baz" };
|
|
|
101 |
function thinger(){
|
|
|
102 |
return [this.foo, arguments.length];
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
var st1 = dojo.hitch(scope, thinger);
|
|
|
106 |
t.assertEqual("bar", st1()[0]);
|
|
|
107 |
t.assertEqual(0, st1()[1]);
|
|
|
108 |
|
|
|
109 |
var st2 = dojo.hitch(scope2, thinger);
|
|
|
110 |
t.assertEqual("baz", st2()[0]);
|
|
|
111 |
t.assertEqual(0, st1()[1]);
|
|
|
112 |
t.assertEqual(1, st1("blah")[1]);
|
|
|
113 |
|
|
|
114 |
// st2 should be "scope proof"
|
|
|
115 |
t.assertEqual("baz", st2.call(scope)[0]);
|
|
|
116 |
},
|
|
|
117 |
|
|
|
118 |
function hitchWithArgs(t){
|
|
|
119 |
var scope = { foo: "bar" };
|
|
|
120 |
var scope2 = { foo: "baz" };
|
|
|
121 |
function thinger(){
|
|
|
122 |
return [this.foo, arguments.length];
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
var st1 = dojo.hitch(scope, thinger, "foo", "bar");
|
|
|
126 |
t.assertEqual("bar", st1()[0]);
|
|
|
127 |
t.assertEqual(2, st1()[1]);
|
|
|
128 |
var st2 = dojo.hitch(scope2, thinger, "foo", "bar");
|
|
|
129 |
t.assertEqual("baz", st2()[0]);
|
|
|
130 |
t.assertEqual(2, st2()[1]);
|
|
|
131 |
},
|
|
|
132 |
|
|
|
133 |
function hitchAsPartial(t){
|
|
|
134 |
var scope = { foo: "bar" };
|
|
|
135 |
var scope2 = { foo: "baz" };
|
|
|
136 |
function thinger(arg1, arg2){
|
|
|
137 |
return [this.foo, arg1, arg2];
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
var st1 = dojo.hitch(null, thinger);
|
|
|
141 |
t.assertEqual("bar", st1.call(scope)[0]);
|
|
|
142 |
t.assertEqual(undefined, st1()[0]);
|
|
|
143 |
var st2 = dojo.hitch(null, thinger, "foo", "bar");
|
|
|
144 |
t.assertEqual("bar", st2()[2]);
|
|
|
145 |
var st3 = dojo.hitch(null, thinger, "foo", "bar");
|
|
|
146 |
},
|
|
|
147 |
|
|
|
148 |
function _toArray(t){
|
|
|
149 |
var obj1 = [ 'foo', 'bar', 'spam', 'ham' ];
|
|
|
150 |
|
|
|
151 |
function thinger(){
|
|
|
152 |
return dojo._toArray(arguments);
|
|
|
153 |
}
|
|
|
154 |
var obj2 = thinger.apply(this, obj1);
|
|
|
155 |
t.assertEqual(obj1[0], obj2[0]);
|
|
|
156 |
},
|
|
|
157 |
|
|
|
158 |
function clone(t) {
|
|
|
159 |
var obj1 = {foo: 'bar', answer: 42, jan102007: new Date(2007, 0, 10),
|
|
|
160 |
baz: {
|
|
|
161 |
a: null,
|
|
|
162 |
b: [
|
|
|
163 |
1, "b", 2.3, true, false
|
|
|
164 |
//, function(){ return 4; }, /\d+/gm
|
|
|
165 |
]
|
|
|
166 |
}
|
|
|
167 |
};
|
|
|
168 |
var obj2 = dojo.clone(obj1);
|
|
|
169 |
t.assertEqual(obj1.foo, obj2.foo);
|
|
|
170 |
t.assertEqual(obj1.answer, obj2.answer);
|
|
|
171 |
t.assertEqual(obj1.jan102007, obj2.jan102007);
|
|
|
172 |
t.assertEqual(obj1.baz.a, obj2.baz.a);
|
|
|
173 |
for(var i = 0; i < obj1.baz.b.length; ++i){
|
|
|
174 |
t.assertEqual(obj1.baz.b[i], obj2.baz.b[i]);
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
]
|
|
|
178 |
);
|
|
|
179 |
|
|
|
180 |
}
|