From 859db4ebddc4322ab1745c8b7f5744574813b465 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 21 Jun 2020 15:49:18 +0200 Subject: [PATCH] Add some unit tests for the dice parser --- Tests/Lib/DiceParser/DiceParser.test.cs | 209 ++++++++++++++++++++++++ Tests/Lib/DiceParser/SingleDie.test.cs | 125 ++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 Tests/Lib/DiceParser/DiceParser.test.cs create mode 100644 Tests/Lib/DiceParser/SingleDie.test.cs diff --git a/Tests/Lib/DiceParser/DiceParser.test.cs b/Tests/Lib/DiceParser/DiceParser.test.cs new file mode 100644 index 0000000..48a362c --- /dev/null +++ b/Tests/Lib/DiceParser/DiceParser.test.cs @@ -0,0 +1,209 @@ +using System.Collections.Generic; +using System.Text.Json; +using Geekbot.net.Lib.DiceParser; +using Geekbot.net.Lib.RandomNumberGenerator; +using Xunit; +using YamlDotNet.Serialization; + +namespace Tests.Lib.DiceParser +{ + public class DiceParserTest + { + private static readonly RandomNumberGenerator _randomNumberGenerator = new RandomNumberGenerator(); + + public struct DiceParserTestDto + { + public string Input { get; set; } + public DiceInput Expected { get; set; } + } + + public static TestData DiceParserTestData => + new TestData + { + { + "Empty Input", + new DiceParserTestDto + { + Input = "", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20, + AdvantageType = DieAdvantageType.None + } + } + } + } + }, + { + "Simple 1d20 input", + new DiceParserTestDto + { + Input = "1d20", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20, + AdvantageType = DieAdvantageType.None + } + } + } + } + }, + { + "2d8 with advantage", + new DiceParserTestDto + { + Input = "+2d8", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 8, + AdvantageType = DieAdvantageType.Advantage + } + } + } + } + }, + { + "2d8 with disadvantage", + new DiceParserTestDto + { + Input = "-2d8", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 8, + AdvantageType = DieAdvantageType.Disadvantage + } + } + } + } + }, + { + "multiple dice", + new DiceParserTestDto + { + Input = "2d8 2d6", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 8 + }, + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 6 + } + } + } + } + }, + { + "with skill modifier, no dice", + new DiceParserTestDto + { + Input = "+2", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20 + } + }, + SkillModifier = 2 + } + } + }, + { + "8d6 with total", + new DiceParserTestDto + { + Input = "8d6 total", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 8, + Sides = 6 + } + }, + Options = new DiceInputOptions + { + ShowTotal = true + } + } + } + }, + { + "All posibilities", + new DiceParserTestDto + { + Input = "2d20 +1d20 -1d20 +1 total", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 20 + }, + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20, + AdvantageType = DieAdvantageType.Advantage + }, + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20, + AdvantageType = DieAdvantageType.Disadvantage + }, + }, + Options = new DiceInputOptions + { + ShowTotal = true + }, + SkillModifier = 1 + } + } + }, + }; + + [Theory, MemberData(nameof(DiceParserTestData))] + public void DiceParserTestFunc(string testName, DiceParserTestDto testData) + { + var parser = new Geekbot.net.Lib.DiceParser.DiceParser(_randomNumberGenerator); + var result = parser.Parse(testData.Input); + + Assert.Equal(JsonSerializer.Serialize(result), JsonSerializer.Serialize(testData.Expected)); + } + } +} \ No newline at end of file diff --git a/Tests/Lib/DiceParser/SingleDie.test.cs b/Tests/Lib/DiceParser/SingleDie.test.cs new file mode 100644 index 0000000..2309f98 --- /dev/null +++ b/Tests/Lib/DiceParser/SingleDie.test.cs @@ -0,0 +1,125 @@ +using Geekbot.net.Lib.DiceParser; +using Geekbot.net.Lib.RandomNumberGenerator; +using Xunit; + +namespace Tests.Lib.DiceParser +{ + public class SingleDieTest + { + public struct SingleDieNameTestDto + { + public DieAdvantageType AdvantageType { get; set; } + public string Expected { get; set; } + } + + public static TestData SingleDieNameTestData => new TestData + { + { + "No advantage", + new SingleDieNameTestDto() + { + AdvantageType = DieAdvantageType.None, + Expected = "1d20" + } + }, + { + "With advantage", + new SingleDieNameTestDto() + { + AdvantageType = DieAdvantageType.Advantage, + Expected = "1d20 (with advantage)" + } + }, + { + "With disadvantage", + new SingleDieNameTestDto() + { + AdvantageType = DieAdvantageType.Disadvantage, + Expected = "1d20 (with disadvantage)" + } + } + }; + + [Theory, MemberData(nameof(SingleDieNameTestData))] + public void SingleDieNameTestFunc(string testName, SingleDieNameTestDto testData) + { + var die = new SingleDie(new RandomNumberGenerator()) {AdvantageType = testData.AdvantageType}; + Assert.Equal(die.DiceName, testData.Expected); + } + + public struct SingleDieValidationTestDto + { + public int Sides { get; set; } + public int Amount { get; set; } + public bool PassValidation { get; set; } + } + + public static TestData SingleDieValidationTestData => new TestData + { + { + "To many sides", + new SingleDieValidationTestDto() + { + Amount = 1, + Sides = 200, + PassValidation = false + } + }, + { + "To few sides", + new SingleDieValidationTestDto() + { + Amount = 1, + Sides = 1, + PassValidation = false + } + }, + { + "To many Dice", + new SingleDieValidationTestDto() + { + Amount = 200, + Sides = 20, + PassValidation = false + } + }, + { + "To few Dice", + new SingleDieValidationTestDto() + { + Amount = 0, + Sides = 20, + PassValidation = false + } + }, + { + "Correct Dice", + new SingleDieValidationTestDto() + { + Amount = 1, + Sides = 20, + PassValidation = true + } + } + }; + + [Theory, MemberData(nameof(SingleDieValidationTestData))] + public void SingleDieValidationTestFunc(string testName, SingleDieValidationTestDto testData) + { + var die = new SingleDie(new RandomNumberGenerator()) + { + Amount = testData.Amount, + Sides = testData.Sides + }; + + if (testData.PassValidation) + { + die.ValidateDie(); + } + else + { + Assert.Throws(() => die.ValidateDie()); + } + } + } +} \ No newline at end of file