From 80afa2c0aec37b7f98cc22615417c36672e695da Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 28 Apr 2020 00:35:52 -0400 Subject: tidy up, work on stateful --- IrcTokens/Tests/Format.cs | 60 +++++++++++--------------------------- IrcTokens/Tests/Hostmask.cs | 2 +- IrcTokens/Tests/Parser.cs | 16 +++++----- IrcTokens/Tests/StatefulDecoder.cs | 12 ++++---- IrcTokens/Tests/StatefulEncoder.cs | 19 ++++++++++-- IrcTokens/Tests/Tokenization.cs | 12 ++++---- 6 files changed, 53 insertions(+), 68 deletions(-) (limited to 'IrcTokens/Tests') diff --git a/IrcTokens/Tests/Format.cs b/IrcTokens/Tests/Format.cs index 64f974a..8ef5344 100644 --- a/IrcTokens/Tests/Format.cs +++ b/IrcTokens/Tests/Format.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; +using System; using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace IrcTokens.Tests { @@ -13,8 +13,8 @@ namespace IrcTokens.Tests var line = new Line { Command = "PRIVMSG", - Params = new List { "#channel", "hello" }, - Tags = new Dictionary { { "id", "\\" + " " + ";" + "\r\n" } } + Params = new List {"#channel", "hello"}, + Tags = new Dictionary {{"id", "\\" + " " + ";" + "\r\n"}} }.Format(); Assert.AreEqual("@id=\\\\\\s\\:\\r\\n PRIVMSG #channel hello", line); @@ -23,11 +23,7 @@ namespace IrcTokens.Tests [TestMethod] public void TestMissingTag() { - var line = new Line - { - Command = "PRIVMSG", - Params = new List { "#channel", "hello" } - }.Format(); + var line = new Line {Command = "PRIVMSG", Params = new List {"#channel", "hello"}}.Format(); Assert.AreEqual("PRIVMSG #channel hello", line); } @@ -38,8 +34,8 @@ namespace IrcTokens.Tests var line = new Line { Command = "PRIVMSG", - Params = new List { "#channel", "hello" }, - Tags = new Dictionary { { "a", null } } + Params = new List {"#channel", "hello"}, + Tags = new Dictionary {{"a", null}} }.Format(); Assert.AreEqual("@a PRIVMSG #channel hello", line); @@ -51,8 +47,8 @@ namespace IrcTokens.Tests var line = new Line { Command = "PRIVMSG", - Params = new List { "#channel", "hello" }, - Tags = new Dictionary { { "a", "" } } + Params = new List {"#channel", "hello"}, + Tags = new Dictionary {{"a", ""}} }.Format(); Assert.AreEqual("@a PRIVMSG #channel hello", line); @@ -63,9 +59,7 @@ namespace IrcTokens.Tests { var line = new Line { - Command = "PRIVMSG", - Params = new List { "#channel", "hello" }, - Source = "nick!user@host" + Command = "PRIVMSG", Params = new List {"#channel", "hello"}, Source = "nick!user@host" }.Format(); Assert.AreEqual(":nick!user@host PRIVMSG #channel hello", line); @@ -74,25 +68,21 @@ namespace IrcTokens.Tests [TestMethod] public void TestCommandLowercase() { - var line = new Line { Command = "privmsg" }.Format(); + var line = new Line {Command = "privmsg"}.Format(); Assert.AreEqual("privmsg", line); } [TestMethod] public void TestCommandUppercase() { - var line = new Line { Command = "PRIVMSG" }.Format(); + var line = new Line {Command = "PRIVMSG"}.Format(); Assert.AreEqual("PRIVMSG", line); } [TestMethod] public void TestTrailingSpace() { - var line = new Line - { - Command = "PRIVMSG", - Params = new List { "#channel", "hello world" } - }.Format(); + var line = new Line {Command = "PRIVMSG", Params = new List {"#channel", "hello world"}}.Format(); Assert.AreEqual("PRIVMSG #channel :hello world", line); } @@ -100,11 +90,7 @@ namespace IrcTokens.Tests [TestMethod] public void TestTrailingNoSpace() { - var line = new Line - { - Command = "PRIVMSG", - Params = new List { "#channel", "helloworld" } - }.Format(); + var line = new Line {Command = "PRIVMSG", Params = new List {"#channel", "helloworld"}}.Format(); Assert.AreEqual("PRIVMSG #channel helloworld", line); } @@ -112,11 +98,7 @@ namespace IrcTokens.Tests [TestMethod] public void TestTrailingDoubleColon() { - var line = new Line - { - Command = "PRIVMSG", - Params = new List { "#channel", ":helloworld" } - }.Format(); + var line = new Line {Command = "PRIVMSG", Params = new List {"#channel", ":helloworld"}}.Format(); Assert.AreEqual("PRIVMSG #channel ::helloworld", line); } @@ -126,11 +108,7 @@ namespace IrcTokens.Tests { Assert.ThrowsException(() => { - new Line - { - Command = "USER", - Params = new List { "user", "0 *", "real name" } - }.Format(); + new Line {Command = "USER", Params = new List {"user", "0 *", "real name"}}.Format(); }); } @@ -139,11 +117,7 @@ namespace IrcTokens.Tests { Assert.ThrowsException(() => { - new Line - { - Command = "PRIVMSG", - Params = new List { ":#channel", "hello" } - }.Format(); + new Line {Command = "PRIVMSG", Params = new List {":#channel", "hello"}}.Format(); }); } } diff --git a/IrcTokens/Tests/Hostmask.cs b/IrcTokens/Tests/Hostmask.cs index 51bc182..6a5cf65 100644 --- a/IrcTokens/Tests/Hostmask.cs +++ b/IrcTokens/Tests/Hostmask.cs @@ -44,7 +44,7 @@ namespace IrcTokens.Tests [TestMethod] public void TestHostmaskFromLine() { - var line = new Line(":nick!user@host PRIVMSG #channel hello"); + var line = new Line(":nick!user@host PRIVMSG #channel hello"); var hostmask = new IrcTokens.Hostmask("nick!user@host"); Assert.AreEqual(hostmask.ToString(), line.Hostmask.ToString()); Assert.AreEqual("nick", line.Hostmask.NickName); diff --git a/IrcTokens/Tests/Parser.cs b/IrcTokens/Tests/Parser.cs index df70b92..ed4e406 100644 --- a/IrcTokens/Tests/Parser.cs +++ b/IrcTokens/Tests/Parser.cs @@ -1,8 +1,8 @@ -using IrcTokens.Tests.Data; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Collections.Generic; +using System.Collections.Generic; using System.Globalization; using System.IO; +using IrcTokens.Tests.Data; +using Microsoft.VisualStudio.TestTools.UnitTesting; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; @@ -26,13 +26,14 @@ namespace IrcTokens.Tests foreach (var test in LoadYaml("Tests/Data/msg-split.yaml").Tests) { var tokens = new Line(test.Input); - var atoms = test.Atoms; + var atoms = test.Atoms; Assert.AreEqual(atoms.Verb.ToUpper(CultureInfo.InvariantCulture), tokens.Command, $"command failed on: '{test.Input}'"); Assert.AreEqual(atoms.Source, tokens.Source, $"source failed on: '{test.Input}'"); CollectionAssert.AreEqual(atoms.Tags, tokens.Tags, $"tags failed on: '{test.Input}'"); - CollectionAssert.AreEqual(atoms.Params ?? new List(), tokens.Params, $"params failed on: '{test.Input}'"); + CollectionAssert.AreEqual(atoms.Params ?? new List(), tokens.Params, + $"params failed on: '{test.Input}'"); } } @@ -44,10 +45,7 @@ namespace IrcTokens.Tests var atoms = test.Atoms; var line = new Line { - Command = atoms.Verb, - Params = atoms.Params, - Source = atoms.Source, - Tags = atoms.Tags + Command = atoms.Verb, Params = atoms.Params, Source = atoms.Source, Tags = atoms.Tags }.Format(); Assert.IsTrue(test.Matches.Contains(line), test.Description); diff --git a/IrcTokens/Tests/StatefulDecoder.cs b/IrcTokens/Tests/StatefulDecoder.cs index 209a6cf..da4009e 100644 --- a/IrcTokens/Tests/StatefulDecoder.cs +++ b/IrcTokens/Tests/StatefulDecoder.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Collections.Generic; +using System.Collections.Generic; using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace IrcTokens.Tests { @@ -25,7 +25,7 @@ namespace IrcTokens.Tests Assert.AreEqual(1, lines.Count); var line = new Line("PRIVMSG #channel hello"); - CollectionAssert.AreEqual(new List { line }, lines); + CollectionAssert.AreEqual(new List {line}, lines); } [TestMethod] @@ -44,9 +44,9 @@ namespace IrcTokens.Tests public void TestEncoding() { var iso8859 = Encoding.GetEncoding("iso-8859-1"); - _decoder = new IrcTokens.StatefulDecoder { Encoding = iso8859 }; + _decoder = new IrcTokens.StatefulDecoder {Encoding = iso8859}; var lines = _decoder.Push(iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n")); - var line = new Line("PRIVMSG #channel :hello Ç"); + var line = new Line("PRIVMSG #channel :hello Ç"); Assert.IsTrue(line.Equals(lines[0])); } @@ -54,7 +54,7 @@ namespace IrcTokens.Tests public void TestEncodingFallback() { var latin1 = Encoding.GetEncoding("iso-8859-1"); - _decoder = new IrcTokens.StatefulDecoder { Encoding = null, Fallback = latin1 }; + _decoder = new IrcTokens.StatefulDecoder {Encoding = null, Fallback = latin1}; var lines = _decoder.Push(latin1.GetBytes("PRIVMSG #channel hélló\r\n")); Assert.AreEqual(1, lines.Count); Assert.IsTrue(new Line("PRIVMSG #channel hélló").Equals(lines[0])); diff --git a/IrcTokens/Tests/StatefulEncoder.cs b/IrcTokens/Tests/StatefulEncoder.cs index e3ed70d..f2cd6c4 100644 --- a/IrcTokens/Tests/StatefulEncoder.cs +++ b/IrcTokens/Tests/StatefulEncoder.cs @@ -1,5 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Text; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace IrcTokens.Tests { @@ -51,6 +51,19 @@ namespace IrcTokens.Tests Assert.AreEqual(0, lines.Count); } + [TestMethod] + public void TestPopMultipleLines() + { + var line1 = new Line("PRIVMSG #channel1 hello"); + _encoder.Push(line1); + var line2 = new Line("PRIVMSG #channel2 hello"); + _encoder.Push(line2); + + var lines = _encoder.Pop(_encoder.Pending().Length); + Assert.AreEqual(2, lines.Count); + Assert.AreEqual(string.Empty, _encoder.Pending()); + } + [TestMethod] public void TestClear() { @@ -63,7 +76,7 @@ namespace IrcTokens.Tests public void TestEncoding() { var iso8859 = Encoding.GetEncoding("iso-8859-1"); - _encoder = new IrcTokens.StatefulEncoder { Encoding = iso8859 }; + _encoder = new IrcTokens.StatefulEncoder {Encoding = iso8859}; _encoder.Push(new Line("PRIVMSG #channel :hello Ç")); CollectionAssert.AreEqual(iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n"), _encoder.PendingBytes); } diff --git a/IrcTokens/Tests/Tokenization.cs b/IrcTokens/Tests/Tokenization.cs index c4970ed..7e2139d 100644 --- a/IrcTokens/Tests/Tokenization.cs +++ b/IrcTokens/Tests/Tokenization.cs @@ -1,5 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Collections.Generic; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace IrcTokens.Tests { @@ -87,14 +87,14 @@ namespace IrcTokens.Tests public void TestParamsTrailing() { var line = new Line("PRIVMSG #channel :hello world"); - CollectionAssert.AreEqual(new List { "#channel", "hello world" }, line.Params); + CollectionAssert.AreEqual(new List {"#channel", "hello world"}, line.Params); } [TestMethod] public void TestParamsOnlyTrailing() { var line = new Line("PRIVMSG :hello world"); - CollectionAssert.AreEqual(new List { "hello world" }, line.Params); + CollectionAssert.AreEqual(new List {"hello world"}, line.Params); } [TestMethod] @@ -109,10 +109,10 @@ namespace IrcTokens.Tests public void TestAllTokens() { var line = new Line("@id=123 :nick!user@host PRIVMSG #channel :hello world"); - CollectionAssert.AreEqual(new Dictionary { { "id", "123" } }, line.Tags); + CollectionAssert.AreEqual(new Dictionary {{"id", "123"}}, line.Tags); Assert.AreEqual("nick!user@host", line.Source); Assert.AreEqual("PRIVMSG", line.Command); - CollectionAssert.AreEqual(new List { "#channel", "hello world" }, line.Params); + CollectionAssert.AreEqual(new List {"#channel", "hello world"}, line.Params); } } } -- cgit 1.4.1