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