-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutting_test.go
221 lines (185 loc) · 6.42 KB
/
cutting_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
package main
import (
"strings"
"testing"
)
func TestCutterFromSeperator(t *testing.T) {
test := func(s, expectedL, expectedR string, findable bool, c StringCutter) {
actualL, actualR, found := c(s)
if expectedL != actualL || expectedR != actualR || findable != found {
t.Errorf("Expected (%s,%s,%v) Got (%s,%s,%v)", expectedL, expectedR, findable, actualL, actualR, found)
}
}
testFindable := func(expectedL, sep, expectedR string) {
s := expectedL + sep + expectedR
c := cutterFromSeperator(sep)
test(s, expectedL, expectedR, true, c)
}
testUnfindable := func(s, sep string) {
c := cutterFromSeperator(sep)
test(s, s, "", false, c)
}
// findable with simple cut
testFindable("l", " ", "r")
testFindable("l", "\t", "r")
testFindable("l", "_", "r")
testFindable("l", "\n", "right")
testFindable("l", " ", "r")
testFindable("l", " ", "r")
testFindable("l", "\ts", "r")
// findable with recurring sepereator
testFindable("l", " ", " r")
testFindable("l", " ", "r ")
testFindable("l", " ", " ")
// not findable seperators
testUnfindable("lr", " ")
testUnfindable("l r", "\t")
}
func TestMultiWsCutter(t *testing.T) {
test := func(s, expectedL, expectedR string, findable bool) {
actualL, actualR, found := multiWsCutter(s)
if expectedL != actualL || expectedR != actualR || findable != found {
t.Errorf("Expected (%s,%s,%v) Got (%s,%s,%v) For (%s)", expectedL, expectedR, findable, actualL, actualR, found, s)
}
}
// test if different combination of whitespace result in the same
test("l r", "l", "r", true)
test("l\t r", "l", "r", true)
test("l \tr", "l", "r", true)
test("l\t\tr", "l", "r", true)
test("l \t r", "l", "r", true)
test("longerleft longerright", "longerleft", "longerright", true)
// test that normal whitespace is no problem
test("no double ws\t\tbut now", "no double ws", "but now", true)
test("a b c", "a b", "c", true)
test("a b c", "a", "b c", true)
// seperator is at the start or at the end
test("a b\tc", "a b", "c", true)
test(" b", "", "b", true)
test("a ", "a", "", true)
// test that it reports if there is no double ws
test("no double ws", "no double ws", "", false)
if tabCountsAsMultiWs {
test("l\tr", "l", "r", true)
test("a b\tc", "a b", "c", true)
test("a\tb c", "a", "b c", true)
test("\tb", "", "b", true)
test("a\t", "a", "", true)
}
}
func TestWsSingleCutter(t *testing.T) {
test := func(s, expectedL, expectedR string, findable bool) {
actualL, actualR, found := singleWsCutter(s)
if expectedL != actualL || expectedR != actualR || findable != found {
t.Errorf("Expected (%s,%s,%v) Got (%s,%s,%v) For (%s)", expectedL, expectedR, findable, actualL, actualR, found, s)
}
}
// test if different combination of whitespace result in the same
test("l r", "l", "r", true)
test("l\tr", "l", "r", true)
test("l r", "l", "r", true)
test("l\t r", "l", "r", true)
test("l \tr", "l", "r", true)
test("l\t\tr", "l", "r", true)
test("l \t r", "l", "r", true)
test("longerleft longerright", "longerleft", "longerright", true)
// seperator is at the start or at the end
test("a b\tc", "a", "b\tc", true)
test(" b", "", "b", true)
test("a ", "a", "", true)
}
func TestCutWithCutters(t *testing.T) {
// it is getting joined on '|' as it is easier to compare
test := func(s, expectedJoined string, cutters []StringCutter) {
actualJoined := strings.Join(cutWithCutters(s, cutters), "|")
if expectedJoined != actualJoined {
t.Errorf("Expected (%s) Actual (%s)", expectedJoined, actualJoined)
}
}
// tests should be completely independend
// on the actual cutter used as they should have their own tests
sCutter := cutterFromSeperator(" ")
tCutter := cutterFromSeperator("\t")
test("a b", "a b", nil) // empty
test("a b", "a b", []StringCutter{}) // empty
test("a b", "a b", []StringCutter{tCutter}) // not found
test("a b", "a|b", []StringCutter{sCutter}) // found
test("a b c", "a|b c", []StringCutter{sCutter})
test("a b c", "a|b|c", []StringCutter{sCutter, sCutter})
test("a b\tc", "a|b|c", []StringCutter{sCutter, tCutter})
test("a\tb c", "a|b|c", []StringCutter{tCutter, sCutter})
}
func TestCutAllWithCutter(t *testing.T) {
test := func(s, expected string, c StringCutter) {
actual := strings.Join(cutAllWithCutter(s, c), "|")
if expected != actual {
t.Errorf("Expected (%s) Got (%s)", expected, actual)
}
}
test("a b", "a b", multiWsCutter)
test("a b", "a|b", multiWsCutter)
test("a b c", "a|b c", multiWsCutter)
test("a c\t\tb c", "a c|b c", multiWsCutter)
test("a b", "a|b", cutterFromSeperator(" "))
test("a b", "a|b", cutterFromSeperator(" "))
test("a b", "a b", cutterFromSeperator("\t"))
}
func TestFlagToCutters(t *testing.T) {
testOk := func(flag, delim string, expectedCutters []StringCutter) {
actualCutters, err := flagToCutters(flag, delim)
if err != nil {
t.Errorf("Did not expect to get an error for flag '%s'", flag)
}
testcases := []string{
"a",
"a b",
"a\tb",
"a b",
"a \t b",
"a\t\tb",
"abc",
" a",
" a",
"\ta",
"\t a",
"\t\ta",
"a ",
"a ",
"a\t",
"a\t\t",
"a\t b c",
"a\t b c",
"a\t\tb\t\tc",
"a\tb\tb c d e",
"ab ab ab",
}
for _, testcase := range testcases {
// we use cut here which has it's own tests
actualJoined := strings.Join(cutWithCutters(testcase, actualCutters), "|")
expectedJoined := strings.Join(cutWithCutters(testcase, expectedCutters), "|")
if expectedJoined != actualJoined {
t.Errorf("Expected (%s) Got (%s) On (%s) For Flag (%s)", expectedJoined, actualJoined, testcase, flag)
}
}
}
testFailed := func(flag, sep string) {
_, err := flagToCutters(flag, sep)
if err == nil {
t.Errorf("Expected to get an error from '%s'", flag)
}
}
// single so that we know that the function can get the right cutters
testOk("s", "|", []StringCutter{cutterFromSeperator(" ")})
testOk("t", "|", []StringCutter{cutterFromSeperator("\t")})
testOk("m", "|", []StringCutter{multiWsCutter})
testOk("<a>", "|", []StringCutter{cutterFromSeperator("a")})
// combination
testOk("s|s", "|", []StringCutter{cutterFromSeperator(" "), cutterFromSeperator(" ")})
testOk("s|t", "|", []StringCutter{cutterFromSeperator(" "), cutterFromSeperator("\t")})
testOk("s|<a>", "|", []StringCutter{cutterFromSeperator(" "), cutterFromSeperator("a")})
// current error hanlding on invalid input
testFailed("s|t", "-")
testFailed("", "|")
testFailed("|", "|")
testFailed("a|", "|")
}