JSON.parse.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. test("basic functionality", () => {
  2. expect(JSON.parse).toHaveLength(2);
  3. const properties = [
  4. ["5", 5],
  5. ["null", null],
  6. ["true", true],
  7. ["false", false],
  8. ['"test"', "test"],
  9. ['[1,2,"foo"]', [1, 2, "foo"]],
  10. ['{"foo":1,"bar":"baz"}', { foo: 1, bar: "baz" }],
  11. ];
  12. properties.forEach(testCase => {
  13. expect(JSON.parse(testCase[0])).toEqual(testCase[1]);
  14. });
  15. });
  16. test("syntax errors", () => {
  17. [
  18. undefined,
  19. NaN,
  20. -NaN,
  21. Infinity,
  22. -Infinity,
  23. '{ "foo" }',
  24. '{ foo: "bar" }',
  25. "[1,2,3,]",
  26. "[1,2,3, ]",
  27. '{ "foo": "bar",}',
  28. '{ "foo": "bar", }',
  29. ].forEach(test => {
  30. expect(() => {
  31. JSON.parse(test);
  32. }).toThrow(SyntaxError);
  33. });
  34. });
  35. test("negative zero", () => {
  36. ["-0", " \n-0", "-0 \t", "\n\t -0\n ", "-0.0"].forEach(testCase => {
  37. expect(JSON.parse(testCase)).toEqual(-0.0);
  38. });
  39. expect(JSON.parse(-0)).toEqual(0);
  40. });
  41. // The underlying parser resolves decimal numbers by storing the decimal portion in an integer
  42. // This test handles a regression where the decimal portion was only using a u32 vs. u64
  43. // and would fail to parse.
  44. test("long decimal parse", () => {
  45. expect(JSON.parse("1644452550.6489999294281")).toEqual(1644452550.6489999294281);
  46. });