-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
252 lines (222 loc) · 6.44 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
/**
* @typedef {import('mdast').Emphasis} Emphasis
* @typedef {import('mdast').InlineCode} InlineCode
* @typedef {import('unist').Node} UnistNode
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {findAfter} from 'unist-util-find-after'
test('findAfter', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
Object.keys(await import('unist-util-find-after')).sort(),
['findAfter']
)
})
const tree = fromMarkdown('Some *emphasis*, **importance**, and `code`.')
assert(tree.type === 'root')
const paragraph = tree.children[0]
assert(paragraph.type === 'paragraph')
const head = paragraph.children[0]
assert(head.type === 'text')
const next = paragraph.children[1]
assert(next.type === 'emphasis')
/** @type {Emphasis} */
const emphasis = {type: 'emphasis', children: []}
/** @type {InlineCode} */
const inlineCode = {type: 'inlineCode', value: 'a'}
await t.test('should fail without parent', async function () {
assert.throws(function () {
// @ts-expect-error: check that a runtime error is thrown.
findAfter()
}, /Expected parent node/)
})
await t.test('should fail without parent node', async function () {
assert.throws(function () {
// @ts-expect-error: check that a runtime error is thrown.
findAfter(inlineCode)
}, /Expected parent node/)
})
await t.test('should fail without index (#1)', async function () {
assert.throws(function () {
// @ts-expect-error: check that a runtime error is thrown.
findAfter(emphasis)
}, /Expected child node or index/)
})
await t.test('should fail without index (#2)', async function () {
assert.throws(function () {
findAfter(emphasis, -1)
}, /Expected positive finite number as index/)
})
await t.test('should fail without index (#3)', async function () {
assert.throws(function () {
findAfter(emphasis, inlineCode)
}, /Expected child node or index/)
})
await t.test('should fail for invalid `test` (#1)', async function () {
assert.throws(function () {
findAfter(
emphasis,
0,
// @ts-expect-error: check that a runtime error is thrown.
false
)
}, /Expected function, string, or object as test/)
})
await t.test('should fail for invalid `test` (#2)', async function () {
assert.throws(function () {
findAfter(
emphasis,
0,
// @ts-expect-error: check that a runtime error is thrown.
true
)
}, /Expected function, string, or object as test/)
})
await t.test(
'should return the following node when without `test` (#1)',
async function () {
assert.strictEqual(
findAfter(paragraph, paragraph.children[1]),
paragraph.children[2]
)
}
)
await t.test(
'should return the following node when without `test` (#1)',
async function () {
assert.strictEqual(findAfter(paragraph, 1), paragraph.children[2])
}
)
await t.test(
'should return the following node when without `test` (#1)',
async function () {
assert.strictEqual(findAfter(paragraph, 7), undefined)
}
)
await t.test(
'should return `node` when given a `node` and existing (#1)',
async function () {
assert.strictEqual(
findAfter(paragraph, 0, paragraph.children[6]),
paragraph.children[6]
)
}
)
await t.test(
'should return `node` when given a `node` and existing (#2)',
async function () {
assert.strictEqual(
findAfter(paragraph, paragraph.children[0], paragraph.children[1]),
paragraph.children[1]
)
}
)
await t.test(
'should return `node` when given a `node` and existing (#3)',
async function () {
assert.strictEqual(
findAfter(paragraph, 0, paragraph.children[1]),
paragraph.children[1]
)
}
)
await t.test(
'should return `node` when given a `node` and existing (#4)',
async function () {
assert.strictEqual(
findAfter(paragraph, paragraph.children[0], paragraph.children[0]),
undefined
)
}
)
await t.test(
'should return `node` when given a `node` and existing (#5)',
async function () {
assert.strictEqual(
findAfter(paragraph, 0, paragraph.children[0]),
undefined
)
}
)
await t.test(
'should return `node` when given a `node` and existing (#6)',
async function () {
assert.strictEqual(
findAfter(paragraph, 1, paragraph.children[1]),
undefined
)
}
)
await t.test(
'should return a child when given a `type` and existing (#1)',
async function () {
assert.strictEqual(
findAfter(paragraph, 0, 'strong'),
paragraph.children[3]
)
}
)
await t.test(
'should return a child when given a `type` and existing (#2)',
async function () {
assert.strictEqual(findAfter(paragraph, 3, 'strong'), undefined)
}
)
await t.test(
'should return a child when given a `type` and existing (#3)',
async function () {
assert.strictEqual(
findAfter(paragraph, paragraph.children[0], 'strong'),
paragraph.children[3]
)
}
)
await t.test(
'should return a child when given a `type` and existing (#4)',
async function () {
assert.strictEqual(
findAfter(paragraph, paragraph.children[3], 'strong'),
undefined
)
}
)
await t.test(
'should return a child when given a `test` and existing (#1)',
async function () {
assert.strictEqual(findAfter(paragraph, 0, check), paragraph.children[5])
}
)
await t.test(
'should return a child when given a `test` and existing (#2)',
async function () {
assert.strictEqual(findAfter(paragraph, 5, check), undefined)
}
)
await t.test(
'should return a child when given a `test` and existing (#3)',
async function () {
assert.strictEqual(
findAfter(paragraph, paragraph.children[4], check),
paragraph.children[5]
)
}
)
await t.test(
'should return a child when given a `test` and existing (#4)',
async function () {
assert.strictEqual(
findAfter(paragraph, paragraph.children[6], check),
undefined
)
}
)
})
/**
* @param {UnistNode} _
* @param {number | null | undefined} n
*/
function check(_, n) {
return n === 5
}