-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
object.test.js
63 lines (61 loc) · 2.1 KB
/
object.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
import * as t from './testing.js'
import * as object from './object.js'
import * as math from './math.js'
/**
* @param {t.TestCase} _tc
*/
export const testObject = _tc => {
t.assert(object.create().constructor === undefined, 'object.create creates an empty object without constructor')
t.describe('object.equalFlat')
t.assert(object.equalFlat({}, {}), 'comparing equal objects')
t.assert(object.equalFlat({ x: 1 }, { x: 1 }), 'comparing equal objects')
t.assert(object.equalFlat({ x: 'dtrn' }, { x: 'dtrn' }), 'comparing equal objects')
t.assert(!object.equalFlat({ x: {} }, { x: {} }), 'flatEqual does not dive deep')
t.assert(object.equalFlat({ x: undefined }, { x: undefined }), 'flatEqual handles undefined')
t.assert(!object.equalFlat({ x: undefined }, { y: {} }), 'flatEqual handles undefined')
t.describe('object.every')
t.assert(object.every({ a: 1, b: 3 }, (v, k) => (v % 2) === 1 && k !== 'c'))
t.assert(!object.every({ a: 1, b: 3, c: 5 }, (v, k) => (v % 2) === 1 && k !== 'c'))
t.describe('object.some')
t.assert(object.some({ a: 1, b: 3 }, (v, k) => v === 3 && k === 'b'))
t.assert(!object.some({ a: 1, b: 5 }, (v, _k) => v === 3))
t.assert(object.some({ a: 1, b: 5 }, () => true))
t.assert(!object.some({ a: 1, b: 5 }, (_v, _k) => false))
t.describe('object.forEach')
let forEachSum = 0
const r = { x: 1, y: 3 }
object.forEach(r, (v, _k) => { forEachSum += v })
t.assert(forEachSum === 4)
t.describe('object.map')
t.assert(object.map({ x: 1, z: 5 }, (v, _k) => v).reduce(math.add) === 6)
t.describe('object.length')
t.assert(object.length({}) === 0)
t.assert(object.length({ x: 1 }) === 1)
t.assert(object.isEmpty({}))
t.assert(!object.isEmpty({ a: 3 }))
}
/**
* @param {t.TestCase} _tc
*/
export const testFreeze = _tc => {
const o1 = { a: { b: [1, 2, 3] } }
const o2 = [1, 2, { a: 'hi' }]
object.deepFreeze(o1)
object.deepFreeze(o2)
t.fails(() => {
o1.a.b.push(4)
})
t.fails(() => {
o1.a.b = [1]
})
t.fails(() => {
o2.push(4)
})
t.fails(() => {
o2[2] = 42
})
t.fails(() => {
// @ts-ignore-next-line
o2[2].a = 'hello'
})
}