-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
174 lines (158 loc) · 4.3 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
package interpol
import (
"testing"
)
// test internal scanner
func TestScanner(t *testing.T) {
var testdata = []struct {
input string
output []string
}{
{"", []string{}},
{"one", []string{"one"}},
{"one two", []string{"one", "two"}},
{" spacey two", []string{"spacey", "two"}},
{"with ' some qoutes '", []string{"with", " some qoutes "}},
{"var=123", []string{"var", "=", "123"}},
{"var='=123'", []string{"var", "=", "=123"}},
{"var\\==123", []string{"var\\=", "=", "123"}},
}
for _, test := range testdata {
s := newScanner(test.input)
for _, str := range test.output {
str2, typ := s.next()
if typ == teof {
t.Errorf("Unexpected EOF in '%s'", test.input)
break
}
if typ == terror {
t.Errorf("Unexpected Error in '%s'", test.input)
break
}
if str != str2 {
t.Errorf("Expected '%s' got '%s'", str, str2)
}
}
str, typ := s.next()
if typ != teof {
t.Errorf("Expected EOF, got '%s':%d in '%s'", str, typ, test.input)
}
}
}
type interpolParserTestdata struct {
log string
cmd string
typ string
data map[string]string
}
type lineParserTestdata struct {
log string
line string
expected []textElement
}
var interpolTestdata = []interpolParserTestdata{
{"keep space 1", "spacetype datax=' '", "spacetype",
map[string]string{"datax": " "}},
{"nodata", "justtype", "justtype", map[string]string{}},
{"withdata", "type1 data1 data2=value2", "type1",
map[string]string{"data1": "", "data2": "value2"}},
{"remove space 1", "spacetype datax = valuex ", "spacetype",
map[string]string{"datax": "valuex"}},
{"remove space 2", "spacetype nodata datax = valuex ", "spacetype",
map[string]string{"nodata": "", "datax": "valuex"}},
{"remove space 3", "spacetype nodata datax =valuex ", "spacetype",
map[string]string{"nodata": "", "datax": "valuex"}},
{"remove space 4", "spacetype nodata1 nodata2", "spacetype",
map[string]string{"nodata1": "", "nodata2": ""}},
}
var lineTestdata = []lineParserTestdata{
{"just text", "justtext", []textElement{
{static: true, text: "justtext"}}},
{"just interpol", "{{justinterpol}}", []textElement{
{static: false, text: "justinterpol"}}},
{"mixed line", "stuffbefore{{interpol}}stuffafter", []textElement{
{static: true, text: "stuffbefore"},
{static: false, text: "interpol"},
{static: true, text: "stuffafter"}}},
}
// test parsing of interpol commands
func TestParseInterpol(t *testing.T) {
for _, test := range interpolTestdata {
id, err := parseInterpolator(test.cmd)
if err != nil {
t.Errorf("%s: failed to parse, %v", test.log, err)
} else {
if id.Type != test.typ {
t.Errorf("%s: expected type '%s' got '%s'", test.log, test.typ, id.Type)
}
if len(id.Properties) != len(test.data) {
t.Errorf("%s: expected %d properties, got %d",
test.log, len(test.data), len(id.Properties))
} else {
for k, v := range test.data {
if id.Properties[k] != v {
t.Errorf("%s: expected %s='%s', got '%s'",
test.log, k, v, id.Properties[k])
}
}
}
}
}
}
func TestParseInterpolCorner(t *testing.T) {
_, err := parseInterpolator("")
if err == nil {
t.Error("should not allow empty interpol")
}
}
// test parsing of lines:
func TestParseLine(t *testing.T) {
for _, test := range lineTestdata {
els, err := parseLine(test.line)
if err != nil {
t.Errorf("%s: could not parse line, %v", test.log, err)
} else {
if len(test.expected) != len(els) {
t.Errorf("%s: expected %d elements, got %d",
test.log, len(test.expected), len(els))
} else {
for i, e := range test.expected {
if e.static != els[i].static || e.text != els[i].text {
t.Errorf("%s: element %d expected %v got %v",
test.log, i, e, els[i])
}
}
}
}
}
}
func TestParseLineCorner(t *testing.T) {
_, err := parseLine("")
if err == nil {
t.Error("should not allow empty line")
}
_, err = parseLine("{{counter")
if err == nil {
t.Error("should not allow open interpolator")
}
}
func TestRemoveQoute(t *testing.T) {
var testdata = []struct {
input string
output string
}{
{"", ""},
{"\"", "\""},
{"'", "'"},
{"''", ""},
{"\"\"", ""},
{"'A'", "A"},
{"\"A\"", "A"},
}
for _, d := range testdata {
result := removeQoute(d.input)
if result != d.output {
t.Errorf("remove qoute expeted %s got %s", d.output, result)
}
}
}