-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlookahead_iterator_test.go
51 lines (41 loc) · 1.58 KB
/
lookahead_iterator_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
package tree_sitter_test
import (
"testing"
"github.com/stretchr/testify/assert"
. "github.com/tree-sitter/go-tree-sitter"
)
func TestLookaheadIterator(t *testing.T) {
parser := NewParser()
defer parser.Close()
language := getLanguage("rust")
parser.SetLanguage(language)
tree := parser.Parse([]byte("struct Stuff {}"), nil)
defer tree.Close()
assert.NotNil(t, tree)
cursor := tree.Walk()
assert.True(t, cursor.GotoFirstChild()) // struct
assert.True(t, cursor.GotoFirstChild()) // struct keyword
nextState := cursor.Node().NextParseState()
assert.NotEqual(t, 0, nextState)
assert.Equal(t, nextState, language.NextState(cursor.Node().ParseState(), cursor.Node().GrammarId()))
assert.True(t, uint(nextState) < uint(language.ParseStateCount()))
assert.True(t, cursor.GotoNextSibling()) // type_identifier
assert.Equal(t, nextState, cursor.Node().ParseState())
assert.Equal(t, cursor.Node().GrammarName(), "identifier")
assert.NotEqual(t, cursor.Node().GrammarId(), cursor.Node().KindId())
expectedSymbols := []string{"//", "/*", "identifier", "line_comment", "block_comment"}
lookahead := language.LookaheadIterator(nextState)
defer lookahead.Close()
assert.NotNil(t, lookahead)
assert.Equal(t, lookahead.Language(), language)
assert.Equal(t, lookahead.IterNames(), expectedSymbols)
lookahead.ResetState(nextState)
assert.Equal(t, lookahead.IterNames(), expectedSymbols)
lookahead.Reset(language, nextState)
var names []string
symbols := lookahead.Iter()
for _, s := range symbols {
names = append(names, language.NodeKindForId(s))
}
assert.Equal(t, names, expectedSymbols)
}