File: stream_test.go

package info (click to toggle)
golang-sorcix-irc-dev 1.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 132 kB
  • sloc: makefile: 3
file content (103 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download
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
// Copyright 2014 Vic Demuzere
//
// Use of this source code is governed by the MIT license.

package irc

import (
	"bytes"
	"crypto/tls"
	"io"
	"log"
	"reflect"
	"strings"
	"testing"
)

// We use the Dial function as a simple shortcut for connecting to an IRC server using a standard TCP socket.
func ExampleDial() {
	conn, err := Dial("irc.quakenet.org:6667")
	if err != nil {
		log.Fatalln("Could not connect to IRC server")
	}

	conn.Close()
}

// Use NewConn when you want to connect using something else than a standard TCP socket.
// This example first opens an encrypted TLS connection and then uses that to communicate with the server.
func ExampleNewConn() {
	tconn, err := tls.Dial("tcp", "irc.quakenet.org:6667", &tls.Config{})
	if err != nil {
		log.Fatalln("Could not connect to IRC server")
	}
	conn := NewConn(tconn)

	conn.Close()
}

var stream = "PING port80a.se.quakenet.org\r\n:port80a.se.quakenet.org PONG port80a.se.quakenet.org :port80a.se.quakenet.org\r\nPING chat.freenode.net\r\n:wilhelm.freenode.net PONG wilhelm.freenode.net :chat.freenode.net\r\n"

var result = [...]*Message{
	{
		Command: PING,
		Params:  []string{"port80a.se.quakenet.org"},
	},
	{
		Prefix: &Prefix{
			Name: "port80a.se.quakenet.org",
		},
		Command:  PONG,
		Params:   []string{"port80a.se.quakenet.org"},
		Trailing: "port80a.se.quakenet.org",
	},
	{
		Command: PING,
		Params:  []string{"chat.freenode.net"},
	},
	{
		Prefix: &Prefix{
			Name: "wilhelm.freenode.net",
		},
		Command:  PONG,
		Params:   []string{"wilhelm.freenode.net"},
		Trailing: "chat.freenode.net",
	},
}

func TestDecoder_Decode(t *testing.T) {

	reader := strings.NewReader(stream)
	dec := NewDecoder(reader)

	for i, test := range result {
		if message, err := dec.Decode(); err != nil {
			t.Fatalf("Unexpected error: %s", err.Error())
		} else {
			if !reflect.DeepEqual(message, test) {
				t.Fatalf("Decoded message looks wrong! (%d)", i)
			}
		}
	}

	if _, err := dec.Decode(); err != io.EOF {
		t.Fatal("Decode should return an EOF error!")
	}
}

func TestEncoder_Encode(t *testing.T) {

	buffer := new(bytes.Buffer)
	enc := NewEncoder(buffer)

	for _, test := range result {
		if err := enc.Encode(test); err != nil {
			t.Fatalf("Unexpected error: %s", err.Error())
		}
	}

	if buffer.String() != stream {
		t.Fatalf("Encoded stream looks wrong!")
	}

}