-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_test.go
More file actions
228 lines (215 loc) · 5.51 KB
/
key_test.go
File metadata and controls
228 lines (215 loc) · 5.51 KB
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
package config
import (
"reflect"
"testing"
)
func TestNewKey(t *testing.T) {
key := NewKey()
if !key.IsEmpty() {
t.Fail()
}
key = Key(nil)
if !key.IsEmpty() {
t.Fail()
}
key = NewKey("a", "", "b")
if key.Len() != 3 || key[0] != "a" || key[1] != "" || key[2] != "b" {
t.Fail()
}
}
func TestNewKeySep(t *testing.T) {
tests := []struct {
value string
sep string
result []string
}{
{"", "", []string{}},
{"", ".", []string{""}},
{"hello", "", []string{"h", "e", "l", "l", "o"}},
{"one . two ", ".", []string{"one ", " two "}},
{"one.two.three.four", ".", []string{"one", "two", "three", "four"}},
{"one, two, three", ", ", []string{"one", "two", "three"}},
}
for _, test := range tests {
result := NewKeySep(test.value, test.sep)
if result == nil {
t.Error("result cannot be nil")
}
if len(result) == 0 && len(test.result) == 0 {
continue
}
if !reflect.DeepEqual(result, Key(test.result)) {
t.Error(result, test.result)
}
}
}
func TestKey_IsEmtpy(t *testing.T) {
tests := []struct {
key Key
result bool
}{
{NewKey(), true},
{Key(nil), true},
{Key([]string{}), true},
{NewKey([]string{}...), true},
{NewKey(""), false},
{NewKey([]string{"hello", "world"}...), false},
{NewKey("a", "b", "c"), false},
}
for _, test := range tests {
result := test.key.IsEmpty()
if result != test.result {
t.Errorf("%v.IsEmpty() = %v WANT %v", test.key, result, test.result)
}
}
}
func TestKey_Len(t *testing.T) {
tests := []struct {
key Key
result int
}{
{NewKey(), 0},
{Key(nil), 0},
{Key([]string{}), 0},
{NewKey([]string{}...), 0},
{NewKey(""), 1},
{NewKey([]string{"hello", "world"}...), 2},
{NewKey("a", "b", "c"), 3},
}
for _, test := range tests {
result := test.key.Len()
if result != test.result {
t.Errorf("%v.Len() = %v WANT %v", test.key, result, test.result)
}
}
}
func TestKey_Equal(t *testing.T) {
tests := []struct {
key Key
other Key
result bool
}{
{NewKey(), NewKey(), true},
{NewKey(), Key(nil), true},
{NewKey(), Key([]string{}), true},
{NewKey(), NewKey([]string{}...), true},
{NewKey("a", "b"), NewKey("a", "b"), true},
{NewKey(""), NewKey(), false},
{NewKey(""), Key(nil), false},
{NewKey(""), Key([]string{}), false},
{NewKey(""), NewKey([]string{}...), false},
{NewKey("a", "b"), NewKey("a"), false},
{NewKey("a", "b"), NewKey("a", "c"), false},
{NewKey("a", "b"), NewKey("a", "b", "c"), false},
}
for _, test := range tests {
result := test.key.Equal(test.other)
if result != test.result {
t.Errorf("%v.Equal(%v) = %v WANT %v", test.key, test.other, result, test.result)
}
}
}
func TestKey_StartsWith(t *testing.T) {
tests := []struct {
key Key
other Key
result bool
}{
{NewKey(), NewKey(), true},
{NewKey(""), NewKey(""), true},
{NewKey("a", "b"), NewKey("a"), true},
{NewKey("a", "b"), NewKey("a", "b"), true},
{NewKey(""), NewKey(), true},
{NewKey("a"), NewKey("b"), false},
{NewKey("a"), NewKey("a", "b"), false},
{NewKey("a", "b"), NewKey(""), false},
{NewKey(), NewKey(""), false},
}
for _, test := range tests {
result := test.key.StartsWith(test.other)
if result != test.result {
t.Errorf("%v.StartsWith(%v) = %v WANT %v", test.key, test.other, result, test.result)
}
}
}
func TestKey_EndWith(t *testing.T) {
tests := []struct {
key Key
other Key
result bool
}{
{NewKey(), NewKey(), true},
{NewKey(""), NewKey(""), true},
{NewKey("a", "b"), NewKey("b"), true},
{NewKey("a", "b"), NewKey("a", "b"), true},
{NewKey(""), NewKey(), true},
{NewKey("a"), NewKey("b"), false},
{NewKey("a"), NewKey("b", "a"), false},
{NewKey("a", "b"), NewKey(""), false},
{NewKey(), NewKey(""), false},
}
for _, test := range tests {
result := test.key.EndsWith(test.other)
if result != test.result {
t.Errorf("%v.EndsWith(%v) = %v WANT %v", test.key, test.other, result, test.result)
}
}
}
func TestKey_Append(t *testing.T) {
tests := []struct {
first Key
others []Key
result Key
}{
{Key(nil), nil, Key(nil)},
{Key(nil), []Key{}, Key(nil)},
{NewKey(), []Key{NewKey()}, NewKey()},
{Key(nil), []Key{NewKey("a")}, NewKey("a")},
{NewKey("a"), []Key{}, NewKey("a")},
{NewKey("a"), []Key{NewKey()}, NewKey("a")},
{NewKey("a"), []Key{NewKey("b", "c")}, NewKey("a", "b", "c")},
}
for _, test := range tests {
result := test.first.Append(test.others...)
if !reflect.DeepEqual(result, test.result) {
t.Errorf("%v.Append(%v) = %v WANT %v", test.first, test.others, result, test.result)
}
}
}
func TestKey_AppendStrings(t *testing.T) {
tests := []struct {
first Key
others []string
result Key
}{
{Key(nil), nil, Key(nil)},
{Key(nil), []string{}, Key(nil)},
{Key(nil), []string{"a"}, NewKey("a")},
{NewKey("a"), []string{"b"}, NewKey("a", "b")},
}
for _, test := range tests {
result := test.first.AppendStrings(test.others...)
if !reflect.DeepEqual(result, test.result) {
t.Errorf("%v.AppendStrings(%v) = %v WANT %v", test.first, test.others, result, test.result)
}
}
}
func TestSeparatorKeyParser_Parse(t *testing.T) {
tests := []struct {
value string
sep string
result Key
}{
{"", "", NewKey()},
{"a.b.c.d", ".", NewKey("a", "b", "c", "d")},
{"a.", ".", NewKey("a", "")},
{"hello, world", ", ", NewKey("hello", "world")},
{"foo", "", NewKey("f", "o", "o")},
}
for _, test := range tests {
result := SeparatorKeyParser(test.sep).Parse(test.value)
if !result.Equal(test.result) {
t.Errorf("SeparatorKeyParser(%v).Parse(%v) = %v WANT %v", test.sep, test.value, result, test.result)
}
}
}