Cross Reference: /yui3/src/json/tests/src/json-stringify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
998276643802ff9fb197fe220cbd9552da00a624Luke Smith// Automated tests should only cover js API. Use a manual test for native API
998276643802ff9fb197fe220cbd9552da00a624Luke SmithY.JSON.useNativeStringify = false;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithvar suite = new Y.Test.Suite("Y.JSON.stringify (JavaScript implementation)");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "stringify",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith _should : {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith error : {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef1 : true,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef2 : true,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef3 : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith setUp: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.one("body").append('<form id="testbed" action="">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<h3>Form used for field value extraction, stringification</h3>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="text" id="empty_text">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="text" id="text" value="text">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="radio" name="radio" id="unchecked_radio" value="unchecked">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="radio" name="radio" id="checked_radio" value="radio" checked="checked">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="checkbox" name="box" id="unchecked_box" value="unchecked">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="checkbox" name="box" id="checked_box" value="box" checked="checked">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<textarea id="empty_textarea"></textarea>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<textarea id="textarea">textarea</textarea>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<select id="select">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="unselected">Unselected</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="selected" selected="selected">Selected</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '</select>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<select id="multiple_select" multiple="multiple" size="3">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="unselected">Unselected</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="selected" selected="selected">Selected</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="selected also" selected="selected">Selected also</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '</select>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<button id="button" type="button">content; no value</button>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<button id="button_with_value" type="button" value="button value">content and value</button>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<button id="button_submit" type="submit">content; no value</button>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<button id="button_submit_with_value" type="submit" value="submit button value">content and value</button>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="button" id="input_button" value="input button">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="submit" id="input_submit" value="input submit">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<!--input type="image" id="input_image" src="404.png" value="input image"-->' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '</form>');
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith tearDown: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.one("#testbed").remove(true);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyNatives: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('[true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify([true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith test_stringifyEscapes: function () {
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith Y.Assert.areSame('["\\\\","\\\\\'","\\\\\\\"","\\\\\\\"","\\b","\\b","\\\\b","\\\\\\b","\\\\\\b","\\b","\\\\x08","\\\\\\b","\\\\\\\\x08","\\n","\\\\n","\\\\\\n"]',
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith Y.JSON.stringify(['\\','\\\'', "\\\"", '\\"', "\b", '\b', "\\b","\\\b", '\\\b', "\x08", "\\x08", "\\\x08","\\\\x08", "\n", "\\n", "\\\n"]));
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith },
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyObject : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // stringify sorts the keys
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"one":1,"two":true,"three":false,"four":null,"five":"String with\\nnewline","six":{"nested":-0.12345}}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({one:1,two:true,three:false,four:null,five:"String with\nnewline",six : {nested:-0.12345}}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef1 : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var o = { key: 'value' };
998276643802ff9fb197fe220cbd9552da00a624Luke Smith o.recurse = o;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(o);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef2 : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var o = [1,2,3];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith o[3] = o;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(o);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Array with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef3 : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var o = [1,2,3,{key:"value",nest:[4,5,6,{foo:"bar"}]}];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith o[4] = o[3].x = o[3].nest[4] =
998276643802ff9fb197fe220cbd9552da00a624Luke Smith o[3].nest[3].y = o[3].nest[3].z = o;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(o);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyFunction : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"arr":[null]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith functions : function (are,ignored) {},
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr : [ function () {} ]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyRegex : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"regex":{},"arr":[{}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith regex : /are treated as objects/,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr : [ new RegExp("in array") ]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyUndefined : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"arr":[null]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith undef : undefined,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr : [ undefined ]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyDate : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // native toJSON method should be called if available
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var d = new Date(Date.UTC(1946,6,6)),
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ref = d.toJSON ? d.toJSON() : "1946-07-06T00:00:00Z";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"dt":"'+ref+'"}', Y.JSON.stringify({dt : d}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyFormValue : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function $(id) { return document.getElementById(id); }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var data = {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith empty_text : $('empty_text').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith text : $('text').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith unchecked_radio : $('unchecked_radio').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith checked_radio : $('checked_radio').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith unchecked_box : $('unchecked_box').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith checked_box : $('checked_box').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith empty_textarea : $('empty_textarea').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith textarea : $('textarea').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith select : $('select').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith multiple_select : $('multiple_select').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Buttons commented out for now because IE reports values
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // differently
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //button : $('button').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //button_with_value : $('button_with_value').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //button_submit : $('button_submit').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //button_submit_with_value: $('button_submit_with_value').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith input_button : $('input_button').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith input_submit : $('input_submit').value//,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //input_image : $('input_image').value
998276643802ff9fb197fe220cbd9552da00a624Luke Smith };
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"empty_text":"",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"text":"text",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"unchecked_radio":"unchecked",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"checked_radio":"radio",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"unchecked_box":"unchecked",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"checked_box":"box",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"empty_textarea":"",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"textarea":"textarea",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"select":"selected",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"multiple_select":"selected",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"button":"",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"button_with_value":"button value",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"button_submit":"",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"button_submit_with_value":"submit button value",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"input_button":"input button",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"input_submit":"input submit"}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"input_image":"input image"}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(data));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "whitelist",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_emptyWhitelistArray : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({foo:1,bar:[1,2,3],baz:true},[]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('[1,true,null,{},["string",null,{}]]',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify([
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,true,null,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : false,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : -0.12345
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },["string",undefined,/some regex/]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],[]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_whitelistArray : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO"}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [1,2,3],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },["foo"]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true},["foo","baz"]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /*
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // REMOVED for spec compatibility
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_whitelistObject : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // (undocumented) supports an obj literal as well
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }, {foo : true, baz : false})); // values ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "formatting",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_falseyIndents : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo0":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo0 : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,0));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /* Commented out because FF3.5 has infinite loop bug for neg indents
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Fixed in FF for next version.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo-4":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "foo-4" : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,-4));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo_false":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo_false : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,false));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo_empty":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo_empty : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,""));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_indentNumber : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame("{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"foo\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" 2,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" {\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"bar\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" 4,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" {\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"baz\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" 6,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" {\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"deep enough\": 7\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" }\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" ]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" }\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" ]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" }\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" ]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"}",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,2));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_indentString : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame("{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"Xo\"foo\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXo2,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXo{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXo\"bar\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXo4,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXo{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXo\"baz\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXoXo6,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXoXo{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXoXoXo\"deep enough\": 7\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXoXo}\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXo]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXo}\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXo]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXo}\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"Xo]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"}",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,"Xo"));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "toJSON",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_toJSON_on_object: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('"toJSON"',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({ toJSON: function () { return "toJSON"; } }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // TODO: complex object with toJSON
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_toJSON_on_proto: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function A() {}
998276643802ff9fb197fe220cbd9552da00a624Luke Smith A.prototype.toJSON = function () { return "A"; };
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function B() {}
998276643802ff9fb197fe220cbd9552da00a624Luke Smith B.prototype = new A();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function C() {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.x = "X";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.y = "Y";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.z = "Z";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith C.prototype = new B();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('"A"', Y.JSON.stringify(new C()));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "replacer",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_replacer : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // replacer applies to even simple value stringifications
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame("20",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(10,function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return typeof(v) === 'number' ? v * 2 : v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // replacer is applied to every nested property as well.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // can modify the host object en route
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // executes from the context of the key:value container
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"num":2,"alpha":"ABC","obj":{"nested_num":100,"alpha":"abc"},"arr":[2,null,4]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith num: 1,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith alpha: "abc",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ignore: "me",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith change: "to a function",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith toUpper: true,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith obj: {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nested_num: 50,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith undef: undefined,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith alpha: "abc"
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr: [1, 7, 2]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var t = typeof v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (k === 'change') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this property should then be ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return function () {};
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (k === 'ignore') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this property should then be ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return undefined;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (t === 'number') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // undefined returned to arrays should become null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v % 7 ? v * 2 : undefined;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this refers to the object containing the key:value
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (t === 'string' && (this.toUpper)) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // modify the object during stringification
998276643802ff9fb197fe220cbd9552da00a624Luke Smith delete this.toUpper;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v.toUpperCase();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // replacer works in conjunction with indent
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame("{\n_\"num\": 2,\n_\"alpha\": \"ABC\",\n_\"obj\": {\n__\"nested_num\": 100,\n__\"alpha\": \"abc\"\n_},\n_\"arr\": [\n__2,\n__null,\n__4\n_]\n}",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith num: 1,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith alpha: "abc",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ignore: "me",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith change: "to a function",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith toUpper: true,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith obj: {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nested_num: 50,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith undef: undefined,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith alpha: "abc"
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr: [1, 7, 2]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var t = typeof v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (k === 'change') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this property should then be ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return function () {};
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (k === 'ignore') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this property should then be ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return undefined;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (t === 'number') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // undefined returned to arrays should become null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v % 7 ? v * 2 : undefined;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this refers to the object containing the key:value
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (t === 'string' && (this.toUpper)) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // modify the object during stringification
998276643802ff9fb197fe220cbd9552da00a624Luke Smith delete this.toUpper;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v.toUpperCase();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },'_'));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_replacer_after_toJSON : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"a":"ABC"}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({a:{ toJSON: function () { return "abc"; } } },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return typeof v === 'string' ? v.toUpperCase() : v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Date instances in ES5 have toJSON that outputs toISOString, which
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // means the replacer should never receive a Date instance
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var str = Y.JSON.stringify([new Date()], function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return (v instanceof Date) ? "X" : v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith });
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame(-1, str.indexOf("X"), "Date incorrectly received by replacer");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_replacer_returning_Date : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var d = new Date(),
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ref = Y.JSON.stringify(d);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"dt":'+ref+',"date_from_replacer":{}}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({ dt: d, date_from_replacer: 1 },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return typeof v === 'number' ? d : v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke SmithY.Test.Runner.add(suite);