/
stringify.spec.ts
70 lines (66 loc) · 2.99 KB
/
stringify.spec.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
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
import * as fc from '../../../lib/fast-check';
import { stringify } from '../../../src/utils/stringify';
declare function BigInt(n: number | bigint | string): bigint;
const checkEqual = (a: any, b: any): boolean => {
try {
expect(a).toEqual(b);
return true;
} catch (err) {
return false;
}
};
describe('stringify', () => {
it('Should be able to stringify fc.anything()', () =>
fc.assert(fc.property(fc.anything(), a => typeof stringify(a) === 'string')));
it('Should be able to stringify fc.char16bits() (ie. possibly invalid strings)', () =>
fc.assert(fc.property(fc.char16bits(), a => typeof stringify(a) === 'string')));
if (typeof BigInt !== 'undefined') {
it('Should be able to stringify bigint in object correctly', () =>
fc.assert(fc.property(fc.bigInt(), b => stringify({ b }) === '{"b":' + b + 'n}')));
}
it('Should be equivalent to JSON.stringify for JSON compliant objects', () =>
fc.assert(
fc.property(
fc.anything({ values: [fc.boolean(), fc.integer(), fc.double(), fc.fullUnicodeString(), fc.constant(null)] }),
obj => {
expect(stringify(obj)).toEqual(JSON.stringify(obj));
}
)
));
it('Should stringify differently distinct objects', () =>
fc.assert(
fc.property(fc.anything(), fc.anything(), (a, b) => {
fc.pre(!checkEqual(a, b));
expect(stringify(a)).not.toEqual(stringify(b));
})
));
it('Should be able to stringify values', () => {
expect(stringify(null)).toEqual('null');
expect(stringify(undefined)).toEqual('undefined');
expect(stringify(false)).toEqual('false');
expect(stringify(42)).toEqual('42');
expect(stringify(-0)).toEqual('-0');
expect(stringify(Number.POSITIVE_INFINITY)).toEqual('Number.POSITIVE_INFINITY');
expect(stringify(Number.NEGATIVE_INFINITY)).toEqual('Number.NEGATIVE_INFINITY');
expect(stringify(Number.NaN)).toEqual('Number.NaN');
expect(stringify('Hello')).toEqual('"Hello"');
if (typeof BigInt !== 'undefined') {
expect(stringify(BigInt(42))).toEqual('42n');
}
});
it('Should be able to stringify boxed values', () => {
expect(stringify(new Boolean(false))).toEqual('new Boolean(false)');
expect(stringify(new Number(42))).toEqual('new Number(42)');
expect(stringify(new Number(-0))).toEqual('new Number(-0)');
expect(stringify(new Number(Number.POSITIVE_INFINITY))).toEqual('new Number(Number.POSITIVE_INFINITY)');
expect(stringify(new Number(Number.NEGATIVE_INFINITY))).toEqual('new Number(Number.NEGATIVE_INFINITY)');
expect(stringify(new Number(Number.NaN))).toEqual('new Number(Number.NaN)');
expect(stringify(new String('Hello'))).toEqual('new String("Hello")');
});
it('Should be able to stringify Set', () => {
expect(stringify(new Set([1, 2]))).toEqual('new Set([1,2])');
});
it('Should be able to stringify Map', () => {
expect(stringify(new Map([[1, 2]]))).toEqual('new Map([[1,2]])');
});
});