From 933a4f85604e21445c9bac8272d64cf3e6f65e00 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 22 Apr 2020 16:28:51 -0400 Subject: rename to IrcSharp also tidy up formatting with vs tools --- IrcTokens/Hostmask.cs | 30 +++++-- IrcTokens/Line.cs | 47 ++++++++-- IrcTokens/Protocol.cs | 2 + IrcTokens/StatefulDecoder.cs | 8 +- IrcTokens/StatefulEncoder.cs | 4 + IrcTokens/Tests/Format.cs | 150 ++++++++++++++++++++++++++++++++ IrcTokens/Tests/FormatTests.cs | 150 -------------------------------- IrcTokens/Tests/Hostmask.cs | 64 ++++++++++++++ IrcTokens/Tests/HostmaskTests.cs | 64 -------------- IrcTokens/Tests/Parser.cs | 57 ++++++++++++ IrcTokens/Tests/ParserTests.cs | 57 ------------ IrcTokens/Tests/StatefulDecoder.cs | 86 ++++++++++++++++++ IrcTokens/Tests/StatefulDecoderTests.cs | 87 ------------------ IrcTokens/Tests/StatefulEncoder.cs | 71 +++++++++++++++ IrcTokens/Tests/StatefulEncoderTests.cs | 72 --------------- IrcTokens/Tests/Tokenization.cs | 118 +++++++++++++++++++++++++ IrcTokens/Tests/TokenizationTests.cs | 118 ------------------------- 17 files changed, 621 insertions(+), 564 deletions(-) create mode 100644 IrcTokens/Tests/Format.cs delete mode 100644 IrcTokens/Tests/FormatTests.cs create mode 100644 IrcTokens/Tests/Hostmask.cs delete mode 100644 IrcTokens/Tests/HostmaskTests.cs create mode 100644 IrcTokens/Tests/Parser.cs delete mode 100644 IrcTokens/Tests/ParserTests.cs create mode 100644 IrcTokens/Tests/StatefulDecoder.cs delete mode 100644 IrcTokens/Tests/StatefulDecoderTests.cs create mode 100644 IrcTokens/Tests/StatefulEncoder.cs delete mode 100644 IrcTokens/Tests/StatefulEncoderTests.cs create mode 100644 IrcTokens/Tests/Tokenization.cs delete mode 100644 IrcTokens/Tests/TokenizationTests.cs (limited to 'IrcTokens') diff --git a/IrcTokens/Hostmask.cs b/IrcTokens/Hostmask.cs index 0b07f80..01fe7d5 100644 --- a/IrcTokens/Hostmask.cs +++ b/IrcTokens/Hostmask.cs @@ -5,29 +5,45 @@ namespace IrcTokens /// /// Represents the three parts of a hostmask. Parse with the constructor. /// - public class Hostmask + public class Hostmask : IEquatable { public string NickName { get; set; } public string UserName { get; set; } public string HostName { get; set; } - public override string ToString() => _source; + public override string ToString() + { + return _source; + } - public override int GetHashCode() => _source.GetHashCode(StringComparison.Ordinal); + public override int GetHashCode() + { + return _source.GetHashCode(StringComparison.Ordinal); + } - public override bool Equals(object obj) + public bool Equals(Hostmask other) { - if (obj == null || GetType() != obj.GetType()) + if (other == null) + { return false; + } - return _source == ((Hostmask) obj)._source; + return _source == other._source; + } + + public override bool Equals(object obj) + { + return Equals(obj as Hostmask); } private readonly string _source; public Hostmask(string source) { - if (source == null) return; + if (source == null) + { + return; + } _source = source; diff --git a/IrcTokens/Line.cs b/IrcTokens/Line.cs index 9056097..24efe4a 100644 --- a/IrcTokens/Line.cs +++ b/IrcTokens/Line.cs @@ -8,7 +8,7 @@ namespace IrcTokens /// /// Tools to represent, parse, and format IRC lines /// - public class Line + public class Line : IEquatable { public Dictionary Tags { get; set; } public string Source { get; set; } @@ -16,32 +16,52 @@ namespace IrcTokens public List Params { get; set; } private Hostmask _hostmask; - private readonly string _rawLine; public override string ToString() { var vars = new List(); if (Command != null) + { vars.Add($"command={Command}"); + } + if (Source != null) + { vars.Add($"source={Source}"); + } + if (Params != null && Params.Any()) + { vars.Add($"params=[{string.Join(",", Params)}]"); + } + if (Tags != null && Tags.Any()) + { vars.Add($"tags=[{string.Join(";", Tags.Select(kvp => $"{kvp.Key}={kvp.Value}"))}]"); + } return $"Line({string.Join(", ", vars)})"; } - public override int GetHashCode() => Format().GetHashCode(StringComparison.Ordinal); + public override int GetHashCode() + { + return Format().GetHashCode(StringComparison.Ordinal); + } - public override bool Equals(object obj) + public bool Equals(Line other) { - if (obj == null || GetType() != obj.GetType()) + if (other == null) + { return false; + } - return Format() == ((Line) obj).Format(); + return Format() == other.Format(); + } + + public override bool Equals(object obj) + { + return Equals(obj as Line); } public Hostmask Hostmask => @@ -56,9 +76,10 @@ namespace IrcTokens public Line(string line) { if (string.IsNullOrWhiteSpace(line)) + { throw new ArgumentNullException(nameof(line)); + } - _rawLine = line; string[] split; if (line.StartsWith('@')) @@ -97,7 +118,7 @@ namespace IrcTokens Params = line.Contains(' ', StringComparison.Ordinal) ? line.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList() - : new List {line}; + : new List { line }; if (Params[0].StartsWith(':')) { @@ -135,7 +156,9 @@ namespace IrcTokens } if (Source != null) + { outs.Add($":{Source}"); + } outs.Add(Command); @@ -147,14 +170,22 @@ namespace IrcTokens foreach (var p in Params) { if (p.Contains(' ', StringComparison.Ordinal)) + { throw new ArgumentException(@"non-last parameters cannot have spaces", p); + } + if (p.StartsWith(':')) + { throw new ArgumentException(@"non-last parameters cannot start with colon", p); + } } outs.AddRange(Params); if (string.IsNullOrWhiteSpace(last) || last.Contains(' ', StringComparison.Ordinal) || last.StartsWith(':')) + { last = $":{last}"; + } + outs.Add(last); } diff --git a/IrcTokens/Protocol.cs b/IrcTokens/Protocol.cs index 3769ea3..6ddb079 100644 --- a/IrcTokens/Protocol.cs +++ b/IrcTokens/Protocol.cs @@ -50,7 +50,9 @@ namespace IrcTokens } } else + { unescaped.Append(current); + } } return unescaped.ToString(); diff --git a/IrcTokens/StatefulDecoder.cs b/IrcTokens/StatefulDecoder.cs index 2304431..47106d9 100644 --- a/IrcTokens/StatefulDecoder.cs +++ b/IrcTokens/StatefulDecoder.cs @@ -18,8 +18,10 @@ namespace IrcTokens set { if (value != null) + { _encoding = Encoding.GetEncoding(value.CodePage, EncoderFallback.ExceptionFallback, DecoderFallback.ReplacementFallback); + } } } @@ -30,8 +32,10 @@ namespace IrcTokens set { if (value != null) - _encoding = Encoding.GetEncoding(value.CodePage, EncoderFallback.ReplacementFallback, + { + _fallback = Encoding.GetEncoding(value.CodePage, EncoderFallback.ReplacementFallback, DecoderFallback.ReplacementFallback); + } } } @@ -55,7 +59,9 @@ namespace IrcTokens public List Push(byte[] data) { if (data == null || data.Length == 0) + { return null; + } _buffer = _buffer.Concat(data).ToArray(); diff --git a/IrcTokens/StatefulEncoder.cs b/IrcTokens/StatefulEncoder.cs index 17295eb..c036400 100644 --- a/IrcTokens/StatefulEncoder.cs +++ b/IrcTokens/StatefulEncoder.cs @@ -16,8 +16,10 @@ namespace IrcTokens set { if (value != null) + { _encoding = Encoding.GetEncoding(value.CodePage, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback); + } } } @@ -52,7 +54,9 @@ namespace IrcTokens public void Push(Line line) { if (line == null) + { throw new ArgumentNullException(nameof(line)); + } PendingBytes = PendingBytes.Concat(Encoding.GetBytes($"{line.Format()}\r\n")).ToArray(); _bufferedLines.Enqueue(line); diff --git a/IrcTokens/Tests/Format.cs b/IrcTokens/Tests/Format.cs new file mode 100644 index 0000000..64f974a --- /dev/null +++ b/IrcTokens/Tests/Format.cs @@ -0,0 +1,150 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; + +namespace IrcTokens.Tests +{ + [TestClass] + public class Format + { + [TestMethod] + public void TestTags() + { + var line = new Line + { + Command = "PRIVMSG", + Params = new List { "#channel", "hello" }, + Tags = new Dictionary { { "id", "\\" + " " + ";" + "\r\n" } } + }.Format(); + + Assert.AreEqual("@id=\\\\\\s\\:\\r\\n PRIVMSG #channel hello", line); + } + + [TestMethod] + public void TestMissingTag() + { + var line = new Line + { + Command = "PRIVMSG", + Params = new List { "#channel", "hello" } + }.Format(); + + Assert.AreEqual("PRIVMSG #channel hello", line); + } + + [TestMethod] + public void TestNullTag() + { + var line = new Line + { + Command = "PRIVMSG", + Params = new List { "#channel", "hello" }, + Tags = new Dictionary { { "a", null } } + }.Format(); + + Assert.AreEqual("@a PRIVMSG #channel hello", line); + } + + [TestMethod] + public void TestEmptyTag() + { + var line = new Line + { + Command = "PRIVMSG", + Params = new List { "#channel", "hello" }, + Tags = new Dictionary { { "a", "" } } + }.Format(); + + Assert.AreEqual("@a PRIVMSG #channel hello", line); + } + + [TestMethod] + public void TestSource() + { + var line = new Line + { + Command = "PRIVMSG", + Params = new List { "#channel", "hello" }, + Source = "nick!user@host" + }.Format(); + + Assert.AreEqual(":nick!user@host PRIVMSG #channel hello", line); + } + + [TestMethod] + public void TestCommandLowercase() + { + var line = new Line { Command = "privmsg" }.Format(); + Assert.AreEqual("privmsg", line); + } + + [TestMethod] + public void TestCommandUppercase() + { + 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(); + + Assert.AreEqual("PRIVMSG #channel :hello world", line); + } + + [TestMethod] + public void TestTrailingNoSpace() + { + var line = new Line + { + Command = "PRIVMSG", + Params = new List { "#channel", "helloworld" } + }.Format(); + + Assert.AreEqual("PRIVMSG #channel helloworld", line); + } + + [TestMethod] + public void TestTrailingDoubleColon() + { + var line = new Line + { + Command = "PRIVMSG", + Params = new List { "#channel", ":helloworld" } + }.Format(); + + Assert.AreEqual("PRIVMSG #channel ::helloworld", line); + } + + [TestMethod] + public void TestInvalidNonLastSpace() + { + Assert.ThrowsException(() => + { + new Line + { + Command = "USER", + Params = new List { "user", "0 *", "real name" } + }.Format(); + }); + } + + [TestMethod] + public void TestInvalidNonLastColon() + { + Assert.ThrowsException(() => + { + new Line + { + Command = "PRIVMSG", + Params = new List { ":#channel", "hello" } + }.Format(); + }); + } + } +} diff --git a/IrcTokens/Tests/FormatTests.cs b/IrcTokens/Tests/FormatTests.cs deleted file mode 100644 index a804c1d..0000000 --- a/IrcTokens/Tests/FormatTests.cs +++ /dev/null @@ -1,150 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using System.Collections.Generic; - -namespace IrcTokens.Tests -{ - [TestClass] - public class FormatTests - { - [TestMethod] - public void TestTags() - { - var line = new Line - { - Command = "PRIVMSG", - Params = new List {"#channel", "hello"}, - Tags = new Dictionary {{"id", "\\" + " " + ";" + "\r\n"}} - }.Format(); - - Assert.AreEqual("@id=\\\\\\s\\:\\r\\n PRIVMSG #channel hello", line); - } - - [TestMethod] - public void TestMissingTag() - { - var line = new Line - { - Command = "PRIVMSG", - Params = new List {"#channel", "hello"} - }.Format(); - - Assert.AreEqual("PRIVMSG #channel hello", line); - } - - [TestMethod] - public void TestNullTag() - { - var line = new Line - { - Command = "PRIVMSG", - Params = new List {"#channel", "hello"}, - Tags = new Dictionary {{"a", null}} - }.Format(); - - Assert.AreEqual("@a PRIVMSG #channel hello", line); - } - - [TestMethod] - public void TestEmptyTag() - { - var line = new Line - { - Command = "PRIVMSG", - Params = new List {"#channel", "hello"}, - Tags = new Dictionary {{"a", ""}} - }.Format(); - - Assert.AreEqual("@a PRIVMSG #channel hello", line); - } - - [TestMethod] - public void TestSource() - { - var line = new Line - { - Command = "PRIVMSG", - Params = new List {"#channel", "hello"}, - Source = "nick!user@host" - }.Format(); - - Assert.AreEqual(":nick!user@host PRIVMSG #channel hello", line); - } - - [TestMethod] - public void TestCommandLowercase() - { - var line = new Line {Command = "privmsg"}.Format(); - Assert.AreEqual("privmsg", line); - } - - [TestMethod] - public void TestCommandUppercase() - { - 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(); - - Assert.AreEqual("PRIVMSG #channel :hello world", line); - } - - [TestMethod] - public void TestTrailingNoSpace() - { - var line = new Line - { - Command = "PRIVMSG", - Params = new List {"#channel", "helloworld"} - }.Format(); - - Assert.AreEqual("PRIVMSG #channel helloworld", line); - } - - [TestMethod] - public void TestTrailingDoubleColon() - { - var line = new Line - { - Command = "PRIVMSG", - Params = new List {"#channel", ":helloworld"} - }.Format(); - - Assert.AreEqual("PRIVMSG #channel ::helloworld", line); - } - - [TestMethod] - public void TestInvalidNonLastSpace() - { - Assert.ThrowsException(() => - { - new Line - { - Command = "USER", - Params = new List {"user", "0 *", "real name"} - }.Format(); - }); - } - - [TestMethod] - public void TestInvalidNonLastColon() - { - Assert.ThrowsException(() => - { - new Line - { - Command = "PRIVMSG", - Params = new List {":#channel", "hello"} - }.Format(); - }); - } - } -} diff --git a/IrcTokens/Tests/Hostmask.cs b/IrcTokens/Tests/Hostmask.cs new file mode 100644 index 0000000..51bc182 --- /dev/null +++ b/IrcTokens/Tests/Hostmask.cs @@ -0,0 +1,64 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IrcTokens.Tests +{ + [TestClass] + public class Hostmask + { + [TestMethod] + public void TestHostmask() + { + var hostmask = new IrcTokens.Hostmask("nick!user@host"); + Assert.AreEqual("nick", hostmask.NickName); + Assert.AreEqual("user", hostmask.UserName); + Assert.AreEqual("host", hostmask.HostName); + } + + [TestMethod] + public void TestNoHostName() + { + var hostmask = new IrcTokens.Hostmask("nick!user"); + Assert.AreEqual("nick", hostmask.NickName); + Assert.AreEqual("user", hostmask.UserName); + Assert.IsNull(hostmask.HostName); + } + + [TestMethod] + public void TestNoUserName() + { + var hostmask = new IrcTokens.Hostmask("nick@host"); + Assert.AreEqual("nick", hostmask.NickName); + Assert.IsNull(hostmask.UserName); + Assert.AreEqual("host", hostmask.HostName); + } + + [TestMethod] + public void TestOnlyNickName() + { + var hostmask = new IrcTokens.Hostmask("nick"); + Assert.AreEqual("nick", hostmask.NickName); + Assert.IsNull(hostmask.UserName); + Assert.IsNull(hostmask.HostName); + } + + [TestMethod] + public void TestHostmaskFromLine() + { + 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); + Assert.AreEqual("user", line.Hostmask.UserName); + Assert.AreEqual("host", line.Hostmask.HostName); + } + + [TestMethod] + public void TestEmptyHostmaskFromLine() + { + var line = new Line("PRIVMSG #channel hello"); + Assert.IsNull(line.Hostmask.HostName); + Assert.IsNull(line.Hostmask.UserName); + Assert.IsNull(line.Hostmask.NickName); + } + } +} diff --git a/IrcTokens/Tests/HostmaskTests.cs b/IrcTokens/Tests/HostmaskTests.cs deleted file mode 100644 index 78b8a54..0000000 --- a/IrcTokens/Tests/HostmaskTests.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace IrcTokens.Tests -{ - [TestClass] - public class HostmaskTests - { - [TestMethod] - public void TestHostmask() - { - var hostmask = new Hostmask("nick!user@host"); - Assert.AreEqual("nick", hostmask.NickName); - Assert.AreEqual("user", hostmask.UserName); - Assert.AreEqual("host", hostmask.HostName); - } - - [TestMethod] - public void TestNoHostName() - { - var hostmask = new Hostmask("nick!user"); - Assert.AreEqual("nick", hostmask.NickName); - Assert.AreEqual("user", hostmask.UserName); - Assert.IsNull(hostmask.HostName); - } - - [TestMethod] - public void TestNoUserName() - { - var hostmask = new Hostmask("nick@host"); - Assert.AreEqual("nick", hostmask.NickName); - Assert.IsNull(hostmask.UserName); - Assert.AreEqual("host", hostmask.HostName); - } - - [TestMethod] - public void TestOnlyNickName() - { - var hostmask = new Hostmask("nick"); - Assert.AreEqual("nick", hostmask.NickName); - Assert.IsNull(hostmask.UserName); - Assert.IsNull(hostmask.HostName); - } - - [TestMethod] - public void TestHostmaskFromLine() - { - var line = new Line(":nick!user@host PRIVMSG #channel hello"); - var hostmask = new Hostmask("nick!user@host"); - Assert.AreEqual(hostmask.ToString(), line.Hostmask.ToString()); - Assert.AreEqual("nick", line.Hostmask.NickName); - Assert.AreEqual("user", line.Hostmask.UserName); - Assert.AreEqual("host", line.Hostmask.HostName); - } - - [TestMethod] - public void TestEmptyHostmaskFromLine() - { - var line = new Line("PRIVMSG #channel hello"); - Assert.IsNull(line.Hostmask.HostName); - Assert.IsNull(line.Hostmask.UserName); - Assert.IsNull(line.Hostmask.NickName); - } - } -} diff --git a/IrcTokens/Tests/Parser.cs b/IrcTokens/Tests/Parser.cs new file mode 100644 index 0000000..df70b92 --- /dev/null +++ b/IrcTokens/Tests/Parser.cs @@ -0,0 +1,57 @@ +using IrcTokens.Tests.Data; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace IrcTokens.Tests +{ + [TestClass] + public class Parser + { + private static T LoadYaml(string path) + { + var deserializer = new DeserializerBuilder() + .WithNamingConvention(CamelCaseNamingConvention.Instance) + .Build(); + + return deserializer.Deserialize(File.ReadAllText(path)); + } + + [TestMethod] + public void TestSplit() + { + foreach (var test in LoadYaml("Tests/Data/msg-split.yaml").Tests) + { + var tokens = new Line(test.Input); + 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}'"); + } + } + + [TestMethod] + public void TestJoin() + { + foreach (var test in LoadYaml("Tests/Data/msg-join.yaml").Tests) + { + var atoms = test.Atoms; + var line = new Line + { + 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/ParserTests.cs b/IrcTokens/Tests/ParserTests.cs deleted file mode 100644 index ad734cf..0000000 --- a/IrcTokens/Tests/ParserTests.cs +++ /dev/null @@ -1,57 +0,0 @@ -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; - -namespace IrcTokens.Tests -{ - [TestClass] - public class ParserTests - { - private static T LoadYaml(string path) - { - var deserializer = new DeserializerBuilder() - .WithNamingConvention(CamelCaseNamingConvention.Instance) - .Build(); - - return deserializer.Deserialize(File.ReadAllText(path)); - } - - [TestMethod] - public void TestSplit() - { - foreach (var test in LoadYaml("Tests/Data/msg-split.yaml").Tests) - { - var tokens = new Line(test.Input); - 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}'"); - } - } - - [TestMethod] - public void TestJoin() - { - foreach (var test in LoadYaml("Tests/Data/msg-join.yaml").Tests) - { - var atoms = test.Atoms; - var line = new Line - { - Command = atoms.Verb, - Params = atoms.Params, - Source = atoms.Source ?? null, - Tags = atoms.Tags - }.Format(); - - Assert.IsTrue(test.Matches.Contains(line), test.Description); - } - } - } -} diff --git a/IrcTokens/Tests/StatefulDecoder.cs b/IrcTokens/Tests/StatefulDecoder.cs new file mode 100644 index 0000000..209a6cf --- /dev/null +++ b/IrcTokens/Tests/StatefulDecoder.cs @@ -0,0 +1,86 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using System.Text; + +namespace IrcTokens.Tests +{ + [TestClass] + public class StatefulDecoder + { + private IrcTokens.StatefulDecoder _decoder; + + [TestInitialize] + public void TestInitialize() + { + _decoder = new IrcTokens.StatefulDecoder(); + } + + [TestMethod] + public void TestPartial() + { + var lines = _decoder.Push("PRIVMSG "); + Assert.AreEqual(0, lines.Count); + + 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() + { + var lines = _decoder.Push("PRIVMSG #channel1 hello\r\nPRIVMSG #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.GetEncoding("iso-8859-1"); + _decoder = new IrcTokens.StatefulDecoder { Encoding = iso8859 }; + var lines = _decoder.Push(iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n")); + var line = new Line("PRIVMSG #channel :hello Ç"); + Assert.IsTrue(line.Equals(lines[0])); + } + + [TestMethod] + public void TestEncodingFallback() + { + var latin1 = Encoding.GetEncoding("iso-8859-1"); + _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])); + } + + [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); + } + } +} diff --git a/IrcTokens/Tests/StatefulDecoderTests.cs b/IrcTokens/Tests/StatefulDecoderTests.cs deleted file mode 100644 index 3e6a078..0000000 --- a/IrcTokens/Tests/StatefulDecoderTests.cs +++ /dev/null @@ -1,87 +0,0 @@ -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(0, lines.Count); - - 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() - { - var lines = _decoder.Push("PRIVMSG #channel1 hello\r\nPRIVMSG #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.GetEncoding("iso-8859-1"); - _decoder = new StatefulDecoder {Encoding = iso8859}; - var lines = _decoder.Push(iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n")); - var line = new Line("PRIVMSG #channel :hello Ç"); - Assert.IsTrue(line.Equals(lines[0])); - } - - [TestMethod] - public void TestEncodingFallback() - { - var latin1 = Encoding.GetEncoding("iso-8859-1"); - _decoder = new 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])); - } - - [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); - } - } -} diff --git a/IrcTokens/Tests/StatefulEncoder.cs b/IrcTokens/Tests/StatefulEncoder.cs new file mode 100644 index 0000000..e3ed70d --- /dev/null +++ b/IrcTokens/Tests/StatefulEncoder.cs @@ -0,0 +1,71 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Text; + +namespace IrcTokens.Tests +{ + [TestClass] + public class StatefulEncoder + { + private IrcTokens.StatefulEncoder _encoder; + + [TestInitialize] + public void TestInitialize() + { + _encoder = new IrcTokens.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 iso8859 = Encoding.GetEncoding("iso-8859-1"); + _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/StatefulEncoderTests.cs b/IrcTokens/Tests/StatefulEncoderTests.cs deleted file mode 100644 index 477b38d..0000000 --- a/IrcTokens/Tests/StatefulEncoderTests.cs +++ /dev/null @@ -1,72 +0,0 @@ -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 iso8859 = Encoding.GetEncoding("iso-8859-1"); - _encoder = new 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 new file mode 100644 index 0000000..c4970ed --- /dev/null +++ b/IrcTokens/Tests/Tokenization.cs @@ -0,0 +1,118 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; + +namespace IrcTokens.Tests +{ + [TestClass] + public class Tokenization + { + [TestMethod] + public void TestTagsMissing() + { + var line = new Line("PRIVMSG #channel"); + Assert.IsNull(line.Tags); + } + + [TestMethod] + public void TestTagsMissingValue() + { + var line = new Line("@id= PRIVMSG #channel"); + Assert.AreEqual(string.Empty, line.Tags["id"]); + } + + [TestMethod] + public void TestTagsMissingEqual() + { + var line = new Line("@id PRIVMSG #channel"); + Assert.AreEqual(string.Empty, line.Tags["id"]); + } + + [TestMethod] + public void TestTagsUnescape() + { + var line = new Line(@"@id=1\\\:\r\n\s2 PRIVMSG #channel"); + Assert.AreEqual("1\\;\r\n 2", line.Tags["id"]); + } + + [TestMethod] + public void TestTagsOverlap() + { + var line = new Line(@"@id=1\\\s\\s PRIVMSG #channel"); + Assert.AreEqual("1\\ \\s", line.Tags["id"]); + } + + [TestMethod] + public void TestTagsLoneEndSlash() + { + var line = new Line("@id=1\\ PRIVMSG #channel"); + Assert.AreEqual("1", line.Tags["id"]); + } + + [TestMethod] + public void TestSourceWithoutTags() + { + var line = new Line(":nick!user@host PRIVMSG #channel"); + Assert.AreEqual("nick!user@host", line.Source); + } + + [TestMethod] + public void TestSourceWithTags() + { + var line = new Line("@id=123 :nick!user@host PRIVMSG #channel"); + Assert.AreEqual("nick!user@host", line.Source); + } + + [TestMethod] + public void TestSourceMissingWithoutTags() + { + var line = new Line("PRIVMSG #channel"); + Assert.IsNull(line.Source); + } + + [TestMethod] + public void TestSourceMissingWithTags() + { + var line = new Line("@id=123 PRIVMSG #channel"); + Assert.IsNull(line.Source); + } + + [TestMethod] + public void TestCommand() + { + var line = new Line("privmsg #channel"); + Assert.AreEqual("PRIVMSG", line.Command); + } + + [TestMethod] + public void TestParamsTrailing() + { + var line = new Line("PRIVMSG #channel :hello world"); + 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); + } + + [TestMethod] + public void TestParamsMissing() + { + var line = new Line("PRIVMSG"); + Assert.AreEqual("PRIVMSG", line.Command); + CollectionAssert.AreEqual(new List(), line.Params); + } + + [TestMethod] + public void TestAllTokens() + { + var line = new Line("@id=123 :nick!user@host PRIVMSG #channel :hello world"); + 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); + } + } +} diff --git a/IrcTokens/Tests/TokenizationTests.cs b/IrcTokens/Tests/TokenizationTests.cs deleted file mode 100644 index 6d8a69d..0000000 --- a/IrcTokens/Tests/TokenizationTests.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System.Collections.Generic; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace IrcTokens.Tests -{ - [TestClass] - public class TokenizationTests - { - [TestMethod] - public void TestTagsMissing() - { - var line = new Line("PRIVMSG #channel"); - Assert.IsNull(line.Tags); - } - - [TestMethod] - public void TestTagsMissingValue() - { - var line = new Line("@id= PRIVMSG #channel"); - Assert.AreEqual(string.Empty, line.Tags["id"]); - } - - [TestMethod] - public void TestTagsMissingEqual() - { - var line = new Line("@id PRIVMSG #channel"); - Assert.AreEqual(string.Empty, line.Tags["id"]); - } - - [TestMethod] - public void TestTagsUnescape() - { - var line = new Line(@"@id=1\\\:\r\n\s2 PRIVMSG #channel"); - Assert.AreEqual("1\\;\r\n 2", line.Tags["id"]); - } - - [TestMethod] - public void TestTagsOverlap() - { - var line = new Line(@"@id=1\\\s\\s PRIVMSG #channel"); - Assert.AreEqual("1\\ \\s", line.Tags["id"]); - } - - [TestMethod] - public void TestTagsLoneEndSlash() - { - var line = new Line("@id=1\\ PRIVMSG #channel"); - Assert.AreEqual("1", line.Tags["id"]); - } - - [TestMethod] - public void TestSourceWithoutTags() - { - var line = new Line(":nick!user@host PRIVMSG #channel"); - Assert.AreEqual("nick!user@host", line.Source); - } - - [TestMethod] - public void TestSourceWithTags() - { - var line = new Line("@id=123 :nick!user@host PRIVMSG #channel"); - Assert.AreEqual("nick!user@host", line.Source); - } - - [TestMethod] - public void TestSourceMissingWithoutTags() - { - var line = new Line("PRIVMSG #channel"); - Assert.IsNull(line.Source); - } - - [TestMethod] - public void TestSourceMissingWithTags() - { - var line = new Line("@id=123 PRIVMSG #channel"); - Assert.IsNull(line.Source); - } - - [TestMethod] - public void TestCommand() - { - var line = new Line("privmsg #channel"); - Assert.AreEqual("PRIVMSG", line.Command); - } - - [TestMethod] - public void TestParamsTrailing() - { - var line = new Line("PRIVMSG #channel :hello world"); - 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); - } - - [TestMethod] - public void TestParamsMissing() - { - var line = new Line("PRIVMSG"); - Assert.AreEqual("PRIVMSG", line.Command); - CollectionAssert.AreEqual(new List(), line.Params); - } - - [TestMethod] - public void TestAllTokens() - { - var line = new Line("@id=123 :nick!user@host PRIVMSG #channel :hello world"); - 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); - } - } -} -- cgit 1.4.1