-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser_test.go
100 lines (70 loc) · 2.78 KB
/
parser_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
package yagnats
import (
"bufio"
"bytes"
. "gopkg.in/check.v1"
)
func (s *YSuite) TestParsePing(c *C) {
packet, err := Parse(bufio.NewReader(bytes.NewBuffer([]byte("PING \r\n"))))
c.Assert(err, Equals, nil)
c.Assert(packet, Not(Equals), nil)
c.Assert(string(packet.Encode()), Equals, "PING\r\n")
}
func (s *YSuite) TestParseMultiPing(c *C) {
io := bufio.NewReader(bytes.NewBuffer([]byte("PING\r\nPING\r\n")))
parse1, err1 := Parse(io)
parse2, err2 := Parse(io)
c.Assert(err1, Equals, nil)
c.Assert(parse1, Not(Equals), nil)
c.Assert(string(parse1.Encode()), Equals, "PING\r\n")
c.Assert(err2, Equals, nil)
c.Assert(parse2, Not(Equals), nil)
c.Assert(string(parse2.Encode()), Equals, "PING\r\n")
}
func (s *YSuite) TestParsePong(c *C) {
packet, err := Parse(bufio.NewReader(bytes.NewBuffer([]byte("PONG \r\n"))))
c.Assert(err, Equals, nil)
c.Assert(packet, Not(Equals), nil)
c.Assert(string(packet.Encode()), Equals, "PONG\r\n")
}
func (s *YSuite) TestParseInfo(c *C) {
packet, err := Parse(bufio.NewReader(bytes.NewBuffer([]byte("INFO {\"a\":1} \r\n"))))
c.Assert(err, Equals, nil)
c.Assert(packet, Not(Equals), nil)
c.Assert(string(packet.Encode()), Equals, "INFO {\"a\":1}\r\n")
}
func (s *YSuite) TestParseOK(c *C) {
packet, err := Parse(bufio.NewReader(bytes.NewBuffer([]byte("+OK\r\n"))))
c.Assert(err, Equals, nil)
c.Assert(packet, Not(Equals), nil)
c.Assert(string(packet.Encode()), Equals, "+OK\r\n")
}
func (s *YSuite) TestParseERR(c *C) {
packet, err := Parse(bufio.NewReader(bytes.NewBuffer([]byte("-ERR 'foo' \r\n"))))
c.Assert(err, Equals, nil)
c.Assert(packet, Not(Equals), nil)
c.Assert(string(packet.Encode()), Equals, "-ERR 'foo'\r\n")
}
func (s *YSuite) TestParseMsg(c *C) {
packet, err := Parse(bufio.NewReader(bytes.NewBuffer([]byte("MSG some.subject 42 4\r\nsup? \r\n"))))
c.Assert(err, Equals, nil)
c.Assert(packet, Not(Equals), nil)
c.Assert(string(packet.Encode()), Equals, "MSG some.subject 42 4\r\nsup?\r\n")
}
func (s *YSuite) TestParseMsgWithReplyTo(c *C) {
packet, err := Parse(bufio.NewReader(bytes.NewBuffer([]byte("MSG some.subject 42 some.reply 4\r\nsup?\r\n"))))
c.Assert(err, Equals, nil)
c.Assert(packet, Not(Equals), nil)
c.Assert(string(packet.Encode()), Equals, "MSG some.subject 42 some.reply 4\r\nsup?\r\n")
}
func (s *YSuite) TestParseMultiMsg(c *C) {
io := bufio.NewReader(bytes.NewBuffer([]byte("MSG some.subject 42 4\r\nsup?\r\nMSG some.other.subject 43 6\r\nsup 2?\r\n")))
parse1, err1 := Parse(io)
parse2, err2 := Parse(io)
c.Assert(err1, Equals, nil)
c.Assert(parse1, Not(Equals), nil)
c.Assert(string(parse1.Encode()), Equals, "MSG some.subject 42 4\r\nsup?\r\n")
c.Assert(err2, Equals, nil)
c.Assert(parse2, Not(Equals), nil)
c.Assert(string(parse2.Encode()), Equals, "MSG some.other.subject 43 6\r\nsup 2?\r\n")
}