json.html revision 32189201db6958c385f2d9e706b0639b6f2057d2
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith <link type="text/css" rel="stylesheet" href="/assets/dpSyntaxHighlighter.css">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith <link type="text/css" rel="stylesheet" href="/build/console/assets/skins/sam/console.css">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith <link type="text/css" rel="stylesheet" href="/build/test/assets/test-console.css">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith <style type="text/css" id="styleblock" class="highlight-ignore">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith font: normal 125%/1.4 Arial, sans-serif;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console .yui-console-content {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith font-size: 10px;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith width: 32em;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console .yui-console-bd {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith height: 50em;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console-entry-pass .yui-console-entry-cat {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith background: #070;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith color: #fff;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console-entry-fail .yui-console-entry-cat {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith background: #700;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith color: #fff;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .highlight-example {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith display: inline;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith float: left;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith width: 650px;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .highlight-example h2 {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith display: none;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/build/yui/yui.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/build/console/console-debug.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/build/json/json-debug.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/build/test/test-debug.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith filter : 'debug',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith useBrowserConsole : false,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith logInclude : { TestRunner: true }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}).use('test','console','json',function (Y) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithvar d = document,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith suite = new Y.Test.Suite("JSON Tests"),
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith JSON_STRING;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke SmithJSON_STRING = '[\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "JSON Test Pattern pass1",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith {"object with 1 member":["array with 1 element"]},\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "integer": 1234567890,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "real": -9876.543210,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "e": 0.123456789e-12,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "E": 1.234567890E+34,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "": 23456789012E66,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "space": " ",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "quote": "\\"",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "backslash": "\\\\",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "controls": "\\b\\f\\n\\r\\t",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "slash": "/ & \\/",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "alpha": "abcdefghijklmnopqrstuvwxyz",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "digit": "0123456789",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "0123456789": "digit",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "true": true,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "false": false,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "null": null,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "array":[ ],\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "object":{ },\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "address": "50 St. James Street",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "comment": "// /* <!-- --",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "# -- --> */": " ",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith " s p a c e d " :[1,2 , 3\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "jsontext": "{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "quotes": "" \\u0022 %22 0x22 034 "",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?" : "A key can be any string"\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith/*****************************/
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith/* Tests begin here */
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith/*****************************/
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith name : "parse",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith //test_failOnNonObjectWrapper : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnEmptyString : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnFunction : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnRegex : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnNew : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedVal : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedKey : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnclosedObject : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnclosedArray : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith //test_failOnExtraCommaInObject : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleExtraCommaInObject : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith //test_failOnExtraCommaInArray : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith //test_failOnDoubleExtraCommaInArray : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith //test_failOnMissingValue : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaAfterClose : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnValueAfterClose : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraClose : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExpression : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith //test_failOnZeroPrefixedNumber : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnHex : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnIllegalBackslashEscape : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMissingColon : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleColon : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaInsteadOfColon : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith //test_failOnExtraClose : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnColonInsteadOfComma : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnSingleQuote : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnTabCharacter : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnLineBreakChar : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMismatchedClose : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_JSONNatives : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // Note: backslashes are double escaped to emulate string returned from
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var data = Y.JSON.parse('{"obj":{},"arr":[],"f":false,"t":true,"n":null,"int":12345,"fl":1.2345,"str":"String\\nwith\\tescapes"}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("String\nwith\tescapes",data.str);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_basics : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var data = Y.JSON.parse(JSON_STRING);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isArray(data[1]["object with 1 member"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1,data[1]["object with 1 member"].length);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1234567890,data[8].integer);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-9876.543210,data[8].real);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(0.123456789e-12,data[8].e);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1.234567890e+34,data[8].E);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(23456789012E66,data[8][""]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(0,data[8].zero);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1,data[8].one);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(" ",data[8].space);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('"',data[8].quote);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("\\",data[8].backslash);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("\b\f\n\r\t",data[8].controls);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("/ & \/",data[8].slash);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("abcdefghijklmnopqrstuvwxyz",data[8].alpha);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("ABCDEFGHIJKLMNOPQRSTUVWXYZ",data[8].ALPHA);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("0123456789",data[8].digit);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("digit",data[8]["0123456789"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1234567890,data[8].integer);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("`1~!@#$%^&*()_+-={':[,]}|;.</>?",data[8].special);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",data[8].hex);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(true,data[8]["true"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(false,data[8]["false"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isNull(data[8]["null"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isArray(data[8]["array"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isObject(data[8]["object"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("http://www.JSON.org/",data[8].url);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("// /* <!-- --",data[8].comment);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(" ",data[8]["# -- --> */"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.ArrayAssert.itemsAreSame([1,2,3,4,5,6,7],data[8][" s p a c e d "]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.ArrayAssert.itemsAreSame([1,2,3,4,5,6,7],data[8].compact);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("{\"object with 1 member\":[\"array with 1 element\"]}",data[8].jsontext);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("" \u0022 %22 0x22 034 "",data[8].quotes);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("A key can be any string",data[8]["\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(98.6,data[10]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(99.44,data[11]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1066,data[12]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(0.1e1,data[14]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1e-1,data[15]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1e00,data[16]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(2e+00,data[17]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(2e-00,data[18]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("rosebud",data[19]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnNonObjectWrapper : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // Technically these should fail as invalid JSON
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('this is a string',Y.JSON.parse('"this is a string"'));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed raw string, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed raw boolean, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed raw number, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed raw null, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnEmptyString : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed empty string, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnFunction : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"fn":function(){}}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed a function, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnRegex : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"re":/abc/}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed regular expression literal, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnNew : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"dt":new Date()}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed <code>new Date()</code>, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedVal : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"foo":bar}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unquoted non-native value, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedKey : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unquoted object key, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnclosedObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"unclosed":"object"');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unclosed object, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnclosedArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["unclosed array"');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unclosed array, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraCommaInObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // Will fail in IE only
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"extra":"comma",}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed object with extra comma, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleExtraCommaInObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"extra":"commas",,}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed object with two extra commas, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraCommaInArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // Will fail in IE only
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["extra","comma",]');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed array with extra comma, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleExtraCommaInArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // Will fail in IE only
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["extra","commas",,]');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed array with two extra commas, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMissingValue : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed array with missing value, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaAfterClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed comma after array close, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnValueAfterClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"misplaced":"value"}" after close"');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed string value after object close, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed extra closing curly brace on object, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExpression : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed expression, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnZeroPrefixedNumber : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed zero prefixed number, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnHex : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed hex value, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnIllegalBackslashEscape : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["illegal backslash escape: \\x15"]');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed illegal backslash escape \\x15, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMissingColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed object with missing colon, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed double colon in object, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaInsteadOfColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed comma in place of colon, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnColonInsteadOfComma : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed colon in place of comma, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnSingleQuote : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed single quote, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnLineBreakChar : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unescaped line break character, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMismatchedClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed curly brace close for array, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyNatives: function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('[true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify([true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // stringify sorts the keys
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"one":1,"two":true,"three":false,"four":null,"five":"String with\\nnewline","six":{"nested":-0.12345}}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({one:1,two:true,three:false,four:null,five:"String with\nnewline",six : {nested:-0.12345}}));
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith test_stringifyRecursiveObject : function () {
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith var o = { key: 'value' };
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith Y.Assert.areSame('{"key":"value","recurse":null}',
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith o = [1,2,3];
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith Y.Assert.areSame('[1,2,3,null]', Y.JSON.stringify(o));
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith o = [1,2,3,{key:"value",nest:[4,5,6,{foo:"bar"}]}];
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith o[4] = o[3].x = o[3].nest[4] =
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith o[3].nest[3].y = o[3].nest[3].z = o;
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('[1,2,3,{"key":"value","nest":[4,5,6,{"foo":"bar","z":null,"y":null},null],"x":null},null]',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyFunction : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith functions : function (are,ignored) {},
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith arr : [ function () {} ]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyRegex : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith regex : /are treated as objects/,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyUndefined : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith undef : undefined,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith arr : [ undefined ]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyDate : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{"dt":"1946-07-06T07:00:00Z"}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({dt : new Date(1946,6,6)}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyWhitelistArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({foo:1,bar:[1,2,3],baz:true},[]));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('[1,true,null,{},["string",null,{}]]',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 1,true,null,{
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : false,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : -0.12345
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_whitelistArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO"}]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [1,2,3],
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_whitelistObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // (undocumented) supports an obj literal as well
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }, {foo : true, baz : false})); // values ignored
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_falseyIndents : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{"foo":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : [ 2, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [ 4, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : [ 6, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{"foo":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : [ 2, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [ 4, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : [ 6, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },null,""));
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"foo":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith foo : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },null,false));
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_indentNumber : function () {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith foo : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_indentString : function () {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith foo : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_reviver : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var data = Y.JSON.parse(JSON_STRING, function (k,v) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith switch (k) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith if (typeof v === 'number') {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return -(Math.abs(v|0));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-1234567890, data[8].integer);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-9876, data[8].real);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-1, data[8].one);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-3, data[8][" s p a c e d "][2]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("NEW ITEM", data[8][" s p a c e d "][99]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("NEW ITEM", data[8].compact[99]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_ : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_ : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke SmithY.Console.prototype.printLogEntry = function (m) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith this._htmlEscapeMessage(m),
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith cat_class : this.getClassName('entry',m.category),
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith src_class : this.getClassName('entry',m.source)
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var n = Y.Node.create(this.get('entryTemplate').replace(/\{([\w-]+)\}/g,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith function (_,k) { return k in m ? m[k] : ''; }));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith this._addToConsole(n);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return this;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithvar yconsole = new Y.Console({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith newestOnTop: false
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith if (m.category === 'section') {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith this._addToConsole(Y.Node.create("<h3>"+m.message+"</h3>"));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return this;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith } else if (m.category === "break") {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return this;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith } else if (m.category === "info") {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return this;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return Y.Console.prototype.printLogEntry.call(this,m);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke SmithY.log("Tests","section","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/assets/dpSyntaxHighlighter.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/assets/dpSyntaxHighlightExample.js"></script>