testValueSourceMakesItAgain
tcmts.loader.testValueSourceMakesItAgain · #sjshkb7m · term
import { valueSource } from "#codebase/tcmts/loader";
import assert from "node:assert/strict";
export const testValueSourceMakesItAgain = () => {
const again = (v: unknown) => new Function(`return (${valueSource(v)})`)() as unknown;
for (const v of [
1,
-0,
NaN,
-Infinity,
10n ** 30n,
'a
"b',
true,
null,
undefined,
[1, [2, "x"]],
{ a: 1, "b c": [null] },
])
assert.deepEqual(again(v), v);
assert.ok(Object.is(again(-0), -0));
const m = again(
new Map<unknown, unknown>([
[1, { a: new Set([2]) }],
["k", new Date(5)],
]),
) as Map<unknown, unknown>;
assert.deepEqual(
m,
new Map<unknown, unknown>([
[1, { a: new Set([2]) }],
["k", new Date(5)],
]),
);
assert.deepEqual(again(/a+b/gi), /a+b/gi);
// a key that sets the prototype when written plainly
const proto = JSON.parse('{"__proto__": 1}') as object;
assert.deepEqual(Object.keys(again(proto) as object), ["__proto__"]);
// what can't be made again the same
const shared = { x: 1 };
for (const v of [
() => 1,
new (class C {})(),
Object.freeze({ a: 1 }),
[shared, shared],
Symbol("s"),
// eslint-disable-next-line no-sparse-arrays -- a hole is what this checks
[1, , 3],
new Uint8Array(2),
{
get a() {
return 1;
},
},
])
assert.equal(valueSource(v), undefined);
assert.equal(
valueSource([shared], new Set([shared])),
undefined,
"another definition's object, which a copy wouldn't be",
);
assert.equal(valueSource("x".repeat(100), new Set(), 50), undefined, "too much code");
}
uses