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