summaryrefslogtreecommitdiff
path: root/vendor/github.com/fluffle/goirc/state/nick_test.go
blob: 1344400b43c59ff1e32fa1361064db4933fef3ac (plain)
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
package state

import "testing"

func compareNick(t *testing.T, nk *nick) {
	n := nk.Nick()
	if n.Nick != nk.nick || n.Ident != nk.ident || n.Host != nk.host || n.Name != nk.name ||
		!n.Modes.Equals(nk.modes) || len(n.Channels) != len(nk.chans) {
		t.Errorf("Nick not duped correctly from internal state.")
	}
	for ch, cp := range nk.chans {
		if other, ok := n.Channels[ch.name]; !ok || !cp.Equals(other) {
			t.Errorf("Channel not duped correctly from internal state.")
		}
	}
}

func TestNewNick(t *testing.T) {
	nk := newNick("test1")

	if nk.nick != "test1" {
		t.Errorf("Nick not created correctly by NewNick()")
	}
	if len(nk.chans) != 0 || len(nk.lookup) != 0 {
		t.Errorf("Nick maps contain data after NewNick()")
	}
	compareNick(t, nk)
}

func TestAddChannel(t *testing.T) {
	nk := newNick("test1")
	ch := newChannel("#test1")
	cp := new(ChanPrivs)

	nk.addChannel(ch, cp)

	if len(nk.chans) != 1 || len(nk.lookup) != 1 {
		t.Errorf("Channel lists not updated correctly for add.")
	}
	if c, ok := nk.chans[ch]; !ok || c != cp {
		t.Errorf("Channel #test1 not properly stored in chans map.")
	}
	if c, ok := nk.lookup["#test1"]; !ok || c != ch {
		t.Errorf("Channel #test1 not properly stored in lookup map.")
	}
	compareNick(t, nk)
}

func TestDelChannel(t *testing.T) {
	nk := newNick("test1")
	ch := newChannel("#test1")
	cp := new(ChanPrivs)

	nk.addChannel(ch, cp)
	nk.delChannel(ch)
	if len(nk.chans) != 0 || len(nk.lookup) != 0 {
		t.Errorf("Channel lists not updated correctly for del.")
	}
	if c, ok := nk.chans[ch]; ok || c != nil {
		t.Errorf("Channel #test1 not properly removed from chans map.")
	}
	if c, ok := nk.lookup["#test1"]; ok || c != nil {
		t.Errorf("Channel #test1 not properly removed from lookup map.")
	}
	compareNick(t, nk)
}

func TestNickParseModes(t *testing.T) {
	nk := newNick("test1")
	md := nk.modes

	// Modes should all be false for a new nick
	if md.Invisible || md.Oper || md.WallOps || md.HiddenHost || md.SSL {
		t.Errorf("Modes for new nick set to true.")
	}

	// Set a couple of modes, for testing.
	md.Invisible = true
	md.HiddenHost = true

	// Parse a mode line that flips one true to false and two false to true
	nk.parseModes("+z-x+w")

	compareNick(t, nk)
	if !md.Invisible || md.Oper || !md.WallOps || md.HiddenHost || !md.SSL {
		t.Errorf("Modes not flipped correctly by ParseModes.")
	}
}