-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter_test.go
132 lines (122 loc) · 3.03 KB
/
converter_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
package resp
import "testing"
func TestSimpleString_Parse_uncompleted(t *testing.T) {
raw := []byte("+test")
s := NewSimpleString()
cmd, err, surplus := s.Parse(raw)
if err != nil {
t.Fatal("expected nil error, got ", err)
}
if cmd != nil {
t.Fatal("expected nil, got ", cmd)
}
if len(surplus) != 0 {
t.Fatal("expected zero surplus length, got", surplus)
}
}
func TestSimpleString_Parse_successful(t *testing.T) {
raw := []byte("+test\n")
s := NewSimpleString()
expectedCmd := &Command{
Raw: raw,
Args: [][]byte{[]byte("test")},
}
cmd, err, surplus := s.Parse(raw)
if err != nil {
t.Fatal("expected nil, got ", err)
}
if len(surplus) != 0 {
t.Fatal("expected zero surplus length, got", surplus)
}
if string(cmd.Raw) != string(expectedCmd.Raw) || string(cmd.Args[0]) != string(expectedCmd.Args[0]) {
t.Fatal("expected", expectedCmd, "got", cmd)
}
}
func TestInteger_Parse(t *testing.T) {
raw := []byte(":1000\n")
s := NewInteger()
cmd, err, surplus := s.Parse(raw)
if len(surplus) != 0 {
t.Fatal("expected 0, got ", string(surplus))
}
if err != nil {
t.Fatal("expected error, got ", err)
}
if string(cmd.Args[0]) != "1000" {
t.Fatal("expected", "1000", "got", string(cmd.Args[0]))
}
}
func TestInteger_Parse2(t *testing.T) {
raw := []byte(":100")
s := NewInteger()
_, _, _ = s.Parse(raw)
raw = []byte("0\n")
cmd, err, surplus := s.Parse(raw)
if len(surplus) != 0 {
t.Fatal("expected 0, got ", string(surplus))
}
if err != nil {
t.Fatal("expected error, got ", err)
}
if string(cmd.Args[0]) != "1000" {
t.Fatal("expected", "1000", "got", string(cmd.Args[0]))
}
}
func TestBulkString_Parse(t *testing.T) {
raw := []byte("$6\ntesthh\n")
s := NewBulkString()
cmd, err, surplus := s.Parse(raw)
if len(surplus) != 0 {
t.Fatal("expected 0, got ", string(surplus))
}
if err != nil {
t.Fatal("expected error, got ", err)
}
if string(cmd.Args[0]) != "testhh" {
t.Fatal("expected", "testhh", "got", string(cmd.Args[0]))
}
}
func TestBulkString_Parse2(t *testing.T) {
raw := []byte("$6\ntest")
s := NewBulkString()
_, _, _ = s.Parse(raw)
raw = []byte("hh\n")
cmd, err, surplus := s.Parse(raw)
if len(surplus) != 0 {
t.Fatal("expected 0, got ", string(surplus))
}
if err != nil {
t.Fatal("expected error, got ", err)
}
if string(cmd.Args[0]) != "testhh" {
t.Fatal("expected", "testhh", "got", string(cmd.Args[0]))
}
}
func TestArray_Parse(t *testing.T) {
raw := []byte("*-1\n")
s := NewArray()
cmd, err, _ := s.Parse(raw)
if err != nil {
t.Fatal("expected", nil, "got", err)
}
if cmd != Nil {
t.Fatal("expected", Nil, "got", cmd)
}
}
func TestArray_Parse2(t *testing.T) {
raw := []byte("*2\n+test\n:1000\n\n")
s := NewArray()
cmd, err, surplus := s.Parse(raw)
if err != nil {
t.Fatal("expected", nil, "got", err)
}
if len(surplus) != 0 {
t.Fatal("expected", 0, "got", string(surplus))
}
if string(cmd.Args[0]) != "test" {
t.Fatal("expected", "test", "got", string(cmd.Args[0]))
}
if string(cmd.Args[1]) != "1000" {
t.Fatal("expected", "test", "got", string(cmd.Args[0]))
}
}