| 1 | // Generated by continue |
| 2 | |
| 3 | import { incrementalParseJson } from "./incrementalParseJson"; |
| 4 | |
| 5 | describe("incrementalParseJson", () => { |
| 6 | it("should return [true, parsedJson] for valid JSON object", () => { |
| 7 | const raw = '{"key": "value"}'; |
| 8 | const [isValid, parsed] = incrementalParseJson(raw); |
| 9 | expect(isValid).toBe(true); |
| 10 | expect(parsed).toEqual({ key: "value" }); |
| 11 | }); |
| 12 | |
| 13 | it("should return [true, parsedJson] for valid JSON array", () => { |
| 14 | const raw = "[1, 2, 3]"; |
| 15 | const [isValid, parsed] = incrementalParseJson(raw); |
| 16 | expect(isValid).toBe(true); |
| 17 | expect(parsed).toEqual([1, 2, 3]); |
| 18 | }); |
| 19 | |
| 20 | it("should return [false, partialParsedJson] for incomplete JSON object", () => { |
| 21 | const raw = '{"key": "value"'; |
| 22 | const [isValid, parsed] = incrementalParseJson(raw); |
| 23 | expect(isValid).toBe(false); |
| 24 | expect(parsed).toEqual({ key: "value" }); |
| 25 | }); |
| 26 | |
| 27 | it("should return [false, partialParsedJson] for incomplete JSON array", () => { |
| 28 | const raw = "[1, 2, 3"; |
| 29 | const [isValid, parsed] = incrementalParseJson(raw); |
| 30 | expect(isValid).toBe(false); |
| 31 | expect(parsed).toEqual([1, 2, 3]); |
| 32 | }); |
| 33 | |
| 34 | it("should return [false, {}] for completely invalid JSON", () => { |
| 35 | const raw = "Hello, World!"; |
| 36 | const [isValid, parsed] = incrementalParseJson(raw); |
| 37 | expect(isValid).toBe(false); |
| 38 | expect(parsed).toEqual({}); |
| 39 | }); |
| 40 | |
| 41 | it("should return [false, {}] for empty string", () => { |
| 42 | const raw = ""; |
| 43 | const [isValid, parsed] = incrementalParseJson(raw); |
| 44 | expect(isValid).toBe(false); |
| 45 | expect(parsed).toEqual({}); |
| 46 | }); |
| 47 | |
| 48 | it("should return [false, {}] for invalid JSON syntax", () => { |
| 49 | const raw = "{key: 'value'}"; |
| 50 | const [isValid, parsed] = incrementalParseJson(raw); |
| 51 | expect(isValid).toBe(false); |
| 52 | expect(parsed).toEqual({}); |
| 53 | }); |
| 54 | |
| 55 | it("should handle whitespace-only string", () => { |
| 56 | const raw = " "; |
| 57 | const [isValid, parsed] = incrementalParseJson(raw); |
| 58 | expect(isValid).toBe(false); |
| 59 | expect(parsed).toEqual({}); |
| 60 | }); |
| 61 | |
| 62 | it("should handle numeric string", () => { |
| 63 | const raw = "12345"; |
| 64 | const [isValid, parsed] = incrementalParseJson(raw); |
| 65 | expect(isValid).toBe(true); |
| 66 | expect(parsed).toEqual(12345); |
| 67 | }); |
| 68 | |
| 69 | it("should handle boolean true string", () => { |
| 70 | const raw = "true"; |
| 71 | const [isValid, parsed] = incrementalParseJson(raw); |
| 72 | expect(isValid).toBe(true); |
| 73 | expect(parsed).toEqual(true); |
| 74 | }); |
| 75 | |
| 76 | it("should handle boolean false string", () => { |
| 77 | const raw = "false"; |
| 78 | const [isValid, parsed] = incrementalParseJson(raw); |
| 79 | expect(isValid).toBe(true); |
| 80 | expect(parsed).toEqual(false); |
| 81 | }); |
| 82 | |
| 83 | it("should handle null string", () => { |
| 84 | const raw = "null"; |
| 85 | const [isValid, parsed] = incrementalParseJson(raw); |
| 86 | expect(isValid).toBe(true); |
| 87 | expect(parsed).toBeNull(); |
| 88 | }); |
| 89 | }); |