IRC parsing, tokenization, and state handling in C#
at tunit 72 lines 2.1 kB view raw
1namespace IRCSharp.Tests.Tokenization; 2 3public class StatefulEncoder 4{ 5 private readonly IRCTokens.StatefulEncoder _encoder = new(); 6 7 [Test] 8 public async Task Push() 9 { 10 var line = new Line("PRIVMSG #channel hello"); 11 _encoder.Push(line); 12 await Assert.That(_encoder.Pending()).IsEqualTo("PRIVMSG #channel hello\r\n"); 13 } 14 15 [Test] 16 public async Task PopPartial() 17 { 18 var line = new Line("PRIVMSG #channel hello"); 19 _encoder.Push(line); 20 _encoder.Pop("PRIVMSG #channel hello".Length); 21 await Assert.That(_encoder.Pending()).IsEqualTo("\r\n"); 22 } 23 24 [Test] 25 public async Task TestPopReturned() 26 { 27 var line = new Line("PRIVMSG #channel hello"); 28 _encoder.Push(line); 29 _encoder.Push(line); 30 var lines = _encoder.Pop("PRIVMSG #channel hello\r\n".Length); 31 await Assert.That(lines).HasCount(1); 32 await Assert.That(lines[0]).IsEqualTo(line); 33 } 34 35 [Test] 36 public async Task PopNoneReturned() 37 { 38 var line = new Line("PRIVMSG #channel hello"); 39 _encoder.Push(line); 40 var lines = _encoder.Pop(1); 41 await Assert.That(lines).IsEmpty(); 42 } 43 44 [Test] 45 public async Task PopMultipleLines() 46 { 47 var line1 = new Line("PRIVMSG #channel1 hello"); 48 _encoder.Push(line1); 49 var line2 = new Line("PRIVMSG #channel2 hello"); 50 _encoder.Push(line2); 51 52 var lines = _encoder.Pop(_encoder.Pending().Length); 53 await Assert.That(lines).HasCount(2); 54 await Assert.That(_encoder.Pending()).IsEmpty(); 55 } 56 57 [Test] 58 public async Task Clear() 59 { 60 _encoder.Push(new Line("PRIVMSG #channel hello")); 61 _encoder.Clear(); 62 await Assert.That(_encoder.Pending()).IsEmpty(); 63 } 64 65 [Test] 66 public async Task EncodingIso8859() 67 { 68 var encoder = new IRCTokens.StatefulEncoder {Encoding = Encoding.Latin1}; 69 encoder.Push(new Line("PRIVMSG #channel :hello Ç")); 70 await Assert.That(encoder.PendingBytes).IsEquivalentTo(Encoding.Latin1.GetBytes("PRIVMSG #channel :hello Ç\r\n")); 71 } 72}