ircsharp/IRCSharp.Tests/Tokenization/Parser.cs

52 lines
1.6 KiB
C#
Raw Normal View History

2024-03-26 20:10:54 +00:00
using System.Globalization;
2020-04-20 00:52:41 +00:00
using System.IO;
2022-03-25 19:11:48 +00:00
using IRCSharp.Tests.Tokenization.Data;
2020-04-20 00:52:41 +00:00
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
2024-03-26 20:10:54 +00:00
namespace IRCSharp.Tests.Tokenization;
[TestClass]
public class Parser
2020-04-20 00:52:41 +00:00
{
2024-03-26 20:10:54 +00:00
private static T LoadYaml<T>(string path)
2020-04-20 00:52:41 +00:00
{
2024-03-26 20:10:54 +00:00
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
2020-04-20 00:52:41 +00:00
2024-03-26 20:10:54 +00:00
return deserializer.Deserialize<T>(File.ReadAllText(path));
}
2020-04-20 00:52:41 +00:00
2024-03-26 20:10:54 +00:00
[TestMethod]
public void Split()
{
foreach (var test in LoadYaml<SplitModel>("Tokenization/Data/msg-split.yaml").Tests)
2020-04-20 00:52:41 +00:00
{
2024-03-26 20:10:54 +00:00
var tokens = new Line(test.Input);
var atoms = test.Atoms;
2020-04-20 00:52:41 +00:00
2024-03-26 20:10:54 +00:00
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 ?? [], tokens.Params,
$"params failed on: '{test.Input}'");
2020-04-20 00:52:41 +00:00
}
2024-03-26 20:10:54 +00:00
}
2020-04-20 00:52:41 +00:00
2024-03-26 20:10:54 +00:00
[TestMethod]
public void Join()
{
foreach (var test in LoadYaml<JoinModel>("Tokenization/Data/msg-join.yaml").Tests)
2020-04-20 00:52:41 +00:00
{
2024-03-26 20:10:54 +00:00
var atoms = test.Atoms;
var line = new Line
2020-04-20 00:52:41 +00:00
{
2024-03-26 20:10:54 +00:00
Command = atoms.Verb, Params = atoms.Params, Source = atoms.Source, Tags = atoms.Tags
}.Format();
2020-04-20 00:52:41 +00:00
2024-03-26 20:10:54 +00:00
Assert.IsTrue(test.Matches.Contains(line), test.Description);
2020-04-20 00:52:41 +00:00
}
}
2024-03-26 20:10:54 +00:00
}