about summary refs log tree commit diff
path: root/IrcTokens/Tests/Parser.cs
blob: df70b92c1c3e04a4a1328a0a9c09f4601d4c4f45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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<T>(string path)
        {
            var deserializer = new DeserializerBuilder()
                .WithNamingConvention(CamelCaseNamingConvention.Instance)
                .Build();

            return deserializer.Deserialize<T>(File.ReadAllText(path));
        }

        [TestMethod]
        public void TestSplit()
        {
            foreach (var test in LoadYaml<SplitModel>("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<string>(), tokens.Params, $"params failed on: '{test.Input}'");
            }
        }

        [TestMethod]
        public void TestJoin()
        {
            foreach (var test in LoadYaml<JoinModel>("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);
            }
        }
    }
}