-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.spec.ts
55 lines (47 loc) · 1.51 KB
/
type.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
import assert from 'assert'
import { Serialize, UndefinedAdapter, FullFunctionAdapter } from '../src/index'
import { getTypeofDetailed, TypeExtra, TypeNativeNonSerializable } from '../src/type'
class NamedClassWithoutMethods {}
class NamedClassWithMethods {
foo (c: string) {
return c + 'bar'
}
}
class NamedArray extends Array {
foo2 (d: number) {
return d + 1
}
}
const ser = new Serialize()
ser.register(UndefinedAdapter, FullFunctionAdapter)
describe('Special Types', () => {
const specialTypes: Record<TypeExtra | TypeNativeNonSerializable, any[]> = {
Null: [null],
NaN: [NaN],
Named: [new NamedClassWithMethods(), new NamedClassWithoutMethods(), new NamedArray(5)],
Infinity: [Infinity, -Infinity],
Array: [new Array(5)],
Constructor: [NamedClassWithMethods, NamedClassWithoutMethods, Array, NamedArray],
bigint: [BigInt(900719925474099133333332)],
symbol: [Symbol('hello')],
undefined: [undefined],
object: [{ a: 1 }],
function: [function fnLiteral (a: any) { return a }, (b: any) => b]
}
Object.entries(specialTypes).map(([typeName, entries]) => {
describe(typeName, () => {
entries.map((el) => {
const is = getTypeofDetailed(el).is
it(is.join(', '), () => {
const s0 = ser.stringify(el)
const r1 = ser.parse(s0)
if (is[0] !== 'symbol') {
assert.equal(s0, ser.stringify(r1))
} else {
assert.notEqual(s0, ser.stringify(r1))
}
})
})
})
})
})