-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_test.go
163 lines (131 loc) · 5.1 KB
/
collection_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
package collection
import (
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIndexOf(t *testing.T) {
var actual int
var objects = []string{"test alongword string 1", "$!@#s^g#$^ alongword", "another string !@#$!@% * @! ! alongword", "4sg alongword"}
var tests = []struct {
testCase string
expected int
}{
{testCase: objects[0], expected: 0},
{testCase: objects[1], expected: 1},
{testCase: objects[2], expected: 2},
{testCase: objects[3], expected: 3},
{testCase: "a missing string", expected: -1},
{testCase: "'", expected: -1},
{testCase: "", expected: -1},
}
for _, test := range tests {
actual = IndexOf(test.testCase, objects)
assert.True(t, test.expected == actual, "Expected %d got %d for testcase [%s]", test.expected, actual, test.testCase)
}
}
func TestContains(t *testing.T) {
var actual bool
var objects = []string{"test alongword string 1", "$!@#s^g#$^ alongword", "another string !@#$!@% * @! ! alongword", "4sg alongword"}
var tests = []struct {
testCase string
expected bool
}{
{testCase: objects[0], expected: true},
{testCase: objects[1], expected: true},
{testCase: objects[2], expected: true},
{testCase: objects[3], expected: true},
{testCase: "a missing string", expected: false},
{testCase: "'", expected: false},
{testCase: "", expected: false},
}
for _, test := range tests {
actual = Contains(test.testCase, objects)
assert.True(t, test.expected == actual, "Expected %t got %t for testcase [%s]", test.expected, actual, test.testCase)
}
}
func TestAny(t *testing.T) {
var actual bool
var objects = []string{"test alongword string 1", "$!@#s^g#$^ alongword", "another string !@#$!@% * @! ! alongword", "4sg alongword"}
var tests = []struct {
testCase string
expected bool
}{
{testCase: "s", expected: true},
{testCase: " alongword", expected: true},
{testCase: "h", expected: true},
{testCase: "@", expected: true},
{testCase: "z", expected: false},
{testCase: "p", expected: false},
{testCase: "_", expected: false},
{testCase: "missing", expected: false},
}
for _, test := range tests {
actual = Any(objects, func(v string) bool {
return strings.Contains(v, test.testCase)
})
assert.True(t, test.expected == actual, "strings.Contains test - Expected %t got %t for testcase [%s]", test.expected, actual, test.testCase)
}
}
func TestAll(t *testing.T) {
var actual bool
var objects = []string{"test alongword string 1", "$!@#s^g#$^ alongword", "another string !@#$!@% * @! ! alongword", "4sg alongword"}
var tests = []struct {
testCase string
expected bool
}{
{testCase: "s", expected: true},
{testCase: " alongword", expected: true},
{testCase: "h", expected: false},
{testCase: "@", expected: false},
{testCase: "z", expected: false},
{testCase: "p", expected: false},
{testCase: "_", expected: false},
{testCase: "missing", expected: false},
}
for _, test := range tests {
actual = All(objects, func(v string) bool {
return strings.Contains(v, test.testCase)
})
assert.True(t, test.expected == actual, "strings.Contains test - Expected %t got %t for testcase [%s]", test.expected, actual, test.testCase)
}
}
func TestFilter(t *testing.T) {
var actual []string
var objects = []string{"test alongword string 1", "$!@#s^g#$^ alongword", "another string !@#$!@% * @! ! alongword", "4sg alongword"}
var tests = []struct {
testCase string
expected []string
}{
{testCase: "s", expected: objects}, // expecting all members
{testCase: " alongword", expected: objects}, // expecting all members
{testCase: "h", expected: objects[2:3]}, // expecting only the member with index 2
{testCase: "@", expected: objects[1:3]}, // expecting only the members with index 1 and 2
{testCase: "z", expected: []string{}}, // expecting an empty slice
{testCase: "p", expected: []string{}}, // expecting an empty slice
{testCase: "_", expected: []string{}}, // expecting an empty slice
{testCase: "missing", expected: []string{}}, // expecting an empty slice
}
for _, test := range tests {
actual = Filter(objects, func(v string) bool {
return strings.Contains(v, test.testCase) // If the string v from objects contains the string test.testCase, return true
})
assert.True(t, reflect.DeepEqual(test.expected, actual), "strings.Contains test - Expected %v got %v for testcase [%s]", test.expected, actual, test.testCase)
}
}
func TestMap(t *testing.T) {
var actual []string
var objects = []string{"a lowercase string", "AN UPPERCASE STRING", "a MiXeD cAsE sTrInG", "!@#^$%&*"}
var tests = []struct {
testCase func(string) string
expected []string
}{
{testCase: strings.ToUpper, expected: []string{"A LOWERCASE STRING", "AN UPPERCASE STRING", "A MIXED CASE STRING", "!@#^$%&*"}},
{testCase: strings.ToLower, expected: []string{"a lowercase string", "an uppercase string", "a mixed case string", "!@#^$%&*"}},
}
for _, test := range tests {
actual = Map(objects, test.testCase)
assert.True(t, reflect.DeepEqual(test.expected, actual), "strings.Contains test - Expected %v got %v for testcase [%s]", test.expected, actual, test.testCase)
}
}