-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_test.gala
More file actions
223 lines (192 loc) · 7.29 KB
/
syntax_test.gala
File metadata and controls
223 lines (192 loc) · 7.29 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
package gala_tui
import (
. "martianoff/gala/test"
)
// ============================================================================
// Syntax highlighter + fenced code block tests.
// ============================================================================
// ----------------------------------------------------------------------------
// Tokenizer
// ----------------------------------------------------------------------------
func TestTokenizeKeyword(t T) T {
val toks = tokenize("if x { return 1 }", "go")
return IsTrue(t, anyTokenOfKind(toks, "keyword"))
}
func TestTokenizeStringLiteral(t T) T {
val toks = tokenize("name = \"alice\"", "go")
return IsTrue(t, anyTokenOfKind(toks, "string"))
}
func TestTokenizeNumberLiteral(t T) T {
val toks = tokenize("count := 42 + 3.14", "go")
return IsTrue(t, anyTokenOfKind(toks, "number"))
}
func TestTokenizeLineComment(t T) T {
val toks = tokenize("x := 1 // inline comment", "go")
return IsTrue(t, anyTokenOfKind(toks, "comment"))
}
func TestTokenizePythonHashComment(t T) T {
val toks = tokenize("x = 1 # Python comment", "python")
return IsTrue(t, anyTokenOfKind(toks, "comment"))
}
func TestTokenizeShellComment(t T) T {
val toks = tokenize("rm /tmp/foo # cleanup", "bash")
return IsTrue(t, anyTokenOfKind(toks, "comment"))
}
func TestTokenizeIdentNotKeyword(t T) T {
val toks = tokenize("foo bar", "go")
val onlyIdents = !anyTokenOfKind(toks, "keyword")
return IsTrue(t, onlyIdents)
}
func TestTokenizeUnknownLanguageHasNoKeywords(t T) T {
val toks = tokenize("if foo then bar", "fortran-77")
val noKw = !anyTokenOfKind(toks, "keyword")
return IsTrue(t, noKw)
}
func TestTokenizeRust(t T) T {
val toks = tokenize("fn main() { let x = 1; }", "rust")
return IsTrue(t, anyTokenOfKind(toks, "keyword"))
}
func TestTokenizeGalaKeywords(t T) T {
val toks = tokenize("val x = 42", "gala")
return IsTrue(t, anyTokenOfKind(toks, "keyword"))
}
// Pin down the full set of gala keywords / built-in types — the
// regression guard if someone narrows the table by accident.
func TestTokenizeGalaCoreKeywordsAllRecognised(t T) T {
val sample =
"func f[T any](xs Array[T]) Array[T] = xs match { " +
"case Nil() => xs; case _ => xs }"
val toks = tokenize(sample, "gala")
val gotKeywords = countTokens(toks, "keyword")
// Expect at least: func, any, match, case (×2). The exact count
// depends on how "Array" is tokenised (it's an identifier), so
// we just check >= 5 keywords were recognised.
return IsTrue(t, gotKeywords >= 5)
}
// Built-in types like `int`, `string`, `bool` should also paint as
// keywords for consistency with how Go / Rust highlighters treat them.
func TestTokenizeGalaPrimitiveTypeIsKeyword(t T) T {
val toks = tokenize("val n int = 0", "gala")
val gotKeywords = countTokens(toks, "keyword")
// "val" and "int" both keywords → at least 2.
return IsTrue(t, gotKeywords >= 2)
}
// String interpolation: the leading `s` is an ident, the quoted
// payload is a string. Verify the string token is emitted.
func TestTokenizeGalaInterpolatedStringHasStringToken(t T) T {
val toks = tokenize("val msg = s\"hello $name\"", "gala")
return IsTrue(t, anyTokenOfKind(toks, "string"))
}
// ----------------------------------------------------------------------------
// HighlightLine rendering
// ----------------------------------------------------------------------------
func TestHighlightLineKeywordIsMagentaBold(t T) T {
val buf = NewBuffer(40, 1)
RenderTo(HighlightLine("if cond { 1 }", "go"),
Rect(X = 0, Y = 0, Width = 40, Height = 1), buf)
// ' ' at col 0 (leading pad), 'i' of "if" at col 1 → bold magenta.
val t1 = IsTrue(t, buf.StyleAt(1, 0).Bold)
return IsTrue(t1, ColorEq(buf.StyleAt(1, 0).Fg, BrightMagenta()))
}
func TestHighlightLineStringIsGreen(t T) T {
val buf = NewBuffer(40, 1)
RenderTo(HighlightLine("x = \"hi\"", "go"),
Rect(X = 0, Y = 0, Width = 40, Height = 1), buf)
// Find the position of '"'. Assume " starts at col 5 (' x = ').
return IsTrue(t, ColorEq(buf.StyleAt(5, 0).Fg, BrightGreen()))
}
// ----------------------------------------------------------------------------
// Markdown fenced code blocks
// ----------------------------------------------------------------------------
func TestMarkdownFencedCodeBlockParsed(t T) T {
val src = "before\n\n```go\nfunc main() { }\n```\n\nafter"
val blocks = ParseMarkdown(src)
val codeCount = blocksOfKind(blocks, "code")
return Eq(t, codeCount, 1)
}
func TestMarkdownFencedCodeBlockExtractsLang(t T) T {
val src = "```python\nprint('hi')\n```"
val blocks = ParseMarkdown(src)
val first = blocks.Get(0)
val isCodeWithLang = first match {
case MdCodeBlock(lang, _) => lang == "python"
case _ => false
}
return IsTrue(t, isCodeWithLang)
}
func TestMarkdownFencedCodeBlockKeepsLines(t T) T {
val src = "```go\nline1\nline2\nline3\n```"
val blocks = ParseMarkdown(src)
val lineCount = blocks.Get(0) match {
case MdCodeBlock(_, lines) => lines.Length()
case _ => -1
}
return Eq(t, lineCount, 3)
}
func TestMarkdownFencedCodeBlockNoLangIsBlankString(t T) T {
val src = "```\nplain code\n```"
val blocks = ParseMarkdown(src)
val emptyLang = blocks.Get(0) match {
case MdCodeBlock(lang, _) => lang == ""
case _ => false
}
return IsTrue(t, emptyLang)
}
func TestMarkdownFencedCodeBlockRendersWithBorder(t T) T {
val src = "```go\nprintln(\"hi\")\n```"
val widget = MarkdownView(src)
val buf = NewBuffer(20, 5)
RenderTo(widget, Rect(X = 0, Y = 0, Width = 20, Height = 5), buf)
val joined = stitchSyntaxRows(buf)
return IsTrue(t, syntaxRowHas(joined, "println"))
}
// ----------------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------------
func anyTokenOfKind(toks Array[SyntaxToken], kind string) bool =
toks.Exists((tok) => tokenKindMatches(tok, kind))
func countTokens(toks Array[SyntaxToken], kind string) int =
toks.Count((tok) => tokenKindMatches(tok, kind))
func tokenKindMatches(tok SyntaxToken, kind string) bool {
return tok match {
case TokKeyword(_) => kind == "keyword"
case TokString(_) => kind == "string"
case TokNumber(_) => kind == "number"
case TokComment(_) => kind == "comment"
case TokIdent(_) => kind == "ident"
case TokPunct(_) => kind == "punct"
}
}
func stitchSyntaxRows(buf *Buffer) string {
var s = ""
var y = 0
for y < buf.Height {
s = s + BufferText(buf, y) + "\n"
y = y + 1
}
return s
}
func syntaxRowHas(hay string, needle string) bool {
val h = syntaxRunes(hay)
val n = syntaxRunes(needle)
val hn = h.Length()
val nn = n.Length()
if nn == 0 { return true }
if nn > hn { return false }
var i = 0
for i <= hn - nn {
var j = 0
var ok = true
for j < nn {
if h.Get(i + j) != n.Get(j) {
ok = false
j = nn
} else {
j = j + 1
}
}
if ok { return true }
i = i + 1
}
return false
}