forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexer_test.go
119 lines (111 loc) · 2.57 KB
/
lexer_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
package prompt
import (
"testing"
istrings "github.com/elk-language/go-prompt/strings"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestEagerLexerNext(t *testing.T) {
tests := map[string]struct {
lexer *EagerLexer
want Token
ok bool
}{
"return the first token when at the beginning": {
lexer: &EagerLexer{
tokens: []Token{
&SimpleToken{lastByteIndex: 0},
&SimpleToken{lastByteIndex: 1},
},
currentIndex: 0,
},
want: &SimpleToken{lastByteIndex: 0},
ok: true,
},
"return the second token": {
lexer: &EagerLexer{
tokens: []Token{
&SimpleToken{lastByteIndex: 3},
&SimpleToken{lastByteIndex: 5},
&SimpleToken{lastByteIndex: 6},
},
currentIndex: 1,
},
want: &SimpleToken{lastByteIndex: 5},
ok: true,
},
"return false when at the end": {
lexer: &EagerLexer{
tokens: []Token{
&SimpleToken{lastByteIndex: 0},
&SimpleToken{lastByteIndex: 4},
&SimpleToken{lastByteIndex: 5},
},
currentIndex: 3,
},
want: nil,
ok: false,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, ok := tc.lexer.Next()
opts := []cmp.Option{
cmp.AllowUnexported(SimpleToken{}, EagerLexer{}),
}
if diff := cmp.Diff(tc.want, got, opts...); diff != "" {
t.Fatalf(diff)
}
if diff := cmp.Diff(tc.ok, ok, opts...); diff != "" {
t.Fatalf(diff)
}
})
}
}
func charLex(s string) []Token {
var result []Token
for i := range s {
result = append(result, NewSimpleToken(istrings.ByteNumber(i), istrings.ByteNumber(i)))
}
return result
}
func TestEagerLexerInit(t *testing.T) {
tests := map[string]struct {
lexer *EagerLexer
input string
want *EagerLexer
}{
"reset the lexer's state": {
lexer: &EagerLexer{
lexFunc: charLex,
tokens: []Token{
&SimpleToken{firstByteIndex: 2, lastByteIndex: 2},
&SimpleToken{firstByteIndex: 10, lastByteIndex: 10},
},
currentIndex: 11,
},
input: "foo",
want: &EagerLexer{
lexFunc: charLex,
tokens: []Token{
&SimpleToken{firstByteIndex: 0, lastByteIndex: 0},
&SimpleToken{firstByteIndex: 1, lastByteIndex: 1},
&SimpleToken{firstByteIndex: 2, lastByteIndex: 2},
},
currentIndex: 0,
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tc.lexer.Init(tc.input)
opts := []cmp.Option{
cmp.AllowUnexported(SimpleToken{}, EagerLexer{}),
cmpopts.IgnoreFields(EagerLexer{}, "lexFunc"),
}
if diff := cmp.Diff(tc.want, tc.lexer, opts...); diff != "" {
t.Fatalf(diff)
}
})
}
}