-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
101 lines (85 loc) · 2.28 KB
/
parse_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
package ircb
import (
"bytes"
"log"
"os"
"testing"
)
var testconfig = &Config{
Nick: "testing",
Master: "tester",
CommandPrefix: "!",
}
var buf = new(bytes.Buffer)
type testconnection struct {
buf *bytes.Buffer
log *log.Logger
}
func (t *testconnection) Close() error {
return nil
}
func (t *testconnection) Write(b []byte) (n int, err error) {
return t.buf.Write(b)
}
func (t *testconnection) Read(b []byte) (n int, err error) {
return t.buf.Read(b)
}
func NewTestConnection() *Connection {
var tc = new(testconnection)
tc.buf = new(bytes.Buffer)
tc.log = log.New(os.Stderr, "testnet:", log.Lshortfile)
return &Connection{
Log: log.New(os.Stderr, "conn:", log.Lshortfile),
conn: tc,
config: testconfig,
}
}
func TestTest(t *testing.T) {
_, err := NewTestConnection().Write([]byte("PING"))
if err != nil {
t.Fail()
t.Log(err)
}
}
func TestParse(t *testing.T) {
c := NewTestConnection()
c.config.CommandPrefix = "!"
irc := c.config.Parse("foo PRIVMSG :bar")
if irc.Verb != "PRIVMSG" {
t.Logf("expected verb: PRIVMSG, got %q", irc.Verb)
t.Fail()
}
irc = c.config.Parse("FOO")
if irc.Verb != "FOO" {
t.Logf("expected verb: FOO, got %q", irc.Verb)
t.Fail()
}
testcases := []struct {
expected, input string
}{
{"433", ":host.test 433 * mustangsally :Nickname is already in use\r\n"},
{"451", ":oragono.test 451 * :You need to register before you can use that command\r\n"},
{"PING", "PING mustangsally\r\n"},
{"PRIVMSG", "mustangsally!ok@ok PRIVMSG #ok :hello"},
}
for _, test := range testcases {
if out := testconfig.Parse(test.input).Verb; out != test.expected {
t.Logf("wanted: %q", test.expected)
t.Logf("but got: %q", out)
}
}
testcases = []struct {
expected, input string
}{
{"mustangsally :Nickname is already in use", ":host.test 433 * mustangsally :Nickname is already in use\r\n"},
{"You need to register before you can use that command", ":oragono.test 451 * :You need to register before you can use that command\r\n"},
{"mustangsally", "PING mustangsally\r\n"},
{"hello", "mustangsally!ok@ok PRIVMSG #ok :hello"},
}
for _, test := range testcases {
if out := testconfig.Parse(test.input).Message; out != test.expected {
t.Logf("wanted: %q", test.expected)
t.Logf("but got: %q", out)
}
}
}