-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
57 lines (50 loc) · 1.81 KB
/
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
var test = require('tape')
var dfs = require('.')
test('identity', function (assert) {
var expected = [
0,
1,
// outer: 2, inner: 4
[3, [5]],
6
]
assert.same(dfs(expected, x => x), expected, 'identity must not modify')
assert.end()
})
test('simple', function (assert) {
assert.same(dfs([], _ => assert.fail('unreachable')), [], 'empty tree')
assert.same(dfs([1, 2, 3], _ => dfs.break), [], 'break on root element causes empty tree')
assert.same(dfs([1, 2, 3], n => n > 2 ? dfs.break : n), [1, 2], 'break causes early return')
assert.same(dfs([1, 2, 3], n => n === 2 ? dfs.continue : n), [1, 3], 'continue skips element')
assert.same(dfs([1, 2, 3], _ => dfs.drop), null, 'drop on root element returns null')
assert.same(dfs([1, 2, 3], _ => dfs.continue), [], 'continue on root element causes empty tree')
assert.end()
})
test('order', function (assert) {
var expected = [
0,
1,
// outer: 2, inner: 4
[3, [5]],
6
]
var i = -1
var actual = dfs([null, null, [null, [null]], null], function (x) {
i++
if (Array.isArray(x)) return x
return i
})
assert.same(actual, expected)
assert.end()
})
test('nested', function (assert) {
assert.same(dfs([[], []], x => Array.isArray(x) ? x : assert.fail('unreachable')), [[], []])
assert.same(dfs([1, []], x => !Array.isArray(x) ? x : dfs.continue), [1])
assert.same(dfs([[], 1], x => !Array.isArray(x) ? x : dfs.continue), [1])
assert.same(dfs([[1]], x => Array.isArray(x) ? x : dfs.drop), [])
assert.same(dfs([[1]], x => dfs.spread(x)), [1])
assert.same(dfs([[[[[[1]]]]]], x => dfs.spread(x)), [1])
assert.same(dfs([[1, 2, 3], [4, 5, 6], [7, 8, 9]], x => Array.isArray(x) ? x : x % 2 === 0 ? x : dfs.continue), [[2], [4, 6], [8]])
assert.same(dfs([[[1], 2], 3], x => dfs.spread(x)), [1, 2, 3])
assert.end()
})