From 6a3bfd314d87bed96611d24440b0961a06bf7e91 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 20 Apr 2020 17:24:59 -0400 Subject: stateful in progress --- IrcTokens/Tests/StatefulDecoderTests.cs | 97 +++++++++++++++++++++++++++++++++ IrcTokens/Tests/StatefulEncoderTests.cs | 72 ++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 IrcTokens/Tests/StatefulDecoderTests.cs create mode 100644 IrcTokens/Tests/StatefulEncoderTests.cs (limited to 'IrcTokens/Tests') diff --git a/IrcTokens/Tests/StatefulDecoderTests.cs b/IrcTokens/Tests/StatefulDecoderTests.cs new file mode 100644 index 0000000..e0c2143 --- /dev/null +++ b/IrcTokens/Tests/StatefulDecoderTests.cs @@ -0,0 +1,97 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IrcTokens.Tests +{ + [TestClass] + public class StatefulDecoderTests + { + private StatefulDecoder _decoder; + + [TestInitialize] + public void TestInitialize() + { + _decoder = new StatefulDecoder(); + } + + [TestMethod] + public void TestPartial() + { + var lines = _decoder.Push("PRIVMSG "); + Assert.AreEqual(new List(), lines); + + lines = _decoder.Push("#channel hello\r\n"); + Assert.AreEqual(1, lines.Count); + + var line = new Line("PRIVMSG #channel hello"); + CollectionAssert.AreEqual(new List {line}, lines); + } + + [TestMethod] + public void TestMultiple() + { + _decoder.Push("PRIVMSG #channel1 hello\r\n"); + var lines = _decoder.Push("PRIVMSG #channel2 hello\r\n"); + Assert.AreEqual(2, lines.Count); + + var line1 = new Line("PRIVMSG #channel1 hello"); + var line2 = new Line("PRIVMSG #channel2 hello"); + Assert.AreEqual(line1, lines[0]); + Assert.AreEqual(line2, lines[1]); + } + + [TestMethod] + public void TestEncoding() + { + var iso8859 = Encoding.GetEncodings().Single(ei => ei.Name == "iso-8859-1"); + _decoder = new StatefulDecoder {Encoding = iso8859}; + var lines = _decoder.Push("PRIVMSG #channel :hello Č\r\n"); + var line = new Line("PRIVMSG #channel :hello Č"); + Assert.AreEqual(line, lines[0]); + } + + [TestMethod] + public void TestEncodingFallback() + { + var latin1 = Encoding.GetEncodings().Single(ei => ei.Name == "latin-1"); + _decoder = new StatefulDecoder {Fallback = latin1}; + var lines = _decoder.Push("PRIVMSG #channel hélló\r\n"); + Assert.AreEqual(1, lines.Count); + Assert.AreEqual(new Line("PRIVMSG #channel hélló"), lines[0]); + } + + [TestMethod] + public void TestEmpty() + { + var lines = _decoder.Push(string.Empty); + Assert.IsNull(lines); + } + + [TestMethod] + public void TestBufferUnfinished() + { + _decoder.Push("PRIVMSG #channel hello"); + var lines = _decoder.Push(string.Empty); + Assert.IsNull(lines); + } + + [TestMethod] + public void TestClear() + { + _decoder.Push("PRIVMSG "); + _decoder.Clear(); + Assert.AreEqual(string.Empty, _decoder.Pending); + } + + [TestMethod] + public void TestTagEncodingMismatch() + { + _decoder.Push("@asd=á "); + var lines = _decoder.Push("PRIVMSG #chan :á\r\n"); + Assert.AreEqual("á", lines[0].Params[0]); + Assert.AreEqual("á", lines[0].Tags["asd"]); + } + } +} diff --git a/IrcTokens/Tests/StatefulEncoderTests.cs b/IrcTokens/Tests/StatefulEncoderTests.cs new file mode 100644 index 0000000..4732573 --- /dev/null +++ b/IrcTokens/Tests/StatefulEncoderTests.cs @@ -0,0 +1,72 @@ +using System.Linq; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IrcTokens.Tests +{ + [TestClass] + public class StatefulEncoderTests + { + private StatefulEncoder _encoder; + + [TestInitialize] + public void TestInitialize() + { + _encoder = new StatefulEncoder(); + } + + [TestMethod] + public void TestPush() + { + var line = new Line("PRIVMSG #channel hello"); + _encoder.Push(line); + Assert.AreEqual("PRIVMSG #channel hello\r\n", _encoder.Pending); + } + + [TestMethod] + public void TestPopPartial() + { + var line = new Line("PRIVMSG #channel hello"); + _encoder.Push(line); + _encoder.Pop("PRIVMSG #channel hello".Length); + Assert.AreEqual("\r\n", _encoder.Pending); + } + + [TestMethod] + public void TestPopReturned() + { + var line = new Line("PRIVMSG #channel hello"); + _encoder.Push(line); + _encoder.Push(line); + var lines = _encoder.Pop("PRIVMSG #channel hello\r\n".Length); + Assert.AreEqual(1, lines.Count); + Assert.AreEqual(line, lines[0]); + } + + [TestMethod] + public void TestPopNoneReturned() + { + var line = new Line("PRIVMSG #channel hello"); + _encoder.Push(line); + var lines = _encoder.Pop(1); + Assert.AreEqual(0, lines.Count); + } + + [TestMethod] + public void TestClear() + { + _encoder.Push(new Line("PRIVMSG #channel hello")); + _encoder.Clear(); + Assert.AreEqual(string.Empty, _encoder.Pending); + } + + [TestMethod] + public void TestEncoding() + { + var iso88592 = Encoding.GetEncodings().Single(ei => ei.Name == "iso-8859-2"); + _encoder = new StatefulEncoder {Encoding = iso88592}; + _encoder.Push(new Line("PRIVMSG #channel :hello Č")); + Assert.AreEqual("PRIVMSG #channel :hello Č\r\n", _encoder.Pending); + } + } +} -- cgit 1.4.1