-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
370 lines (307 loc) · 10.2 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* @typedef {import('unist').Node} Node
*/
/**
* @typedef {Node & {id: number}} IdNode
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {u} from 'unist-builder'
import {Index} from 'unist-util-index'
import {select} from 'unist-util-select'
test('core', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('./index.js')).sort(), ['Index'])
})
const node = {type: 'a', id: 1}
const alt = {type: 'b', id: 1}
const tree = {type: 'root', children: [node, alt]}
await t.test('should create an index from a prop field', async function () {
const instance = new Index('id')
instance.add(node)
assert.deepEqual(
[instance instanceof Index, instance.get(1)],
[true, [node]],
'new Index(prop)'
)
})
await t.test('should create an index from a key function', async function () {
const instance = new Index(function (node) {
const idNode = /** @type {IdNode} */ (node)
return idNode.id
})
instance.add(node)
assert.deepEqual(
[instance instanceof Index, instance.get(1)],
[true, [node]],
'new Index(keyFn)'
)
})
await t.test(
'should create an index from a prop and initialize with a tree',
async function () {
const instance = new Index('id', tree)
assert.deepEqual(
[instance instanceof Index, instance.get(1)],
[true, [node, alt]],
'new Index(prop, tree)'
)
}
)
await t.test(
'should create an index from a prop, initialize with a tree, and filter',
async function () {
const instance = new Index('id', tree, function (node) {
return node.type === 'a'
})
assert.deepEqual(
[instance instanceof Index, instance.get(1)],
[true, [node]],
'new Index(prop, tree, filter)'
)
}
)
})
test('add', async function (t) {
const foo = u('node', {word: 'foo'})
const otherFoo = u('node', {word: 'foo'})
const tree = u('root', [foo, u('node', {word: 'bar'})])
const index = new Index('word', tree)
await t.test('should index on the initialize tree', async function () {
assert.deepEqual(index.get('foo'), [foo])
})
const result = index.add(otherFoo)
await t.test('should add other nodes', async function () {
assert.deepEqual(index.get('foo'), [foo, otherFoo])
})
await t.test('should return `this`', async function () {
assert.equal(result, index)
})
index.add(foo)
await t.test(
'should not index existing nodes in tree again',
async function () {
assert.deepEqual(index.get('foo'), [foo, otherFoo])
}
)
index.add(otherFoo)
await t.test('should not index extra nodes again', async function () {
assert.deepEqual(index.get('foo'), [foo, otherFoo])
})
})
test('get', async function (t) {
const tree = u('node', {color: 'black', id: 0}, [
u('node', {color: 'black', id: 1}, [
u('node', {color: 'red', id: 2}, [
u('node', {color: 'black', id: 3}),
u('node', {color: 'black', id: 4})
]),
u('node', {color: 'black', id: 5})
]),
u('node', {color: 'red', id: 6}, [
u('node', {color: 'black', id: 7}, [
u('node', {color: 'black', id: 8}),
u('node', {color: 'black', id: 9})
]),
u('node', {color: 'black', id: 10}, [
u('node', {color: 'red', id: 11}, [
u('node', {color: 'black', id: 12}),
u('node', {color: 'black', id: 13})
]),
u('node', {color: 'black', id: 14})
])
])
])
const index = new Index('color', tree)
await t.test('should index (#1)', async function () {
assert.deepEqual(index.get('black'), [
select('[id=0]', tree),
select('[id=1]', tree),
select('[id=3]', tree),
select('[id=4]', tree),
select('[id=5]', tree),
select('[id=7]', tree),
select('[id=8]', tree),
select('[id=9]', tree),
select('[id=10]', tree),
select('[id=12]', tree),
select('[id=13]', tree),
select('[id=14]', tree)
])
})
await t.test('should index (#2)', async function () {
assert.deepEqual(index.get('red'), [
select('[id=2]', tree),
select('[id=6]', tree),
select('[id=11]', tree)
])
})
await t.test('should index (#3)', async function () {
assert.deepEqual(index.get('yellow'), [])
})
})
test('get: degenerate keys (Object.prototype keys)', async function (t) {
const tree = u('node', {word: '__proto__', id: 0}, [
u('node', {word: 'constructor', id: 1}),
u('node', {word: 'toString', id: 2})
])
const index = new Index('word', tree)
await t.test('should work on `__proto__`', async function () {
assert.deepEqual(index.get('__proto__'), [select('[id=0]', tree)])
})
await t.test('should work on `constructor`', async function () {
assert.deepEqual(index.get('constructor'), [select('[id=1]', tree)])
})
await t.test('should work on `toString`', async function () {
assert.deepEqual(index.get('toString'), [select('[id=2]', tree)])
})
})
test('get: identity keys', async function (t) {
const id1 = {foo: 'bar'}
const id2 = {foo: 'bar'}
const tree = u('root', [
u('node', {word: false, id: 0}),
u('node', {word: 'false', id: 1}),
u('node', {word: 1, id: 2}),
u('node', {word: '1', id: 3}),
u('node', {word: id1, id: 4}),
u('node', {word: id2, id: 5})
])
const index = new Index('word', tree)
await t.test('should index on booleans', async function () {
assert.deepEqual(index.get(false), [select('[id=0]', tree)])
})
await t.test('should index on strings of booleans', async function () {
assert.deepEqual(index.get('false'), [select('[id=1]', tree)])
})
await t.test('should index on numbers', async function () {
assert.deepEqual(index.get(1), [select('[id=2]', tree)])
})
await t.test('should index on strings of numbers', async function () {
assert.deepEqual(index.get('1'), [select('[id=3]', tree)])
})
await t.test('should index on objects by reference (#1)', async function () {
assert.deepEqual(index.get(id1), [select('[id=4]', tree)])
})
await t.test('should index on objects by reference (#2)', async function () {
assert.deepEqual(index.get(id2), [select('[id=5]', tree)])
})
await t.test('should not index on object equivalence', async function () {
assert.deepEqual(index.get({foo: 'bar'}), [])
})
})
test('get: empty index', async function (t) {
await t.test('should work on a missing tree', async function () {
assert.deepEqual(new Index('foo', null).get('bar'), [])
})
await t.test('should work on no tree', async function () {
assert.deepEqual(new Index('foo').get('bar'), [])
})
})
test('get: filter: type test', async function (t) {
const tree = u('root', [
u('node', {word: 'foo'}),
u('node', {word: 'bar'}),
u('skip', {word: 'foo'}),
u('skip', {word: 'bar'})
])
const index = new Index('word', tree, 'node')
await t.test('should support filter as a string (#1)', async function () {
assert.deepEqual(index.get('foo'), [select('node[word="foo"]', tree)])
})
await t.test('should support filter as a string (#2)', async function () {
assert.deepEqual(index.get('bar'), [select('node[word="bar"]', tree)])
})
})
test('get: filter: function test', async function (t) {
const tree = u('root', [
u('node', {word: 'foo'}),
u('node', {word: 'bar'}),
u('skip', {word: 'foo'}),
u('skip', {word: 'bar'})
])
const index = new Index('word', tree, function (node, index, parent) {
return Boolean(
typeof index === 'number' &&
parent &&
'word' in node &&
index < 2 &&
parent.type === 'root'
)
})
await t.test('should support a filter function (#1)', async function () {
assert.deepEqual(index.get('foo'), [select('node[word="foo"]', tree)])
})
await t.test('should support a filter function (#2)', async function () {
assert.deepEqual(index.get('bar'), [select('node[word="bar"]', tree)])
})
})
test('get: computed keys', async function (t) {
const tree = u('root', {x: 0, y: 4, id: 0}, [
u('node', {x: 3, y: 2, id: 1}),
u('node', {x: 2, y: 2, id: 2}),
u('node', {x: 3, y: 1, id: 3}),
u('node', {x: 4, y: 1, id: 4})
])
const index = new Index(xPlusY, tree)
await t.test('should index with a key function (#1)', async function () {
assert.deepEqual(index.get(4), [
select('[id=0]', tree),
select('[id=2]', tree),
select('[id=3]', tree)
])
})
await t.test('should index with a key function (#2)', async function () {
assert.deepEqual(index.get(0), [])
})
await t.test('should index with a key function (#3)', async function () {
assert.deepEqual(index.get(5), [
select('[id=1]', tree),
select('[id=4]', tree)
])
})
await t.test('should index with a key function (#4)', async function () {
assert.deepEqual(new Index(xPlusY, tree, 'node').get(4), [
select('[id=2]', tree),
select('[id=3]', tree)
])
})
/**
* @param {Node} node
*/
function xPlusY(node) {
const funky = /** @type {{type: string, x: number, y: number}} */ (node)
return funky.x + funky.y
}
})
test('index.remove', async function (t) {
const badFoo = u('bad', {word: 'foo'})
const nodeFoo = u('node', {word: 'foo'})
const tree = u('root', [badFoo, nodeFoo, u('node', {word: 'bar'})])
const index = new Index('word', tree)
await t.test('shoulds work (baseline)', async function () {
assert.deepEqual(index.get('foo'), [badFoo, nodeFoo])
})
const result = index.remove(badFoo)
await t.test('should remove', async function () {
assert.deepEqual(index.get('foo'), [nodeFoo])
})
await t.test('should yield `this`', async function () {
assert.equal(result, index)
})
index.remove(badFoo)
await t.test('should do nothing when removing again', async function () {
assert.deepEqual(index.get('foo'), [nodeFoo])
})
index.remove(u('terrible', {word: 'baz'}))
await t.test(
'should do nothing when removing nodes that aren’t indexed',
async function () {
assert.deepEqual(index.get('foo'), [nodeFoo])
}
)
index.remove(nodeFoo)
await t.test('should support removing the last node', async function () {
assert.deepEqual(index.get('foo'), [])
})
})