-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
test.js
400 lines (339 loc) · 12.8 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
* @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 function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('@wooorm/starry-night')).sort(), [
'all',
'common',
'createStarryNight'
])
})
})
test('createStarryNight', async function (t) {
await t.test('should support `getOnigurumaUrlFs`', async function () {
assert.rejects(async function () {
await createStarryNight(common, {
getOnigurumaUrlFs() {
return new URL('file:///foo/baz/onig.wasm')
}
})
}, /no such file or directory/)
})
})
test('.flagToScope(flag)', async function (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 function () {
assert.throws(function () {
// @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 function () {
assert.equal(starryNight.flagToScope('pandoc'), 'text.md')
})
await t.test('should support extensions (w/o dot)', async function () {
assert.equal(starryNight.flagToScope('workbook'), 'text.md')
})
await t.test('should support extensions (w/ dot)', async function () {
assert.equal(starryNight.flagToScope('.workbook'), 'text.md')
})
await t.test('should return `undefined` w/o match', async function () {
assert.equal(starryNight.flagToScope('whatever'), undefined)
})
await t.test(
'should support the scope that GH uses for an extension w/ dot (1)',
async function () {
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 function () {
assert.equal(phpThenAssembly.flagToScope('inc'), textPhp.scopeName)
}
)
await t.test(
'should support the scope that GH uses for an extension w/ dot (2)',
async function () {
assert.equal(sourceAssembly.scopeName, 'source.assembly')
}
)
await t.test(
'should support the scope that GH uses for an extension w/o dot (2)',
async function () {
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 function () {
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 function () {
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 function () {
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 function () {
assert.equal(
phpThenAssembly.flagToScope('path/to/example.inc?asd=1'),
undefined
)
}
)
await t.test(
'should support file paths with a known extension',
async function () {
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 function () {
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 function () {
assert.equal(phpThenAssembly.flagToScope('path/to/inc'), undefined)
}
)
await t.test(
'should support language names with dots (`.`)',
async function () {
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 function () {
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 function () {
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 function () {
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 function () {
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 function () {
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 function () {
const {default: json} = await import('@wooorm/starry-night/source.json')
// @ts-expect-error: TS is wrong, it doesn’t understand that `.json` is not an extension.
const grammar = /** @type {Grammar} */ (json)
const starryJson = await createStarryNight([grammar])
assert.equal(starryJson.flagToScope('max/msp'), 'source.json')
}
)
})
test('.highlight(value, scope)', async function (t) {
const starryNight = await createStarryNight(common)
await t.test('should throw when not given a `value`', async function () {
assert.throws(function () {
// @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 function () {
assert.throws(function () {
// @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 function () {
assert.throws(function () {
starryNight.highlight('alert(1)', 'whatever')
}, /Expected grammar `whatever` to be registered/)
}
)
await t.test('should work on an empty string', async function () {
assert.equal(toHtml(starryNight.highlight('', 'source.js')), '')
})
await t.test('should work on some whitespace (1, js)', async function () {
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 function () {
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 function () {
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 function () {
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 function () {
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 function () {
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 function () {
assert.equal(
toHtml(starryNight.highlight('<!doctype html>', 'text.html.basic')),
'<!doctype html>'
)
})
await t.test('should highlight some kotlin', async function () {
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 function () {
// 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 function (t) {
const svg = await createStarryNight([textXmlSvg])
const svgAndXml = await createStarryNight([textXmlSvg, textXml])
await t.test('should list missing scopes', async function () {
assert.deepEqual(svg.missingScopes(), ['text.xml'])
})
await t.test('should list no missing scopes', async function () {
assert.deepEqual(svgAndXml.missingScopes(), [])
})
})
test('.register(grammars)', async function (t) {
const starryNight = await createStarryNight([textMd])
await starryNight.register([sourceCss])
await t.test('should support adding languages', async function () {
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 function () {
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 function (t) {
const starryNight = await createStarryNight(common)
const list = starryNight.scopes()
await t.test('should return an array', async function () {
assert.ok(Array.isArray(list))
})
await t.test('should return an array of strings', async function () {
assert.ok(
list.every(function (d) {
return typeof d === 'string'
})
)
})
await t.test('should include `js`', async function () {
assert.ok(list.includes('source.js'))
})
})