-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
167 lines (140 loc) · 4.13 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {u} from 'unist-builder'
import {filter} from 'unist-util-filter'
test('filter', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('unist-util-filter')).sort(), [
'filter'
])
})
await t.test(
'should not traverse into children of filtered out nodes',
function () {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
/** @type {Record<string, number>} */
const types = {}
assert.deepEqual(filter(tree, predicate), u('root', [u('leaf', '2')]))
assert.deepEqual(types, {root: 1, node: 1, leaf: 1})
/**
* @param {Node} node
*/
function predicate(node) {
types[node.type] = (types[node.type] || 0) + 1
return node.type !== 'node'
}
}
)
await t.test(
'should return `undefined` if root node is filtered out',
function () {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
assert.deepEqual(filter(tree, predicate), undefined)
function predicate() {
return false
}
}
)
await t.test('should cascade-remove parent nodes', function () {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
assert.deepEqual(filter(tree, notOne), u('root', [u('leaf', '2')]))
assert.deepEqual(filter(tree, notLeaf), undefined)
/**
* @param {Node} node
*/
function notOne(node) {
return 'value' in node ? node.value !== '1' : true
}
/**
* @param {Node} node
*/
function notLeaf(node) {
return node.type !== 'leaf'
}
})
await t.test(
'should not cascade-remove nodes that were empty initially',
function () {
const tree = u('node', [u('node', []), u('node', [u('leaf')])])
assert.deepEqual(filter(tree, 'node'), u('node', [u('node', [])]))
}
)
await t.test(
'should call iterator with `index` and `parent` args',
function () {
const leaf1 = u('leaf', '1')
const leaf2 = u('leaf', '2')
const node = u('node', [leaf1])
const tree = u('root', [node, leaf2])
/** @type {Array<[Node, number | null | undefined, Parent | null | undefined]>} */
const callLog = []
assert.deepEqual(
filter(tree, function (a, b, c) {
callLog.push([a, b, c])
return true
}),
tree
)
assert.deepEqual(callLog, [
[tree, undefined, undefined],
[node, 0, tree],
[leaf1, 0, node],
[leaf2, 1, tree]
])
}
)
await t.test('should support type and node tests', function () {
const tree = u('node', [u('node', [u('leaf', '1')]), u('leaf', '2')])
assert.deepEqual(filter(tree, 'node'), undefined)
assert.deepEqual(
filter(tree, {cascade: false}, 'node'),
u('node', [u('node', [])])
)
assert.deepEqual(filter(tree, {cascade: false}, 'leaf'), undefined)
})
await t.test('opts.cascade', function () {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
assert.deepEqual(
filter(tree, {cascade: true}, predicate),
undefined,
'opts.cascade = true'
)
assert.deepEqual(
filter(tree, {cascade: false}, predicate),
u('root', [u('node', [])]),
'opts.cascade = false'
)
/**
* @param {Node} node
*/
function predicate(node) {
return node.type !== 'leaf'
}
})
await t.test('example from README', function () {
const tree = u('root', [
u('leaf', '1'),
u('node', [u('leaf', '2'), u('node', [u('leaf', '3')])]),
u('leaf', '4')
])
assert.deepEqual(
filter(tree, predicate),
u('root', [u('node', [u('leaf', '2')]), u('leaf', '4')]),
'example from readme'
)
/**
* @param {Node} node
*/
function predicate(node) {
if (node.type === 'leaf') {
assert('value' in node)
return Number(node.value) % 2 === 0
}
return true
}
})
})