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