IRC parsing, tokenization, and state handling in C#
at ircrobotsv2 122 lines 3.9 kB view raw
1namespace IRCSharp.Tests.State; 2 3public class Cap 4{ 5 private readonly Server _server = new("test"); 6 7 [Test] 8 public async Task LSOneLine() 9 { 10 await Assert.That(_server.HasCap).IsFalse(); 11 await Assert.That(_server.AvailableCaps).IsEmpty(); 12 _server.Parse(new Line("CAP * LS :a b")); 13 await Assert.That(_server.AvailableCaps).ContainsKey("a").And.ContainsKey("b"); 14 await Assert.That(_server.AvailableCaps) 15 .IsEquivalentTo(new Dictionary<string, string> { { "a", "" }, { "b", "" } }); 16 } 17 18 [Test] 19 public async Task LsTwoLines() 20 { 21 _server.Parse(new Line("CAP * LS * :a b")); 22 await Assert.That(_server.AvailableCaps).IsEmpty(); 23 _server.Parse(new Line("CAP * LS :c")); 24 await Assert.That(_server.AvailableCaps).ContainsKey("a") 25 .And.ContainsKey("b") 26 .And.ContainsKey("c"); 27 } 28 29 [Test] 30 public async Task LsValues() 31 { 32 _server.Parse(new Line("CAP * LS :a b= c=1")); 33 await Assert.That(_server.AvailableCaps) 34 .IsEquivalentTo(new Dictionary<string, string> { { "a", "" }, { "b", "" }, { "c", "1" } }); 35 } 36 37 [Test] 38 public async Task ACKOneLine() 39 { 40 _server.Parse(new Line("CAP * LS :a b")); 41 _server.Parse(new Line("CAP * ACK :a b")); 42 await Assert.That(_server.AgreedCaps).Contains("a").And.Contains("b"); 43 } 44 45 [Test] 46 public async Task ACKTwoLines() 47 { 48 _server.Parse(new Line("CAP * LS :a b c")); 49 _server.Parse(new Line("CAP * ACK * :a b")); 50 _server.Parse(new Line("CAP * ACK :c")); 51 await Assert.That(_server.AgreedCaps).IsEquivalentTo(["a", "b", "c"]); 52 } 53 54 [Test] 55 public async Task ACKNotLs() 56 { 57 _server.Parse(new Line("CAP * LS a")); 58 _server.Parse(new Line("CAP * ACK b")); 59 await Assert.That(_server.AgreedCaps).IsEmpty(); 60 } 61 62 [Test] 63 public async Task NewNoLs() 64 { 65 _server.Parse(new Line("CAP * NEW :a")); 66 await Assert.That(_server.AvailableCaps).IsEquivalentTo(new Dictionary<string, string> { { "a", "" } }); 67 } 68 69 [Test] 70 public async Task NewOneLine() 71 { 72 _server.Parse(new Line("CAP * LS :a")); 73 _server.Parse(new Line("CAP * NEW :b")); 74 await Assert.That(_server.AvailableCaps) 75 .IsEquivalentTo(new Dictionary<string, string> { { "a", "" }, { "b", "" } }); 76 } 77 78 [Test] 79 public async Task NewTwoLines() 80 { 81 _server.Parse(new Line("CAP * LS :a")); 82 _server.Parse(new Line("CAP * NEW :b c")); 83 await Assert.That(_server.AvailableCaps) 84 .IsEquivalentTo(new Dictionary<string, string> { { "a", "" }, { "b", "" }, { "c", "" } }); 85 } 86 87 [Test] 88 public async Task DelNotAcked() 89 { 90 await Assert.That(() => _server.Parse(new Line("CAP * DEL a"))).ThrowsNothing(); 91 } 92 93 [Test] 94 public async Task DELOneLS() 95 { 96 _server.Parse(new Line("CAP * LS :a")); 97 _server.Parse(new Line("CAP * ACK :a")); 98 _server.Parse(new Line("CAP * DEL :a")); 99 await Assert.That(_server.AvailableCaps).IsEmpty(); 100 await Assert.That(_server.AgreedCaps).IsEmpty(); 101 } 102 103 [Test] 104 public async Task DelTwoLs() 105 { 106 _server.Parse(new Line("CAP * LS :a b")); 107 _server.Parse(new Line("CAP * ACK :a b")); 108 _server.Parse(new Line("CAP * DEL :a")); 109 await Assert.That(_server.AvailableCaps).IsEquivalentTo(new Dictionary<string, string> { { "b", "" } }); 110 await Assert.That(_server.AgreedCaps).IsEquivalentTo(["b"]); 111 } 112 113 [Test] 114 public async Task DelTwoDel() 115 { 116 _server.Parse(new Line("CAP * LS :a b")); 117 _server.Parse(new Line("CAP * ACK :a b")); 118 _server.Parse(new Line("CAP * DEL :a b")); 119 await Assert.That(_server.AvailableCaps).IsEmpty(); 120 await Assert.That(_server.AgreedCaps).IsEmpty(); 121 } 122}