1package client
2
3import (
4	"reflect"
5	"testing"
6)
7
8func TestCutNewLines(t *testing.T) {
9	tests := []struct{ in, out string }{
10		{"", ""},
11		{"foo bar", "foo bar"},
12		{"foo bar\rbaz", "foo bar"},
13		{"foo bar\nbaz", "foo bar"},
14		{"blorp\r\n\r\nbloop", "blorp"},
15		{"\n\rblaap", ""},
16		{"\r\n", ""},
17		{"boo\\r\\n\\n\r", "boo\\r\\n\\n"},
18	}
19	for i, test := range tests {
20		out := cutNewLines(test.in)
21		if test.out != out {
22			t.Errorf("test %d: expected %q, got %q", i, test.out, out)
23		}
24	}
25}
26
27func TestIndexFragment(t *testing.T) {
28	tests := []struct {
29		in  string
30		out int
31	}{
32		{"", -1},
33		{"foobarbaz", -1},
34		{"foo bar baz", 8},
35		{"foo. bar baz", 5},
36		{"foo: bar baz", 5},
37		{"foo; bar baz", 5},
38		{"foo, bar baz", 5},
39		{"foo! bar baz", 5},
40		{"foo? bar baz", 5},
41		{"foo\" bar baz", 5},
42		{"foo' bar baz", 5},
43		{"foo. bar. baz beep", 10},
44		{"foo. bar, baz beep", 10},
45	}
46	for i, test := range tests {
47		out := indexFragment(test.in)
48		if test.out != out {
49			t.Errorf("test %d: expected %d, got %d", i, test.out, out)
50		}
51	}
52}
53
54func TestSplitMessage(t *testing.T) {
55	tests := []struct {
56		in  string
57		sp  int
58		out []string
59	}{
60		{"", 0, []string{""}},
61		{"foo", 0, []string{"foo"}},
62		{"foo bar baz beep", 0, []string{"foo bar baz beep"}},
63		{"foo bar baz beep", 15, []string{"foo bar baz ...", "beep"}},
64		{"foo bar, baz beep", 15, []string{"foo bar, ...", "baz beep"}},
65		{"0123456789012345", 0, []string{"0123456789012345"}},
66		{"0123456789012345", 15, []string{"012345678901...", "2345"}},
67		{"0123456789012345", 16, []string{"0123456789012345"}},
68	}
69	for i, test := range tests {
70		out := splitMessage(test.in, test.sp)
71		if !reflect.DeepEqual(test.out, out) {
72			t.Errorf("test %d: expected %q, got %q", i, test.out, out)
73		}
74	}
75}
76
77func TestClientCommands(t *testing.T) {
78	c, s := setUp(t)
79	defer s.tearDown()
80
81	// Avoid having to type ridiculously long lines to test that
82	// messages longer than SplitLen are correctly sent to the server.
83	c.cfg.SplitLen = 23
84
85	c.Pass("password")
86	s.nc.Expect("PASS password")
87
88	c.Nick("test")
89	s.nc.Expect("NICK test")
90
91	c.User("test", "Testing IRC")
92	s.nc.Expect("USER test 12 * :Testing IRC")
93
94	c.Raw("JUST a raw :line")
95	s.nc.Expect("JUST a raw :line")
96
97	c.Join("#foo")
98	s.nc.Expect("JOIN #foo")
99	c.Join("#foo bar")
100	s.nc.Expect("JOIN #foo bar")
101
102	c.Part("#foo")
103	s.nc.Expect("PART #foo")
104	c.Part("#foo", "Screw you guys...")
105	s.nc.Expect("PART #foo :Screw you guys...")
106
107	c.Quit()
108	s.nc.Expect("QUIT :GoBye!")
109	c.Quit("I'm going home.")
110	s.nc.Expect("QUIT :I'm going home.")
111
112	c.Whois("somebody")
113	s.nc.Expect("WHOIS somebody")
114
115	c.Who("*@some.host.com")
116	s.nc.Expect("WHO *@some.host.com")
117
118	c.Privmsg("#foo", "bar")
119	s.nc.Expect("PRIVMSG #foo :bar")
120
121	c.Privmsgln("#foo", "bar")
122	s.nc.Expect("PRIVMSG #foo :bar")
123
124	c.Privmsgf("#foo", "say %s", "foo")
125	s.nc.Expect("PRIVMSG #foo :say foo")
126
127	c.Privmsgln("#foo", "bar", 1, 3.54, []int{24, 36})
128	s.nc.Expect("PRIVMSG #foo :bar 1 3.54 [24 36]")
129
130	c.Privmsgf("#foo", "user %d is at %s", 2, "home")
131	s.nc.Expect("PRIVMSG #foo :user 2 is at home")
132
133	//                 0123456789012345678901234567890123
134	c.Privmsg("#foo", "foo bar baz blorp. woo woobly woo.")
135	s.nc.Expect("PRIVMSG #foo :foo bar baz blorp. ...")
136	s.nc.Expect("PRIVMSG #foo :woo woobly woo.")
137
138	c.Privmsgln("#foo", "foo bar baz blorp. woo woobly woo.")
139	s.nc.Expect("PRIVMSG #foo :foo bar baz blorp. ...")
140	s.nc.Expect("PRIVMSG #foo :woo woobly woo.")
141
142	c.Privmsgf("#foo", "%s %s", "foo bar baz blorp.", "woo woobly woo.")
143	s.nc.Expect("PRIVMSG #foo :foo bar baz blorp. ...")
144	s.nc.Expect("PRIVMSG #foo :woo woobly woo.")
145
146	c.Privmsgln("#foo", "foo bar", 3.54, "blorp.", "woo", "woobly", []int{1, 2})
147	s.nc.Expect("PRIVMSG #foo :foo bar 3.54 blorp. ...")
148	s.nc.Expect("PRIVMSG #foo :woo woobly [1 2]")
149
150	c.Privmsgf("#foo", "%s %.2f %s %s %s %v", "foo bar", 3.54, "blorp.", "woo", "woobly", []int{1, 2})
151	s.nc.Expect("PRIVMSG #foo :foo bar 3.54 blorp. ...")
152	s.nc.Expect("PRIVMSG #foo :woo woobly [1 2]")
153
154	c.Notice("somebody", "something")
155	s.nc.Expect("NOTICE somebody :something")
156
157	//                    01234567890123456789012345678901234567
158	c.Notice("somebody", "something much much longer that splits")
159	s.nc.Expect("NOTICE somebody :something much much ...")
160	s.nc.Expect("NOTICE somebody :longer that splits")
161
162	c.Ctcp("somebody", "ping", "123456789")
163	s.nc.Expect("PRIVMSG somebody :\001PING 123456789\001")
164
165	c.Ctcp("somebody", "ping", "123456789012345678901234567890")
166	s.nc.Expect("PRIVMSG somebody :\001PING 12345678901234567890...\001")
167	s.nc.Expect("PRIVMSG somebody :\001PING 1234567890\001")
168
169	c.CtcpReply("somebody", "pong", "123456789012345678901234567890")
170	s.nc.Expect("NOTICE somebody :\001PONG 12345678901234567890...\001")
171	s.nc.Expect("NOTICE somebody :\001PONG 1234567890\001")
172
173	c.CtcpReply("somebody", "pong", "123456789")
174	s.nc.Expect("NOTICE somebody :\001PONG 123456789\001")
175
176	c.Version("somebody")
177	s.nc.Expect("PRIVMSG somebody :\001VERSION\001")
178
179	c.Action("#foo", "pokes somebody")
180	s.nc.Expect("PRIVMSG #foo :\001ACTION pokes somebody\001")
181
182	c.Topic("#foo")
183	s.nc.Expect("TOPIC #foo")
184	c.Topic("#foo", "la la la")
185	s.nc.Expect("TOPIC #foo :la la la")
186
187	c.Mode("#foo")
188	s.nc.Expect("MODE #foo")
189	c.Mode("#foo", "+o somebody")
190	s.nc.Expect("MODE #foo +o somebody")
191
192	c.Away()
193	s.nc.Expect("AWAY")
194	c.Away("Dave's not here, man.")
195	s.nc.Expect("AWAY :Dave's not here, man.")
196
197	c.Invite("somebody", "#foo")
198	s.nc.Expect("INVITE somebody #foo")
199
200	c.Oper("user", "pass")
201	s.nc.Expect("OPER user pass")
202
203	c.VHost("user", "pass")
204	s.nc.Expect("VHOST user pass")
205}
206