-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner_test.go
242 lines (179 loc) · 4.94 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
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
package scanner_test
import (
"github.com/Southern/scanner"
"io/ioutil"
"strings"
"testing"
)
var s = scanner.New()
func TestScannerBasics(t *testing.T) {
str := "test-1 test + 1 test+1 -1 1000 -1000"
expects := [][]string{
{"WORD", "test"},
{"CHAR", "-"},
{"NUMBER", "1"},
{"WHITESPACE", " "},
{"WORD", "test"},
{"WHITESPACE", " "},
{"CHAR", "+"},
{"WHITESPACE", " "},
{"NUMBER", "1"},
{"WHITESPACE", " "},
{"WORD", "test"},
{"CHAR", "+"},
{"NUMBER", "1"},
{"WHITESPACE", " "},
{"CHAR", "-"},
{"NUMBER", "1"},
{"WHITESPACE", " "},
{"NUMBER", "1000"},
{"WHITESPACE", " "},
{"CHAR", "-"},
{"NUMBER", "1000"},
}
Status("Parsing \"%s\"", str)
s, err := s.Parse(str)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
for i, expect := range expects {
if i > len(s.Tokens)-1 {
t.Fatalf("Excpected more output: %+v\n", expects[i:])
}
if s.Tokens[i][0] != expect[0] || s.Tokens[i][1] != expect[1] {
t.Fatalf("Expected %+v, got %+v", expect, s.Tokens[i])
}
}
Status("Parsed: %s", s)
}
func TestScannerManipulation(t *testing.T) {
str := "test test test"
expects := [][]string{
{"WORD", "test"},
{"WHITESPACE", " "},
{"WORD", "test2"},
{"WHITESPACE", " "},
{"WORD", "test"},
}
Status("Parsing \"%s\"", str)
s, err := s.Parse(str)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Status("Parsed data: %+v\n", s)
s.Tokens[2][1] = "test2"
for i, expect := range expects {
if i > len(s.Tokens)-1 {
t.Fatalf("Excpected more output: %+v\n", expects[i:])
}
if s.Tokens[i][0] != expect[0] || s.Tokens[i][1] != expect[1] {
t.Fatalf("Expected %+v, got %+v", expect, s.Tokens[i])
}
}
Status("Data after manipulation: %s", s.Join())
}
func TestScannerReadFile(t *testing.T) {
Status("Reading all files in testdata directory")
files, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Status("Scanning all files found in testdata directory")
for len(files) > 0 {
file := strings.Join([]string{"testdata", files[0].Name()}, "/")
Status("Scanning file: %s", file)
s, err = s.ReadFile(file)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Status("Scanned: %+v", s)
files = files[1:]
}
}
func TestScannerNonexistentFile(t *testing.T) {
s := scanner.New()
Status("Trying to read nonexistent file")
s, err := s.ReadFile("idontevenexist")
Status("Scan: %+v\n", s)
if len(s.Tokens) > 0 || err == nil {
t.Fatal("Expected this test to fail.")
}
}
func TestJoiningLexBackToString(t *testing.T) {
data, err := s.ReadFile(strings.Join([]string{"testdata", "html.txt"}, "/"))
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Status("Scanned data: %+v", data)
joined := data.Join()
Status("Joined data: %+v", joined)
}
func TestInvalidDataType(t *testing.T) {
Status("Trying to parse an invalid data type")
_, err := s.Parse([]int{1, 2, 3, 4})
Status("Error returned: %s", err)
if err == nil {
t.Fatalf("Expected this test to fail.")
}
}
func TestScannerRandomString(t *testing.T) {
str := "ZoMg testΩ≈∂œ™£¢˜Ωπππ¬˜£™¡¢∞•ªº test< > & ; ?"
Status("Trying to parse random string")
s, err := s.Parse(str)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Status("Scanned: %+v", s)
Status("String was parsed. Joining back together and checking the result.")
joined := s.Join()
if joined != str {
t.Fatal("The joined string was not the same as what was input.")
}
Status("String parsed correctly.")
}
func TestScannerRussianString(t *testing.T) {
str := "This isn't Russian, but this is: ру́сский язы́к"
Status("Trying to parse Russian string")
s, err := s.Parse(str)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Status("Scanned: %+v", s)
Status("String was parsed. Joining back together and checking the result.")
joined := s.Join()
if joined != str {
t.Fatal("The joined string was not the same as what was input.")
}
Status("String parsed correctly.")
}
func TestScannerGreekString(t *testing.T) {
str := "This isn't Greek, but this is: ελληνικά"
Status("Trying to parse Greek string")
s, err := s.Parse(str)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Status("Scanned: %+v", s)
Status("String was parsed. Joining back together and checking the result.")
joined := s.Join()
if joined != str {
t.Fatal("The joined string was not the same as what was input.")
}
Status("String parsed correctly")
}
func TestScannerArabicString(t *testing.T) {
str := "This isn't Arabic, but this is: عربي ,عربى"
Status("Trying to parse Arabic string")
s, err := s.Parse(str)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
return
}
Status("Scanned: %+v", s)
Status("String was parsed. Joining back together and checking the result.")
joined := s.Join()
if joined != str {
t.Fatal("The joined string was not the same as what was input.")
}
Status("String parsed correctly")
}