-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner_test.go
154 lines (139 loc) · 2.68 KB
/
scanner_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
package scan
import (
"strconv"
"testing"
)
func TestKeep(t *testing.T) {
src := "1234567"
tests := []struct {
n int
value string
}{
{0, ""},
{1, "1"},
{3, "123"},
{8, "1234567"},
}
for _, test := range tests {
t.Run(test.value, func(t *testing.T) {
s := NewScannerFromString("", src)
Repeat(s.Keep, test.n)
have := s.Emit()
if have.Val != test.value {
t.Errorf("\n have: [%v] \n want: [%v]", have.Val, test.value)
}
})
}
}
func TestUndo(t *testing.T) {
s := NewScannerFromString("", "123abcd")
While(s, IsDigit, s.Discard)
s.Keep()
s.Keep()
s.Undo()
While(s, IsAny, s.Keep)
tok := s.Emit()
want := "abcd"
if tok.Val != want {
t.Errorf("\n have: %v \n want: %v", tok.Val, want)
}
}
func TestPeek(t *testing.T) {
s := NewScannerFromString("", "xyz0123")
tests := []struct {
i int
ch rune
}{
{0, '0'},
{1, '1'},
{2, '2'},
{3, '3'},
{4, EndOfText},
{-1, 'z'},
{-3, 'x'},
{-4, EndOfText},
}
Repeat(s.Skip, 3)
for _, test := range tests {
t.Run(strconv.Itoa(test.i), func(t *testing.T) {
have := s.Peek(test.i)
if have != test.ch {
t.Errorf("\n have: %c \n want: %c", have, test.ch)
}
})
}
}
func TestEmpty(t *testing.T) {
s := NewScannerFromString("", "")
s.Keep()
want := Token{
Pos: Pos{Line: 1, Col: 1},
Type: EndOfTextType,
}
tok := s.Emit()
if !tok.Equal(want) {
t.Errorf("\n have: %v \n want: %v", tok, want)
}
}
func TestEnd(t *testing.T) {
s := NewScannerFromString("", "1")
s.Keep()
s.Emit()
Repeat(s.Keep, 10)
want := Token{
Pos: Pos{Line: 1, Col: 2},
Type: EndOfTextType,
}
tok := s.Emit()
if !tok.Equal(want) {
t.Errorf("\n have: %v \n want: %v", tok, want)
}
}
func TestReInit(t *testing.T) {
s := NewScannerFromString("foo", "abc")
s.Discard()
s.Keep()
have := s.Emit()
want := "b"
if have.Val != want {
t.Errorf("\n have: %v \n want: %v", have, want)
}
wantPos := Pos{Name: "foo", Line: 1, Col: 2}
if have.Pos != wantPos {
t.Errorf("\n have: %v \n want: %v", have.Pos, wantPos)
}
s.InitFromString("bar", "123")
s.Discard()
s.Keep()
have = s.Emit()
want = "2"
if have.Val != want {
t.Errorf("\n have: %v \n want: %v", have, want)
}
wantPos = Pos{Name: "bar", Line: 1, Col: 2}
if have.Pos != wantPos {
t.Errorf("\n have: %v \n want: %v", have.Pos, wantPos)
}
}
func TestInit(t *testing.T) {
var s Scanner
s.InitFromString("foo", "abc")
s.Discard()
s.Keep()
have := s.Emit()
want := "b"
if have.Val != want {
t.Errorf("\n have: %v \n want: %v", have, want)
}
}
func TestNotAdvancing(t *testing.T) {
var s Scanner
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
for i := 0; i < 100; i++ {
s.Emit()
}
}