JSON.parse.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. });