ladybird/Libraries/LibJS/Tests/builtins/JSON/JSON.rawJSON.js
aplefull 223c9c91e6
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
LibJS: Implement rawJSON and isRawJSON functions
2025-04-24 09:33:49 -04:00

57 lines
2.2 KiB
JavaScript

test("JSON.rawJSON basic functionality", () => {
expect(Object.isExtensible(JSON.rawJSON)).toBeTrue();
expect(typeof JSON.rawJSON).toBe("function");
expect(Object.getPrototypeOf(JSON.rawJSON)).toBe(Function.prototype);
expect(Object.getOwnPropertyDescriptor(JSON.rawJSON, "prototype")).toBeUndefined();
expect(JSON.stringify(JSON.rawJSON(1))).toBe("1");
expect(JSON.stringify(JSON.rawJSON(1.1))).toBe("1.1");
expect(JSON.stringify(JSON.rawJSON(-1))).toBe("-1");
expect(JSON.stringify(JSON.rawJSON(-1.1))).toBe("-1.1");
expect(JSON.stringify(JSON.rawJSON(1.1e1))).toBe("11");
expect(JSON.stringify(JSON.rawJSON(1.1e-1))).toBe("0.11");
expect(JSON.stringify(JSON.rawJSON(null))).toBe("null");
expect(JSON.stringify(JSON.rawJSON(true))).toBe("true");
expect(JSON.stringify(JSON.rawJSON(false))).toBe("false");
expect(JSON.stringify(JSON.rawJSON('"foo"'))).toBe('"foo"');
expect(JSON.stringify({ 42: JSON.rawJSON(37) })).toBe('{"42":37}');
expect(JSON.stringify({ x: JSON.rawJSON(1), y: JSON.rawJSON(2) })).toBe('{"x":1,"y":2}');
expect(JSON.stringify({ x: { x: JSON.rawJSON(1), y: JSON.rawJSON(2) } })).toBe(
'{"x":{"x":1,"y":2}}'
);
expect(JSON.stringify([JSON.rawJSON(1), JSON.rawJSON(1.1)])).toBe("[1,1.1]");
expect(
JSON.stringify([
JSON.rawJSON('"1"'),
JSON.rawJSON(true),
JSON.rawJSON(null),
JSON.rawJSON(false),
])
).toBe('["1",true,null,false]');
expect(JSON.stringify([{ x: JSON.rawJSON(1), y: JSON.rawJSON(1) }])).toBe('[{"x":1,"y":1}]');
});
test("JSON.rawJSON error cases", () => {
expect(() => JSON.rawJSON(Symbol("123"))).toThrow(TypeError);
expect(() => JSON.rawJSON(undefined)).toThrow(SyntaxError);
expect(() => JSON.rawJSON({})).toThrow(SyntaxError);
expect(() => JSON.rawJSON([])).toThrow(SyntaxError);
const illegalChars = ["\n", "\t", "\r", " "];
illegalChars.forEach(char => {
expect(() => {
JSON.rawJSON(`${char}123`);
}).toThrow(SyntaxError);
expect(() => {
JSON.rawJSON(`123${char}`);
}).toThrow(SyntaxError);
});
expect(() => {
JSON.rawJSON("");
}).toThrow(SyntaxError);
});