-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest.js
More file actions
392 lines (328 loc) · 12.7 KB
/
Copy pathtest.js
File metadata and controls
392 lines (328 loc) · 12.7 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* @import {Grammar} from '@wooorm/starry-night'
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {toHtml} from 'hast-util-to-html'
import sourceAssembly from '@wooorm/starry-night/source.assembly'
import sourceCss from '@wooorm/starry-night/source.css'
import textPhp from '@wooorm/starry-night/text.html.php'
import textMd from '@wooorm/starry-night/text.md'
import textXml from '@wooorm/starry-night/text.xml'
import textXmlSvg from '@wooorm/starry-night/text.xml.svg'
import {common, createStarryNight} from '@wooorm/starry-night'
test('@wooorm/starry-night', async (t) => {
await t.test('should expose the public api', async () => {
assert.deepEqual(Object.keys(await import('@wooorm/starry-night')).sort(), [
'all',
'common',
'createStarryNight'
])
})
})
test('createStarryNight', async (t) => {
await t.test('should support `getOnigurumaUrlFs`', async () => {
await assert.rejects(async () => {
await createStarryNight(common, {
getOnigurumaUrlFs() {
return new URL('file:///foo/baz/onig.wasm')
}
})
}, /no such file or directory/)
})
})
test('.flagToScope(flag)', async (t) => {
const starryNight = await createStarryNight(common)
const phpThenAssembly = await createStarryNight([textPhp, sourceAssembly])
const assemblyThenPhp = await createStarryNight([sourceAssembly, textPhp])
await t.test('should throw when not given a `flag`', async () => {
assert.throws(() => {
// @ts-expect-error: check that the runtime throws an error w/o flag.
starryNight.flagToScope()
}, /Expected `string` for `flag`, got `undefined`/)
})
await t.test('should support names', async () => {
assert.equal(starryNight.flagToScope('pandoc'), 'text.md')
})
await t.test('should ignore leading and trailing whitespace and slashes', async () => {
assert.equal(starryNight.flagToScope(' \tpandoc/ \t/'), 'text.md')
})
await t.test('should handle flags containing only whitespace', async () => {
assert.equal(starryNight.flagToScope(' \t '), undefined)
})
await t.test('should handle flags containing only slashes', async () => {
assert.equal(starryNight.flagToScope('///'), undefined)
})
await t.test('should support extensions (w/o dot)', async () => {
assert.equal(starryNight.flagToScope('workbook'), 'text.md')
})
await t.test('should support extensions (w/ dot)', async () => {
assert.equal(starryNight.flagToScope('.workbook'), 'text.md')
})
await t.test('should return `undefined` w/o match', async () => {
assert.equal(starryNight.flagToScope('whatever'), undefined)
})
await t.test(
'should support the scope that GH uses for an extension w/ dot (1)',
async () => {
assert.equal(
phpThenAssembly.flagToScope('.inc'),
sourceAssembly.scopeName
)
}
)
await t.test(
'should support the scope that GH uses for an extension w/o dot (1)',
async () => {
assert.equal(phpThenAssembly.flagToScope('inc'), textPhp.scopeName)
}
)
await t.test(
'should support the scope that GH uses for an extension w/ dot (2)',
async () => {
assert.equal(sourceAssembly.scopeName, 'source.assembly')
}
)
await t.test(
'should support the scope that GH uses for an extension w/o dot (2)',
async () => {
assert.equal(assemblyThenPhp.flagToScope('inc'), textPhp.scopeName)
}
)
await t.test(
'should not support file paths with a known extension followed by an unknown one',
async () => {
assert.equal(
phpThenAssembly.flagToScope('path/to/example.inc.bak'),
undefined
)
}
)
await t.test(
'should support file paths with an unknown extension followed by a known one',
async () => {
assert.equal(
phpThenAssembly.flagToScope('path/to/example.bak.inc'),
sourceAssembly.scopeName
)
}
)
await t.test(
'should not support file paths with a known extension followed by a hash',
async () => {
assert.equal(
phpThenAssembly.flagToScope('path/to/example.inc#asd'),
undefined
)
}
)
await t.test(
'should not support file paths with a known extension followed by a search',
async () => {
assert.equal(
phpThenAssembly.flagToScope('path/to/example.inc?asd=1'),
undefined
)
}
)
await t.test('should support file paths with a known extension', async () => {
assert.equal(
phpThenAssembly.flagToScope('path/to/example.inc'),
sourceAssembly.scopeName
)
})
await t.test(
'should not support file paths with a known extension, without the needed dot',
async () => {
assert.equal(phpThenAssembly.flagToScope('path/to/exampleinc'), undefined)
}
)
await t.test(
'should not support file paths with a known extension, without the needed dot, as a filename',
async () => {
assert.equal(phpThenAssembly.flagToScope('path/to/inc'), undefined)
}
)
await t.test('should support language names with dots (`.`)', async () => {
const {default: asn} = await import('@wooorm/starry-night/source.asn')
const starryAsn = await createStarryNight([asn])
assert.equal(starryAsn.flagToScope('asn.1'), 'source.asn')
})
await t.test(
'should support language names with number signs (`#`)',
async () => {
const {default: cs} = await import('@wooorm/starry-night/source.cs')
const starryCs = await createStarryNight([cs])
assert.equal(starryCs.flagToScope('c#'), 'source.cs')
}
)
await t.test('should support language names with plusses (`+`)', async () => {
const {default: cpp} = await import('@wooorm/starry-night/source.c++')
const starryCpp = await createStarryNight([cpp])
assert.equal(starryCpp.flagToScope('c++'), 'source.c++')
})
await t.test(
'should support language names with asterisks (`*`)',
async () => {
const {default: fStar} = await import('@wooorm/starry-night/source.fstar')
const starryFStar = await createStarryNight([fStar])
assert.equal(starryFStar.flagToScope('f*'), 'source.fstar')
}
)
await t.test(
"should support language names with apostrophes (`'`)",
async () => {
const {default: capnp} = await import('@wooorm/starry-night/source.capnp')
const starryCapnp = await createStarryNight([capnp])
assert.equal(starryCapnp.flagToScope("cap'n-proto"), 'source.capnp')
}
)
await t.test(
'should support language names with parens (`(`, `)`)',
async () => {
const {default: dot} = await import('@wooorm/starry-night/source.dot')
const starryDot = await createStarryNight([dot])
assert.equal(starryDot.flagToScope('graphviz-(dot)'), 'source.dot')
}
)
await t.test('should support language names with slashes (`/`)', async () => {
const {default: json} = await import('@wooorm/starry-night/source.json')
const grammar = /** @type {Grammar} */ (json)
const starryJson = await createStarryNight([grammar])
assert.equal(starryJson.flagToScope('max/msp'), 'source.json')
})
})
test('.highlight(value, scope)', async (t) => {
const starryNight = await createStarryNight(common)
await t.test('should throw when not given a `value`', async () => {
assert.throws(() => {
// @ts-expect-error: check that the runtime throws an error w/o `value`.
starryNight.highlight()
}, /Expected `string` for `value`, got `undefined`/)
})
await t.test('should throw when not given a `scope`', async () => {
assert.throws(() => {
// @ts-expect-error: check that the runtime throws an error w/o `scope`.
starryNight.highlight('alert(1)')
}, /Expected `string` for `scope`, got `undefined`/)
})
await t.test('should throw when given an unregistered `scope`', async () => {
assert.throws(() => {
starryNight.highlight('alert(1)', 'whatever')
}, /Expected grammar `whatever` to be registered/)
})
await t.test('should work on an empty string', async () => {
assert.equal(toHtml(starryNight.highlight('', 'source.js')), '')
})
await t.test('should work on some whitespace (1, js)', async () => {
assert.equal(
toHtml(starryNight.highlight('\n \n\t\r\n \r', 'source.js')),
'\n \n\t\r\n \r'
)
})
await t.test('should work on some whitespace (2, js)', async () => {
assert.equal(
toHtml(starryNight.highlight('\n \n\t\r\n \r', 'text.html.basic')),
'\n \n\t\r\n \r'
)
})
await t.test('should generate', async () => {
assert.equal(
toHtml(starryNight.highlight('alert(1)', 'source.js')),
'<span class="pl-en">alert</span>(<span class="pl-c1">1</span>)'
)
})
await t.test('should generate parents', async () => {
assert.equal(
toHtml(starryNight.highlight('# asd *qwe* rty', 'text.md')),
'<span class="pl-mh"># <span class="pl-en">asd </span></span><span class="pl-s"><span class="pl-mh"><span class="pl-en">*</span></span></span><span class="pl-mh"><span class="pl-en">qwe</span></span><span class="pl-s"><span class="pl-mh"><span class="pl-en">*</span></span></span><span class="pl-mh"><span class="pl-en"> rty</span></span>'
)
})
await t.test('should generate grandparents', async () => {
assert.equal(
toHtml(starryNight.highlight('let a = `${b}`', 'source.js')),
'<span class="pl-k">let</span> a <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">`</span><span class="pl-pse"><span class="pl-s1">${</span></span><span class="pl-s1">b</span><span class="pl-pse"><span class="pl-s1">}</span></span><span class="pl-pds">`</span></span>'
)
})
await t.test('should generate multiple lines', async () => {
assert.equal(
toHtml(starryNight.highlight('1 +\n2', 'source.js')),
'<span class="pl-c1">1</span> <span class="pl-k">+</span>\n<span class="pl-c1">2</span>'
)
})
await t.test('should generate w/o style', async () => {
assert.equal(
toHtml(starryNight.highlight('<!doctype html>', 'text.html.basic')),
'<!doctype html>'
)
})
await t.test('should highlight some kotlin', async () => {
assert.equal(
toHtml(
starryNight.highlight(
`package addressbook
class Country(val name : String)`,
'source.kotlin'
)
),
'<span class="pl-k">package</span> <span class="pl-en">addressbook</span>\n\n<span class="pl-k">class</span> <span class="pl-en">Country</span>(<span class="pl-k">val</span> <span class="pl-smi">name</span> <span class="pl-k">:</span> <span class="pl-c1">String</span>)'
)
})
await t.test('should be able to match on empty lines', async () => {
// Real world example of this test case:
// <https://github.com/microsoft/vscode-markdown-tm-grammar/blob/eed2308/markdown.tmLanguage.base.yaml#L125>
const starryNightBlankLines = await createStarryNight([
{
extensions: [],
names: [],
patterns: [{begin: '^a$', end: '^[\\t ]*$', name: 'invalid.illegal'}],
scopeName: 'x'
}
])
assert.equal(
toHtml(starryNightBlankLines.highlight('a\n\nb', 'x')),
'<span class="pl-ii">a</span>\n\nb'
)
})
})
test('.missingScopes()', async (t) => {
const svg = await createStarryNight([textXmlSvg])
const svgAndXml = await createStarryNight([textXmlSvg, textXml])
await t.test('should list missing scopes', async () => {
assert.deepEqual(svg.missingScopes(), ['text.xml'])
})
await t.test('should list no missing scopes', async () => {
assert.deepEqual(svgAndXml.missingScopes(), [])
})
})
test('.register(grammars)', async (t) => {
const starryNight = await createStarryNight([textMd])
await starryNight.register([sourceCss])
await t.test('should support adding languages', async () => {
assert.equal(
toHtml(starryNight.highlight('em { color: red }', 'source.css')),
'<span class="pl-ent">em</span> { <span class="pl-c1">color</span>: <span class="pl-c1">red</span> }'
)
})
await t.test('should support adding deep languages', async () => {
assert.equal(
toHtml(
starryNight.highlight('```css\nem { color: red }\n```', 'text.md')
),
'<span class="pl-s">```</span><span class="pl-en">css</span>\n<span class="pl-ent">em</span> { <span class="pl-c1">color</span>: <span class="pl-c1">red</span> }\n<span class="pl-s">```</span>'
)
})
})
test('.scopes()', async (t) => {
const starryNight = await createStarryNight(common)
const list = starryNight.scopes()
await t.test('should return an array', async () => {
assert.ok(Array.isArray(list))
})
await t.test('should return an array of strings', async () => {
assert.ok(list.every((d) => typeof d === 'string'))
})
await t.test('should include `js`', async () => {
assert.ok(list.includes('source.js'))
})
})