-
Notifications
You must be signed in to change notification settings - Fork 220
/
test_test.go
128 lines (107 loc) · 3.01 KB
/
test_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
package miniredis
import (
"math/rand"
"reflect"
"strings"
"testing"
"github.com/alicebob/miniredis/v2/proto"
)
// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
tb.Helper()
if !condition {
tb.Errorf(msg, v...)
}
}
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
tb.Helper()
if err != nil {
tb.Errorf("unexpected error: %s", err.Error())
}
}
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}) {
tb.Helper()
if !reflect.DeepEqual(exp, act) {
tb.Errorf("expected: %#v got: %#v", exp, act)
}
}
func equalStr(tb testing.TB, want, have string) {
tb.Helper()
if have != want {
tb.Errorf("want: %q have: %q", want, have)
}
}
// mustFail compares the error strings
func mustFail(tb testing.TB, err error, want string) {
tb.Helper()
if err == nil {
tb.Errorf("expected an error, but got a nil")
return
}
if have := err.Error(); have != want {
tb.Errorf("have %q, want %q", have, want)
}
}
// execute a Do(args[,-1]...), which needs to be the same as the last arg.
func mustDo(tb testing.TB, c *proto.Client, args ...string) {
tb.Helper()
args, want := args[:len(args)-1], args[len(args)-1]
res, err := c.Do(args...)
ok(tb, err)
equals(tb, want, res)
}
// mustOK is a mustDo() which expects an "OK" response
func mustOK(tb testing.TB, c *proto.Client, args ...string) {
tb.Helper()
mustDo(tb, c, append(args, proto.Inline("OK"))...)
}
// mustNil is a mustDo() which expects a nil response
func mustNil(tb testing.TB, c *proto.Client, args ...string) {
tb.Helper()
mustDo(tb, c, append(args, proto.Nil)...)
}
// mustNilList is a mustDo() which expects a list nil (-1) response
func mustNilList(tb testing.TB, c *proto.Client, args ...string) {
tb.Helper()
mustDo(tb, c, append(args, proto.NilList)...)
}
// must0 is a mustDo() which expects a `0` response
func must0(tb testing.TB, c *proto.Client, args ...string) {
tb.Helper()
mustDo(tb, c, append(args, proto.Int(0))...)
}
// must1 is a mustDo() which expects a `1` response
func must1(tb testing.TB, c *proto.Client, args ...string) {
tb.Helper()
mustDo(tb, c, append(args, proto.Int(1))...)
}
// execute a Read()
func mustRead(tb testing.TB, c *proto.Client, want string) {
tb.Helper()
res, err := c.Read()
ok(tb, err)
equals(tb, want, res)
}
// execute a Do(args[,-1]...), which result needs to Contain() the same as the last arg.
func mustContain(tb testing.TB, c *proto.Client, args ...string) {
tb.Helper()
args, want := args[:len(args)-1], args[len(args)-1]
res, err := c.Do(args...)
ok(tb, err)
if !strings.Contains(res, want) {
tb.Errorf("expected %q in %q", want, res)
}
}
func useRESP3(t *testing.T, c *proto.Client) {
mustContain(t, c, "HELLO", "3", "miniredis")
}
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randomStr(length int) string {
b := make([]byte, length)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}