-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_test.go
113 lines (97 loc) · 2.7 KB
/
command_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
package resp
import (
"reflect"
"testing"
)
type commandTest struct {
given []byte
expected [][]byte
}
func TestCommandSlice_Valid(t *testing.T) {
tests := []commandTest{
{[]byte("*1\r\n$4\r\nPING\r\n"), [][]byte{[]byte("PING")}},
{[]byte("*2\r\n$4\r\nINFO\r\n$3\r\nALL\r\n"), [][]byte{[]byte("INFO"), []byte("ALL")}},
}
for i, test := range tests {
command := Command(test.given)
args, err := command.Slices()
if err != nil {
t.Errorf("tests[%d]: %s", i, err.Error())
} else if !reflect.DeepEqual(test.expected, args) {
t.Errorf("tests[%d]:\nexpected: %v\ngot: %v", i, test.expected, args)
}
args, err = command.Bytes()
if err != nil {
t.Errorf("tests[%d]: %s", i, err.Error())
} else if !reflect.DeepEqual(test.expected, args) {
t.Errorf("tests[%d]:\nexpected: %v\ngot: %v", i, test.expected, args)
}
expectedStrings := make([]string, len(test.expected))
for i, bytes := range test.expected {
expectedStrings[i] = string(bytes)
}
stringArgs, err := command.Strings()
if err != nil {
t.Errorf("tests[%d]: %s", i, err.Error())
} else if !reflect.DeepEqual(expectedStrings, stringArgs) {
t.Errorf("tests[%d]:\nexpected: %v\ngot: %v", i, expectedStrings, stringArgs)
}
}
}
func TestCommandSlice_Invalid(t *testing.T) {
tests := [][]byte{
// empty
[]byte(""),
// wrong type
[]byte("$3\r\nfoo\r\n"),
// missing array elements
[]byte("*2\r\n$1\r\nX\r\n"),
// bad bulk string
[]byte("*1\r\n$100\r\noops"),
// nil bulk string
[]byte("*1\r\n$-1\r\n"),
// too short
[]byte("*1\r\n$0\r\n\r\n"),
// bad line ending
[]byte("*1\r\n$4\r\nPING\r"),
}
for i, test := range tests {
command := Command(test)
_, err := command.Slices()
if err == nil {
t.Errorf("test[%d]: expected error but got none", i)
}
_, err = command.Bytes()
if err == nil {
t.Errorf("test[%d]: expected error but got none", i)
}
_, err = command.Strings()
if err == nil {
t.Errorf("test[%d]: expected error but got none", i)
}
}
}
type newCommandStringsTest struct {
args []string
bytes []byte
}
func TestNewCommandStrings(t *testing.T) {
tests := []newCommandStringsTest{
{[]string{}, []byte("*0\r\n")},
{[]string{"PING"}, []byte("*1\r\n$4\r\nPING\r\n")},
{[]string{"INFO", "ALL"}, []byte("*2\r\n$4\r\nINFO\r\n$3\r\nALL\r\n")},
{[]string{"INFO", ""}, []byte("*2\r\n$4\r\nINFO\r\n$0\r\n\r\n")},
}
for i, test := range tests {
command := NewCommand(test.args...)
if !reflect.DeepEqual(test.bytes, []byte(command)) {
t.Errorf("tests[%d]:\nexpected: %v\ngot: %v", i, test.bytes, command)
}
}
}
func BenchmarkCommandSlices(b *testing.B) {
raw := Command("*2\r\n$4\r\nINFO\r\n$3\r\nALL\r\n")
for i := 0; i < b.N; i++ {
raw.Slices()
}
}