incremental parse json test
This commit is contained in:
parent
f3e4061730
commit
e2230f8074
1 changed files with 89 additions and 0 deletions
89
core/util/incrementalParseJson.test.ts
Normal file
89
core/util/incrementalParseJson.test.ts
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
// Generated by continue
|
||||
|
||||
import { incrementalParseJson } from "./incrementalParseJson";
|
||||
|
||||
describe("incrementalParseJson", () => {
|
||||
it("should return [true, parsedJson] for valid JSON object", () => {
|
||||
const raw = '{"key": "value"}';
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(true);
|
||||
expect(parsed).toEqual({ key: "value" });
|
||||
});
|
||||
|
||||
it("should return [true, parsedJson] for valid JSON array", () => {
|
||||
const raw = "[1, 2, 3]";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(true);
|
||||
expect(parsed).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it("should return [false, partialParsedJson] for incomplete JSON object", () => {
|
||||
const raw = '{"key": "value"';
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(false);
|
||||
expect(parsed).toEqual({ key: "value" });
|
||||
});
|
||||
|
||||
it("should return [false, partialParsedJson] for incomplete JSON array", () => {
|
||||
const raw = "[1, 2, 3";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(false);
|
||||
expect(parsed).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it("should return [false, {}] for completely invalid JSON", () => {
|
||||
const raw = "Hello, World!";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(false);
|
||||
expect(parsed).toEqual({});
|
||||
});
|
||||
|
||||
it("should return [false, {}] for empty string", () => {
|
||||
const raw = "";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(false);
|
||||
expect(parsed).toEqual({});
|
||||
});
|
||||
|
||||
it("should return [false, {}] for invalid JSON syntax", () => {
|
||||
const raw = "{key: 'value'}";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(false);
|
||||
expect(parsed).toEqual({});
|
||||
});
|
||||
|
||||
it("should handle whitespace-only string", () => {
|
||||
const raw = " ";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(false);
|
||||
expect(parsed).toEqual({});
|
||||
});
|
||||
|
||||
it("should handle numeric string", () => {
|
||||
const raw = "12345";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(true);
|
||||
expect(parsed).toEqual(12345);
|
||||
});
|
||||
|
||||
it("should handle boolean true string", () => {
|
||||
const raw = "true";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(true);
|
||||
expect(parsed).toEqual(true);
|
||||
});
|
||||
|
||||
it("should handle boolean false string", () => {
|
||||
const raw = "false";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(true);
|
||||
expect(parsed).toEqual(false);
|
||||
});
|
||||
|
||||
it("should handle null string", () => {
|
||||
const raw = "null";
|
||||
const [isValid, parsed] = incrementalParseJson(raw);
|
||||
expect(isValid).toBe(true);
|
||||
expect(parsed).toBeNull();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue