]> Cypherpunks.ru repositories - goircd.git/blob - client_test.go
Unify copyright comment format
[goircd.git] / client_test.go
1 // goircd -- minimalistic simple Internet Relay Chat (IRC) server
2 // Copyright (C) 2014-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "testing"
20 )
21
22 func TestNewClient(t *testing.T) {
23         conn := NewTestingConn()
24         events := make(chan ClientEvent)
25         host := "foohost"
26         hostname = &host
27         client := NewClient(conn, events)
28         defer func() {
29                 client.Close()
30         }()
31
32         event := <-events
33         if event.eventType != EventNew {
34                 t.Fatal("no NEW event", event)
35         }
36         conn.inbound <- "foo"
37         event = <-events
38         if (event.eventType != EventMsg) || (event.text != "foo") {
39                 t.Fatal("no first MSG", event)
40         }
41         conn.inbound <- "bar"
42         event = <-events
43         if (event.eventType != EventMsg) || (event.text != "bar") {
44                 t.Fatal("no second MSG", event)
45         }
46         conn.inbound <- ""
47         event = <-events
48         if event.eventType != EventDel {
49                 t.Fatal("no client termination", event)
50         }
51 }
52
53 func TestClientReplies(t *testing.T) {
54         conn := NewTestingConn()
55         host := "foohost"
56         hostname = &host
57         client := NewClient(conn, make(chan ClientEvent, 2))
58         defer func() {
59                 client.Close()
60         }()
61         client.nickname = "мойник"
62
63         client.Reply("hello")
64         if r := <-conn.outbound; r != ":foohost hello\r\n" {
65                 t.Fatal("did not recieve hello message", r)
66         }
67
68         client.ReplyParts("200", "foo", "bar")
69         if r := <-conn.outbound; r != ":foohost 200 foo :bar\r\n" {
70                 t.Fatal("did not recieve 200 message", r)
71         }
72
73         client.ReplyNicknamed("200", "foo", "bar")
74         if r := <-conn.outbound; r != ":foohost 200 мойник foo :bar\r\n" {
75                 t.Fatal("did not recieve nicknamed message", r)
76         }
77
78         client.ReplyNotEnoughParameters("CMD")
79         if r := <-conn.outbound; r != ":foohost 461 мойник CMD :Not enough parameters\r\n" {
80                 t.Fatal("did not recieve 461 message", r)
81         }
82 }