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