-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
355 lines (321 loc) · 8.98 KB
/
utils_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package tooey
import (
"testing"
)
type TestCase struct {
TestID string
Purpose string
F func([]rune) []rune
Case string
ExpectedResult string
}
func DoSliceTest(t *testing.T, c *TestCase) {
rContent := []rune(c.Case)
result := c.F(rContent)
if string(result) != c.ExpectedResult {
t.Fatalf("[%s] [%s]\n - Expected [%s] (%v) to equal [%s] (%v) \nSource: [%s]", c.TestID, c.Purpose, string(result), result, c.ExpectedResult, []rune(c.ExpectedResult), c.Case)
}
}
func TestShiftRuneSliceRight(t *testing.T) {
testID := "TestShiftRuneSliceRight"
tests := []*TestCase{
{
TestID: testID,
Purpose: "Ensure func shifts runes to the right",
F: ShiftRuneSliceRight,
Case: "abcdefg",
ExpectedResult: "gabcdef",
},
{
TestID: testID,
Purpose: "Ensure func shifts runes to the right",
F: ShiftRuneSliceRight,
Case: "abcdef ",
ExpectedResult: " abcdef",
},
{
TestID: testID,
Purpose: "Ensure func shifts runes to the right",
F: ShiftRuneSliceRight,
Case: " f e h sdfd34 %1",
ExpectedResult: "1 f e h sdfd34 %",
},
}
for _, test := range tests {
DoSliceTest(t, test)
}
}
func TestShiftRuneSliceLeft(t *testing.T) {
testID := "TestShiftRuneSliceLeft"
tests := []*TestCase{
{
TestID: testID,
Purpose: "Ensure func shifts runes to the left",
F: ShiftRuneSliceLeft,
Case: "abcdef",
ExpectedResult: "bcdefa",
},
{
TestID: testID,
Purpose: "Ensure func shifts runes to the left",
F: ShiftRuneSliceLeft,
Case: "abcdef ",
ExpectedResult: "bcdef a",
},
{
TestID: testID,
Purpose: "Ensure func shifts runes to the left",
F: ShiftRuneSliceLeft,
Case: "Oaaaaa",
ExpectedResult: "aaaaaO",
},
}
for _, test := range tests {
DoSliceTest(t, test)
}
}
func TestShiftRuneWhitespaceToLeft(t *testing.T) {
testID := "TestShiftRuneWhitespaceToLeft"
tests := []*TestCase{
{
TestID: testID,
Purpose: "Ensure func shifts whitespace to the left",
F: ShiftRuneWhitespaceToLeft,
Case: "abcdef",
ExpectedResult: "abcdef",
},
{
TestID: testID,
Purpose: "Ensure func shifts whitespace to the left",
F: ShiftRuneWhitespaceToLeft,
Case: "abcdef ",
ExpectedResult: " abcdef",
},
{
TestID: testID,
Purpose: "Ensure func doesn't spinlock on string of only whitespace",
F: ShiftRuneWhitespaceToLeft,
Case: " ",
ExpectedResult: " ",
},
{
TestID: testID,
Purpose: "Ensure func shifts whitespace to the left",
F: ShiftRuneWhitespaceToLeft,
Case: " abcdef ",
ExpectedResult: " abcdef",
},
}
for _, test := range tests {
DoSliceTest(t, test)
}
}
func TestShiftRuneWhitespaceToRight(t *testing.T) {
testID := "ShiftRuneWhitespaceToRight"
tests := []*TestCase{
{
TestID: testID,
Purpose: "Ensure func shifts whitespace to the right",
F: ShiftRuneWhitespaceToRight,
Case: "abcdef",
ExpectedResult: "abcdef",
},
{
TestID: testID,
Purpose: "Ensure func shifts whitespace to the right",
F: ShiftRuneWhitespaceToRight,
Case: " abcdef",
ExpectedResult: "abcdef ",
},
{
TestID: testID,
Purpose: "Ensure func doesn't spinlock on string of only whitespace",
F: ShiftRuneWhitespaceToRight,
Case: " ",
ExpectedResult: " ",
},
{
TestID: testID,
Purpose: "Ensure func shifts whitespace to the right",
F: ShiftRuneWhitespaceToRight,
Case: " abcdef ",
ExpectedResult: "abcdef ",
},
}
for _, test := range tests {
DoSliceTest(t, test)
}
}
func TestSpreadWhitespaceAcrossSliceInterior(t *testing.T) {
testID := "TestSpreadWhitespaceAcrossSliceInterior"
tests := []*TestCase{
{
TestID: testID,
Purpose: "Ensure func evenly distributes left whitespace across row",
F: SpreadWhitespaceAcrossSliceInterior,
Case: " 123 56 9",
ExpectedResult: "123 56 9",
},
{
TestID: testID,
Purpose: "Ensure func evenly distributes left whitespace across row",
F: SpreadWhitespaceAcrossSliceInterior,
Case: " a b cd e f g ",
ExpectedResult: "a b cd e f g",
},
{
TestID: testID,
Purpose: "Ensure func doesn't spinlock on string of only whitespace",
F: SpreadWhitespaceAcrossSliceInterior,
Case: " ",
ExpectedResult: " ",
},
{
TestID: testID,
Purpose: "Ensure func evenly distributes left whitespace across row",
F: SpreadWhitespaceAcrossSliceInterior,
Case: " abc def ghi ",
ExpectedResult: "abc def ghi",
},
}
for _, test := range tests {
DoSliceTest(t, test)
}
}
func TestNormalizeLeftWhitespace(t *testing.T) {
testID := "TestNormalizeLeftWhitespace"
tests := []*TestCase{
{
TestID: testID,
Purpose: "Ensure func evenly distributes whitespace across width",
F: NormalizeLeftWhitespace,
Case: " abc def",
ExpectedResult: "abc def",
},
{
TestID: testID,
Purpose: "Ensure func evenly distributes whitespace across width",
F: NormalizeLeftWhitespace,
Case: " abc def ghi ",
ExpectedResult: "abc def ghi ",
},
{
TestID: testID,
Purpose: "Ensure func doesn't spinlock on string of only whitespace",
F: NormalizeLeftWhitespace,
Case: " ",
ExpectedResult: " ",
},
{
TestID: testID,
Purpose: "Ensure func doesn't touch whitespace without gaps to respec it into",
F: NormalizeLeftWhitespace,
Case: " abcdef ",
ExpectedResult: " abcdef ",
},
}
for _, test := range tests {
DoSliceTest(t, test)
}
}
func TestCountWordsInRuneSlice(t *testing.T) {
tests := map[string]int{
" abc def ghi": 3,
"abc def": 2,
" abc def ghi ": 3,
}
for content, expected := range tests {
rContent := []rune(content)
c := CountWordsInRuneSlice(rContent)
if c != expected {
t.Fatalf("Expected [%d] words in [%s] but counted [%d]", expected, content, c)
}
}
}
func TestCheckWhichPositionHasFewest(t *testing.T) {
source := make([]int, 5)
source[0] = 5
source[1] = 4
source[2] = 2
source[3] = 3
source[4] = 7
d := CheckWhichPositionHasFewest(source)
if d != 2 {
t.Fatalf("Failed to detect position with lowest count, got [%d]", d)
}
}
func TestInterfaceSlice(t *testing.T) {
testID := "TestInterfaceSlice"
test1Content := []string{
"this is a string",
"this is another string",
}
test2Content := 5
test3Content := rune('#')
test1 := []interface{}{
test1Content,
test2Content,
test3Content,
}
var i interface{} = test1
d := InterfaceSlice(i)
if len(d) != 3 {
t.Fatalf("[%s] - Expected length of result to be 3 but got [%d]", testID, len(d))
}
for i, result := range d {
if i == 0 {
val, ok := result.([]string)
if !ok {
t.Fatal("Failed to unpack expected string slice from interface")
}
if len(val) != 2 {
t.Fatalf("Count of string slice is wrong. Expected 2 but got %d", len(val))
}
for i, str := range val {
if test1Content[i] != str {
t.Fatalf("Expected value at index [%d] to be [%s] but got [%s]", i, test1Content[i], str)
}
}
} else if i == 1 {
val, ok := result.(int)
if !ok {
t.Fatal("Failed to unpack expected int from interface")
}
if val != 5 {
t.Fatalf("Expected to get int 5 but got %d", val)
}
} else if i == 2 {
val, ok := result.(rune)
if !ok {
t.Fatal("Failed to unpack expected rune from interface")
}
if val != test3Content {
t.Fatalf("Expected to get rune %s but got %s", string(test3Content), string(val))
}
}
}
}
func TestCountWhiteSpace(t *testing.T) {
test1 := "this should have 4 whitespaces"
test1r := []rune(test1)
d := CountWhiteSpace(test1r)
if d != 4 {
t.Fatalf("Expected 4 whitespaces but counted %d", d)
}
}
func TestTrimString(t *testing.T) {
test1Content := "first string"
test1Expected := "first…"
d := TrimString(test1Content, 6)
if d != test1Expected {
t.Fatalf("Expected %s to match %s", d, test1Expected)
}
d2 := TrimString(test1Content, 0)
if d2 != "" {
t.Fatal("Expected string to be truncated and it wasn't")
}
d3 := TrimString(test1Content, len(test1Content))
if d3 != test1Content {
t.Fatal("Expected string to be untouched but it was altered")
}
}