-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbindings_test.go
347 lines (276 loc) · 8.52 KB
/
bindings_test.go
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package sitter_test
import (
"bytes"
"runtime"
"sync"
"testing"
"time"
sitter "github.com/kiteco/go-tree-sitter"
"github.com/kiteco/go-tree-sitter/golang"
"github.com/kiteco/go-tree-sitter/javascript"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRootNode(t *testing.T) {
assert := assert.New(t)
n := sitter.Parse([]byte("let a = 1"), javascript.GetLanguage())
assert.Equal(uint32(0), n.StartByte())
assert.Equal(uint32(9), n.EndByte())
assert.Equal(sitter.Point{
Row: 0,
Column: 0,
}, n.StartPoint())
assert.Equal(sitter.Point{
Row: 0,
Column: 9,
}, n.EndPoint())
assert.Equal("(program (lexical_declaration (variable_declarator name: (identifier) value: (number))))", n.String())
assert.Equal("program", n.Type())
assert.Equal(sitter.Symbol(119), n.Symbol())
assert.Equal(false, n.IsNull())
assert.Equal(true, n.IsNamed())
assert.Equal(false, n.IsMissing())
assert.Equal(false, n.HasChanges())
assert.Equal(false, n.HasError())
assert.Equal(uint32(1), n.ChildCount())
assert.Equal(uint32(1), n.NamedChildCount())
assert.Nil(n.Parent())
assert.Nil(n.NextSibling())
assert.Nil(n.NextNamedSibling())
assert.Nil(n.PrevSibling())
assert.Nil(n.PrevNamedSibling())
assert.NotNil(n.Child(0))
assert.NotNil(n.NamedChild(0))
assert.Nil(n.ChildByFieldName("unknown"))
assert.NotNil(n.NamedChild(0).NamedChild(0).ChildByFieldName("name"))
}
func TestTree(t *testing.T) {
assert := assert.New(t)
parser := sitter.NewParser()
parser.Debug()
parser.SetLanguage(javascript.GetLanguage())
tree := parser.Parse([]byte("let a = 1"))
n := tree.RootNode()
assert.Equal(uint32(0), n.StartByte())
assert.Equal(uint32(9), n.EndByte())
assert.Equal("program", n.Type())
assert.Equal("(program (lexical_declaration (variable_declarator name: (identifier) value: (number))))", n.String())
tree2 := parser.Parse([]byte("let a = 'a'"))
n = tree2.RootNode()
assert.Equal("(program (lexical_declaration (variable_declarator name: (identifier) value: (string))))", n.String())
// change 'a' -> true
newText := []byte("let a = true")
tree2.Edit(sitter.EditInput{
StartIndex: 8,
OldEndIndex: 11,
NewEndIndex: 12,
StartPoint: sitter.Point{
Row: 0,
Column: 8,
},
OldEndPoint: sitter.Point{
Row: 0,
Column: 11,
},
NewEndPoint: sitter.Point{
Row: 0,
Column: 12,
},
})
// check that it changed tree
assert.True(n.HasChanges())
assert.True(n.Child(0).HasChanges())
assert.False(n.Child(0).Child(0).HasChanges()) // left side of tree didn't change
assert.True(n.Child(0).Child(1).HasChanges())
tree3 := parser.ParseWithTree(newText, tree2)
n = tree3.RootNode()
assert.Equal("(program (lexical_declaration (variable_declarator name: (identifier) value: (true))))", n.String())
}
func TestLanguage(t *testing.T) {
assert := assert.New(t)
js := javascript.GetLanguage()
assert.True(js.SymbolCount() > 171)
assert.Equal(js.SymbolName(171), "class_declaration")
assert.Equal(js.SymbolType(171), sitter.SymbolTypeRegular)
assert.Equal(sitter.SymbolTypeRegular.String(), "Regular")
}
func TestGC(t *testing.T) {
assert := assert.New(t)
parser := sitter.NewParser()
parser.SetLanguage(javascript.GetLanguage())
tree := parser.Parse([]byte("let a = 1"))
n := tree.RootNode()
r := isNamedWithGC(n)
assert.True(r)
}
func isNamedWithGC(n *sitter.Node) bool {
runtime.GC()
time.Sleep(500 * time.Microsecond)
return n.IsNamed()
}
func TestSetOperationLimit(t *testing.T) {
assert := assert.New(t)
parser := sitter.NewParser()
assert.Equal(0, parser.OperationLimit())
parser.SetOperationLimit(10)
assert.Equal(10, parser.OperationLimit())
}
func TestIncludedRanges(t *testing.T) {
assert := assert.New(t)
// go code with JS in comment
code := "package main\n//console.log('sup');"
parser := sitter.NewParser()
parser.SetLanguage(golang.GetLanguage())
goTree := parser.Parse([]byte(code))
commentNode := goTree.RootNode().NamedChild(1)
assert.Equal("comment", commentNode.Type())
jsRange := sitter.Range{
StartPoint: sitter.Point{
Row: commentNode.StartPoint().Row,
Column: commentNode.StartPoint().Column + 2,
},
EndPoint: commentNode.EndPoint(),
StartByte: commentNode.StartByte() + 2,
EndByte: commentNode.EndByte(),
}
parser.SetIncludedRanges([]sitter.Range{jsRange})
parser.SetLanguage(javascript.GetLanguage())
jsTree := parser.Parse([]byte(code))
assert.Equal(
"(program (expression_statement (call_expression function: (member_expression object: (identifier) property: (property_identifier)) arguments: (arguments (string)))))",
jsTree.RootNode().String(),
)
}
func TestSameNode(t *testing.T) {
assert := assert.New(t)
parser := sitter.NewParser()
parser.SetLanguage(javascript.GetLanguage())
tree := parser.Parse([]byte("let a = 1"))
n1 := tree.RootNode()
n2 := tree.RootNode()
assert.True(n1.Equal(n2))
n1 = tree.RootNode().NamedChild(0)
n2 = tree.RootNode().NamedChild(0)
assert.True(n1.Equal(n2))
}
func TestQuery(t *testing.T) {
js := `
class Person {
constructor(firstName, parent) {}
getName() {}
}
`
// test single capture
testCaptures(t, js, "(class_declaration name: (identifier) @class-name)", []string{
"Person",
})
// test multiple captures
testCaptures(t, js, "(method_definition name: * @method-name)", []string{
"constructor",
"getName",
})
// test match only
parser := sitter.NewParser()
parser.SetLanguage(javascript.GetLanguage())
tree := parser.Parse([]byte(js))
root := tree.RootNode()
q, err := sitter.NewQuery([]byte("(identifier) (method_definition)"), javascript.GetLanguage())
assert.Nil(t, err)
qc := sitter.NewQueryCursor()
qc.Exec(q, root)
var matched int
for {
_, ok := qc.NextMatch()
if !ok {
break
}
matched++
}
assert.Equal(t, 5, matched)
}
func testCaptures(t *testing.T, body, sq string, expected []string) {
assert := assert.New(t)
parser := sitter.NewParser()
parser.SetLanguage(javascript.GetLanguage())
tree := parser.Parse([]byte(body))
root := tree.RootNode()
q, err := sitter.NewQuery([]byte(sq), javascript.GetLanguage())
assert.Nil(err)
qc := sitter.NewQueryCursor()
qc.Exec(q, root)
actual := []string{}
for {
m, ok := qc.NextMatch()
if !ok {
break
}
for _, c := range m.Captures {
actual = append(actual, c.Node.Content([]byte(body)))
}
}
assert.EqualValues(expected, actual)
}
func TestQueryError(t *testing.T) {
assert := assert.New(t)
q, err := sitter.NewQuery([]byte("((unknown) name: (identifier))"), javascript.GetLanguage())
assert.Nil(q)
assert.NotNil(err)
assert.EqualValues(&sitter.QueryError{Offset: 0x02, Type: sitter.QueryErrorNodeType}, err)
}
func doWorkLifetime(t testing.TB, n *sitter.Node) {
for i := 0; i < 100; i++ {
// this will trigger an actual bug (if it still there)
s := n.String()
require.Equal(t, "(program (variable_declaration (variable_declarator name: (identifier))))", s)
}
}
func TestParserLifetime(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
p := sitter.NewParser()
p.SetLanguage(javascript.GetLanguage())
data := []byte("var a;")
// create some memory/CPU pressure
data = append(data, bytes.Repeat([]byte(" "), 1024*1024)...)
root := p.Parse(data).RootNode()
// make sure we have no references to the Parser
p = nil
// must be a separate function, and it shouldn't accept the parser, only the Tree
doWorkLifetime(t, root)
}
}()
}
wg.Wait()
}
func TestTreeCursor(t *testing.T) {
assert := assert.New(t)
input := []byte(`let a = 1;`)
root := sitter.Parse(input, javascript.GetLanguage())
c := sitter.NewTreeCursor(root)
assert.True(c.CurrentNode().Equal(root))
assert.Equal("", c.CurrentFieldName())
assert.False(c.GoToParent())
assert.False(c.GoToNextSibling())
assert.Equal(int64(-1), c.GoToFirstChildForByte(100))
assert.True(c.GoToFirstChild())
assert.Equal("lexical_declaration", c.CurrentNode().Type())
assert.True(c.GoToFirstChild())
assert.Equal("let", c.CurrentNode().Type())
assert.True(c.GoToNextSibling())
assert.Equal("variable_declarator", c.CurrentNode().Type())
assert.True(c.GoToFirstChild())
assert.Equal("identifier", c.CurrentNode().Type())
assert.Equal("name", c.CurrentFieldName())
assert.True(c.GoToParent())
assert.Equal("variable_declarator", c.CurrentNode().Type())
nodeForReset := c.CurrentNode()
assert.Equal(int64(2), c.GoToFirstChildForByte(7))
assert.Equal("number", c.CurrentNode().Type())
c.Reset(nodeForReset)
assert.Equal("variable_declarator", c.CurrentNode().Type())
assert.False(c.GoToParent())
}