pike.git / src / post_modules / JSON5 / testsuite.in

version» Context lines:

pike.git/src/post_modules/JSON5/testsuite.in:1: + START_MARKER    -  + test_true(Val.true) + test_false(Val.false) + test_false(Val.null) +  + test_eq(Standards.JSON5.true, Val.true) + test_eq(Standards.JSON5.false, Val.false) + test_eq(Standards.JSON5.null, Val.null) +  + test_eq(Standards.JSON5.ASCII_ONLY, 1) + test_eq(Standards.JSON5.HUMAN_READABLE, 2) + test_eq(Standards.JSON5.PIKE_CANONICAL, 4) +  + define(test_dec,[[ +  test_eq(Standards.JSON5.decode([[$1]]), [[$2]]) +  ]]) +  + define(test_enc,[[ +  test_eq(Standards.JSON5.encode([[$1]], +  Standards.JSON5.PIKE_CANONICAL|([[$3]]-0)), +  [[$2]]) + ]]) + define(test_dec_enc,[[ +  test_equal(Standards.JSON5.decode ([[$1]]), [[$2]]) +  test_eq(Standards.JSON5.validate ([[$1]]), -1) +  test_equal(Standards.JSON5.decode_utf8 (string_to_utf8 ([[$1]])), [[$2]]) +  test_eq(Standards.JSON5.validate_utf8 (string_to_utf8 ([[$1]])), -1) +  test_equal(Standards.JSON5.decode (Standards.JSON5.encode ([[$2]])), [[$2]]) +  test_equal(Standards.JSON5.decode (Standards.JSON5.encode ([[$2]], Standards.JSON5.ASCII_ONLY)), [[$2]]) +  test_equal(Standards.JSON5.decode (Standards.JSON5.encode ([[$2]], Standards.JSON5.HUMAN_READABLE)), [[$2]]) +  test_equal(Standards.JSON5.decode (Standards.JSON5.encode ([[$2]], Standards.JSON5.PIKE_CANONICAL)), [[$2]]) +  test_equal(Standards.JSON5.decode (Standards.JSON5.encode ([[$2]], 7)), [[$2]]) + ]]) +  + define(test_dec_enc_canon,[[ +  test_dec_enc([[$1]], [[$2]]) +  test_enc([[$2]], [[$1]], [[$3]]) + ]]) +  + define(test_dec_enc_string,[[ +  test_dec_enc_canon("\"[[$1]]\"", "[[$2]]", [[$3]]) +  test_eq(Standards.JSON5.escape_string ("[[$2]]", [[$3]]), "[[$1]]") + ]]) +  + define(test_dec_error,[[ +  test_eq(Standards.JSON5.validate ([[$1]]), [[$2]]) +  test_eval_error(Standards.JSON5.decode ([[$1]])) +  test_any([[ +  string s; +  if (catch (s = string_to_utf8 ($1))) return 1; +  return !!Standards.JSON5.validate_utf8 (s); +  ]], 1) +  test_eval_error(Standards.JSON5.decode_utf8 (string_to_utf8 ([[$1]]))) + ]]) +  + test_dec_enc_canon("-1", -1) + test_dec_enc_canon("0", 0) + test_dec("00", 0) + test_dec("+0", 0) + test_dec("-0", 0) + test_dec("1", 1) + test_dec("+1", 1) + test_dec("-1", -1) + test_dec_enc_canon((string) (Int.NATIVE_MAX + 1), Int.NATIVE_MAX+1) + test_dec_enc_canon((string) (Int.NATIVE_MIN - 1), Int.NATIVE_MIN-1) + test_enc(UNDEFINED, "null") +  + test_dec_enc_canon("-1.0", -1.0) + test_dec_enc_canon("0.0", 0.0) + test_dec("-0.0", 0.0) + test_dec("+0.0", 0.0) + test_dec_enc_canon("1.0", 1.0) + test_dec_enc_canon("-1.0", -1.0) + test_dec("+1.0", 1.0) + test_dec("01.0", 1.0) + test_dec("01.", 1.0) + test_dec("1.", 1.0) + test_dec(".0", 0.0) + test_dec(".1", 0.1) + test_dec("+.1", 0.1) + test_dec("-.1", -0.1) +  + test_eq(Standards.JSON5.encode(Math.inf),"null") + test_eq(Standards.JSON5.encode(Math.nan),"null") +  + test_true(Float.isnan(Standards.JSON5.decode("NaN"))); + test_true(Float.isnan(Standards.JSON5.decode("+NaN"))); + test_true(Float.isnan(Standards.JSON5.decode("-NaN"))); +  + test_dec("Infinity", Math.inf); + test_dec("+Infinity", Math.inf); + test_dec("-Infinity", -Math.inf); +  + test_dec("0x0", 0); + test_dec("+0x0", 0); + test_dec("-0x0", 0); +  + test_dec("0x1", 1); + test_dec("+0x1", 1); + test_dec("-0x1", -1); +  + test_dec("0x10", 16); + test_dec("+0x10", 16); + test_dec("-0x10", -16); +  + test_dec("''", ""); + test_dec("\"a \\\nb\"", "a \nb") + test_dec("'a \\\nb'", "a \nb") +  + test_dec_enc_canon("\"\"", "") + test_dec_enc_string(\\\"\\\\\\b\\f\\n\\r\\t, \"\\\b\f\n\r\t) + test_dec_enc_string(\u0123, \x0123) + test_dec_enc_string(\u20ac, \x20AC) + test_eval_error(Standards.JSON5.encode("\x0123456")) +  + test_dec_enc_string(\u2027\\u2028\\u2029\u2030, \u2027\u2028\u2029\u2030) + test_dec_enc_string(foo\\ud834\\udd1ebar, foo\U0001d11ebar, +  Standards.JSON5.ASCII_ONLY) + test_dec_enc_string(\ud7ff, \ud7ff) + test_dec_enc_string(\ue000, \ue000) + test_dec_error("\"\U00110000\"", 1) + test_dec_error("\"\\ud800\"", 7) + test_dec_error("\"\\ud800\\ud834\\udd1e\"", 12) + test_dec_error("\"\\udc47\"", 6) +  + test_dec_enc_canon("[]", ({})) + test_dec_enc_canon([[ "[1,2.0,\"3\"]" ]], ({1,2.0,"3"})) + test_eval_error([[ + array a = ({ 1, 2 }); + a[1] = a; + return Standards.JSON5.encode(a); + ]]) +  + test_dec_enc_canon("{}", ([])) + test_dec_enc_canon([["{\"a\":1,\"b\":2.0,\"c\":\"3\"}"]], +  ([ "a":1, "b":2.0, "c":"3" ])) + test_eval_error(Standards.JSON5.encode( ([1:2]) )) + test_eval_error([[ + mapping m = ([ "a":1 ]); + m->a = m; + return Standards.JSON5.encode(m); + ]]) +  + test_dec_enc_canon([["{\"a\":[{\"b\":2}]}"]], ([ "a":({(["b":2])}) ])) +  + test_enc(Val.null, "null") + test_eval_error( Standards.JSON5.encode(Val.Null) ) dnl FIXME? +  dnl No, we can't encode types. /mast + test_any([[ + class X { string encode_json5() { return "smallpox"; } }; + return Standards.JSON5.encode(X()); + ]], "smallpox") +  + test_true(functionp(Standards.JSON5.decode)) + test_true(functionp(Standards.JSON5.encode)) + test_true(functionp(Standards.JSON5.validate)) +  + // test the special types + test_eq(Val.true, Standards.JSON5.decode("true")) + test_eq(Val.false, Standards.JSON5.decode("false")) + test_eq(Val.null, Standards.JSON5.decode("null")) +  + // test numbers + test_eq(Standards.JSON5.decode("0E4"), 0.0) +  + // test reference counter. + // not sure how stable that is among pike versions.... + // It better be, or else we got garbage trouble. /mast + test_true(Debug.refs(Standards.JSON5.decode("\" It says little, does less, means nothing.\"")) == 1) + test_eq(Debug.refs(Standards.JSON5.decode(" { \"asd\" : 45 } ")), 1) + test_eq(Debug.refs(Standards.JSON5.decode(" [ 45, 34, 3, 4, 5 ] ")), 1) +  + // testing if junk at the end breaks + test_true(-1 != Standards.JSON5.validate_utf8("[ 32, 23, 43 , 54 ,45] uh")) + test_eq(-1, Standards.JSON5.validate_utf8("[ 32, 23, 43 , 54 ,45] ")) +  + test_dec_enc_string(\\\\/\\\"\\\"\\\\, \\/\"\"\\) + test_dec_enc_string(\\\\/skjdhf\\\"sdf\\\"d\\\\, \\/skjdhf\"sdf\"d\\) +  + test_equal(Standards.JSON5.decode("{a:1}"), (["a": 1])) + test_equal(Standards.JSON5.decode("{'a':1}"), (["a": 1])) + test_equal(Standards.JSON5.encode((["a":"b"]), Standards.JSON5.UNQUOTED_IDENTIFIERS), "{a:\"b\"}") + test_equal(Standards.JSON5.encode((["0a":"b"]), Standards.JSON5.UNQUOTED_IDENTIFIERS), "{\"0a\":\"b\"}") + test_equal(Standards.JSON5.encode((["0a":"b"]), Standards.JSON5.SINGLE_QUOTED_STRINGS), "{'0a':'b'}") + test_equal(Standards.JSON5.encode((["0a":"b'"]), Standards.JSON5.SINGLE_QUOTED_STRINGS), "{'0a':'b\\''}") + test_equal(Standards.JSON5.encode((["0a":"b\""]), Standards.JSON5.SINGLE_QUOTED_STRINGS), "{'0a':'b\"'}") + test_equal(Standards.JSON5.encode((["0a":"b\""])), "{\"0a\":\"b\\\"\"}") + test_equal(Standards.JSON5.encode((["0a":"b'"])), "{\"0a\":\"b'\"}") +  + // test examples from json.org + test_dec_enc([[ + "{\ +  \"glossary\": {\ +  \"title\": \"example glossary\",\ +  \"GlossDiv\": {\ +  \"title\": \"S\",\ +  \"GlossList\": {\ +  \"GlossEntry\": {\ +  \"ID\": \"SGML\",\ +  \"SortAs\": \"SGML\",\ +  \"GlossTerm\": \"Standard Generalized Markup Language\",\ +  \"Acronym\": \"SGML\",\ +  \"Abbrev\": \"ISO 8879:1986\",\ +  \"GlossDef\": {\ +  \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\ +  \"GlossSeeAlso\": [\"GML\", \"XML\"]\ +  },\ +  \"GlossSee\": \"markup\"\ +  }\ +  }\ +  }\ +  }\ + }\ + "]], [[([ +  "glossary": ([ +  "title": "example glossary", +  "GlossDiv": ([ +  "title": "S", +  "GlossList": ([ +  "GlossEntry": ([ +  "ID": "SGML", +  "SortAs": "SGML", +  "GlossTerm": "Standard Generalized Markup Language", +  "Acronym": "SGML", +  "Abbrev": "ISO 8879:1986", +  "GlossDef": ([ +  "para": "A meta-markup language, used to create markup languages such as DocBook.", +  "GlossSeeAlso": ({"GML", "XML"}) +  ]), +  "GlossSee": "markup" +  ]) +  ]) +  ]) +  ]) + ])]]) + test_dec_enc([[" { \"menu\": {\ +  \"id\": \"file\",\ +  \"value\": \"File\",\ +  \"popup\": {\ +  \"menuitem\": [\ +  {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\ +  {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\ +  {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\ +  ]\ +  }\ + } }\ + "]], [[(["menu": ([ +  "id": "file", +  "value": "File", +  "popup": ([ +  "menuitem": ({ +  (["value": "New", "onclick": "CreateNewDoc()"]), +  (["value": "Open", "onclick": "OpenDoc()"]), +  (["value": "Close", "onclick": "CloseDoc()"]) +  }) +  ]) + ])])]]) + test_dec_enc([["{\"menu\": {\ +  \"header\": \"SVG Viewer\",\ +  \"items\": [\ +  {\"id\": \"Open\"},\ +  {\"id\": \"OpenNew\", \"label\": \"Open New\"},\ +  null,\ +  {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},\ +  {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},\ +  {\"id\": \"OriginalView\", \"label\": \"Original View\"},\ +  null,\ +  {\"id\": \"Quality\"},\ +  {\"id\": \"Pause\"},\ +  {\"id\": \"Mute\"},\ +  null,\ +  {\"id\": \"Find\", \"label\": \"Find...\"},\ +  {\"id\": \"FindAgain\", \"label\": \"Find Again\"},\ +  {\"id\": \"Copy\"},\ +  {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},\ +  {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},\ +  {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},\ +  {\"id\": \"ViewSource\", \"label\": \"View Source\"},\ +  {\"id\": \"SaveAs\", \"label\": \"Save As\"},\ +  null,\ +  {\"id\": \"Help\"},\ +  {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}\ +  ]\ + }}\ + "]], [[(["menu": ([ +  "header": "SVG Viewer", +  "items": ({ +  (["id": "Open"]), +  (["id": "OpenNew", "label": "Open New"]), +  Standards.JSON5.null, +  (["id": "ZoomIn", "label": "Zoom In"]), +  (["id": "ZoomOut", "label": "Zoom Out"]), +  (["id": "OriginalView", "label": "Original View"]), +  Standards.JSON5.null, +  (["id": "Quality"]), +  (["id": "Pause"]), +  (["id": "Mute"]), +  Standards.JSON5.null, +  (["id": "Find", "label": "Find..."]), +  (["id": "FindAgain", "label": "Find Again"]), +  (["id": "Copy"]), +  (["id": "CopyAgain", "label": "Copy Again"]), +  (["id": "CopySVG", "label": "Copy SVG"]), +  (["id": "ViewSVG", "label": "View SVG"]), +  (["id": "ViewSource", "label": "View Source"]), +  (["id": "SaveAs", "label": "Save As"]), +  Standards.JSON5.null, +  (["id": "Help"]), +  (["id": "About", "label": "About Adobe CVG Viewer..."]) +  }) + ])])]]) + test_dec_enc_canon([[ +  "\"" + String.int2char(76) + String.int2char(2000) + +  String.int2char(10000) + String.int2char(900000) + "\"" + ]], [[ +  "" + String.int2char(76) + String.int2char(2000) + +  String.int2char(10000) + String.int2char(900000) + "" + ]]) + test_eq([[Standards.JSON5.validate("{\"menu\": {\ +  \"header\": \"SVG Viewer\",\ +  \"items\": [\ +  {\"id\": \"Open\"},\ +  {\"id\": \"OpenNew\", \"label\": \"Open New\"},\ +  null,\ +  {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},\ +  {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},\ +  {\"id\": \"OriginalView\", \"label\": \"Original View\"},\ +  null,\ +  {\"id\": \"Quality\"},\ +  {\"id\": \"Pause\"},\ +  {\"id\": \"Mute\"},\ +  null,\ +  {\"id\": \"Find\", \"label\": \"Find...\"},\ +  {\"id\": \"FindAgain\", \"label\": \"Find Again\"},\ +  {\"id\": \"Copy\"},\ +  {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},\ +  {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},\ +  {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},\ +  {\"id\": \"ViewSource\", \"label\": \"View Source\"},\ +  {\"id\": \"SaveAs\", \"label\": \"Save As\"},\ +  null,\ +  {\"id\": \"Help\"},\ +  {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}\ +  ]\ + }}\ + ")]], -1 ) +  + test_eval_error(Standards.JSON5.decode(Standards.JSON5.encode("sdfsdf \xdfff skldjf "))) + test_eval_error(Standards.JSON5.decode_utf8(Standards.JSON5.encode(string_to_utf8("sdfsdf \xdfff skldjf ")))) + test_dec_enc_canon([["{\"key\":null}"]], (["key": Standards.JSON5.null])) + test_dec_enc("\"http:\\/\\/foobar\\/\"","http://foobar/"); +  + test_dec_enc_canon([["[\"abc\",\"r\344ksm\366rg\345s\",\"def\"]"]], +  [[({"abc", "r\344ksm\366rg\345s", "def"})]]) + test_dec_enc_canon([["[\"abc\",\"\u20acuro\",\"def\"]"]], +  [[({"abc", "\u20acuro", "def"})]]) +  + dnl http://testsuites.opera.com/JSON5/correctness/scripts/ + test_do(add_constant("parse",Standards.JSON5.decode)) + dnl 001 + test_equal(parse("{ \"test\": [1,2,3], \"test2\": \"value\", \"test3\": 5 }"), +  ([ "test": ({ 1, 2, 3 }), "test2": "value", "test3": 5 ]) ) + dnl 003 + test_equal(parse("{\"test\": 011}"), (["test": 11])) + dnl 004 + test_equal(parse("{\"test\": [0,1,2,3]}"), ([ "test": ({ 0,1,2,3 }) ]) ) + dnl 005 + test_equal(parse("{\"test\": false, \"test\": true}"), (["test":Val.true])) + dnl 011 + dnl 013 + test_equal(parse("{\"quotes\": \"" \\\\u0022 \"}"), +  ([ "quotes" : "" \\u0022 " ])) + dnl 014 + test_equal(parse("{ \"tab-separated\" : \"source text\", \r\n \"with\" : \"newline\" }"), +  ([ "tab-separated" : "source text", "with" : "newline" ])) + dnl 015 + dnl test_any([[ + dnl array ranges = ({ 0,8, + dnl 14,31, + dnl 127,127 }); + dnl foreach(ranges/2, [int from, int to]) + dnl foreach(enumerate(to+1-from,1,from), int c) + dnl if( !catch(parse( sprintf("{\"test\": \"%c\"}",c) )) ) + dnl return c; + dnl return -1; + dnl ]],-1) + dnl test_do([[ + dnl array ranges = ({ 9,13, + dnl 32,34, + dnl 35,126 }); + dnl foreach(ranges/2, [int from, int to]) + dnl parse("\""+(string)enumerate(to+1-from,1,from)+"\""); + dnl ]]) +  + test_dec("//\n0", 0); + test_dec("/* */0", 0); + test_dec("/**/0/**/", 0); + test_equal(parse("/* */{a: 1/* */}"), (["a":1])) + test_equal(parse("/* */[0, /* */1]"), ({0,1})) + test_eval_error(parse("//\n")); + test_eval_error(parse("/**/\n")); + test_eval_error(parse("/**//**/\n")); + test_eval_error(parse("//")) + test_dec("//\n1//\n", 1); + test_dec("/*2*/1", 1); + test_dec("//\n/**/1", 1); + test_equal(parse("{}"),([])) + test_equal(parse("[]"),({})) + test_equal(parse("[0,]"),({0})) + test_equal(parse("{a:1,}"), (["a": 1])) + test_eval_error(parse("{3:3}")) + test_eval_error(parse("{{}:3}")) + test_eval_error(parse("{[]:3}")) + test_equal(parse("{true:3}"), (["true": 3])) + test_equal(parse("{false:3}"), (["false": 3])) + test_equal(parse("{null:3}"), (["null": 3])) + test_equal(parse("{\"x\":\"x\"}"),(["x":"x"])) + test_equal(parse("{\"x\":3}"),(["x":3])) + test_equal(parse("{\"x\":{}}"),(["x":([])])) + test_equal(parse("{\"x\":[]}"),(["x":({})])) + test_equal(parse("{\"x\":true}"),(["x":Val.true])) + test_equal(parse("{\"x\":false}"),(["x":Val.false])) + test_equal(parse("{\"x\":null}"),(["x":Val.null])) + test_eq(Standards.JSON5.encode(class {}(), 0, "foo"),"foo") + test_eq(Standards.JSON5.encode(class {}(), 0, lambda(mixed ... a) { return "bar"; }),"bar") + test_do(add_constant("parse")) +  + END_MARKER   Newline at end of file added.