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