-
Notifications
You must be signed in to change notification settings - Fork 562
/
assert.go
156 lines (145 loc) · 4.71 KB
/
assert.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
package asserts
import (
"fmt"
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol"
"runtime"
"testing"
)
// AssertIntReply checks if the given redis.Reply is the expected integer
func AssertIntReply(t *testing.T, actual redis.Reply, expected int) {
intResult, ok := actual.(*protocol.IntReply)
if !ok {
t.Errorf("expected int protocol, actually %s, %s", actual.ToBytes(), printStack())
return
}
if intResult.Code != int64(expected) {
t.Errorf("expected %d, actually %d, %s", expected, intResult.Code, printStack())
}
}
func AssertIntReplyGreaterThan(t *testing.T, actual redis.Reply, expected int) {
intResult, ok := actual.(*protocol.IntReply)
if !ok {
t.Errorf("expected int protocol, actually %s, %s", actual.ToBytes(), printStack())
return
}
if intResult.Code < int64(expected) {
t.Errorf("expected %d, actually %d, %s", expected, intResult.Code, printStack())
}
}
// AssertBulkReply checks if the given redis.Reply is the expected string
func AssertBulkReply(t *testing.T, actual redis.Reply, expected string) {
bulkReply, ok := actual.(*protocol.BulkReply)
if !ok {
t.Errorf("expected bulk protocol, actually %s, %s", actual.ToBytes(), printStack())
return
}
if !utils.BytesEquals(bulkReply.Arg, []byte(expected)) {
t.Errorf("expected %s, actually %s, %s", expected, actual.ToBytes(), printStack())
}
}
// AssertStatusReply checks if the given redis.Reply is the expected status
func AssertStatusReply(t *testing.T, actual redis.Reply, expected string) {
statusReply, ok := actual.(*protocol.StatusReply)
if !ok {
// may be a protocol.OkReply e.g.
expectBytes := protocol.MakeStatusReply(expected).ToBytes()
if utils.BytesEquals(actual.ToBytes(), expectBytes) {
return
}
t.Errorf("expected bulk protocol, actually %s, %s", actual.ToBytes(), printStack())
return
}
if statusReply.Status != expected {
t.Errorf("expected %s, actually %s, %s", expected, actual.ToBytes(), printStack())
}
}
// AssertErrReply checks if the given redis.Reply is the expected error
func AssertErrReply(t *testing.T, actual redis.Reply, expected string) {
errReply, ok := actual.(protocol.ErrorReply)
if !ok {
expectBytes := protocol.MakeErrReply(expected).ToBytes()
if utils.BytesEquals(actual.ToBytes(), expectBytes) {
return
}
t.Errorf("expected err protocol, actually %s, %s", actual.ToBytes(), printStack())
return
}
if errReply.Error() != expected {
t.Errorf("expected %s, actually %s, %s", expected, actual.ToBytes(), printStack())
}
}
// AssertNotError checks if the given redis.Reply is not error protocol
func AssertNotError(t *testing.T, result redis.Reply) {
if result == nil {
t.Errorf("result is nil %s", printStack())
return
}
bytes := result.ToBytes()
if len(bytes) == 0 {
t.Errorf("result is empty %s", printStack())
return
}
if bytes[0] == '-' {
t.Errorf("result is err protocol %s", printStack())
}
}
// AssertNullBulk checks if the given redis.Reply is protocol.NullBulkReply
func AssertNullBulk(t *testing.T, result redis.Reply) {
if result == nil {
t.Errorf("result is nil %s", printStack())
return
}
bytes := result.ToBytes()
if len(bytes) == 0 {
t.Errorf("result is empty %s", printStack())
return
}
expect := (&protocol.NullBulkReply{}).ToBytes()
if !utils.BytesEquals(expect, bytes) {
t.Errorf("result is not null-bulk-protocol %s", printStack())
}
}
// AssertMultiBulkReply checks if the given redis.Reply has the expected content
func AssertMultiBulkReply(t *testing.T, actual redis.Reply, expected []string) {
multiBulk, ok := actual.(*protocol.MultiBulkReply)
if !ok {
t.Errorf("expected bulk protocol, actually %s, %s", actual.ToBytes(), printStack())
return
}
if len(multiBulk.Args) != len(expected) {
t.Errorf("expected %d elements, actually %d, %s",
len(expected), len(multiBulk.Args), printStack())
return
}
for i, v := range multiBulk.Args {
str := string(v)
if str != expected[i] {
t.Errorf("expected %s, actually %s, %s", expected[i], actual, printStack())
}
}
}
// AssertMultiBulkReplySize check if redis.Reply has expected length
func AssertMultiBulkReplySize(t *testing.T, actual redis.Reply, expected int) {
multiBulk, ok := actual.(*protocol.MultiBulkReply)
if !ok {
if expected == 0 &&
utils.BytesEquals(actual.ToBytes(), protocol.MakeEmptyMultiBulkReply().ToBytes()) {
return
}
t.Errorf("expected bulk protocol, actually %s, %s", actual.ToBytes(), printStack())
return
}
if len(multiBulk.Args) != expected {
t.Errorf("expected %d elements, actually %d, %s", expected, len(multiBulk.Args), printStack())
return
}
}
func printStack() string {
_, file, no, ok := runtime.Caller(2)
if ok {
return fmt.Sprintf("at %s:%d", file, no)
}
return ""
}