-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathfunction.test.js
147 lines (133 loc) · 3.41 KB
/
function.test.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as f from './function.js'
import * as t from './testing.js'
/**
* @param {t.TestCase} _tc
*/
export const testBasics = _tc => {
let calls = 0
f.apply(() => calls++)
t.assert(calls === 1)
t.assert(f.isOneOf(1, [3, 2, 1]))
t.assert(!f.isOneOf(0, [3, 2, 1]))
// test is*
const arr = [1, 'two', [1], { one: 1 }]
arr.forEach(val => {
if (f.isArray(val)) {
/**
* @type {Array<any>}
*/
const yy = val
t.assert(yy)
}
if (f.isString(val)) {
/**
* @type {string}
*/
const yy = val
t.assert(yy)
}
if (f.isNumber(val)) {
/**
* @type {number}
*/
const yy = val
t.assert(yy)
}
if (f.is(val, String)) {
/**
* @type {string}
*/
const yy = val
t.assert(yy)
}
if (f.isTemplate(Number)(val)) {
/**
* @type {number}
*/
const yy = val
t.assert(yy)
}
})
}
/**
* @param {t.TestCase} _tc
*/
export const testCallAll = _tc => {
const err = new Error()
let calls = 0
try {
f.callAll([
() => {
calls++
},
() => {
throw err
},
f.nop,
() => {
calls++
}
], [])
} catch (e) {
t.assert(calls === 2)
return
}
t.fail('Expected callAll to throw error')
}
/**
* @param {t.TestCase} _tc
*/
export const testDeepEquality = _tc => {
t.assert(f.equalityDeep(1, 1))
t.assert(!f.equalityDeep(1, 2))
t.assert(!f.equalityDeep(1, '1'))
t.assert(!f.equalityDeep(1, null))
const obj = { b: 5 }
const map1 = new Map()
const map2 = new Map()
const map3 = new Map()
const map4 = new Map()
map1.set('a', obj)
map2.set('a', { b: 5 })
map3.set('b', obj)
map4.set('a', obj)
map4.set('b', obj)
t.assert(f.equalityDeep({ a: 4 }, { a: 4 }))
t.assert(f.equalityDeep({ a: 4, obj: { b: 5 } }, { a: 4, obj }))
t.assert(!f.equalityDeep({ a: 4 }, { a: 4, obj }))
t.assert(f.equalityDeep({ a: [], obj }, { a: [], obj }))
t.assert(!f.equalityDeep({ a: [], obj }, { a: [], obj: undefined }))
t.assert(f.equalityDeep({}, {}))
t.assert(!f.equalityDeep({}, { a: 4 }))
t.assert(f.equalityDeep([{ a: 4 }, 1], [{ a: 4 }, 1]))
t.assert(!f.equalityDeep([{ a: 4 }, 1], [{ a: 4 }, 2]))
t.assert(!f.equalityDeep([{ a: 4 }, 1], [{ a: 4 }, 1, 3]))
t.assert(f.equalityDeep([], []))
t.assert(!f.equalityDeep([1], []))
t.assert(f.equalityDeep(map1, map2))
t.assert(!f.equalityDeep(map1, map3))
t.assert(!f.equalityDeep(map1, map4))
const set1 = new Set([1])
const set2 = new Set([true])
const set3 = new Set([1, true])
const set4 = new Set([true])
t.assert(f.equalityDeep(set2, set4))
t.assert(!f.equalityDeep(set1, set2))
t.assert(!f.equalityDeep(set1, set3))
t.assert(!f.equalityDeep(set1, set4))
t.assert(!f.equalityDeep(set2, set3))
t.assert(f.equalityDeep(set2, set4))
const buf1 = Uint8Array.from([1, 2])
const buf2 = Uint8Array.from([1, 3])
const buf3 = Uint8Array.from([1, 2, 3])
const buf4 = Uint8Array.from([1, 2])
t.assert(!f.equalityDeep(buf1, buf2))
t.assert(!f.equalityDeep(buf2, buf3))
t.assert(!f.equalityDeep(buf3, buf4))
t.assert(f.equalityDeep(buf4, buf1))
t.assert(!f.equalityDeep(buf1.buffer, buf2.buffer))
t.assert(!f.equalityDeep(buf2.buffer, buf3.buffer))
t.assert(!f.equalityDeep(buf3.buffer, buf4.buffer))
t.assert(f.equalityDeep(buf4.buffer, buf1.buffer))
t.assert(!f.equalityDeep(buf1, buf4.buffer))
}