-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil_test.go
339 lines (294 loc) · 7.12 KB
/
util_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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/dustin/gomemcached"
"github.com/steveyen/gkvlite"
)
func TestDumpColls(t *testing.T) {
x := 0
printf := func(format string, a ...interface{}) (n int, err error) {
x++
return 0, nil
}
store, _ := gkvlite.NewStore(nil)
c := store.SetCollection("test", nil)
n, err := dumpColl(printf, c, "")
if err != nil || n != 0 || x != 0 {
t.Errorf("expected dumpColl on empty coll to work, got: %v, %v", n, err)
}
n, err = dumpCollAsItems(printf, c, "")
if err != nil || n != 0 || x != 0 {
t.Errorf("expected dumpCollAsItems on empty coll to work, got: %v, %v", n, err)
}
c.Set([]byte("test-key"), (&item{}).toValueBytes())
x = 0
n, err = dumpColl(printf, c, "")
if err != nil || n != 1 || x != 1 {
t.Errorf("expected dumpColl on 1-item coll to work, got: %v, %v", n, err)
}
x = 0
n, err = dumpCollAsItems(printf, c, "")
if err != nil || n != 1 || x != 1 {
t.Errorf("expected dumpCollAsItems on 1-item coll to work, got: %v, %v", n, err)
}
}
func TestGetHTTPInt(t *testing.T) {
form := url.Values{
"word": []string{"up"},
"number": []string{"1"},
"numbers": []string{"1", "2"},
}
def := int64(28485)
tests := map[string]int64{
"word": def,
"number": 1,
"numbers": 1,
"nonexistent": def,
}
for k, v := range tests {
got := getIntValue(form, k, def)
if got != v {
t.Errorf("Expected %v for %v (%q) got %v",
v, k, k[v], got)
}
}
}
func TestMustEncodeBadInput(t *testing.T) {
recorder := httptest.NewRecorder()
mustEncode(recorder, make(chan bool))
if recorder.Code != 500 {
t.Errorf("Expected code 500, was %v", recorder.Code)
}
if !strings.Contains(recorder.Body.String(), "json: unsupported type") {
t.Errorf("Expected unsupported type error, got %v", recorder.Body)
}
}
func TestMust(t *testing.T) {
must(nil) // no problem
exp := errors.New("the spanish inquisition")
var err interface{}
func() {
defer func() { err = recover() }()
must(exp)
}()
if err != exp {
t.Errorf("Expected %v, got %v", exp, err)
}
}
func TestOneResponderImplicit(t *testing.T) {
rr := httptest.NewRecorder()
or := oneResponder{w: rr}
or.Header().Set("a", "1")
if rr.Header().Get("a") != "1" {
t.Fatalf("Expected header a to be 1, but it's %q",
rr.Header().Get("a"))
}
or.Write([]byte{'x'})
if rr.Code != 200 {
t.Fatalf("Expected write to lead to code 200, it's %v", rr.Code)
}
if rr.Body.String() != "x" {
t.Fatalf("Expected an x, but it's %q", rr.Body.String())
}
or.WriteHeader(500)
if rr.Code != 200 {
t.Fatalf("Expected code to still be 200, it's %v", rr.Code)
}
}
func TestOneResponderExplicit(t *testing.T) {
rr := httptest.NewRecorder()
or := oneResponder{w: rr}
or.WriteHeader(200)
if rr.Code != 200 {
t.Fatalf("Expected code to be 200, it's %v", rr.Code)
}
or.WriteHeader(500)
if rr.Code != 200 {
t.Fatalf("Expected code to still be 200, it's %v", rr.Code)
}
}
// Run through the sessionLoop code with a quit command.
//
// This test doesn't do much other than confirm that the session loop
// actually would terminate the real session goroutine on quit (by
// completing).
func TestSessionLoop(t *testing.T) {
req := &gomemcached.MCRequest{
Opcode: gomemcached.QUIT,
}
rh := &reqHandler{}
sessionLoop(rwCloser{bytes.NewBuffer(req.Bytes())}, "test", rh,
func() {})
}
func TestBytesEncoder(t *testing.T) {
tests := map[string]string{
"simple": `"simple"`,
"O'Hair": `"O%27Hair"`,
}
for in, out := range tests {
b := Bytes(in)
got, err := json.Marshal(&b)
if err != nil {
t.Errorf("Error marshaling %v", in)
}
if string(got) != out {
t.Errorf("Expected %s, got %s", out, got)
}
}
}
func TestBytesDecoder(t *testing.T) {
pos := map[string]string{
`"simple"`: "simple",
`"O%27Hair"`: "O'Hair",
}
for in, out := range pos {
b := Bytes{}
err := jsonUnmarshal([]byte(in), &b)
if err != nil {
t.Errorf("Error unmarshaling %v", in)
}
if out != b.String() {
t.Errorf("Expected %v for %v, got %v", out, in, b)
}
}
neg := []string{"xxx no quotes", `"invalid esc %2x"`}
for _, in := range neg {
b := Bytes{}
err := jsonUnmarshal([]byte(in), &b)
if err == nil {
t.Errorf("Expected error unmarshaling %v, got %v", in, b)
}
}
// This is odd looking, but I use the internal decoder
// directly since the interior error is just about impossible
// to encounter otherwise.
for _, in := range neg {
b := Bytes{}
err := b.UnmarshalJSON([]byte(in))
if err == nil {
t.Errorf("Expected error unmarshaling %v, got %v", in, b)
}
}
}
func TestDirIsDir(t *testing.T) {
if !isDir(".") {
t.Errorf("expected . to be a dir")
}
}
func TestRing(t *testing.T) {
r := NewRing(1)
r.Push(0)
exp := []int{0}
cur := 0
r.Visit(func(v interface{}) {
if v != exp[cur] {
t.Errorf("ring visit expected %v, got: %v", exp[cur], v)
}
cur++
})
if cur != len(exp) {
t.Errorf("expected to see: %#v, but got one more", exp)
}
r.Push(1)
exp = []int{1}
cur = 0
r.Visit(func(v interface{}) {
if v != exp[cur] {
t.Errorf("ring visit expected %v, got: %v", exp[cur], v)
}
cur++
})
if cur != len(exp) {
t.Errorf("expected to see: %#v, but got one more", exp)
}
r = NewRing(2)
r.Push(0)
r.Push(1)
r.Push(2)
r.Push(3)
exp = []int{2, 3}
cur = 0
r.Visit(func(v interface{}) {
if v != exp[cur] {
t.Errorf("ring visit expected %v, got: %v", exp[cur], v)
}
cur++
})
if cur != len(exp) {
t.Errorf("expected to see: %#v, but got one more", exp)
}
}
func TestRingConversions(t *testing.T) {
e0 := fmt.Errorf("e0")
e1 := fmt.Errorf("e1")
emptyRing := NewRing(10)
justNums := NewRing(5)
for i := 0; i < 6; i++ {
justNums.Push(i)
}
r := NewRing(10)
r.Push(0)
r.Push("hi")
r.Push("bye")
r.Push(e0)
r.Push(1)
r.Push(e1)
checkStrings := func(got, exp []string) {
if len(got) != len(exp) {
t.Errorf("got vs exp len mismatch, %#v, %#v", got, exp)
}
for i, g := range got {
if g != exp[i] {
t.Errorf("got vs exp item mismatch, %#v, %#v", g, exp[i])
}
}
}
checkStrings(RingToStrings(r), []string{"hi", "bye"})
checkStrings(RingToStrings(emptyRing), []string{})
checkStrings(RingToStrings(justNums), []string{})
checkErrors := func(got, exp []error) {
if len(got) != len(exp) {
t.Errorf("got vs exp len mismatch, %#v, %#v", got, exp)
}
for i, g := range got {
if g != exp[i] {
t.Errorf("got vs exp item mismatch, %#v, %#v", g, exp[i])
}
}
}
checkErrors(RingToErrors(r), []error{e0, e1})
checkErrors(RingToErrors(emptyRing), []error{})
checkErrors(RingToErrors(justNums), []error{})
}
func TestParseFileNameSuffix(t *testing.T) {
tests := []struct {
in string
out string
ok bool
}{
{"", "", false},
{".", "", false},
{"hello", "", false},
{"hello.", "", false},
{".foo", "foo", true},
{"a.b", "b", true},
}
for _, test := range tests {
got, err := parseFileNameSuffix(test.in)
if err != nil && test.ok {
t.Errorf("expected good parseFileNameSuffix(%v), got: %v, %v",
test.in, got, err)
}
if got != test.out {
t.Errorf("expected parseFileNameSuffix(%v) to be: %v, got: %v",
test.in, test.out, got)
}
}
}