JSON.parse.js 898 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. load("test-common.js");
  2. try {
  3. assert(JSON.parse.length === 2);
  4. const properties = [
  5. ["5", 5],
  6. ["null", null],
  7. ["true", true],
  8. ["false", false],
  9. ['"test"', "test"],
  10. ['[1,2,"foo"]', [1, 2, "foo"]],
  11. ['{"foo":1,"bar":"baz"}', { foo: 1, bar: "baz" }],
  12. ];
  13. properties.forEach(testCase => {
  14. assertDeepEquals(JSON.parse(testCase[0]), testCase[1]);
  15. });
  16. let syntaxErrors = [
  17. undefined,
  18. NaN,
  19. -NaN,
  20. Infinity,
  21. -Infinity,
  22. '{ "foo" }',
  23. '{ foo: "bar" }',
  24. "[1,2,3,]",
  25. "[1,2,3, ]",
  26. '{ "foo": "bar",}',
  27. '{ "foo": "bar", }',
  28. ];
  29. syntaxErrors.forEach(error => assertThrowsError(() => {
  30. JSON.parse(error);
  31. }, {
  32. error: SyntaxError,
  33. }));
  34. console.log("PASS");
  35. } catch (e) {
  36. console.log("FAIL: " + e);
  37. }