-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsyntax_test.go
More file actions
309 lines (270 loc) · 7.79 KB
/
syntax_test.go
File metadata and controls
309 lines (270 loc) · 7.79 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
// SPDX-License-Identifier: MIT
package main
import (
"strings"
"testing"
)
func TestTokenize_GoFuncLine(t *testing.T) {
line := `func main() {`
tokens := Tokenize(line)
if len(tokens) == 0 {
t.Fatal("expected tokens, got none")
}
// First token should be "func" keyword
if tokens[0].Kind != TkKeyword {
t.Errorf("expected first token to be TkKeyword, got %d", tokens[0].Kind)
}
if line[tokens[0].Start:tokens[0].End] != "func" {
t.Errorf("expected 'func', got %q", line[tokens[0].Start:tokens[0].End])
}
// Should find "main" as an identifier (plain, lowercase)
found := false
for _, tok := range tokens {
text := line[tok.Start:tok.End]
if text == "main" {
found = true
if tok.Kind != TkPlain {
t.Errorf("expected 'main' to be TkPlain, got %d", tok.Kind)
}
}
}
if !found {
t.Error("did not find 'main' token")
}
}
func TestTokenize_StringLiteral(t *testing.T) {
line := `x := "hello world"`
tokens := Tokenize(line)
foundString := false
for _, tok := range tokens {
if tok.Kind == TkString {
text := line[tok.Start:tok.End]
if text == `"hello world"` {
foundString = true
}
}
}
if !foundString {
t.Error("expected to find string literal token")
}
}
func TestTokenize_Comment(t *testing.T) {
line := `x := 1 // a comment`
tokens := Tokenize(line)
foundComment := false
for _, tok := range tokens {
if tok.Kind == TkComment {
foundComment = true
}
}
if !foundComment {
t.Error("expected to find comment token")
}
}
func TestTokenize_Number(t *testing.T) {
line := `y := 42 + 3.14`
tokens := Tokenize(line)
numCount := 0
for _, tok := range tokens {
if tok.Kind == TkNumber {
numCount++
}
}
if numCount < 2 {
t.Errorf("expected at least 2 number tokens, got %d", numCount)
}
}
func TestTokenize_UppercaseType(t *testing.T) {
line := `var x MyType`
tokens := Tokenize(line)
found := false
for _, tok := range tokens {
text := line[tok.Start:tok.End]
if text == "MyType" {
found = true
if tok.Kind != TkType {
t.Errorf("expected 'MyType' to be TkType, got %d", tok.Kind)
}
}
}
if !found {
t.Error("did not find 'MyType' token")
}
}
func TestTokenize_CoversFullSource(t *testing.T) {
line := `func Foo(x int) string { return "bar" }`
tokens := Tokenize(line)
// Verify tokens cover the entire source with no gaps
if len(tokens) == 0 {
t.Fatal("expected tokens")
}
if tokens[0].Start != 0 {
t.Errorf("first token starts at %d, expected 0", tokens[0].Start)
}
if tokens[len(tokens)-1].End != len(line) {
t.Errorf("last token ends at %d, expected %d", tokens[len(tokens)-1].End, len(line))
}
for i := 1; i < len(tokens); i++ {
if tokens[i].Start != tokens[i-1].End {
t.Errorf("gap between token %d (end=%d) and %d (start=%d)",
i-1, tokens[i-1].End, i, tokens[i].Start)
}
}
}
func TestBuildKindArray_MatchOverridesSyntax(t *testing.T) {
line := `func main()`
tokens := Tokenize(line)
matchLocs := [][]int{{0, 4}} // "func" match
kinds := BuildKindArray(line, tokens, matchLocs)
// Positions 0-3 should be TkMatch (overriding TkKeyword)
for i := 0; i < 4; i++ {
if kinds[i] != TkMatch {
t.Errorf("position %d: expected TkMatch, got %d", i, kinds[i])
}
}
// Position after "func " should NOT be TkMatch
if kinds[5] == TkMatch {
t.Error("position 5 should not be TkMatch")
}
}
func TestBuildKindArray_EmptyLine(t *testing.T) {
kinds := BuildKindArray("", nil, nil)
if len(kinds) != 0 {
t.Errorf("expected empty kinds for empty line, got %d", len(kinds))
}
}
func TestRenderANSI_PlainText(t *testing.T) {
line := "hello"
kinds := make([]TokenKind, len(line)) // all TkPlain
result := RenderANSI(line, kinds)
if result != "hello" {
t.Errorf("expected plain 'hello', got %q", result)
}
}
func TestRenderANSI_WithKeyword(t *testing.T) {
line := `func main`
tokens := Tokenize(line)
kinds := BuildKindArray(line, tokens, nil)
result := RenderANSI(line, kinds)
// Should contain ANSI escape for keyword
if !strings.Contains(result, "\033[38;5;75m") {
t.Error("expected keyword ANSI color in output")
}
if !strings.Contains(result, "func") {
t.Error("expected 'func' in output")
}
}
func TestRenderANSI_WithMatch(t *testing.T) {
line := `func main`
tokens := Tokenize(line)
matchLocs := [][]int{{5, 9}} // "main"
kinds := BuildKindArray(line, tokens, matchLocs)
result := RenderANSI(line, kinds)
// Should contain match ANSI (red bold)
if !strings.Contains(result, "\033[1;31m") {
t.Error("expected match ANSI color in output")
}
}
func TestRenderANSILine_Convenience(t *testing.T) {
line := `if x == 42 { return "yes" }`
result := RenderANSILine(line, [][]int{{3, 5}}, false)
if !strings.Contains(result, "if") {
t.Error("expected 'if' in output")
}
if !strings.Contains(result, "42") {
t.Error("expected '42' in output")
}
}
func TestRenderLipgloss_EmptyLine(t *testing.T) {
result := RenderLipgloss("", nil, false)
if result != "" {
t.Errorf("expected empty string for empty line, got %q", result)
}
}
func TestRenderHTML_PlainText(t *testing.T) {
line := "hello"
kinds := make([]TokenKind, len(line)) // all TkPlain
result := RenderHTML(line, kinds)
if result != "hello" {
t.Errorf("expected plain 'hello', got %q", result)
}
}
func TestRenderHTML_WithKeyword(t *testing.T) {
line := `func main`
tokens := Tokenize(line)
kinds := BuildKindArray(line, tokens, nil)
result := RenderHTML(line, kinds)
if !strings.Contains(result, `<span class="syn-kw">`) {
t.Error("expected keyword span in output")
}
if !strings.Contains(result, "func") {
t.Error("expected 'func' in output")
}
}
func TestRenderHTML_WithMatch(t *testing.T) {
line := `func main`
tokens := Tokenize(line)
matchLocs := [][]int{{5, 9}} // "main"
kinds := BuildKindArray(line, tokens, matchLocs)
result := RenderHTML(line, kinds)
if !strings.Contains(result, "<strong>main</strong>") {
t.Errorf("expected <strong>main</strong> in output, got %q", result)
}
}
func TestRenderHTML_HTMLEscaping(t *testing.T) {
line := `x < y && z > 0`
tokens := Tokenize(line)
kinds := BuildKindArray(line, tokens, nil)
result := RenderHTML(line, kinds)
if strings.Contains(result, "<") && !strings.Contains(result, "<") {
// check that raw < from source is escaped
t.Error("expected HTML escaping of '<'")
}
// Ensure no raw '<' that isn't part of a tag
// The result should contain < and & for the source chars
if !strings.Contains(result, "&") {
t.Error("expected '&' to be escaped as '&'")
}
if !strings.Contains(result, "<") {
t.Error("expected '<' to be escaped as '<'")
}
if !strings.Contains(result, ">") {
t.Error("expected '>' to be escaped as '>'")
}
}
func TestRenderHTMLLine_Convenience(t *testing.T) {
line := `if x == 42 { return "yes" }`
result := RenderHTMLLine(line, [][]int{{3, 5}}, false)
if !strings.Contains(result, `<span class="syn-kw">`) {
t.Error("expected keyword span for 'if'")
}
if !strings.Contains(result, "<strong>") {
t.Error("expected strong tag for match")
}
if !strings.Contains(result, `<span class="syn-str">`) {
t.Error("expected string span for '\"yes\"'")
}
}
func BenchmarkRenderHTMLLine(b *testing.B) {
line := `func extractRelevantV3(res *FileJob, documentTermFrequency map[string]int, snippetLength int) []Snippet {`
matchLocs := [][]int{{5, 22}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
RenderHTMLLine(line, matchLocs, false)
}
}
func BenchmarkTokenize(b *testing.B) {
line := `func extractRelevantV3(res *FileJob, documentTermFrequency map[string]int, snippetLength int) []Snippet {`
b.ResetTimer()
for i := 0; i < b.N; i++ {
Tokenize(line)
}
}
func BenchmarkRenderANSILine(b *testing.B) {
line := `func extractRelevantV3(res *FileJob, documentTermFrequency map[string]int, snippetLength int) []Snippet {`
matchLocs := [][]int{{5, 22}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
RenderANSILine(line, matchLocs, false)
}
}