add some more tests

This commit is contained in:
Ben Harris 2024-04-19 18:13:54 -04:00
parent 7299bfe402
commit 9ac8d3c920
5 changed files with 304 additions and 16 deletions

View File

@ -106,6 +106,8 @@ namespace ChatSharp
/// </summary>
public string Source { get; set; }
public IrcUser User => new IrcUser(Source);
/// <summary>
/// The message command.
/// </summary>

View File

@ -1,4 +1,7 @@
using YamlDotNet.Serialization;
// ReSharper disable CollectionNeverUpdated.Global
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
namespace ChatSharp.Tests.Data;
@ -8,7 +11,8 @@ public class JoinModel
public class Test
{
[YamlMember(Alias = "desc")] public string Description { get; set; }
[YamlMember(Alias = "desc")]
public string Description { get; set; }
public Atoms Atoms { get; set; }
@ -18,11 +22,8 @@ public class JoinModel
public class Atoms
{
public Dictionary<string, string> Tags { get; set; }
public string Source { get; set; }
public string Verb { get; set; }
public List<string> Params { get; set; }
}
}

View File

@ -1,4 +1,7 @@
namespace ChatSharp.Tests.Data;
// ReSharper disable CollectionNeverUpdated.Global
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
namespace ChatSharp.Tests.Data;
public class SplitModel
{

View File

@ -1,4 +1,6 @@
namespace ChatSharp.Tests;
using System.Linq;
namespace ChatSharp.Tests;
[TestClass]
public class IrcMessageTests
@ -16,6 +18,12 @@ public class IrcMessageTests
}
}
[TestMethod]
public void NewInvalidMessage()
{
Assert.ThrowsException<ArgumentException>(() => new IrcMessage("USER", "user", "0 *", "real name").Format());
}
[TestMethod]
public void NewValidMessage_Command()
{
@ -35,20 +43,30 @@ public class IrcMessageTests
{
IrcMessage fromMessage = new(":user!~ident@host PRIVMSG target :Lorem ipsum dolor sit amet");
var compareParams = new[] { "target", "Lorem ipsum dolor sit amet" };
CollectionAssert.AreEqual(compareParams, fromMessage.Parameters);
}
[TestMethod]
public void NewValidMessage_UppercaseCommand()
{
IrcMessage fromMessage = new(":user!~ident@host privmsg target :Lorem ipsum dolor sit amet");
Assert.AreEqual("PRIVMSG", fromMessage.Command);
}
[TestMethod]
public void NewValidMessage_Tags()
{
IrcMessage fromMessage =
new("@a=123;b=456;c=789 :user!~ident@host PRIVMSG target :Lorem ipsum dolor sit amet");
var compareTags = new[]
{
new KeyValuePair<string, string>("a", "123"),
new KeyValuePair<string, string>("b", "456"),
new KeyValuePair<string, string>("c", "789")
};
CollectionAssert.AreEqual(compareTags, fromMessage.Tags);
}
@ -56,12 +74,14 @@ public class IrcMessageTests
public void NewValidMessage_Tags02()
{
IrcMessage fromMessage = new("@aaa=bbb;ccc;example.com/ddd=eee :nick!ident@host.com PRIVMSG me :Hello");
var compareTags = new[]
{
new KeyValuePair<string, string>("aaa", "bbb"),
new KeyValuePair<string, string>("ccc", null),
new KeyValuePair<string, string>("example.com/ddd", "eee")
};
CollectionAssert.AreEqual(fromMessage.Tags, compareTags);
}
@ -70,12 +90,14 @@ public class IrcMessageTests
{
IrcMessage fromMessage =
new(@"@a=123\:456;b=456\:789;c=789\:123 :user!~ident@host PRIVMSG target :Lorem ipsum dolor sit amet");
var compareTags = new[]
{
new KeyValuePair<string, string>("a", "123;456"),
new KeyValuePair<string, string>("b", "456;789"),
new KeyValuePair<string, string>("c", "789;123")
};
CollectionAssert.AreEqual(fromMessage.Tags, compareTags);
}
@ -83,13 +105,219 @@ public class IrcMessageTests
public void NewValidMessage_TagsNoValue()
{
IrcMessage fromMessage = new("@a=;b :nick!ident@host.com PRIVMSG me :Hello");
var compareTags = new[]
{
new KeyValuePair<string, string>("a", ""),
new KeyValuePair<string, string>("b", null)
};
CollectionAssert.AreEqual(fromMessage.Tags, compareTags);
}
[TestMethod]
public void TagsMissing()
{
var line = new IrcMessage("PRIVMSG #channel");
Assert.IsNull(line.Tags);
}
[TestMethod]
public void TagsMissingValue()
{
var line = new IrcMessage("@id= PRIVMSG #channel");
Assert.AreEqual(string.Empty, line.Tags["id"]);
}
[TestMethod]
public void TagsMissingEqual()
{
var line = new IrcMessage("@id PRIVMSG #channel");
Assert.IsNull(line.Tags["id"]);
}
[TestMethod]
public void TagsUnescape()
{
var line = new IrcMessage(@"@id=1\\\:\r\n\s2 PRIVMSG #channel");
Assert.AreEqual("1\\;\r\n 2", line.Tags["id"]);
}
[TestMethod]
public void TagsOverlap()
{
var line = new IrcMessage(@"@id=1\\\s\\s PRIVMSG #channel");
Assert.AreEqual(@"1\ \s", line.Tags["id"]);
}
[TestMethod]
public void TagsLoneEndSlash()
{
var line = new IrcMessage("@id=1\\ PRIVMSG #channel");
Assert.AreEqual("1", line.Tags["id"]);
}
[TestMethod]
public void SourceWithoutTags()
{
var line = new IrcMessage(":nick!user@host PRIVMSG #channel");
Assert.AreEqual("nick!user@host", line.Source);
}
[TestMethod]
public void SourceWithTags()
{
var line = new IrcMessage("@id=123 :nick!user@host PRIVMSG #channel");
Assert.AreEqual("nick!user@host", line.Source);
}
[TestMethod]
public void SourceMissingWithoutTags()
{
var line = new IrcMessage("PRIVMSG #channel");
Assert.IsNull(line.Source);
}
[TestMethod]
public void SourceMissingWithTags()
{
var line = new IrcMessage("@id=123 PRIVMSG #channel");
Assert.IsNull(line.Source);
}
[TestMethod]
public void Command()
{
var line = new IrcMessage("privmsg #channel");
Assert.AreEqual("PRIVMSG", line.Command);
}
[TestMethod]
public void ParamsTrailing()
{
var line = new IrcMessage("PRIVMSG #channel :hello world");
CollectionAssert.AreEqual(new List<string> {"#channel", "hello world"}, line.Parameters);
}
[TestMethod]
public void ParamsOnlyTrailing()
{
var line = new IrcMessage("PRIVMSG :hello world");
CollectionAssert.AreEqual(new List<string> {"hello world"}, line.Parameters);
}
[TestMethod]
public void ParamsMissing()
{
var line = new IrcMessage("PRIVMSG");
Assert.AreEqual("PRIVMSG", line.Command);
CollectionAssert.AreEqual(new List<string>(), line.Parameters);
}
[TestMethod]
public void AllTokens()
{
var line = new IrcMessage("@id=123 :nick!user@host PRIVMSG #channel :hello world");
CollectionAssert.AreEqual(new Dictionary<string, string> {{"id", "123"}}, line.Tags);
Assert.AreEqual("nick!user@host", line.Source);
Assert.AreEqual("PRIVMSG", line.Command);
CollectionAssert.AreEqual(new List<string> {"#channel", "hello world"}, line.Parameters);
}
[TestMethod]
public void Tags()
{
var line = new IrcMessage("PRIVMSG", "#channel", "hello")
{
Tags = new() {{"id", "\\" + " " + ";" + "\r\n"}}
}.Format();
Assert.AreEqual(@"@id=\\\s\:\r\n PRIVMSG #channel hello", line);
}
[TestMethod]
public void MissingTag()
{
var line = new IrcMessage("PRIVMSG", "#channel", "hello").Format();
Assert.AreEqual("PRIVMSG #channel hello", line);
}
[TestMethod]
public void NullTag()
{
var line = new IrcMessage("PRIVMSG", "#channel", "hello") {Tags = new() {{"a", null}}}
.Format();
Assert.AreEqual("@a PRIVMSG #channel hello", line);
}
[TestMethod]
public void EmptyTag()
{
var line = new IrcMessage("PRIVMSG", "#channel", "hello") {Tags = new() {{"a", ""}}}
.Format();
Assert.AreEqual("@a PRIVMSG #channel hello", line);
}
[TestMethod]
public void Source()
{
var line = new IrcMessage("PRIVMSG", "#channel", "hello") {Source = "nick!user@host"}.Format();
Assert.AreEqual(":nick!user@host PRIVMSG #channel hello", line);
}
[TestMethod]
public void CommandLowercase()
{
var line = new IrcMessage {Command = "privmsg"}.Format();
Assert.AreEqual("privmsg", line);
}
[TestMethod]
public void CommandUppercase()
{
var line = new IrcMessage {Command = "PRIVMSG"}.Format();
Assert.AreEqual("PRIVMSG", line);
}
[TestMethod]
public void TrailingSpace()
{
var line = new IrcMessage("PRIVMSG", "#channel", "hello world").Format();
Assert.AreEqual("PRIVMSG #channel :hello world", line);
}
[TestMethod]
public void TrailingNoSpace()
{
var line = new IrcMessage("PRIVMSG", "#channel", "helloworld").Format();
Assert.AreEqual("PRIVMSG #channel helloworld", line);
}
[TestMethod]
public void TrailingDoubleColon()
{
var line = new IrcMessage("PRIVMSG", "#channel", ":helloworld").Format();
Assert.AreEqual("PRIVMSG #channel ::helloworld", line);
}
[TestMethod]
public void InvalidNonLastSpace()
{
Assert.ThrowsException<ArgumentException>(() => { new IrcMessage("USER", "user", "0 *", "real name").Format(); });
}
[TestMethod]
public void InvalidNonLastColon()
{
Assert.ThrowsException<ArgumentException>(() => { new IrcMessage("PRIVMSG", ":#channel", "hello").Format(); });
}
[TestMethod]
public void Timestamp_CompareISOString()
@ -106,8 +334,7 @@ public class IrcMessageTests
"2012-06-30T23:59:59.419Z"
];
Assert.AreEqual(messages[0].Timestamp.ToISOString(), timestamps[0]);
Assert.AreEqual(messages[1].Timestamp.ToISOString(), timestamps[1]);
CollectionAssert.AreEqual(timestamps, messages.Select(m => m.Timestamp.ToISOString()).ToList());
}
[TestMethod]
@ -125,8 +352,7 @@ public class IrcMessageTests
"2017-09-09T02:26:12.000Z"
];
Assert.AreEqual(messages[0].Timestamp.ToISOString(), timestamps[0]);
Assert.AreEqual(messages[1].Timestamp.ToISOString(), timestamps[1]);
CollectionAssert.AreEqual(timestamps, messages.Select(m => m.Timestamp.ToISOString()).ToList());
}
[TestMethod]

