/js/src/tests/ecma_5/JSON/parse.js

http://github.com/zpao/v8monkey · JavaScript · 178 lines · 149 code · 17 blank · 12 comment · 0 complexity · d524e0be25b49348e32b67bee970f8b6 MD5 · raw file

  1. // Ported from dom/src/json/test/unit/test_wrappers.js and
  2. // dom/src/json/test/unit/test_decode.js
  3. function assertIsObject(x)
  4. {
  5. assertEq(typeof x, "object");
  6. assertEq(x instanceof Object, true);
  7. }
  8. function assertIsArray(x)
  9. {
  10. assertIsObject(x);
  11. assertEq(Array.isArray(x), true);
  12. assertEq(Object.getPrototypeOf(x), Array.prototype);
  13. assertEq(x instanceof Array, true);
  14. assertEq(x.constructor, Array);
  15. }
  16. var x;
  17. var props;
  18. // empty object
  19. x = JSON.parse("{}");
  20. assertIsObject(x);
  21. assertEq(Object.getOwnPropertyNames(x).length, 0);
  22. // empty array
  23. x = JSON.parse("[]");
  24. assertIsArray(x);
  25. assertEq(x.length, 0);
  26. // one element array
  27. x = JSON.parse("[[]]");
  28. assertIsArray(x);
  29. assertEq(x.length, 1);
  30. assertIsArray(x[0]);
  31. assertEq(x[0].length, 0);
  32. // multiple arrays
  33. x = JSON.parse("[[],[],[]]");
  34. assertIsArray(x);
  35. assertEq(x.length, 3);
  36. assertIsArray(x[0]);
  37. assertEq(x[0].length, 0);
  38. assertIsArray(x[1]);
  39. assertEq(x[1].length, 0);
  40. assertIsArray(x[2]);
  41. assertEq(x[2].length, 0);
  42. // array key/value
  43. x = JSON.parse('{"foo":[]}');
  44. assertIsObject(x);
  45. props = Object.getOwnPropertyNames(x);
  46. assertEq(props.length, 1);
  47. assertEq(props[0], "foo");
  48. assertIsArray(x.foo);
  49. assertEq(x.foo.length, 0);
  50. x = JSON.parse('{"foo":[], "bar":[]}');
  51. assertIsObject(x);
  52. props = Object.getOwnPropertyNames(x).sort();
  53. assertEq(props.length, 2);
  54. assertEq(props[0], "bar");
  55. assertEq(props[1], "foo");
  56. assertIsArray(x.foo);
  57. assertEq(x.foo.length, 0);
  58. assertIsArray(x.bar);
  59. assertEq(x.bar.length, 0);
  60. // nesting
  61. x = JSON.parse('{"foo":[{}]}');
  62. assertIsObject(x);
  63. props = Object.getOwnPropertyNames(x);
  64. assertEq(props.length, 1);
  65. assertEq(props[0], "foo");
  66. assertIsArray(x.foo);
  67. assertEq(x.foo.length, 1);
  68. assertIsObject(x.foo[0]);
  69. assertEq(Object.getOwnPropertyNames(x.foo[0]).length, 0);
  70. x = JSON.parse('{"foo":[{"foo":[{"foo":{}}]}]}');
  71. assertIsObject(x.foo[0].foo[0].foo);
  72. x = JSON.parse('{"foo":[{"foo":[{"foo":[]}]}]}');
  73. assertIsArray(x.foo[0].foo[0].foo);
  74. // strings
  75. x = JSON.parse('{"foo":"bar"}');
  76. assertIsObject(x);
  77. props = Object.getOwnPropertyNames(x);
  78. assertEq(props.length, 1);
  79. assertEq(props[0], "foo");
  80. assertEq(x.foo, "bar");
  81. x = JSON.parse('["foo", "bar", "baz"]');
  82. assertIsArray(x);
  83. assertEq(x.length, 3);
  84. assertEq(x[0], "foo");
  85. assertEq(x[1], "bar");
  86. assertEq(x[2], "baz");
  87. // numbers
  88. x = JSON.parse('{"foo":5.5, "bar":5}');
  89. assertIsObject(x);
  90. props = Object.getOwnPropertyNames(x).sort();
  91. assertEq(props.length, 2);
  92. assertEq(props[0], "bar");
  93. assertEq(props[1], "foo");
  94. assertEq(x.foo, 5.5);
  95. assertEq(x.bar, 5);
  96. // keywords
  97. x = JSON.parse('{"foo": true, "bar":false, "baz":null}');
  98. assertIsObject(x);
  99. props = Object.getOwnPropertyNames(x).sort();
  100. assertEq(props.length, 3);
  101. assertEq(props[0], "bar");
  102. assertEq(props[1], "baz");
  103. assertEq(props[2], "foo");
  104. assertEq(x.foo, true);
  105. assertEq(x.bar, false);
  106. assertEq(x.baz, null);
  107. // short escapes
  108. x = JSON.parse('{"foo": "\\"", "bar":"\\\\", "baz":"\\b","qux":"\\f", "quux":"\\n", "quuux":"\\r","quuuux":"\\t"}');
  109. props = Object.getOwnPropertyNames(x).sort();
  110. assertEq(props.length, 7);
  111. assertEq(props[0], "bar");
  112. assertEq(props[1], "baz");
  113. assertEq(props[2], "foo");
  114. assertEq(props[3], "quuuux");
  115. assertEq(props[4], "quuux");
  116. assertEq(props[5], "quux");
  117. assertEq(props[6], "qux");
  118. assertEq(x.foo, '"');
  119. assertEq(x.bar, '\\');
  120. assertEq(x.baz, '\b');
  121. assertEq(x.qux, '\f');
  122. assertEq(x.quux, "\n");
  123. assertEq(x.quuux, "\r");
  124. assertEq(x.quuuux, "\t");
  125. // unicode escape
  126. x = JSON.parse('{"foo":"hmm\\u006dmm"}');
  127. assertIsObject(x);
  128. props = Object.getOwnPropertyNames(x);
  129. assertEq(props.length, 1);
  130. assertEq(props[0], "foo");
  131. assertEq("hmm\u006dmm", x.foo);
  132. x = JSON.parse('{"hmm\\u006dmm":"foo"}');
  133. assertIsObject(x);
  134. props = Object.getOwnPropertyNames(x);
  135. assertEq(props.length, 1);
  136. assertEq(props[0], "hmmmmm");
  137. assertEq(x.hmm\u006dmm, "foo");
  138. // miscellaneous
  139. x = JSON.parse('{"JSON Test Pattern pass3": {"The outermost value": "must be an object or array.","In this test": "It is an object." }}');
  140. assertIsObject(x);
  141. props = Object.getOwnPropertyNames(x);
  142. assertEq(props.length, 1);
  143. assertEq(props[0], "JSON Test Pattern pass3");
  144. assertIsObject(x["JSON Test Pattern pass3"]);
  145. props = Object.getOwnPropertyNames(x["JSON Test Pattern pass3"]).sort();
  146. assertEq(props.length, 2);
  147. assertEq(props[0], "In this test");
  148. assertEq(props[1], "The outermost value");
  149. assertEq(x["JSON Test Pattern pass3"]["The outermost value"],
  150. "must be an object or array.");
  151. assertEq(x["JSON Test Pattern pass3"]["In this test"], "It is an object.");
  152. /******************************************************************************/
  153. if (typeof reportCompare === "function")
  154. reportCompare(true, true);
  155. print("Tests complete");