This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
request_test.go
154 lines (145 loc) · 3.39 KB
/
request_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
package redis
import (
"bytes"
"testing"
)
func TestRequestExpectArgument(t *testing.T) {
r := &Request{Name: "Hi", Args: [][]byte{}}
for i := 0; i < 10; i += 1 {
reply := r.ExpectArgument(i)
if reply == nil {
t.Fatalf("Expected error reply, got nil")
}
}
r = &Request{Name: "Hi", Args: [][]byte{{'h', 'i'}}}
reply := r.ExpectArgument(0)
if reply != nil {
t.Fatalf("Expected nil reply, got %s", reply)
}
reply = r.ExpectArgument(1)
if reply == nil {
t.Fatalf("Expected error reply, got nil")
}
}
func TestRequestGetString(t *testing.T) {
s := "Hello, World!"
r := &Request{Name: "Hi", Args: [][]byte{[]byte(s)}}
val, reply := r.GetString(0)
if reply != nil {
t.Fatalf("Expected nil reply, got %s", reply)
}
if val != s {
t.Fatalf("Expected %s, got %s", s, val)
}
val, reply = r.GetString(5)
if reply == nil {
t.Fatalf("Expected reply, got nil")
}
}
func TestRequestGetInteger(t *testing.T) {
invalid := []*Request{
{Name: "Hi", Args: [][]byte{}},
{Name: "Hi", Args: [][]byte{{'h', 'i'}}},
}
for _, request := range invalid {
_, reply := request.GetInteger(0)
if reply == nil {
t.Fatalf("Expected error reply, got nil")
}
}
valid := []struct {
request *Request
index int
number int
}{
{&Request{Name: "Hi", Args: [][]byte{{'1'}}}, 0, 1},
{&Request{Name: "Hi", Args: [][]byte{{'1'}, []byte("42")}}, 1, 42},
{&Request{Name: "Hi", Args: [][]byte{{'1'}, []byte("-1043")}}, 1, -1043},
}
for _, v := range valid {
number, reply := v.request.GetInteger(v.index)
if reply != nil {
t.Fatalf("Expected nil reply, got %s", reply)
}
if v.number != number {
t.Fatalf("Expected %d reply, got %d", number, v.number)
}
number, reply = v.request.GetPositiveInteger(v.index)
if v.number > 0 {
if reply != nil {
t.Fatalf("Expected nil reply, got %s", reply)
}
} else {
if reply == nil {
t.Fatalf("Expected error reply, got %s", reply)
}
}
}
}
func TestRequestGetMap(t *testing.T) {
invalid := []struct {
request *Request
index int
}{
{&Request{Name: "Hi", Args: [][]byte{}}, 0},
{&Request{Name: "Hi", Args: [][]byte{}}, 100},
{&Request{Name: "Hi", Args: [][]byte{{'h', 'i'}}}, 0},
{&Request{Name: "Hi", Args: [][]byte{{'h', 'i'}, {'h', 'i'}, {'h', 'i'}}}, 0},
}
for _, v := range invalid {
_, reply := v.request.GetMap(v.index)
if reply == nil {
t.Fatalf("Expected error reply, got nil for %s %d", v.request, v.index)
}
}
valid := []struct {
request *Request
index int
expected map[string][]byte
}{
{
request: &Request{
Name: "Hi",
Args: [][]byte{
{'h', 'i'},
{'y', 'o'},
},
},
index: 0,
expected: map[string][]byte{"hi": []byte("yo")},
},
{
request: &Request{
Name: "Hi",
Args: [][]byte{
[]byte("hi"),
[]byte("yo"),
[]byte("key"),
[]byte("value"),
},
},
index: 0,
expected: map[string][]byte{"hi": []byte("yo"), "key": []byte("value")},
},
}
for _, v := range valid {
m, reply := v.request.GetMap(v.index)
if reply != nil {
t.Fatalf("Expected nil reply, got %s for %s %d", reply, v.request, v.index)
}
if !mapsEqual(m, v.expected) {
t.Fatalf("Expected %s got %s for %s", v.expected, v.request, m)
}
}
}
func mapsEqual(a map[string][]byte, b map[string][]byte) bool {
if len(a) != len(b) {
return false
}
for key, val := range a {
if !bytes.Equal(b[key], val) {
return false
}
}
return true
}