IRC parsing, tokenization, and state handling in C#
at a1df2ed6b496cfac5770a6bd7e4806ca18bf39a5 80 lines 2.2 kB view raw
1namespace IRCSharp.Tests.Tokenization; 2 3[TestClass] 4public class StatefulEncoder 5{ 6 private IRCTokens.StatefulEncoder _encoder; 7 8 [TestInitialize] 9 public void Initialize() 10 { 11 _encoder = new(); 12 } 13 14 [TestMethod] 15 public void Push() 16 { 17 var line = new Line("PRIVMSG #channel hello"); 18 _encoder.Push(line); 19 Assert.AreEqual("PRIVMSG #channel hello\r\n", _encoder.Pending()); 20 } 21 22 [TestMethod] 23 public void PopPartial() 24 { 25 var line = new Line("PRIVMSG #channel hello"); 26 _encoder.Push(line); 27 _encoder.Pop("PRIVMSG #channel hello".Length); 28 Assert.AreEqual("\r\n", _encoder.Pending()); 29 } 30 31 [TestMethod] 32 public void TestPopReturned() 33 { 34 var line = new Line("PRIVMSG #channel hello"); 35 _encoder.Push(line); 36 _encoder.Push(line); 37 var lines = _encoder.Pop("PRIVMSG #channel hello\r\n".Length); 38 Assert.AreEqual(1, lines.Count); 39 Assert.AreEqual(line, lines[0]); 40 } 41 42 [TestMethod] 43 public void PopNoneReturned() 44 { 45 var line = new Line("PRIVMSG #channel hello"); 46 _encoder.Push(line); 47 var lines = _encoder.Pop(1); 48 Assert.AreEqual(0, lines.Count); 49 } 50 51 [TestMethod] 52 public void PopMultipleLines() 53 { 54 var line1 = new Line("PRIVMSG #channel1 hello"); 55 _encoder.Push(line1); 56 var line2 = new Line("PRIVMSG #channel2 hello"); 57 _encoder.Push(line2); 58 59 var lines = _encoder.Pop(_encoder.Pending().Length); 60 Assert.AreEqual(2, lines.Count); 61 Assert.AreEqual(string.Empty, _encoder.Pending()); 62 } 63 64 [TestMethod] 65 public void Clear() 66 { 67 _encoder.Push(new("PRIVMSG #channel hello")); 68 _encoder.Clear(); 69 Assert.AreEqual(string.Empty, _encoder.Pending()); 70 } 71 72 [TestMethod] 73 public void EncodingIso8859() 74 { 75 var iso8859 = Encoding.GetEncoding("iso-8859-1"); 76 _encoder = new() {Encoding = iso8859}; 77 _encoder.Push(new("PRIVMSG #channel :hello Ç")); 78 CollectionAssert.AreEqual(iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n"), _encoder.PendingBytes); 79 } 80}