| 📦 Turbo Golo d710c1b k33g 12h ago | 1 | package gololang_test |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "rickub.com/turbo-editors/turbo-core/syntax" |
| 8 | |
| 9 | "rickub.com/turbo-editors/turbo-golo/internal/gololang" |
| 10 | ) |
| 11 | |
| 12 | // coloured is one span with the text it covers, which is what a test wants to |
| 13 | // talk about: "the word function is a keyword", not "columns 0 to 8 are class |
| 14 | // 1". |
| 15 | type coloured struct { |
| 16 | text string |
| 17 | class syntax.Class |
| 18 | } |
| 19 | |
| 20 | func (c coloured) String() string { return c.text + ":" + c.class.String() } |
| 21 | |
| 22 | // colouredLine returns every span of one line of source, with its text. |
| 23 | func colouredLine(t *testing.T, src string) []coloured { |
| 24 | t.Helper() |
| 25 | |
| 26 | lines := gololang.Highlight(src) |
| 27 | if len(lines) != 1 { |
| 28 | t.Fatalf("Highlight(%q) returned %d lines, want 1", src, len(lines)) |
| 29 | } |
| 30 | return withText([]rune(src), lines[0]) |
| 31 | } |
| 32 | |
| 33 | // withText pairs each span with the runes it covers. |
| 34 | func withText(line []rune, spans []syntax.Span) []coloured { |
| 35 | out := make([]coloured, 0, len(spans)) |
| 36 | for _, span := range spans { |
| 37 | out = append(out, coloured{string(line[span.Start:span.End]), span.Class}) |
| 38 | } |
| 39 | return out |
| 40 | } |
| 41 | |
| 42 | // find returns the span covering exactly the given text, if there is one. |
| 43 | func find(spans []coloured, text string) (coloured, bool) { |
| 44 | for _, span := range spans { |
| 45 | if span.text == text { |
| 46 | return span, true |
| 47 | } |
| 48 | } |
| 49 | return coloured{}, false |
| 50 | } |
| 51 | |
| 52 | // assertClass fails unless one span covers exactly text and has the wanted |
| 53 | // class. Asking for the whole text means a scanner that split a construct in |
| 54 | // two is caught, not only one that coloured it wrongly. |
| 55 | func assertClass(t *testing.T, src, text string, want syntax.Class) { |
| 56 | t.Helper() |
| 57 | |
| 58 | spans := colouredLine(t, src) |
| 59 | got, ok := find(spans, text) |
| 60 | if !ok { |
| 61 | t.Fatalf("in %q: no single span covers %q; got %v", src, text, spans) |
| 62 | } |
| 63 | if got.class != want { |
| 64 | t.Errorf("in %q: %q is %s, want %s", src, text, got.class, want) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // lineOf returns the spans of one line of a multi-line document, with text. |
| 69 | func lineOf(src string, number int) []coloured { |
| 70 | lines := strings.Split(src, "\n") |
| 71 | return withText([]rune(lines[number]), gololang.Highlight(src)[number]) |
| 72 | } |
| 73 | |
| 74 | // --- the three invariants the editor relies on ------------------------------ |
| 75 | |
| 76 | // A representative body of Golo, used by the invariant tests below. It is |
| 77 | // deliberately a mixture: every construct the scanner knows, some broken |
| 78 | // input, and the constructs that most easily run into one another. |
| 79 | const sample = `#!/usr/bin/env golo |
| 80 | module demo.Shapes |
| 81 | |
| 82 | import gololang.Errors |
| 83 | |
| 84 | ---- |
| 85 | A block comment, with --- near misses |
| 86 | and a "quote" inside it. |
| 87 | ---- |
| 88 | |
| 89 | struct Point = { x, y } |
| 90 | |
| 91 | union Shape = { |
| 92 | Circle = { radius } |
| 93 | Rect = { width, height } |
| 94 | } |
| 95 | |
| 96 | augment Shape$Circle { |
| 97 | function area = |this| -> 3.14159 * this: radius() * this: radius() |
| 98 | } |
| 99 | |
| 100 | function main = |args| { |
| 101 | let p = Point(1, 2) |
| 102 | var big = 42L |
| 103 | let ratio = 2.5e-3F |
| 104 | let text = """ |
| 105 | a multi-line "string" |
| 106 | """ |
| 107 | let label = match { |
| 108 | when p: x() > 0 then "positive" |
| 109 | otherwise "other" |
| 110 | } |
| 111 | foreach i in range(0, 3) { |
| 112 | println("i = " + i + '\n') |
| 113 | } |
| 114 | let ok = p?: x() orIfNull 0 |
| 115 | let 😀 = list[1, 2, 3] |
| 116 | let broken = "unterminated |
| 117 | let after = 1 |
| 118 | }` |
| 119 | |
| 120 | func TestEveryLineGetsExactlyOneEntry(t *testing.T) { |
| 121 | // The editor indexes the result by line number without checking, so a |
| 122 | // scanner that returned one entry fewer would draw every line below the |
| 123 | // gap in the wrong colours. |
| 124 | src := sample + "\n\n\ntrailing\n" |
| 125 | want := len(strings.Split(src, "\n")) |
| 126 | |
| 127 | if got := len(gololang.Highlight(src)); got != want { |
| 128 | t.Errorf("Highlight returned %d lines for %d lines of source", got, want) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestSpansAreInOrderAndDoNotOverlap(t *testing.T) { |
| 133 | // Spans are drawn in the order they arrive. Two out of order paint over |
| 134 | // each other, and nothing fails. |
| 135 | for number, spans := range gololang.Highlight(sample) { |
| 136 | line := []rune(strings.Split(sample, "\n")[number]) |
| 137 | previousEnd := 0 |
| 138 | |
| 139 | for _, span := range spans { |
| 140 | switch { |
| 141 | case span.Start < previousEnd: |
| 142 | t.Errorf("line %d: span %v starts before the previous one ended at %d", number+1, span, previousEnd) |
| 143 | case span.Start >= span.End: |
| 144 | t.Errorf("line %d: span %v is empty or inverted", number+1, span) |
| 145 | case span.End > len(line): |
| 146 | t.Errorf("line %d: span %v runs past the %d runes of the line", number+1, span, len(line)) |
| 147 | } |
| 148 | previousEnd = span.End |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestBrokenInputStillColours(t *testing.T) { |
| 154 | // Source under the cursor is invalid most of the time it is being typed. |
| 155 | broken := []string{ |
| 156 | `let x = "`, |
| 157 | `let x = '`, |
| 158 | `let x = "\`, |
| 159 | `"""`, |
| 160 | `""`, |
| 161 | `----`, |
| 162 | `---`, |
| 163 | `-----`, |
| 164 | `#`, |
| 165 | `function`, |
| 166 | `function (`, |
| 167 | `module`, |
| 168 | `import a.`, |
| 169 | `.`, |
| 170 | `..`, |
| 171 | `1.`, |
| 172 | `1e`, |
| 173 | `1e-`, |
| 174 | `$`, |
| 175 | `}}}`, |
| 176 | `|`, |
| 177 | `->`, |
| 178 | `?:`, |
| 179 | } |
| 180 | for _, src := range broken { |
| 181 | spans := gololang.Highlight(src) |
| 182 | if len(spans) != 1 { |
| 183 | t.Errorf("Highlight(%q) returned %d lines, want 1", src, len(spans)) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func TestAnEmptyDocumentIsOneEmptyLine(t *testing.T) { |
| 189 | if got := gololang.Highlight(""); len(got) != 1 || len(got[0]) != 0 { |
| 190 | t.Errorf("Highlight(\"\") = %v, want one line with no spans", got) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func TestCRLFColoursTheSameAsLF(t *testing.T) { |
| 195 | unix := gololang.Highlight("function main = |args| {\n println(\"hi\")\n}") |
| 196 | windows := gololang.Highlight("function main = |args| {\r\n println(\"hi\")\r\n}") |
| 197 | |
| 198 | if len(unix) != len(windows) { |
| 199 | t.Fatalf("CRLF gave %d lines, LF gave %d", len(windows), len(unix)) |
| 200 | } |
| 201 | for i := range unix { |
| 202 | if len(unix[i]) != len(windows[i]) { |
| 203 | t.Errorf("line %d: CRLF gave %v, LF gave %v", i+1, windows[i], unix[i]) |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // --- what crosses a line break ---------------------------------------------- |
| 209 | |
| 210 | func TestABlockCommentIsCarriedToItsClosingDashes(t *testing.T) { |
| 211 | src := "let a = 1 ----\nstill a comment\n---- let b = 2\nlet c = 3" |
| 212 | |
| 213 | if got, ok := find(lineOf(src, 1), "still a comment"); !ok || got.class != syntax.ClassComment { |
| 214 | t.Errorf("the line inside a block comment is %v, want all comment", lineOf(src, 1)) |
| 215 | } |
| 216 | third := lineOf(src, 2) |
| 217 | if got, ok := find(third, "----"); !ok || got.class != syntax.ClassComment { |
| 218 | t.Errorf("the closing dashes are %v, want a comment", third) |
| 219 | } |
| 220 | if got, ok := find(third, "let"); !ok || got.class != syntax.ClassKeyword { |
| 221 | t.Errorf("code after the closing dashes is %v, want a keyword", third) |
| 222 | } |
| 223 | if got, ok := find(lineOf(src, 3), "let"); !ok || got.class != syntax.ClassKeyword { |
| 224 | t.Errorf("the line after the comment is %v, want code", lineOf(src, 3)) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | func TestThreeDashesDoNotCloseABlockComment(t *testing.T) { |
| 229 | // The lexer asks for four dashes. Three inside a comment are its text, and |
| 230 | // the tree-sitter grammar's own test — "with --- near misses" — is the |
| 231 | // same case. |
| 232 | src := "----\nwith --- near misses\nlet x = 1 ----\nlet y = 2" |
| 233 | |
| 234 | if got, ok := find(lineOf(src, 2), "let x = 1 ----"); !ok || got.class != syntax.ClassComment { |
| 235 | t.Errorf("a line inside the comment after a near miss is %v, want all comment", lineOf(src, 2)) |
| 236 | } |
| 237 | if got, ok := find(lineOf(src, 3), "let"); !ok || got.class != syntax.ClassKeyword { |
| 238 | t.Errorf("the line after the comment is %v, want code", lineOf(src, 3)) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func TestAStringIsCarriedToItsClosingQuote(t *testing.T) { |
| 243 | // The interpreter reads a string to its closing quote and stops at |
| 244 | // nothing in between, so the colour follows it. This is the decision the |
| 245 | // other scanners in the family make the other way, for languages whose |
| 246 | // grammar forbids the newline; Golo's lexer does not. |
| 247 | src := "let s = \"first line\nsecond line\nthird\" + rest\nlet next = 1" |
| 248 | |
| 249 | if got, ok := find(lineOf(src, 1), "second line"); !ok || got.class != syntax.ClassString { |
| 250 | t.Errorf("the middle of a multi-line string is %v, want all string", lineOf(src, 1)) |
| 251 | } |
| 252 | third := lineOf(src, 2) |
| 253 | if got, ok := find(third, `third"`); !ok || got.class != syntax.ClassString { |
| 254 | t.Errorf("the end of the string is %v, want string up to the quote", third) |
| 255 | } |
| 256 | if got, ok := find(third, "rest"); !ok || got.class != syntax.ClassIdentifier { |
| 257 | t.Errorf("code after the closing quote is %v, want a name", third) |
| 258 | } |
| 259 | if got, ok := find(lineOf(src, 3), "let"); !ok || got.class != syntax.ClassKeyword { |
| 260 | t.Errorf("the line after the string is %v, want code", lineOf(src, 3)) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | func TestATripleQuotedStringIsCarriedToItsClosingQuotes(t *testing.T) { |
| 265 | src := "let s = \"\"\"\na \"quoted\" line # not a comment\n\"\"\" + rest\nlet next = 1" |
| 266 | |
| 267 | second := lineOf(src, 1) |
| 268 | if len(second) != 1 || second[0].class != syntax.ClassString { |
| 269 | t.Errorf("a line inside a triple-quoted string is %v, want one string span", second) |
| 270 | } |
| 271 | third := lineOf(src, 2) |
| 272 | if got, ok := find(third, `"""`); !ok || got.class != syntax.ClassString { |
| 273 | t.Errorf("the closing quotes are %v, want string", third) |
| 274 | } |
| 275 | if got, ok := find(third, "rest"); !ok || got.class != syntax.ClassIdentifier { |
| 276 | t.Errorf("code after the closing quotes is %v, want a name", third) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestACharacterLiteralIsCarriedLikeAString(t *testing.T) { |
| 281 | // The same loop in lexer.go reads both, so a stray apostrophe paints to |
| 282 | // the next apostrophe, wherever that is. |
| 283 | src := "let c = 'x\nstill' + 1" |
| 284 | |
| 285 | if got, ok := find(lineOf(src, 1), "still'"); !ok || got.class != syntax.ClassChar { |
| 286 | t.Errorf("the continuation of a character literal is %v, want char", lineOf(src, 1)) |
| 287 | } |
| 288 | if got, ok := find(lineOf(src, 1), "1"); !ok || got.class != syntax.ClassNumber { |
| 289 | t.Errorf("code after the closing apostrophe is %v, want a number", lineOf(src, 1)) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | func TestAnEscapedQuoteAtTheEndOfALineKeepsTheStringOpen(t *testing.T) { |
| 294 | // A backslash before the newline escapes it, and the lexer keeps reading. |
| 295 | src := "let s = \"ends with a slash \\\nand goes on\"\nlet next = 1" |
| 296 | |
| 297 | if got, ok := find(lineOf(src, 1), `and goes on"`); !ok || got.class != syntax.ClassString { |
| 298 | t.Errorf("after an escaped newline the string is %v, want string", lineOf(src, 1)) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func TestALineCommentEndsAtTheLine(t *testing.T) { |
| 303 | src := "# a comment\nlet x = 1" |
| 304 | |
| 305 | if got, ok := find(lineOf(src, 1), "let"); !ok || got.class != syntax.ClassKeyword { |
| 306 | t.Errorf("the line after a # comment is %v, want code", lineOf(src, 1)) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // --- one case per construct ------------------------------------------------- |
| 311 | |
| 312 | func TestConstructs(t *testing.T) { |
| 313 | cases := []struct { |
| 314 | name string |
| 315 | src string |
| 316 | text string |
| 317 | class syntax.Class |
| 318 | }{ |
| 319 | {"line comment", `let x = 1 # why`, `# why`, syntax.ClassComment}, |
| 320 | {"shebang", `#!/usr/bin/env golo`, `#!/usr/bin/env golo`, syntax.ClassComment}, |
| 321 | {"block comment on one line", `let x = 1 ---- why ---- + 2`, `---- why ----`, syntax.ClassComment}, |
| 322 | {"code after a one-line block comment", `let x = 1 ---- why ---- + 2`, `2`, syntax.ClassNumber}, |
| 323 | {"empty block comment", `--------`, `--------`, syntax.ClassComment}, |
| 324 | {"hash inside a string is not a comment", `let s = "# not a comment"`, `"# not a comment"`, syntax.ClassString}, |
| 325 | {"dashes inside a string are not a comment", `let s = "---- not a comment ----"`, `"---- not a comment ----"`, syntax.ClassString}, |
| 326 | |
| 327 | {"string", `let s = "hi"`, `"hi"`, syntax.ClassString}, |
| 328 | {"string stops at its closing quote", `let s = "hi" + name`, `"hi"`, syntax.ClassString}, |
| 329 | {"code after a string is still code", `let s = "hi" + name`, `name`, syntax.ClassIdentifier}, |
| 330 | {"empty string", `let s = ""`, `""`, syntax.ClassString}, |
| 331 | {"string with an escaped quote", `let s = "he said \"hi\""`, `"he said \"hi\""`, syntax.ClassString}, |
| 332 | {"string with a hex escape", `let s = "\x41"`, `"\x41"`, syntax.ClassString}, |
| 333 | {"triple-quoted string on one line", `let s = """a "b" c""" + d`, `"""a "b" c"""`, syntax.ClassString}, |
| 334 | {"code after a triple-quoted string", `let s = """a "b" c""" + d`, `d`, syntax.ClassIdentifier}, |
| 335 | {"char literal", `let c = 'x'`, `'x'`, syntax.ClassChar}, |
| 336 | {"escaped char literal", `let c = '\n'`, `'\n'`, syntax.ClassChar}, |
| 337 | {"char stops at its closing quote", `let c = 'x' + 1`, `1`, syntax.ClassNumber}, |
| 338 | |
| 339 | {"integer", `let n = 42`, `42`, syntax.ClassNumber}, |
| 340 | {"long", `let n = 42L`, `42L`, syntax.ClassNumber}, |
| 341 | {"double", `let n = 3.14`, `3.14`, syntax.ClassNumber}, |
| 342 | {"float with a capital suffix", `let n = 3.14F`, `3.14F`, syntax.ClassNumber}, |
| 343 | {"float with a small suffix", `let n = 2.0f`, `2.0f`, syntax.ClassNumber}, |
| 344 | {"exponent", `let n = 1.5e3`, `1.5e3`, syntax.ClassNumber}, |
| 345 | {"negative exponent", `let n = 1.5e-3`, `1.5e-3`, syntax.ClassNumber}, |
| 346 | {"integer with an exponent", `let n = 2E10`, `2E10`, syntax.ClassNumber}, |
| 347 | {"minus is an operator, not part of the number", `let n = -1`, `-`, syntax.ClassOperator}, |
| 348 | |
| 349 | {"keyword", `function main = |args| {`, `function`, syntax.ClassKeyword}, |
| 350 | {"local keyword", `local function helper = |x| -> x`, `local`, syntax.ClassKeyword}, |
| 351 | {"word operator", `let ok = a and b`, `and`, syntax.ClassKeyword}, |
| 352 | {"orIfNull", `let v = x orIfNull 0`, `orIfNull`, syntax.ClassKeyword}, |
| 353 | {"oftype", `if x oftype String.class {`, `oftype`, syntax.ClassKeyword}, |
| 354 | {"match", `let l = match {`, `match`, syntax.ClassKeyword}, |
| 355 | {"when then otherwise", ` when x then "y"`, `then`, syntax.ClassKeyword}, |
| 356 | {"constant true", `let ok = true`, `true`, syntax.ClassConstant}, |
| 357 | {"constant null", `let n = null`, `null`, syntax.ClassConstant}, |
| 358 | {"builtin", `println("hi")`, `println`, syntax.ClassBuiltin}, |
| 359 | {"builtin collection literal", `let xs = list[1, 2]`, `list`, syntax.ClassBuiltin}, |
| 360 | {"builtin spelt like a type", `let o = DynamicObject()`, `DynamicObject`, syntax.ClassBuiltin}, |
| 361 | {"builtin range", `foreach i in range(0, 3) {`, `range`, syntax.ClassBuiltin}, |
| 362 | |
| 363 | {"declared function", `function main = |args| {`, `main`, syntax.ClassFunction}, |
| 364 | {"declared function with an arrow body", `function twice = |x| -> x * 2`, `twice`, syntax.ClassFunction}, |
| 365 | {"declared emoji function", `function 🚀launch = {`, `🚀launch`, syntax.ClassFunction}, |
| 366 | {"call", `helper(1)`, `helper`, syntax.ClassFunction}, |
| 367 | {"method call after a colon", `this: radius()`, `radius`, syntax.ClassFunction}, |
| 368 | {"plain identifier", `let shape = other`, `other`, syntax.ClassIdentifier}, |
| 369 | {"emoji identifier", `let 😀 = 1`, `😀`, syntax.ClassIdentifier}, |
| 370 | {"accented identifier", `let été = 1`, `été`, syntax.ClassIdentifier}, |
| 371 | {"CJK identifier", `let 名前 = 1`, `名前`, syntax.ClassIdentifier}, |
| 372 | {"underscore identifier", `let _hidden = 1`, `_hidden`, syntax.ClassIdentifier}, |
| 373 | |
| 374 | {"struct name", `struct Point = { x, y }`, `Point`, syntax.ClassType}, |
| 375 | {"union name", `union Shape = {`, `Shape`, syntax.ClassType}, |
| 376 | {"variant", ` Circle = { radius }`, `Circle`, syntax.ClassType}, |
| 377 | {"constructor call", `let p = Point(1, 2)`, `Point`, syntax.ClassType}, |
| 378 | {"variant constructor", `let r = Result_Failure("no")`, `Result_Failure`, syntax.ClassType}, |
| 379 | {"augment target", `augment Person {`, `Person`, syntax.ClassType}, |
| 380 | {"union variant separator", `augment Shape$Circle {`, `$`, syntax.ClassPunctuation}, |
| 381 | |
| 382 | {"module path", `module hello.World`, `hello.World`, syntax.ClassType}, |
| 383 | {"import path", `import gololang.Errors`, `gololang.Errors`, syntax.ClassType}, |
| 384 | {"three-part import path", `import java.util.List`, `java.util.List`, syntax.ClassType}, |
| 385 | |
| 386 | {"closure bars", `let f = |x| -> x`, `|`, syntax.ClassOperator}, |
| 387 | {"arrow", `let f = |x| -> x`, `->`, syntax.ClassOperator}, |
| 388 | {"colon", `this: name()`, `:`, syntax.ClassOperator}, |
| 389 | {"safe navigation", `let n = p?: x()`, `?:`, syntax.ClassOperator}, |
| 390 | {"comparison", `if a <= b {`, `<=`, syntax.ClassOperator}, |
| 391 | {"not equal", `if a != b {`, `!=`, syntax.ClassOperator}, |
| 392 | {"range operator", `let r = 1..3`, `..`, syntax.ClassOperator}, |
| 393 | {"range does not swallow the number", `let r = 1..3`, `1`, syntax.ClassNumber}, |
| 394 | {"variadic dots", `function f = |args...| {`, `...`, syntax.ClassOperator}, |
| 395 | {"module dot outside a path", `let x = a.b`, `.`, syntax.ClassPunctuation}, |
| 396 | {"brace", `function main = |args| {`, `{`, syntax.ClassPunctuation}, |
| 397 | {"bracket", `let xs = list[1]`, `[`, syntax.ClassPunctuation}, |
| 398 | {"comma", `struct Point = { x, y }`, `,`, syntax.ClassPunctuation}, |
| 399 | } |
| 400 | |
| 401 | for _, c := range cases { |
| 402 | t.Run(c.name, func(t *testing.T) { |
| 403 | assertClass(t, c.src, c.text, c.class) |
| 404 | }) |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // --- one case per thing the scanner deliberately refuses -------------------- |
| 409 | |
| 410 | func TestRefusals(t *testing.T) { |
| 411 | cases := []struct { |
| 412 | name string |
| 413 | why string |
| 414 | src string |
| 415 | text string |
| 416 | class syntax.Class |
| 417 | }{ |
| 418 | { |
| 419 | name: "no digit separators", |
| 420 | why: "the lexer has none, so 1_000 is the number 1 followed by the name _000", |
| 421 | src: `let n = 1_000`, |
| 422 | text: `1`, |
| 423 | class: syntax.ClassNumber, |
| 424 | }, |
| 425 | { |
| 426 | name: "no hexadecimal", |
| 427 | why: "the lexer has none, so 0xFF is the number 0 followed by the name xFF", |
| 428 | src: `let n = 0xFF`, |
| 429 | text: `xFF`, |
| 430 | class: syntax.ClassIdentifier, |
| 431 | }, |
| 432 | { |
| 433 | name: "a leading dot is never a number", |
| 434 | why: "the lexer requires a digit before the point, so .5 is a dot and then a number", |
| 435 | src: `let n = .5`, |
| 436 | text: `.`, |
| 437 | class: syntax.ClassPunctuation, |
| 438 | }, |
| 439 | { |
| 440 | name: "a lower-case l is not a long suffix", |
| 441 | why: "the lexer accepts only the upper-case L, so 42l is 42 and then the name l", |
| 442 | src: `let n = 42l`, |
| 443 | text: `42`, |
| 444 | class: syntax.ClassNumber, |
| 445 | }, |
| 446 | { |
| 447 | name: "three dashes are an operator run", |
| 448 | why: "the lexer asks for four dashes to open a comment; three are two minus signs and a third", |
| 449 | src: `let x = a --- b`, |
| 450 | text: `---`, |
| 451 | class: syntax.ClassOperator, |
| 452 | }, |
| 453 | { |
| 454 | name: "a constructor of your own is a type", |
| 455 | why: "nothing in the syntax separates Circle(1.0) from a type applied to arguments", |
| 456 | src: `let c = Circle(1.0)`, |
| 457 | text: `Circle`, |
| 458 | class: syntax.ClassType, |
| 459 | }, |
| 460 | { |
| 461 | name: "Some is not a constant", |
| 462 | why: "in Golo it is a variant of an ordinary union declared in gololang.Errors, not a builtin", |
| 463 | src: `let s = Some(1)`, |
| 464 | text: `Some`, |
| 465 | class: syntax.ClassType, |
| 466 | }, |
| 467 | { |
| 468 | name: "a capitalised variable is a type", |
| 469 | why: "the case rule is a convention, and the scanner follows the convention rather than the parser", |
| 470 | src: `let Count = 1`, |
| 471 | text: `Count`, |
| 472 | class: syntax.ClassType, |
| 473 | }, |
| 474 | { |
| 475 | name: "a keyword used as a method name stays a keyword", |
| 476 | why: "the scanner does not track what a colon introduces, and the lexer would refuse the word anyway", |
| 477 | src: `obj: match()`, |
| 478 | text: `match`, |
| 479 | class: syntax.ClassKeyword, |
| 480 | }, |
| 481 | { |
| 482 | name: "a module path stops at a dot with nothing after it", |
| 483 | why: "half-typed `import a.` leaves the dot as punctuation rather than swallowing it", |
| 484 | src: `import a.`, |
| 485 | text: `.`, |
| 486 | class: syntax.ClassPunctuation, |
| 487 | }, |
| 488 | { |
| 489 | name: "a string nothing closes runs to the end of the line and beyond", |
| 490 | why: "the interpreter reads to the closing quote wherever it is, so the colour follows it", |
| 491 | src: `let s = "oops`, |
| 492 | text: `"oops`, |
| 493 | class: syntax.ClassString, |
| 494 | }, |
| 495 | { |
| 496 | name: "no escapes inside a triple-quoted string", |
| 497 | why: "the lexer appends every rune until the three quotes, so a backslash-quote does not protect them", |
| 498 | src: `let s = """a\""" + b`, |
| 499 | text: `"""a\"""`, |
| 500 | class: syntax.ClassString, |
| 501 | }, |
| 502 | } |
| 503 | |
| 504 | for _, c := range cases { |
| 505 | t.Run(c.name, func(t *testing.T) { |
| 506 | assertClass(t, c.src, c.text, c.class) |
| 507 | }) |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | func TestAnUnterminatedStringPaintsTheNextLine(t *testing.T) { |
| 512 | // The other half of the carry decision, stated as what a user sees: the |
| 513 | // line after a stray quote is coloured as string, because that is what |
| 514 | // the interpreter will read it as. |
| 515 | src := "let broken = \"unterminated\nlet after = 1" |
| 516 | |
| 517 | if got, ok := find(lineOf(src, 1), "let after = 1"); !ok || got.class != syntax.ClassString { |
| 518 | t.Errorf("the line after an unterminated string is %v, want all string", lineOf(src, 1)) |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | func TestTheDeclaredNameAfterFunctionIsAFunctionEvenWhenNoParenthesisFollows(t *testing.T) { |
| 523 | // Everywhere else a name is a function because a parenthesis follows it. |
| 524 | // A declaration is followed by an equals sign, and is the one place a |
| 525 | // reader most wants the colour. |
| 526 | spans := colouredLine(t, `function main = |args| {`) |
| 527 | |
| 528 | got, ok := find(spans, "main") |
| 529 | if !ok || got.class != syntax.ClassFunction { |
| 530 | t.Errorf("the declared name is %v, want a function; got %v", got, spans) |
| 531 | } |
| 532 | if got, ok := find(spans, "args"); !ok || got.class != syntax.ClassIdentifier { |
| 533 | t.Errorf("the parameter is %v, want a plain name", got) |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | func TestTheKeywordTableIsEveryReservedWordButTheLiterals(t *testing.T) { |
| 538 | // token/token.go in GoloScript reserves 41 words. Three of them are the |
| 539 | // literal values, which are constants here; the other 38 are keywords. |
| 540 | keywords := gololang.Keywords() |
| 541 | |
| 542 | if len(keywords) != 38 { |
| 543 | t.Errorf("the scanner knows %d keywords, want 38", len(keywords)) |
| 544 | } |
| 545 | for _, literal := range []string{"true", "false", "null"} { |
| 546 | for _, keyword := range keywords { |
| 547 | if keyword == literal { |
| 548 | t.Errorf("%q is in the keyword table; it is a constant", literal) |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | func TestTheBuiltinTableHoldsWhatTheInterpreterProvides(t *testing.T) { |
| 555 | // evaluator.BuiltinNames() answers 162 names, five of which begin with a |
| 556 | // double underscore and are the test runner's own counters. A test in |
| 557 | // editor_test.go holds this table to a real golo when one is installed; |
| 558 | // this one holds its shape when none is. |
| 559 | builtins := gololang.Builtins() |
| 560 | |
| 561 | if len(builtins) != 157 { |
| 562 | t.Errorf("the scanner knows %d builtins, want 157", len(builtins)) |
| 563 | } |
| 564 | seen := map[string]bool{} |
| 565 | for _, name := range builtins { |
| 566 | if strings.HasPrefix(name, "__") { |
| 567 | t.Errorf("%q is an internal helper and should not be coloured as a builtin", name) |
| 568 | } |
| 569 | if seen[name] { |
| 570 | t.Errorf("%q is listed twice", name) |
| 571 | } |
| 572 | seen[name] = true |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | func TestHighlightIsWhatTheRegistryUses(t *testing.T) { |
| 577 | gololang.Register() |
| 578 | |
| 579 | spans := syntax.Highlight(gololang.Language, "function main = |args| {\n") |
| 580 | if len(spans) == 0 || len(spans[0]) == 0 { |
| 581 | t.Fatalf("syntax.Highlight gave nothing for Golo: %v", spans) |
| 582 | } |
| 583 | if spans[0][0].Class != syntax.ClassKeyword { |
| 584 | t.Errorf("the registered highlighter coloured function as %s, want a keyword", spans[0][0].Class) |
| 585 | } |
| 586 | } |