about summary refs log tree commit diff
path: root/IRCSharp.Tests/Tokenization/Tokenization.cs
blob: b270e4ef77a7db77a8ea39db09b696fc79fbfc94 (plain) (blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRCTokens;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IRCSharp.Tests.Tokenization
{
    [TestClass]
    public class Tokenization
    {
        [TestMethod]
        public void TagsMissing()
        {
            var line = new Line("PRIVMSG #channel");
            Assert.IsNull(line.Tags);
        }

        [TestMethod]
        public void TagsMissingValue()
        {
            var line = new Line("@id= PRIVMSG #channel");
            Assert.AreEqual(string.Empty, line.Tags["id"]);
        }

        [TestMethod]
        public void TagsMissingEqual()
        {
            var line = new Line("@id PRIVMSG #channel");
            Assert.AreEqual(string.Empty, line.Tags["id"]);
        }

        [TestMethod]
        public void TagsUnescape()
        {
            var line = new Line(@"@id=1\\\:\r\n\s2 PRIVMSG #channel");
            Assert.AreEqual("1\\;\r\n 2", line.Tags["id"]);
        }

        [TestMethod]
        public void TagsOverlap()
        {
            var line = new Line(@"@id=1\\\s\\s PRIVMSG #channel");
            Assert.AreEqual("1\\ \\s", line.Tags["id"]);
        }

        [TestMethod]
        public void TagsLoneEndSlash()
        {
            var line = new Line("@id=1\\ PRIVMSG #channel");
            Assert.AreEqual("1", line.Tags["id"]);
        }

        [TestMethod]
        public void SourceWithoutTags()
        {
            var line = new Line(":nick!user@host PRIVMSG #channel");
            Assert.AreEqual("nick!user@host", line.Source);
        }

        [TestMethod]
        public void SourceWithTags()
        {
            var line = new Line("@id=123 :nick!user@host PRIVMSG #channel");
            Assert.AreEqual("nick!user@host", line.Source);
        }

        [TestMethod]
        public void SourceMissingWithoutTags()
        {
            var line = new Line("PRIVMSG #channel");
            Assert.IsNull(line.Source);
        }

        [TestMethod]
        public void SourceMissingWithTags()
        {
            var line = new Line("@id=123 PRIVMSG #channel");
            Assert.IsNull(line.Source);
        }

        [TestMethod]
        public void Command()
        {
            var line = new Line("privmsg #channel");
            Assert.AreEqual("PRIVMSG", line.Command);
        }

        [TestMethod]
        public void ParamsTrailing()
        {
            var line = new Line("PRIVMSG #channel :hello world");
            CollectionAssert.AreEqual(new List<string> {"#channel", "hello world"}, line.Params);
        }

        [TestMethod]
        public void ParamsOnlyTrailing()
        {
            var line = new Line("PRIVMSG :hello world");
            CollectionAssert.AreEqual(new List<string> {"hello world"}, line.Params);
        }

        [TestMethod]
        public void ParamsMissing()
        {
            var line = new Line("PRIVMSG");
            Assert.AreEqual("PRIVMSG", line.Command);
            CollectionAssert.AreEqual(new List<string>(), line.Params);
        }

        [TestMethod]
        public void AllTokens()
        {
            var line = new Line("@id=123 :nick!user@host PRIVMSG #channel :hello world");
            CollectionAssert.AreEqual(new Dictionary<string, string> {{"id", "123"}}, line.Tags);
            Assert.AreEqual("nick!user@host", line.Source);
            Assert.AreEqual("PRIVMSG", line.Command);
            CollectionAssert.AreEqual(new List<string> {"#channel", "hello world"}, line.Params);
        }

        [TestMethod]
        public void NulByte()
        {
            var decoder = new IRCTokens.StatefulDecoder();
            var bytes = Encoding.UTF8.GetBytes(":nick!user@host PRIVMSG #channel :hello")
                .Concat(Encoding.UTF8.GetBytes("\0"))
                .Concat(Encoding.UTF8.GetBytes("world"))
                .ToArray();
            var line = decoder.Push(bytes, bytes.Length).First();
            
            CollectionAssert.AreEqual(new List<string> {"#channel", "hello"}, line.Params);
        }
    }
}