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
|
package syntax
import (
"testing"
"codeberg.org/turbo-editors/turbo-core/theme"
)
// classAt returns the class covering a rune column on a line, and whether any
// span covers it at all. It is how nearly every test below asks its question.
func classAt(spans [][]Span, line, col int) (Class, bool) {
if line < 0 || line >= len(spans) {
return 0, false
}
for _, s := range spans[line] {
if col >= s.Start && col < s.End {
return s.Class, true
}
}
return 0, false
}
func TestLanguageOfRecognisesAFileByItsExtension(t *testing.T) {
tests := map[string]Language{
"settings.toml": LanguageTOML,
".turbo-go/settings.toml": LanguageTOML,
"Theme.TOML": LanguageTOML,
"README.md": LanguageMarkdown,
"notes.markdown": LanguageMarkdown,
"app.js": LanguageJavaScript,
"module.mjs": LanguageJavaScript,
"legacy.cjs": LanguageJavaScript,
"index.html": LanguageHTML,
"old.HTM": LanguageHTML,
"install.sh": LanguageBash,
"script.bash": LanguageBash,
"prompt.zsh": LanguageBash,
"Makefile": LanguageNone,
"go.mod": LanguageNone,
"noextension": LanguageNone,
"weird.md.backup": LanguageNone,
"": LanguageNone,
}
for path, want := range tests {
if got := LanguageOf(path, ""); got != want {
t.Errorf("LanguageOf(%q, \"\") = %v, want %v", path, got, want)
}
}
}
func TestAShebangMakesAFileWithNoExtensionAScript(t *testing.T) {
shebangs := []string{
"#!/bin/sh",
"#!/bin/bash",
"#!/usr/bin/env bash",
"#!/usr/bin/env -S bash -e",
"#! /bin/zsh",
"#!/usr/bin/dash",
"#!/bin/ksh",
}
for _, first := range shebangs {
t.Run(first, func(t *testing.T) {
if got := LanguageOf("configure", first); got != LanguageBash {
t.Errorf("LanguageOf(\"configure\", %q) = %v, want bash", first, got)
}
})
}
}
func TestAShebangForSomethingElseIsNotAScript(t *testing.T) {
notShells := []string{
"#!/usr/bin/env python3",
"#!/usr/bin/perl",
"#!/usr/bin/env node",
"# not a shebang at all, just a comment about bash",
"",
"#!",
}
for _, first := range notShells {
t.Run(first, func(t *testing.T) {
if got := LanguageOf("configure", first); got != LanguageNone {
t.Errorf("LanguageOf(\"configure\", %q) = %v, want none", first, got)
}
})
}
}
func TestTheExtensionWinsOverTheFirstLine(t *testing.T) {
// A .md file whose first line happens to be a shebang is still Markdown.
if got := LanguageOf("README.md", "#!/bin/sh"); got != LanguageMarkdown {
t.Errorf("LanguageOf() = %v, want markdown", got)
}
}
func TestAnUncolouredLanguageStillGivesOneEntryPerLine(t *testing.T) {
// The editor indexes the result by line number without checking, so a file
// this package cannot colour must still be as long as the file.
spans := Highlight(LanguageNone, "one\ntwo\nthree\n")
if len(spans) != 4 {
t.Fatalf("Highlight() returned %d lines, want 4", len(spans))
}
for i, line := range spans {
if len(line) != 0 {
t.Errorf("line %d was coloured: %v", i, line)
}
}
}
func TestLanguageString(t *testing.T) {
tests := map[Language]string{
LanguageTOML: "toml", LanguageMarkdown: "markdown",
LanguageJavaScript: "javascript", LanguageHTML: "html", LanguageBash: "bash",
// A language this package never heard of prints its own name, because
// that name is what a snippets file writes down.
Language("rust"): "rust",
LanguageNone: "none",
}
for language, want := range tests {
if got := language.String(); got != want {
t.Errorf("%q.String() = %q, want %q", string(language), got, want)
}
}
}
func TestClassStyleKeysAreTheThemeKeys(t *testing.T) {
tests := map[Class]string{
ClassKeyword: theme.KeySyntaxKeyword,
ClassString: theme.KeySyntaxString,
ClassComment: theme.KeySyntaxComment,
ClassPunctuation: theme.KeySyntaxPunctuation,
}
for class, want := range tests {
if got := class.StyleKey(); got != want {
t.Errorf("%v.StyleKey() = %q, want %q", class, got, want)
}
}
if got := Class(200).StyleKey(); got != theme.KeySyntaxIdentifier {
t.Errorf("an out-of-range class gives %q, want the identifier key", got)
}
}
func TestClassString(t *testing.T) {
if got := ClassKeyword.String(); got != "keyword" {
t.Errorf("ClassKeyword.String() = %q", got)
}
if got := Class(200).String(); got != "unknown" {
t.Errorf("an out-of-range class prints %q, want %q", got, "unknown")
}
}
// The registry is the extension point an editor built on this library uses to
// add its own language. The tests below drive it exactly as Turbo Go and Turbo
// Rust do, and restore the registry afterwards so they leave nothing behind for
// the tests in the other files here.
// registerForTest adds a language and removes it again when the test ends.
func registerForTest(t *testing.T, d Definition) {
t.Helper()
previous, existed := registry[d.Language]
Register(d)
t.Cleanup(func() {
if existed {
registry[d.Language] = previous
return
}
delete(registry, d.Language)
})
}
// wholeLineAs colours every line of a document in one class, which is enough to
// tell "this scanner ran" from "it did not".
func wholeLineAs(class Class) func(string) [][]Span {
return func(src string) [][]Span {
return ScanLines(src, func(line []rune, carry struct{}) ([]Span, struct{}) {
s := NewLineScanner(line)
s.TakeRest(class)
return s.Spans(), carry
})
}
}
func TestARegisteredLanguageIsRecognisedByItsExtension(t *testing.T) {
registerForTest(t, Definition{
Language: "rust",
Extensions: []string{".rs"},
Highlight: wholeLineAs(ClassKeyword),
})
if got := LanguageOf("main.rs", ""); got != "rust" {
t.Errorf("LanguageOf(\"main.rs\", \"\") = %q, want rust", got)
}
}
func TestARegisteredLanguageIsColouredByItsOwnScanner(t *testing.T) {
registerForTest(t, Definition{
Language: "rust",
Extensions: []string{".rs"},
Highlight: wholeLineAs(ClassKeyword),
})
class, ok := classAt(Highlight("rust", "fn main() {}"), 0, 0)
if !ok || class != ClassKeyword {
t.Errorf("Highlight(\"rust\", …) gave %v (covered: %v), want keyword", class, ok)
}
}
func TestARegisteredLanguageIsRecognisedByItsShebang(t *testing.T) {
registerForTest(t, Definition{
Language: "python",
Extensions: []string{".py"},
Shebangs: []string{"python3"},
Highlight: wholeLineAs(ClassComment),
})
if got := LanguageOf("script", "#!/usr/bin/env python3"); got != "python" {
t.Errorf("LanguageOf(\"script\", …) = %q, want python", got)
}
}
func TestRegisteringALanguageTwiceReplacesIt(t *testing.T) {
// An editor overriding a built-in is the point: the second registration is
// the more specific statement, so it wins.
registerForTest(t, Definition{
Language: LanguageMarkdown,
Extensions: []string{".md"},
Highlight: wholeLineAs(ClassNumber),
})
class, ok := classAt(Highlight(LanguageMarkdown, "# Title"), 0, 0)
if !ok || class != ClassNumber {
t.Errorf("Highlight(markdown, …) gave %v (covered: %v), want the replacement scanner", class, ok)
}
}
func TestRegisteredListsWhatThisPackageColours(t *testing.T) {
got := Registered()
// Presence rather than an exact list: the examples in this package's test
// binary register a language of their own, and a test that counted would
// pass or fail on the order the two happened to run in.
for _, want := range []Language{LanguageBash, LanguageHTML, LanguageJavaScript, LanguageMarkdown, LanguageTOML} {
if !containsLanguage(got, want) {
t.Errorf("Registered() = %v, want it to include %q", got, want)
}
}
for i := 1; i < len(got); i++ {
if got[i-1] >= got[i] {
t.Fatalf("Registered() = %v, want it sorted", got)
}
}
}
// containsLanguage reports whether a language is in a list.
func containsLanguage(list []Language, want Language) bool {
for _, language := range list {
if language == want {
return true
}
}
return false
}
func TestALanguageWithNoScannerStillGivesOneEntryPerLine(t *testing.T) {
// A Definition may name a language without colouring it, which is what an
// editor does when it wants a file recognised but has no scanner yet.
registerForTest(t, Definition{Language: "plain", Extensions: []string{".plain"}})
spans := Highlight("plain", "one\ntwo\n")
if len(spans) != 3 {
t.Fatalf("Highlight() returned %d lines, want 3", len(spans))
}
}
func TestTheEightBuiltInLanguagesAreRegistered(t *testing.T) {
// The five the library started with, plus the three ticket 8 added. A
// language dropped by accident would otherwise show up as a file quietly
// going plain, which nothing else here notices.
got := Registered()
for _, want := range []Language{
LanguageBash, LanguageDockerfile, LanguageHTML, LanguageJavaScript,
LanguageMarkdown, LanguageTOML, LanguageXML, LanguageYAML,
} {
if !containsLanguage(got, want) {
t.Errorf("Registered() = %v, want it to include %q", got, want)
}
}
}
func TestEveryBuiltInLanguageActuallyColoursSomething(t *testing.T) {
// A Definition with a nil Highlight registers a name and colours nothing,
// which looks identical to a scanner that is simply quiet.
samples := map[Language]string{
LanguageBash: "if true; then echo hi; fi",
LanguageDockerfile: "FROM alpine",
LanguageHTML: "<p>hi</p>",
LanguageJavaScript: "const a = 1;",
LanguageMarkdown: "# Title",
LanguageTOML: `a = "b"`,
LanguageXML: "<a/>",
LanguageYAML: "a: 1",
}
for language, sample := range samples {
t.Run(string(language), func(t *testing.T) {
if spans := Highlight(language, sample); len(spans[0]) == 0 {
t.Errorf("Highlight(%q, %q) coloured nothing", language, sample)
}
})
}
}
|