forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.test.ts
More file actions
126 lines (114 loc) · 2.79 KB
/
json.test.ts
File metadata and controls
126 lines (114 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {expect, test} from 'vitest';
import {
type JSONValue,
assertJSONValue,
deepEqual,
isJSONValue,
} from './json.ts';
test('JSON deep equal', () => {
const t = (
a: JSONValue | undefined,
b: JSONValue | undefined,
expected = true,
) => {
const res = deepEqual(a, b);
const res2 = deepEqual(b, a);
expect(res).toBe(res2);
if (res !== expected) {
throw new Error(
JSON.stringify(a) + (expected ? ' === ' : ' !== ') + JSON.stringify(b),
);
}
};
const oneLevelOfData = [
0,
1,
2,
3,
456789,
true,
false,
null,
'',
'a',
'b',
'cdefefsfsafasdadsaas',
[],
{},
{x: 4, y: 5, z: 6},
[7, 8, 9],
] as const;
const testData = [
...oneLevelOfData,
[...oneLevelOfData],
Object.fromEntries(oneLevelOfData.map(v => [JSON.stringify(v), v])),
];
for (let i = 0; i < testData.length; i++) {
for (let j = 0; j < testData.length; j++) {
const a = testData[i];
// "clone" to ensure we do not end up with a and b being the same object.
const b = JSON.parse(JSON.stringify(testData[j]));
t(a, b, i === j);
}
}
t({a: 1, b: 2}, {b: 2, a: 1});
t({a: undefined}, {a: undefined});
t({a: 1}, Object.create({a: 1}), false);
});
test('assertJSONValue', () => {
assertJSONValue(null);
assertJSONValue(true);
assertJSONValue(false);
assertJSONValue(1);
assertJSONValue(123.456);
assertJSONValue('');
assertJSONValue('abc');
assertJSONValue([]);
assertJSONValue([1, 2, 3]);
assertJSONValue({});
assertJSONValue({a: 1, b: 2});
assertJSONValue({a: 1, b: 2, c: [3, 4, 5]});
assertJSONValue({a: 1, b: undefined});
assertJSONValue(
Object.create({b: Symbol()}, Object.getOwnPropertyDescriptors({a: 1})),
);
expect(() => assertJSONValue(Symbol())).toThrow(Error);
expect(() => assertJSONValue(() => 0)).toThrow(Error);
expect(() => assertJSONValue(undefined)).toThrow(Error);
expect(() => assertJSONValue(BigInt(123))).toThrow(Error);
// Cycle
const o = {x: {}};
o.x = o;
expect(() => assertJSONValue(o)).toThrow(Error);
});
test('isJSONValue', () => {
const t = (v: unknown, expectedPath?: (string | number)[]) => {
if (expectedPath) {
const path: (string | number)[] = [];
expect(isJSONValue(v, path)).toBe(false);
expect(path).toEqual(expectedPath);
} else {
expect(isJSONValue(v, [])).toBe(true);
}
};
t(null);
t(true);
t(false);
t(1);
t(123.456);
t('');
t('abc');
t([]);
t([1, 2, 3]);
t({});
t({a: 1, b: 2});
t({a: 1, b: 2, c: [3, 4, 5]});
t({x: undefined});
t(Object.create({b: Symbol()}, Object.getOwnPropertyDescriptors({a: 1})));
t(Symbol(), []);
t(() => 0, []);
t(undefined, []);
t(123n, []);
t([undefined], [0]);
t({x: [undefined]}, ['x', 0]);
});