// Automated tests should only cover js API. Use a manual test for native API
Y.JSON.useNativeStringify = false;
name : "stringify",
_should : {
error : {
test_failOnStringifyCyclicalRef1 : true,
test_failOnStringifyCyclicalRef2 : true,
}
},
setUp: function () {
'<h3>Form used for field value extraction, stringification</h3>' +
'<input type="text" id="empty_text">' +
'<input type="text" id="text" value="text">' +
'<input type="radio" name="radio" id="unchecked_radio" value="unchecked">' +
'<input type="radio" name="radio" id="checked_radio" value="radio" checked="checked">' +
'<input type="checkbox" name="box" id="unchecked_box" value="unchecked">' +
'<input type="checkbox" name="box" id="checked_box" value="box" checked="checked">' +
'<textarea id="empty_textarea"></textarea>' +
'<textarea id="textarea">textarea</textarea>' +
'<select id="select">' +
'<option value="unselected">Unselected</option>' +
'<option value="selected" selected="selected">Selected</option>' +
'</select>' +
'<select id="multiple_select" multiple="multiple" size="3">' +
'<option value="unselected">Unselected</option>' +
'<option value="selected" selected="selected">Selected</option>' +
'<option value="selected also" selected="selected">Selected also</option>' +
'</select>' +
'<button id="button" type="button">content; no value</button>' +
'<button id="button_with_value" type="button" value="button value">content and value</button>' +
'<button id="button_submit" type="submit">content; no value</button>' +
'<button id="button_submit_with_value" type="submit" value="submit button value">content and value</button>' +
'<input type="button" id="input_button" value="input button">' +
'<input type="submit" id="input_submit" value="input submit">' +
'<!--input type="image" id="input_image" src="404.png" value="input image"-->' +
'</form>');
},
tearDown: function () {
},
test_stringifyNatives: function () {
Y.Assert.areSame('[true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]',
Y.JSON.stringify([true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]));
},
test_stringifyEscapes: function () {
Y.Assert.areSame('["\\\\","\\\\\'","\\\\\\\"","\\\\\\\"","\\b","\\b","\\\\b","\\\\\\b","\\\\\\b","\\b","\\\\x08","\\\\\\b","\\\\\\\\x08","\\n","\\\\n","\\\\\\n"]',
Y.JSON.stringify(['\\','\\\'', "\\\"", '\\"', "\b", '\b', "\\b","\\\b", '\\\b', "\x08", "\\x08", "\\\x08","\\\\x08", "\n", "\\n", "\\\n"]));
},
test_stringifyObject : function () {
// stringify sorts the keys
Y.Assert.areSame('{"one":1,"two":true,"three":false,"four":null,"five":"String with\\nnewline","six":{"nested":-0.12345}}',
Y.JSON.stringify({one:1,two:true,three:false,four:null,five:"String with\nnewline",six : {nested:-0.12345}}));
},
test_failOnStringifyCyclicalRef1 : function () {
var o = { key: 'value' };
o.recurse = o;
// Should throw an error
},
test_failOnStringifyCyclicalRef2 : function () {
var o = [1,2,3];
o[3] = o;
// Should throw an error
},
test_failOnStringifyCyclicalRef3 : function () {
// Should throw an error
},
test_stringifyFunction : function () {
arr : [ function () {} ]
}));
},
test_stringifyRegex : function () {
arr : [ new RegExp("in array") ]
}));
},
test_stringifyUndefined : function () {
}));
},
test_stringifyDate : function () {
// native toJSON method should be called if available
},
test_stringifyFormValue : function () {
var data = {
// Buttons commented out for now because IE reports values
// differently
//button : $('button').value,
//button_with_value : $('button_with_value').value,
//button_submit : $('button_submit').value,
//button_submit_with_value: $('button_submit_with_value').value,
//input_image : $('input_image').value
};
'"empty_text":"",'+
'"text":"text",'+
'"unchecked_radio":"unchecked",'+
'"checked_radio":"radio",'+
'"unchecked_box":"unchecked",'+
'"checked_box":"box",'+
'"empty_textarea":"",'+
'"textarea":"textarea",'+
'"select":"selected",'+
'"multiple_select":"selected",'+
//'"button":"",'+
//'"button_with_value":"button value",'+
//'"button_submit":"",'+
//'"button_submit_with_value":"submit button value",'+
'"input_button":"input button",'+
'"input_submit":"input submit"}',
//'"input_image":"input image"}',
}
}));
name : "whitelist",
test_emptyWhitelistArray : function () {
1,true,null,{
foo : false,
bar : -0.12345
],[]));
},
test_whitelistArray : function () {
foo : [
1,2,3,{
foo : "FOO",
baz : true
}
],
baz : true
},["foo"]));
foo : [
1,2,3,{
foo : "FOO",
baz : true
}
],
bar : [
1,2,3,{
foo : "FOO",
baz : true
}
],
}
/*
// REMOVED for spec compatibility
test_whitelistObject : function () {
// (undocumented) supports an obj literal as well
Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
Y.JSON.stringify({
foo : [
1,2,3,{
foo : "FOO",
baz : true
}
],
bar : [
1,2,3,{
foo : "FOO",
baz : true
}
],
baz : true
}, {foo : true, baz : false})); // values ignored
}
*/
}));
name : "formatting",
test_falseyIndents : function () {
foo0 : [ 2, {
bar : [ 4, {
baz : [ 6, {
"deep enough" : 7
}]
}]
}]
},null,0));
/* Commented out because FF3.5 has infinite loop bug for neg indents
* Fixed in FF for next version.
Y.Assert.areSame('{"foo-4":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
Y.JSON.stringify({
"foo-4" : [ 2, {
bar : [ 4, {
baz : [ 6, {
"deep enough" : 7
}]
}]
}]
},null,-4));
*/
foo_false : [ 2, {
bar : [ 4, {
baz : [ 6, {
"deep enough" : 7
}]
}]
}]
},null,false));
foo_empty : [ 2, {
bar : [ 4, {
baz : [ 6, {
"deep enough" : 7
}]
}]
}]
},null,""));
},
test_indentNumber : function () {
" \"foo\": [\n" +
" 2,\n" +
" {\n" +
" \"bar\": [\n" +
" 4,\n" +
" {\n" +
" \"baz\": [\n" +
" 6,\n" +
" {\n" +
" \"deep enough\": 7\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}",
foo : [ 2, {
bar : [ 4, {
baz : [ 6, {
"deep enough" : 7
}]
}]
}]
},null,2));
},
test_indentString : function () {
"Xo\"foo\": [\n" +
"XoXo2,\n" +
"XoXo{\n" +
"XoXoXo\"bar\": [\n" +
"XoXoXoXo4,\n" +
"XoXoXoXo{\n" +
"XoXoXoXoXo\"baz\": [\n" +
"XoXoXoXoXoXo6,\n" +
"XoXoXoXoXoXo{\n" +
"XoXoXoXoXoXoXo\"deep enough\": 7\n" +
"XoXoXoXoXoXo}\n" +
"XoXoXoXoXo]\n" +
"XoXoXoXo}\n" +
"XoXoXo]\n" +
"XoXo}\n" +
"Xo]\n" +
"}",
foo : [ 2, {
bar : [ 4, {
baz : [ 6, {
"deep enough" : 7
}]
}]
}]
},null,"Xo"));
}
}));
name : "toJSON",
test_toJSON_on_object: function () {
// TODO: complex object with toJSON
},
test_toJSON_on_proto: function () {
function A() {}
function B() {}
B.prototype = new A();
function C() {
this.x = "X";
this.y = "Y";
this.z = "Z";
}
C.prototype = new B();
}
}));
name : "replacer",
test_replacer : function () {
// replacer applies to even simple value stringifications
return typeof(v) === 'number' ? v * 2 : v;
}));
// replacer is applied to every nested property as well.
// can modify the host object en route
// executes from the context of the key:value container
num: 1,
alpha: "abc",
ignore: "me",
change: "to a function",
toUpper: true,
obj: {
nested_num: 50,
alpha: "abc"
},
},
function (k,v) {
var t = typeof v;
if (k === 'change') {
// this property should then be ignored
return function () {};
} else if (k === 'ignore') {
// this property should then be ignored
return undefined;
} else if (t === 'number') {
// undefined returned to arrays should become null
// this refers to the object containing the key:value
} else if (t === 'string' && (this.toUpper)) {
// modify the object during stringification
delete this.toUpper;
return v.toUpperCase();
} else {
return v;
}
}));
// replacer works in conjunction with indent
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}",
num: 1,
alpha: "abc",
ignore: "me",
change: "to a function",
toUpper: true,
obj: {
nested_num: 50,
alpha: "abc"
},
},
function (k,v) {
var t = typeof v;
if (k === 'change') {
// this property should then be ignored
return function () {};
} else if (k === 'ignore') {
// this property should then be ignored
return undefined;
} else if (t === 'number') {
// undefined returned to arrays should become null
// this refers to the object containing the key:value
} else if (t === 'string' && (this.toUpper)) {
// modify the object during stringification
delete this.toUpper;
return v.toUpperCase();
} else {
return v;
}
},'_'));
},
test_replacer_after_toJSON : function () {
function (k,v) {
return typeof v === 'string' ? v.toUpperCase() : v;
}));
// Date instances in ES5 have toJSON that outputs toISOString, which
// means the replacer should never receive a Date instance
return (v instanceof Date) ? "X" : v;
});
},
test_replacer_returning_Date : function () {
var d = new Date(),
function (k,v) {
return typeof v === 'number' ? d : v;
}));
}
}));