1 |
// DATA_TEMPLATE: dom_data |
2 |
oTest.fnStart( "Check behaviour of the data get functions that DataTables uses" ); |
3 |
|
4 |
$(document).ready( function () { |
5 |
// Slightly unusual test set this one, in that we don't really care about the DOM |
6 |
// but want to test the internal data handling functions but we do need a table to |
7 |
// get at the functions! |
8 |
var table = $('#example').dataTable(); |
9 |
var fn, test; |
10 |
|
11 |
// Object property access |
12 |
oTest.fnTest( |
13 |
"Single object, single property", |
14 |
function () { |
15 |
fn = table.oApi._fnGetObjectDataFn('test'); |
16 |
test = fn( { "test": true } ); |
17 |
}, |
18 |
function () { return test } |
19 |
); |
20 |
|
21 |
oTest.fnTest( |
22 |
"Single property from object", |
23 |
function () { |
24 |
fn = table.oApi._fnGetObjectDataFn('test'); |
25 |
test = fn( { "test": true, "test2": false } ); |
26 |
}, |
27 |
function () { return test } |
28 |
); |
29 |
|
30 |
oTest.fnTest( |
31 |
"Single property from object - different property", |
32 |
function () { |
33 |
fn = table.oApi._fnGetObjectDataFn('test2'); |
34 |
test = fn( { "test": true, "test2": false } ); |
35 |
}, |
36 |
function () { return test===false } |
37 |
); |
38 |
|
39 |
oTest.fnTest( |
40 |
"Undefined property from object", |
41 |
function () { |
42 |
fn = table.oApi._fnGetObjectDataFn('test3'); |
43 |
test = fn( { "test": true, "test2": false } ); |
44 |
}, |
45 |
function () { return test===undefined } |
46 |
); |
47 |
|
48 |
// Array index access |
49 |
oTest.fnTest( |
50 |
"Array access - index 0", |
51 |
function () { |
52 |
fn = table.oApi._fnGetObjectDataFn(0); |
53 |
test = fn( [true, false, false, false] ); |
54 |
}, |
55 |
function () { return test } |
56 |
); |
57 |
|
58 |
oTest.fnTest( |
59 |
"Array access - index 1", |
60 |
function () { |
61 |
fn = table.oApi._fnGetObjectDataFn(2); |
62 |
test = fn( [false, false, true, false] ); |
63 |
}, |
64 |
function () { return test } |
65 |
); |
66 |
|
67 |
oTest.fnTest( |
68 |
"Array access - undefined", |
69 |
function () { |
70 |
fn = table.oApi._fnGetObjectDataFn(7); |
71 |
test = fn( [false, false, true, false] ); |
72 |
}, |
73 |
function () { return test===undefined } |
74 |
); |
75 |
|
76 |
// null source |
77 |
oTest.fnTest( |
78 |
"null source", |
79 |
function () { |
80 |
fn = table.oApi._fnGetObjectDataFn( null ); |
81 |
test = fn( [false, false, true, false] ); |
82 |
}, |
83 |
function () { return test===null } |
84 |
); |
85 |
|
86 |
// nested objects |
87 |
oTest.fnTest( |
88 |
"Nested object property", |
89 |
function () { |
90 |
fn = table.oApi._fnGetObjectDataFn( 'a.b' ); |
91 |
test = fn( { |
92 |
"a":{ |
93 |
"b": true, |
94 |
"c": false, |
95 |
"d": 1 |
96 |
} |
97 |
} ); |
98 |
}, |
99 |
function () { return test } |
100 |
); |
101 |
|
102 |
oTest.fnTest( |
103 |
"Nested object property - different prop", |
104 |
function () { |
105 |
fn = table.oApi._fnGetObjectDataFn( 'a.d' ); |
106 |
test = fn( { |
107 |
"a":{ |
108 |
"b": true, |
109 |
"c": false, |
110 |
"d": 1 |
111 |
} |
112 |
} ); |
113 |
}, |
114 |
function () { return test===1 } |
115 |
); |
116 |
|
117 |
oTest.fnTest( |
118 |
"Nested object property - undefined prop", |
119 |
function () { |
120 |
fn = table.oApi._fnGetObjectDataFn( 'a.z' ); |
121 |
test = fn( { |
122 |
"a":{ |
123 |
"b": true, |
124 |
"c": false, |
125 |
"d": 1 |
126 |
} |
127 |
} ); |
128 |
}, |
129 |
function () { return test===undefined } |
130 |
); |
131 |
|
132 |
// Nested array |
133 |
oTest.fnTest( |
134 |
"Nested array index property", |
135 |
function () { |
136 |
fn = table.oApi._fnGetObjectDataFn( 'a.0' ); |
137 |
test = fn( { |
138 |
"a": [ |
139 |
true, |
140 |
false, |
141 |
1 |
142 |
] |
143 |
} ); |
144 |
}, |
145 |
function () { return test } |
146 |
); |
147 |
|
148 |
oTest.fnTest( |
149 |
"Nested array index property - different index", |
150 |
function () { |
151 |
fn = table.oApi._fnGetObjectDataFn( 'a.2' ); |
152 |
test = fn( { |
153 |
"a": [ |
154 |
true, |
155 |
false, |
156 |
1 |
157 |
] |
158 |
} ); |
159 |
}, |
160 |
function () { return test===1 } |
161 |
); |
162 |
|
163 |
oTest.fnTest( |
164 |
"Nested array index property - undefined index", |
165 |
function () { |
166 |
fn = table.oApi._fnGetObjectDataFn( 'a.10' ); |
167 |
test = fn( { |
168 |
"a": [ |
169 |
true, |
170 |
false, |
171 |
1 |
172 |
] |
173 |
} ); |
174 |
}, |
175 |
function () { return test===undefined } |
176 |
); |
177 |
|
178 |
// Nested array object property |
179 |
oTest.fnTest( |
180 |
"Nested array index object property", |
181 |
function () { |
182 |
fn = table.oApi._fnGetObjectDataFn( 'a.0.m' ); |
183 |
test = fn( { |
184 |
"a": [ |
185 |
{ "m": true, "n": 1 }, |
186 |
{ "m": false, "n": 2 }, |
187 |
{ "m": false, "n": 3 } |
188 |
] |
189 |
} ); |
190 |
}, |
191 |
function () { return test } |
192 |
); |
193 |
|
194 |
oTest.fnTest( |
195 |
"Nested array index object property - different index", |
196 |
function () { |
197 |
fn = table.oApi._fnGetObjectDataFn( 'a.2.n' ); |
198 |
test = fn( { |
199 |
"a": [ |
200 |
{ "m": true, "n": 1 }, |
201 |
{ "m": false, "n": 2 }, |
202 |
{ "m": false, "n": 3 } |
203 |
] |
204 |
} ); |
205 |
}, |
206 |
function () { return test===3 } |
207 |
); |
208 |
|
209 |
oTest.fnTest( |
210 |
"Nested array index object property - undefined index", |
211 |
function () { |
212 |
fn = table.oApi._fnGetObjectDataFn( 'a.0.z' ); |
213 |
test = fn( { |
214 |
"a": [ |
215 |
{ "m": true, "n": 1 }, |
216 |
{ "m": false, "n": 2 }, |
217 |
{ "m": false, "n": 3 } |
218 |
] |
219 |
} ); |
220 |
}, |
221 |
function () { return test===undefined } |
222 |
); |
223 |
|
224 |
// Array notation - no join |
225 |
oTest.fnTest( |
226 |
"Array notation - no join - property", |
227 |
function () { |
228 |
fn = table.oApi._fnGetObjectDataFn( 'a[].n' ); |
229 |
test = fn( { |
230 |
"a": [ |
231 |
{ "m": true, "n": 1 }, |
232 |
{ "m": false, "n": 2 }, |
233 |
{ "m": false, "n": 3 } |
234 |
] |
235 |
} ); |
236 |
}, |
237 |
function () { |
238 |
return test.length===3 && test[0]===1 |
239 |
&& test[1]===2 && test[2]===3; |
240 |
} |
241 |
); |
242 |
|
243 |
oTest.fnTest( |
244 |
"Array notation - no join - property (2)", |
245 |
function () { |
246 |
fn = table.oApi._fnGetObjectDataFn( 'a[].m' ); |
247 |
test = fn( { |
248 |
"a": [ |
249 |
{ "m": true, "n": 1 }, |
250 |
{ "m": false, "n": 2 } |
251 |
] |
252 |
} ); |
253 |
}, |
254 |
function () { |
255 |
return test.length===2 && test[0]===true |
256 |
&& test[1]===false; |
257 |
} |
258 |
); |
259 |
|
260 |
oTest.fnTest( |
261 |
"Array notation - no join - undefined property", |
262 |
function () { |
263 |
fn = table.oApi._fnGetObjectDataFn( 'a[].z' ); |
264 |
test = fn( { |
265 |
"a": [ |
266 |
{ "m": true, "n": 1 }, |
267 |
{ "m": false, "n": 2 } |
268 |
] |
269 |
} ); |
270 |
}, |
271 |
function () { |
272 |
return test.length===2 && test[0]===undefined |
273 |
&& test[1]===undefined; |
274 |
} |
275 |
); |
276 |
|
277 |
// Array notation - join |
278 |
oTest.fnTest( |
279 |
"Array notation - space join - property", |
280 |
function () { |
281 |
fn = table.oApi._fnGetObjectDataFn( 'a[ ].n' ); |
282 |
test = fn( { |
283 |
"a": [ |
284 |
{ "m": true, "n": 1 }, |
285 |
{ "m": false, "n": 2 }, |
286 |
{ "m": false, "n": 3 } |
287 |
] |
288 |
} ); |
289 |
}, |
290 |
function () { return test === '1 2 3'; } |
291 |
); |
292 |
|
293 |
oTest.fnTest( |
294 |
"Array notation - space join - property (2)", |
295 |
function () { |
296 |
fn = table.oApi._fnGetObjectDataFn( 'a[ ].m' ); |
297 |
test = fn( { |
298 |
"a": [ |
299 |
{ "m": true, "n": 1 }, |
300 |
{ "m": false, "n": 2 } |
301 |
] |
302 |
} ); |
303 |
}, |
304 |
function () { return test === 'true false'; } |
305 |
); |
306 |
|
307 |
oTest.fnTest( |
308 |
"Array notation - sapce join - undefined property", |
309 |
function () { |
310 |
fn = table.oApi._fnGetObjectDataFn( 'a[ ].z' ); |
311 |
test = fn( { |
312 |
"a": [ |
313 |
{ "m": true, "n": 1 }, |
314 |
{ "m": false, "n": 2 } |
315 |
] |
316 |
} ); |
317 |
}, |
318 |
function () { return test === ' '; } |
319 |
); |
320 |
oTest.fnTest( |
321 |
"Array notation - string join - property", |
322 |
function () { |
323 |
fn = table.oApi._fnGetObjectDataFn( 'a[qwerty].n' ); |
324 |
test = fn( { |
325 |
"a": [ |
326 |
{ "m": true, "n": 1 }, |
327 |
{ "m": false, "n": 2 }, |
328 |
{ "m": false, "n": 3 } |
329 |
] |
330 |
} ); |
331 |
}, |
332 |
function () { return test === '1qwerty2qwerty3'; } |
333 |
); |
334 |
|
335 |
oTest.fnTest( |
336 |
"Array notation - string join - property (2)", |
337 |
function () { |
338 |
fn = table.oApi._fnGetObjectDataFn( 'a[qwerty].m' ); |
339 |
test = fn( { |
340 |
"a": [ |
341 |
{ "m": true, "n": 1 }, |
342 |
{ "m": false, "n": 2 } |
343 |
] |
344 |
} ); |
345 |
}, |
346 |
function () { return test === 'trueqwertyfalse'; } |
347 |
); |
348 |
|
349 |
// Array alone join |
350 |
oTest.fnTest( |
351 |
"Flat array join", |
352 |
function () { |
353 |
fn = table.oApi._fnGetObjectDataFn( 'a[ ]' ); |
354 |
test = fn( { |
355 |
"a": [ |
356 |
true, |
357 |
false, |
358 |
1 |
359 |
] |
360 |
} ); |
361 |
}, |
362 |
function () { return test==="true false 1"; } |
363 |
); |
364 |
|
365 |
oTest.fnTest( |
366 |
"Flat array string join", |
367 |
function () { |
368 |
fn = table.oApi._fnGetObjectDataFn( 'a[qwerty]' ); |
369 |
test = fn( { |
370 |
"a": [ |
371 |
true, |
372 |
false, |
373 |
1 |
374 |
] |
375 |
} ); |
376 |
}, |
377 |
function () { return test==="trueqwertyfalseqwerty1"; } |
378 |
); |
379 |
|
380 |
oTest.fnTest( |
381 |
"Flat array no join", |
382 |
function () { |
383 |
fn = table.oApi._fnGetObjectDataFn( 'a[]' ); |
384 |
test = fn( { |
385 |
"a": [ |
386 |
true, |
387 |
false, |
388 |
1 |
389 |
] |
390 |
} ); |
391 |
}, |
392 |
function () { return test.length===3 && test[0]===true && |
393 |
test[1]===false && test[2]===1; } |
394 |
); |
395 |
|
396 |
|
397 |
|
398 |
oTest.fnComplete(); |
399 |
} ); |