json-parse-tests.js revision 998276643802ff9fb197fe220cbd9552da00a624
// Automated tests should only cover js API. Use a manual test for native API
Y.JSON.useNativeParse = false;
JSON_STRING = '[' +
'"JSON Test Pattern pass1",' +
'{"object with 1 member":["array with 1 element"]},' +
'{},' +
'[],' +
'-42,' +
'true,' +
'false,' +
'null,' +
'{' +
'"integer": 1234567890,' +
'"real": -9876.543210,' +
'"e": 0.123456789e-12,' +
'"E": 1.234567890E+34,' +
'"": 23456789012E66,' +
'"zero": 0,' +
'"one": 1,' +
'"space": " ",' +
'"quote": "\\"",' +
'"backslash": "\\\\",' +
'"controls": "\\b\\f\\n\\r\\t",' +
'"slash": "/ & \\/",' +
'"alpha": "abcdefghijklmnopqrstuvwxyz",' +
'"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",' +
'"digit": "0123456789",' +
'"0123456789": "digit",' +
'"special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",' +
'"hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",' +
'"true": true,' +
'"false": false,' +
'"null": null,' +
'"array":[ ],' +
'"object":{ },' +
'"address": "50 St. James Street",' +
'"url": "http://www.JSON.org/",' +
'"comment": "// /* <!-- --",' +
'"# -- --> */": " ",' +
'" s p a c e d " :[1,2 , 3,4 , 5 , 6 ,7 ],' +
'"compact":[1,2,3,4,5,6,7],' +
'"jsontext": "{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}",' +
'"quotes": "" \\u0022 %22 0x22 034 "",' +
'"\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?" : "A key can be any string"' +
'},' +
'0.5 ,98.6,' +
'99.44,' +
'1066,' +
'1e1,' +
'0.1e1,' +
'1e-1,' +
'1e00,' +
'2e+00,' +
'2e-00,' +
'"rosebud"]';
/*****************************/
/* Tests begin here */
/*****************************/
name : "parse",
_should : {
error : {
test_failOnEmptyString : true,
test_failOnFunction : true,
test_failOnRegex : true,
test_failOnNew : true,
test_failOnUnquotedVal : true,
test_failOnUnquotedKey : true,
test_failOnUnclosedObject : true,
test_failOnUnclosedArray : true,
test_failOnExtraCommaInObject : true,
test_failOnExtraCommaInArray : true,
test_failOnMissingValue : true,
test_failOnCommaAfterClose : true,
test_failOnValueAfterClose : true,
test_failOnExtraClose : true,
test_failOnExpression : true,
test_failOnZeroPrefixedNumber : true,
test_failOnHex : true,
test_failOnMissingColon : true,
test_failOnDoubleColon : true,
test_failOnCommaInsteadOfColon : true,
test_failOnColonInsteadOfComma : true,
test_failOnSingleQuote : true,
test_failOnTabCharacter : true,
test_failOnLineBreakChar : true,
test_failOnMismatchedClose : true,
test_failOnObjectInput : true,
test_failOnArrayInput : true,
test_failOnDateInput : true,
test_failOnRegExpInput : true,
test_failOnErrorInput : true,
test_failOnFunctionInput : true,
test_failOnNaNInput : true,
test_failOnInfinityInput : true,
test_failOnUndefinedInput : true
}
},
test_emptyObject : function () {
},
test_emptyArray : function () {
},
test_JSONNatives : function () {
// Note: backslashes are double escaped to emulate string returned from
// server.
var data = Y.JSON.parse('{"obj":{},"arr":[],"f":false,"t":true,"n":null,"int":12345,"fl":1.2345,"str":"String\\nwith\\tescapes"}');
},
test_basics : function () {
Y.Assert.areSame("A key can be any string",data[8]["\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"]);
},
test_nonObjectWrapper : function () {
//Y.Assert.areSame('this is a string',Y.JSON.parse('"this is a string"'));
//Y.Assert.areSame(true,Y.JSON.parse('true'));
//Y.Assert.areSame(12345,Y.JSON.parse("12345"));
//Y.Assert.areSame(1.2345,Y.JSON.parse("1.2345"));
},
test_failOnEmptyString : function () {
// parse should throw an error
},
test_failOnFunction : function () {
// parse should throw an error
},
test_failOnRegex : function () {
// parse should throw an error
},
test_failOnNew : function () {
// parse should throw an error
},
test_failOnUnquotedVal : function () {
// parse should throw an error
},
test_failOnUnquotedKey : function () {
// parse should throw an error
},
test_failOnUnclosedObject : function () {
// parse should throw an error
},
test_failOnUnclosedArray : function () {
// parse should throw an error
},
test_failOnExtraCommaInObject : function () {
// JS validator will allow, FF 3.1b2 native will allow. IE8 errors.
// eval will fail in IE6-7, but pass in others
// Trailing commas are invalid, but not a security risk, so acceptable
throw new Error("Parsed object with extra comma, but should have failed.");
},
test_failOnDoubleExtraCommaInObject : function () {
// parse should throw an error
},
test_failOnExtraCommaInArray : function () {
// Correct failure in IE6-8. FF accepts trailing commas without error
// Trailing commas are invalid, but not a security risk, so acceptable
throw new Error("Parsed array with extra comma, but should have failed.");
},
test_failOnDoubleExtraCommaInArray : function () {
// Correct failure in IE6-8. FF accepts trailing commas without error
// Trailing commas are invalid, but not a security risk, so acceptable
throw new Error("Parsed array with two extra commas, but should have failed.");
},
test_failOnMissingValue : function () {
// Correct failure in IE6-8. FF accepts trailing commas without error
// Trailing commas are invalid, but not a security risk, so acceptable
Y.log("Parsed array with missing value ("+data[0]+"), but should have failed.","warn","TestRunner");
},
test_failOnCommaAfterClose : function () {
// parse should throw an error
},
test_failOnValueAfterClose : function () {
// parse should throw an error
},
test_failOnExtraClose : function () {
// parse should throw an error
throw new Error("Parsed extra closing curly brace on object, but should have failed.");
},
test_failOnExpression : function () {
// parse should throw an error
},
test_failOnZeroPrefixedNumber : function () {
// Correct failure in IE8. FF accepts leading zeros without error
// Leading zeros are invalid, but not a security risk, so acceptable
throw new Error("Parsed zero prefixed number, but should have failed.");
},
test_failOnHex : function () {
// parse should throw an error
},
test_failOnIllegalBackslashEscape : function () {
// Correctly fails in all but IE8's native parse.
// The spec does not specify a limitation to the escape characters a
// decoder supports, so either is acceptable.
throw new Error("Parsed illegal backslash escape \\x15, but should have failed.");
},
test_failOnMissingColon : function () {
// parse should throw an error
},
test_failOnDoubleColon : function () {
// parse should throw an error
},
test_failOnCommaInsteadOfColon : function () {
// parse should throw an error
},
test_failOnColonInsteadOfComma : function () {
// parse should throw an error
},
test_failOnSingleQuote : function () {
// parse should throw an error
},
test_failOnLineBreakChar : function () {
// FF3.1b2 currently allows linebreak chars in native implementation
// Harmless, so permissable
throw new Error("Parsed unescaped line break character, but should have failed.");
},
test_failOnMismatchedClose : function () {
// parse should throw an error
},
test_failOnObjectInput: function () {
// parse should throw an error
},
test_arrayContainingValidJSON: function () {
// Should be ToString'ed to '{"foo":"bar"}' which is valid
},
test_failOnArrayInput: function () {
// parse should throw an error
},
test_failOnDateInput: function () {
// parse should throw an error
},
test_failOnRegExpInput: function () {
// parse should throw an error
},
test_failOnErrorInput: function () {
// parse should throw an error
},
test_failOnFunctionInput: function () {
// parse should throw an error
},
test_failOnNaNInput: function () {
// parse should throw an error
},
test_failOnInfinityInput: function () {
// parse should throw an error
},
test_failOnUndefinedInput: function () {
// Should be ToString'ed to 'undefined'
},
test_booleanInput: function () {
// Should be ToString'ed to 'true'
// Should be ToString'ed to 'true'
},
test_stringObjectInput: function () {
// Should be ToString'ed to '{"foo":"bar"}' which is valid
},
test_numberInput: function () {
},
test_nullInput: function () {
// Should be ToString'ed to 'null'
},
test_objectWithToStringInput: function () {
// Should be ToString'ed to '{"foo":"bar"}' which is valid
toString: function () {
return '{"foo":"bar"}';
}
});
}
}));
name : "reviver",
test_reviver : function () {
switch (k) {
case "alpha" : return "LOWER CASE";
case "ALPHA" : return "upper case";
case "true" :
case "false" :
case "null" : return undefined;
}
if (typeof v === 'number') {
return -(Math.abs(v|0));
}
v[99] = "NEW ITEM";
}
return v;
});
}
}));