-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathhighlight_parser_go_regression_test.go
More file actions
78 lines (69 loc) · 2.08 KB
/
Copy pathhighlight_parser_go_regression_test.go
File metadata and controls
78 lines (69 loc) · 2.08 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
package gotreesitter_test
import (
"os"
"testing"
gotreesitter "github.com/odvcencio/gotreesitter"
"github.com/odvcencio/gotreesitter/grammars"
)
func TestHighlightParserGoRealFile(t *testing.T) {
src, err := os.ReadFile("parser.go")
if err != nil {
t.Fatalf("read parser.go: %v", err)
}
entry := grammars.DetectLanguage("parser.go")
if entry == nil {
t.Fatal("DetectLanguage(parser.go) returned nil")
}
lang := entry.Language()
if lang == nil {
t.Fatal("go language is nil")
}
parser := gotreesitter.NewParser(lang)
var tree *gotreesitter.Tree
if entry.TokenSourceFactory != nil {
// ts2go Go blob path — custom lexer registered.
tree, err = parser.ParseWithTokenSource(src, entry.TokenSourceFactory(src, lang))
} else {
// grammargen Go blob path (default in 0.14.0+). The baked DFA parses
// Go without a custom lexer.
tree, err = parser.Parse(src)
}
if err != nil {
t.Fatalf("parse: %v", err)
}
defer tree.Release()
root := tree.RootNode()
if root == nil {
t.Fatal("parse root is nil")
}
if got, want := tree.ParseStopReason(), gotreesitter.ParseStopAccepted; got != want {
t.Fatalf("parse stop reason = %q, want %q (runtime=%s)", got, want, tree.ParseRuntime().Summary())
}
if root.HasError() {
t.Fatalf("parse root has error=true (runtime=%s)", tree.ParseRuntime().Summary())
}
hlOpts := []gotreesitter.HighlighterOption{}
if entry.TokenSourceFactory != nil {
hlOpts = append(hlOpts, gotreesitter.WithTokenSourceFactory(func(source []byte) gotreesitter.TokenSource {
return entry.TokenSourceFactory(source, lang)
}))
}
hl, err := gotreesitter.NewHighlighter(lang, entry.HighlightQuery, hlOpts...)
if err != nil {
t.Fatalf("NewHighlighter: %v", err)
}
ranges := hl.Highlight(src)
if len(ranges) == 0 {
t.Fatal("Highlight(parser.go) returned 0 ranges")
}
foundPackageKeyword := false
for _, r := range ranges {
if r.Capture == "keyword" && string(src[r.StartByte:r.EndByte]) == "package" {
foundPackageKeyword = true
break
}
}
if !foundPackageKeyword {
t.Fatal("missing keyword capture for package token in parser.go")
}
}