ircsharp/IRCSharp.Tests/Tokenization/Tokenization.cs

135 lines
4.0 KiB
C#
Raw Normal View History

2020-04-28 04:35:52 +00:00
using System.Collections.Generic;
2020-05-17 02:08:40 +00:00
using System.Linq;
using System.Text;
2022-03-25 19:11:48 +00:00
using IRCTokens;
2020-04-28 04:35:52 +00:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
2020-04-20 00:52:41 +00:00
2022-03-25 19:11:48 +00:00
namespace IRCSharp.Tests.Tokenization
2020-04-20 00:52:41 +00:00
{
[TestClass]
public class Tokenization
2020-04-20 00:52:41 +00:00
{
[TestMethod]
2020-05-17 02:08:40 +00:00
public void TagsMissing()
2020-04-20 00:52:41 +00:00
{
var line = new Line("PRIVMSG #channel");
Assert.IsNull(line.Tags);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void TagsMissingValue()
2020-04-20 00:52:41 +00:00
{
var line = new Line("@id= PRIVMSG #channel");
Assert.AreEqual(string.Empty, line.Tags["id"]);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void TagsMissingEqual()
2020-04-20 00:52:41 +00:00
{
var line = new Line("@id PRIVMSG #channel");
Assert.AreEqual(string.Empty, line.Tags["id"]);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void TagsUnescape()
2020-04-20 00:52:41 +00:00
{
var line = new Line(@"@id=1\\\:\r\n\s2 PRIVMSG #channel");
Assert.AreEqual("1\\;\r\n 2", line.Tags["id"]);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void TagsOverlap()
2020-04-20 00:52:41 +00:00
{
var line = new Line(@"@id=1\\\s\\s PRIVMSG #channel");
Assert.AreEqual("1\\ \\s", line.Tags["id"]);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void TagsLoneEndSlash()
2020-04-20 00:52:41 +00:00
{
var line = new Line("@id=1\\ PRIVMSG #channel");
Assert.AreEqual("1", line.Tags["id"]);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void SourceWithoutTags()
2020-04-20 00:52:41 +00:00
{
var line = new Line(":nick!user@host PRIVMSG #channel");
Assert.AreEqual("nick!user@host", line.Source);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void SourceWithTags()
2020-04-20 00:52:41 +00:00
{
var line = new Line("@id=123 :nick!user@host PRIVMSG #channel");
Assert.AreEqual("nick!user@host", line.Source);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void SourceMissingWithoutTags()
2020-04-20 00:52:41 +00:00
{
var line = new Line("PRIVMSG #channel");
Assert.IsNull(line.Source);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void SourceMissingWithTags()
2020-04-20 00:52:41 +00:00
{
var line = new Line("@id=123 PRIVMSG #channel");
Assert.IsNull(line.Source);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void Command()
2020-04-20 00:52:41 +00:00
{
var line = new Line("privmsg #channel");
Assert.AreEqual("PRIVMSG", line.Command);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void ParamsTrailing()
2020-04-20 00:52:41 +00:00
{
var line = new Line("PRIVMSG #channel :hello world");
2020-04-28 04:35:52 +00:00
CollectionAssert.AreEqual(new List<string> {"#channel", "hello world"}, line.Params);
2020-04-20 00:52:41 +00:00
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void ParamsOnlyTrailing()
2020-04-20 00:52:41 +00:00
{
var line = new Line("PRIVMSG :hello world");
2020-04-28 04:35:52 +00:00
CollectionAssert.AreEqual(new List<string> {"hello world"}, line.Params);
2020-04-20 00:52:41 +00:00
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void ParamsMissing()
2020-04-20 00:52:41 +00:00
{
var line = new Line("PRIVMSG");
Assert.AreEqual("PRIVMSG", line.Command);
CollectionAssert.AreEqual(new List<string>(), line.Params);
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void AllTokens()
2020-04-20 00:52:41 +00:00
{
var line = new Line("@id=123 :nick!user@host PRIVMSG #channel :hello world");
2020-04-28 04:35:52 +00:00
CollectionAssert.AreEqual(new Dictionary<string, string> {{"id", "123"}}, line.Tags);
2020-04-20 00:52:41 +00:00
Assert.AreEqual("nick!user@host", line.Source);
Assert.AreEqual("PRIVMSG", line.Command);
2020-04-28 04:35:52 +00:00
CollectionAssert.AreEqual(new List<string> {"#channel", "hello world"}, line.Params);
2020-04-20 00:52:41 +00:00
}
2020-05-17 02:08:40 +00:00
[TestMethod]
public void NulByte()
{
var decoder = new IRCTokens.StatefulDecoder();
var bytes = Encoding.UTF8.GetBytes(":nick!user@host PRIVMSG #channel :hello")
.Concat(Encoding.UTF8.GetBytes("\0"))
.Concat(Encoding.UTF8.GetBytes("world"))
.ToArray();
var line = decoder.Push(bytes, bytes.Length).First();
CollectionAssert.AreEqual(new List<string> {"#channel", "hello"}, line.Params);
}
2020-04-20 00:52:41 +00:00
}
}