View File

@ -11,7 +11,7 @@ public class IrcUserTests
var userModes = client.ServerInfo.GetModesForNick(user.Nick);
Assert.IsTrue(userModes.Count == 5);
Assert.AreEqual(5, userModes.Count);
}
[TestMethod]
@ -22,7 +22,7 @@ public class IrcUserTests
var userModes = client.ServerInfo.GetModesForNick(user.Nick);
Assert.IsTrue(userModes.Count == 4);
Assert.AreEqual(4, userModes.Count);
}
[TestMethod]
@ -33,7 +33,7 @@ public class IrcUserTests
var userModes = client.ServerInfo.GetModesForNick(user.Nick);
Assert.IsTrue(userModes.Count == 3);
Assert.AreEqual(3, userModes.Count);
}
[TestMethod]
@ -44,7 +44,7 @@ public class IrcUserTests
var userModes = client.ServerInfo.GetModesForNick(user.Nick);
Assert.IsTrue(userModes.Count == 2);
Assert.AreEqual(2, userModes.Count);
}
[TestMethod]
@ -55,7 +55,7 @@ public class IrcUserTests
var userModes = client.ServerInfo.GetModesForNick(user.Nick);
Assert.IsTrue(userModes.Count == 1);
Assert.AreEqual(1, userModes.Count);
}
[TestMethod]
@ -66,6 +66,62 @@ public class IrcUserTests
var userModes = client.ServerInfo.GetModesForNick(user.Nick);
Assert.IsTrue(userModes.Count == 0);
Assert.AreEqual(0, userModes.Count);
}
[TestMethod]
public void FullHostmask()
{
var hostmask = new IrcUser("nick!user@host");
Assert.AreEqual("nick", hostmask.Nick);
Assert.AreEqual("user", hostmask.User);
Assert.AreEqual("host", hostmask.Hostname);
}
[TestMethod]
public void NoHostname()
{
var hostmask = new IrcUser("nick!user");
Assert.AreEqual("nick", hostmask.Nick);
Assert.AreEqual("user", hostmask.User);
Assert.IsNull(hostmask.Hostname);
}
[TestMethod]
public void NoUser()
{
var hostmask = new IrcUser("nick@host");
Assert.AreEqual("nick", hostmask.Nick);
Assert.IsNull(hostmask.User);
Assert.AreEqual("host", hostmask.Hostname);
}
[TestMethod]
public void OnlyNick()
{
var hostmask = new IrcUser("nick");
Assert.AreEqual("nick", hostmask.Nick);
Assert.IsNull(hostmask.User);
Assert.IsNull(hostmask.Hostname);
}
[TestMethod]
public void HostmaskFromLine()
{
var message = new IrcMessage(":nick!user@host PRIVMSG #channel hello");
var hostmask = new IrcUser("nick!user@host");
Assert.AreEqual(hostmask.ToString(), message.User.ToString());
Assert.AreEqual("nick", message.User.Nick);
Assert.AreEqual("user", message.User.User);
Assert.AreEqual("host", message.User.Hostname);
}
[TestMethod]
public void EmptyHostmaskFromLine()
{
var message = new IrcMessage("PRIVMSG #channel hello");
Assert.IsNull(message.User.Hostname);
Assert.IsNull(message.User.User);
Assert.IsNull(message.User.Nick);
}
}