IRC parsing, tokenization, and state handling in C#
1// ReSharper disable StringLiteralTypo
2namespace IRCSharp.Tests.Tokens;
3
4[TestClass]
5public class Format
6{
7 [TestMethod]
8 public void Tags()
9 {
10 var line = new Line("PRIVMSG", "#channel", "hello")
11 {
12 Tags = new() {{"id", "\\" + " " + ";" + "\r\n"}}
13 }.Format();
14
15 Assert.AreEqual(@"@id=\\\s\:\r\n PRIVMSG #channel hello", line);
16 }
17
18 [TestMethod]
19 public void MissingTag()
20 {
21 var line = new Line("PRIVMSG", "#channel", "hello").Format();
22
23 Assert.AreEqual("PRIVMSG #channel hello", line);
24 }
25
26 [TestMethod]
27 public void NullTag()
28 {
29 var line = new Line("PRIVMSG", "#channel", "hello") {Tags = new() {{"a", null}}}
30 .Format();
31
32 Assert.AreEqual("@a PRIVMSG #channel hello", line);
33 }
34
35 [TestMethod]
36 public void EmptyTag()
37 {
38 var line = new Line("PRIVMSG", "#channel", "hello") {Tags = new() {{"a", ""}}}
39 .Format();
40
41 Assert.AreEqual("@a PRIVMSG #channel hello", line);
42 }
43
44 [TestMethod]
45 public void Source()
46 {
47 var line = new Line("PRIVMSG", "#channel", "hello") {Source = "nick!user@host"}.Format();
48
49 Assert.AreEqual(":nick!user@host PRIVMSG #channel hello", line);
50 }
51
52 [TestMethod]
53 public void CommandLowercase()
54 {
55 var line = new Line {Command = "privmsg"}.Format();
56 Assert.AreEqual("privmsg", line);
57 }
58
59 [TestMethod]
60 public void CommandUppercase()
61 {
62 var line = new Line {Command = "PRIVMSG"}.Format();
63 Assert.AreEqual("PRIVMSG", line);
64 }
65
66 [TestMethod]
67 public void TrailingSpace()
68 {
69 var line = new Line("PRIVMSG", "#channel", "hello world").Format();
70
71 Assert.AreEqual("PRIVMSG #channel :hello world", line);
72 }
73
74 [TestMethod]
75 public void TrailingNoSpace()
76 {
77 var line = new Line("PRIVMSG", "#channel", "helloworld").Format();
78
79 Assert.AreEqual("PRIVMSG #channel helloworld", line);
80 }
81
82 [TestMethod]
83 public void TrailingDoubleColon()
84 {
85 var line = new Line("PRIVMSG", "#channel", ":helloworld").Format();
86
87 Assert.AreEqual("PRIVMSG #channel ::helloworld", line);
88 }
89
90 [TestMethod]
91 public void InvalidNonLastSpace()
92 {
93 Assert.ThrowsException<ArgumentException>(() => { new Line("USER", "user", "0 *", "real name").Format(); });
94 }
95
96 [TestMethod]
97 public void InvalidNonLastColon()
98 {
99 Assert.ThrowsException<ArgumentException>(() => { new Line("PRIVMSG", ":#channel", "hello").Format(); });
100 }
101}