ircsharp/IRCSharp.Tests/Tokenization/StatefulDecoder.cs

90 lines
2.7 KiB
C#
Raw Normal View History

2020-04-28 04:35:52 +00:00
using System.Collections.Generic;
2020-04-20 21:24:59 +00:00
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 21:24:59 +00:00
2022-03-25 19:11:48 +00:00
namespace IRCSharp.Tests.Tokenization
2020-04-20 21:24:59 +00:00
{
[TestClass]
public class StatefulDecoder
2020-04-20 21:24:59 +00:00
{
2020-05-15 03:06:10 +00:00
private IRCTokens.StatefulDecoder _decoder;
2020-04-20 21:24:59 +00:00
[TestInitialize]
2020-05-17 02:08:40 +00:00
public void Initialize()
2020-04-20 21:24:59 +00:00
{
2020-05-15 03:06:10 +00:00
_decoder = new IRCTokens.StatefulDecoder();
2020-04-20 21:24:59 +00:00
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void Partial()
2020-04-20 21:24:59 +00:00
{
var lines = _decoder.Push("PRIVMSG ");
Assert.AreEqual(0, lines.Count);
2020-04-20 21:24:59 +00:00
lines = _decoder.Push("#channel hello\r\n");
Assert.AreEqual(1, lines.Count);
var line = new Line("PRIVMSG #channel hello");
2020-04-28 04:35:52 +00:00
CollectionAssert.AreEqual(new List<Line> {line}, lines);
2020-04-20 21:24:59 +00:00
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void Multiple()
2020-04-20 21:24:59 +00:00
{
var lines = _decoder.Push("PRIVMSG #channel1 hello\r\nPRIVMSG #channel2 hello\r\n");
2020-04-20 21:24:59 +00:00
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]
2020-05-17 02:08:40 +00:00
public void EncodingIso8859()
2020-04-20 21:24:59 +00:00
{
var iso8859 = Encoding.GetEncoding("iso-8859-1");
2020-05-15 03:06:10 +00:00
_decoder = new IRCTokens.StatefulDecoder {Encoding = iso8859};
2020-04-29 01:07:23 +00:00
var bytes = iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n");
var lines = _decoder.Push(bytes, bytes.Length);
2020-04-28 04:35:52 +00:00
var line = new Line("PRIVMSG #channel :hello Ç");
Assert.IsTrue(line.Equals(lines[0]));
2020-04-20 21:24:59 +00:00
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void EncodingFallback()
2020-04-20 21:24:59 +00:00
{
var latin1 = Encoding.GetEncoding("iso-8859-1");
2020-05-15 03:06:10 +00:00
_decoder = new IRCTokens.StatefulDecoder {Encoding = null, Fallback = latin1};
2020-04-29 01:07:23 +00:00
var bytes = latin1.GetBytes("PRIVMSG #channel hélló\r\n");
var lines = _decoder.Push(bytes, bytes.Length);
2020-04-20 21:24:59 +00:00
Assert.AreEqual(1, lines.Count);
Assert.IsTrue(new Line("PRIVMSG #channel hélló").Equals(lines[0]));
2020-04-20 21:24:59 +00:00
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void Empty()
2020-04-20 21:24:59 +00:00
{
var lines = _decoder.Push(string.Empty);
2020-04-29 01:16:38 +00:00
Assert.AreEqual(0, lines.Count);
2020-04-20 21:24:59 +00:00
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void BufferUnfinished()
2020-04-20 21:24:59 +00:00
{
_decoder.Push("PRIVMSG #channel hello");
var lines = _decoder.Push(string.Empty);
2020-04-29 01:16:38 +00:00
Assert.AreEqual(0, lines.Count);
2020-04-20 21:24:59 +00:00
}
[TestMethod]
2020-05-17 02:08:40 +00:00
public void Clear()
2020-04-20 21:24:59 +00:00
{
_decoder.Push("PRIVMSG ");
_decoder.Clear();
Assert.AreEqual(string.Empty, _decoder.Pending);
}
}
}