|
| 1 | +import { assertEquals } from "@std/assert"; |
| 2 | +import { isCustomJsonable } from "./custom_jsonable.ts"; |
| 3 | + |
| 4 | +export function buildTestcases() { |
| 5 | + return [ |
| 6 | + ["undefined", undefined], |
| 7 | + ["null", null], |
| 8 | + ["string", ""], |
| 9 | + ["number", 0], |
| 10 | + ["boolean", true], |
| 11 | + ["array", []], |
| 12 | + ["object", {}], |
| 13 | + ["bigint", 0n], |
| 14 | + ["function", () => {}], |
| 15 | + ["symbol", Symbol()], |
| 16 | + ] as const satisfies readonly (readonly [name: string, value: unknown])[]; |
| 17 | +} |
| 18 | + |
| 19 | +Deno.test("isCustomJsonable", async (t) => { |
| 20 | + for (const [name, value] of buildTestcases()) { |
| 21 | + await t.step(`return false for ${name}`, () => { |
| 22 | + assertEquals(isCustomJsonable(value), false); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + for (const [name, value] of buildTestcases()) { |
| 27 | + switch (name) { |
| 28 | + case "undefined": |
| 29 | + case "null": |
| 30 | + // Skip undefined, null that is not supported by Object.assign. |
| 31 | + continue; |
| 32 | + default: |
| 33 | + // Object.assign() doesn't make a value CustomJsonable. |
| 34 | + await t.step( |
| 35 | + `return false for ${name} even if it is wrapped by Object.assign()`, |
| 36 | + () => { |
| 37 | + assertEquals( |
| 38 | + isCustomJsonable( |
| 39 | + Object.assign(value as NonNullable<unknown>, { a: 0 }), |
| 40 | + ), |
| 41 | + false, |
| 42 | + ); |
| 43 | + }, |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + for (const [name, value] of buildTestcases()) { |
| 49 | + switch (name) { |
| 50 | + case "undefined": |
| 51 | + case "null": |
| 52 | + // Skip undefined, null that is not supported by Object.assign. |
| 53 | + continue; |
| 54 | + default: |
| 55 | + // toJSON method applied with Object.assign() makes a value CustomJsonable. |
| 56 | + await t.step( |
| 57 | + `return true for ${name} if it has own toJSON method`, |
| 58 | + () => { |
| 59 | + assertEquals( |
| 60 | + isCustomJsonable( |
| 61 | + Object.assign(value as NonNullable<unknown>, { |
| 62 | + toJSON: () => "custom", |
| 63 | + }), |
| 64 | + ), |
| 65 | + true, |
| 66 | + ); |
| 67 | + }, |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + for (const [name, value] of buildTestcases()) { |
| 73 | + switch (name) { |
| 74 | + case "undefined": |
| 75 | + case "null": |
| 76 | + // Skip undefined, null that does not have constructor. |
| 77 | + continue; |
| 78 | + case "string": |
| 79 | + case "number": |
| 80 | + case "boolean": |
| 81 | + case "symbol": |
| 82 | + // toJSON method defined in the class prototype does NOT make a value CustomJsonable if the value is |
| 83 | + // string, number, boolean, or symbol. |
| 84 | + // See https://tc39.es/ecma262/multipage/structured-data.html#sec-serializejsonproperty for details. |
| 85 | + await t.step( |
| 86 | + `return false for ${name} if the class prototype defines toJSON method`, |
| 87 | + () => { |
| 88 | + const proto = Object.getPrototypeOf(value); |
| 89 | + proto.toJSON = () => "custom"; |
| 90 | + try { |
| 91 | + assertEquals(isCustomJsonable(value), false); |
| 92 | + } finally { |
| 93 | + delete proto.toJSON; |
| 94 | + } |
| 95 | + }, |
| 96 | + ); |
| 97 | + break; |
| 98 | + default: |
| 99 | + // toJSON method defined in the class prototype makes a value CustomJsonable. |
| 100 | + await t.step( |
| 101 | + `return true for ${name} if the class prototype defines toJSON method`, |
| 102 | + () => { |
| 103 | + const proto = Object.getPrototypeOf(value); |
| 104 | + proto.toJSON = () => "custom"; |
| 105 | + try { |
| 106 | + assertEquals(isCustomJsonable(value), true); |
| 107 | + } finally { |
| 108 | + delete proto.toJSON; |
| 109 | + } |
| 110 | + }, |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | +}); |
0 commit comments