Skip to content

Commit db7581c

Browse files
committed
highlighter: Add basic syntax highlighting test
1 parent f56e627 commit db7581c

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package highlight
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
const syntax = `
10+
filetype: test
11+
detect:
12+
filename: "test"
13+
rules:
14+
- type: "\\b(one|two|three)\\b"
15+
- constant.string:
16+
start: "\""
17+
end: "\""
18+
- constant.string:
19+
start: "'"
20+
end: "'"
21+
- comment:
22+
start: "//"
23+
end: "$"
24+
rules:
25+
- todo: "(TODO):?"
26+
- comment:
27+
start: "/\\*"
28+
end: "\\*/"
29+
rules:
30+
- todo: "(TODO):?"
31+
`
32+
33+
const content = `
34+
one two three
35+
// comment
36+
one
37+
two
38+
three
39+
/*
40+
multi
41+
line
42+
// comment
43+
with
44+
TODO
45+
*/
46+
"string"
47+
one two three
48+
"
49+
multi
50+
line
51+
str'ng
52+
"
53+
one two three
54+
'string'
55+
one two three
56+
// '
57+
one "string" two /*rule*/ three //all
58+
/* " */
59+
`
60+
61+
var expectation = []map[int]string{
62+
{},
63+
{0: "type", 3: "", 4: "type", 7: "", 8: "type"},
64+
{0: "comment"},
65+
{0: "type"},
66+
{0: "type"},
67+
{0: "type"},
68+
{0: "comment"},
69+
{0: "comment"},
70+
{0: "comment"},
71+
{0: "comment"},
72+
{0: "comment"},
73+
{0: "todo"},
74+
{0: "comment"},
75+
{0: "constant.string"},
76+
{0: "type", 3: "", 4: "type", 7: "", 8: "type"},
77+
{0: "constant.string"},
78+
{0: "constant.string"},
79+
{0: "constant.string"},
80+
{0: "constant.string"},
81+
{0: "constant.string"},
82+
{0: "type", 3: "", 4: "type", 7: "", 8: "type"},
83+
{0: "constant.string"},
84+
{0: "type", 3: "", 4: "type", 7: "", 8: "type"},
85+
{0: "comment"},
86+
{0: "type", 3: "", 4: "constant.string", 12: "", 13: "type", 16: "", 17: "comment", 25: "", 26: "type", 31: "", 32: "comment"},
87+
{0: "comment"},
88+
{},
89+
}
90+
91+
func TestHighlightString(t *testing.T) {
92+
header, err := MakeHeaderYaml([]byte(syntax))
93+
if !assert.NoError(t, err) {
94+
return
95+
}
96+
97+
file, err := ParseFile([]byte(syntax))
98+
if !assert.NoError(t, err) {
99+
return
100+
}
101+
102+
def, err := ParseDef(file, header)
103+
if !assert.NoError(t, err) {
104+
return
105+
}
106+
107+
highlighter := NewHighlighter(def)
108+
matches := highlighter.HighlightString(content)
109+
result := assert.Equal(t, len(expectation), len(matches))
110+
if !result {
111+
return
112+
}
113+
114+
for i, m := range matches {
115+
result = assert.Equal(t, len(expectation[i]), len(m))
116+
if !result {
117+
return
118+
}
119+
actual := map[int]string{}
120+
for k, g := range m {
121+
actual[k] = g.String()
122+
}
123+
result := assert.Equal(t, expectation[i], actual, i)
124+
if !result {
125+
return
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)