forked from samchon/typia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_test_json_stringify.ts
33 lines (28 loc) · 1.01 KB
/
_test_json_stringify.ts
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
import { TestStructure } from "../helpers/TestStructure";
import { primitive_equal_to } from "../helpers/primitive_equal_to";
export const _test_json_stringify =
(name: string) =>
<T>(factory: TestStructure<T>) =>
(stringify: (input: T) => string) =>
() => {
const data: T = factory.generate();
const optimized: string = stringify(data);
if (predicate(data, optimized) === false)
throw new Error(
`Bug on typia.json.stringify(): failed to understand the ${name} type.`,
);
};
function predicate<T>(data: any, optimized: string): boolean {
// SPECIAL CASE, UNDEFINED
if (
optimized === undefined &&
(data === undefined ||
typeof data === "function" ||
(data.toJSON && data.toJSON() === undefined))
)
return true;
// DO COMPARE
const parsed: T = JSON.parse(optimized);
const expected: T = JSON.parse(JSON.stringify(data));
return primitive_equal_to(parsed, expected);
}