Add some unit tests for the dice parser

This commit is contained in:
runebaas 2020-06-21 15:49:18 +02:00
parent acd1cee16c
commit 859db4ebdd
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
2 changed files with 334 additions and 0 deletions

View file

@ -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<DiceParserTestDto> DiceParserTestData =>
new TestData<DiceParserTestDto>
{
{
"Empty Input",
new DiceParserTestDto
{
Input = "",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 1,
Sides = 20,
AdvantageType = DieAdvantageType.None
}
}
}
}
},
{
"Simple 1d20 input",
new DiceParserTestDto
{
Input = "1d20",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 1,
Sides = 20,
AdvantageType = DieAdvantageType.None
}
}
}
}
},
{
"2d8 with advantage",
new DiceParserTestDto
{
Input = "+2d8",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 2,
Sides = 8,
AdvantageType = DieAdvantageType.Advantage
}
}
}
}
},
{
"2d8 with disadvantage",
new DiceParserTestDto
{
Input = "-2d8",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 2,
Sides = 8,
AdvantageType = DieAdvantageType.Disadvantage
}
}
}
}
},
{
"multiple dice",
new DiceParserTestDto
{
Input = "2d8 2d6",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
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<SingleDie>
{
new SingleDie(_randomNumberGenerator)
{
Amount = 1,
Sides = 20
}
},
SkillModifier = 2
}
}
},
{
"8d6 with total",
new DiceParserTestDto
{
Input = "8d6 total",
Expected = new DiceInput()
{
Dice = new List<SingleDie>
{
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<SingleDie>
{
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));
}
}
}

View file

@ -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<SingleDieNameTestDto> SingleDieNameTestData => new TestData<SingleDieNameTestDto>
{
{
"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<SingleDieValidationTestDto> SingleDieValidationTestData => new TestData<SingleDieValidationTestDto>
{
{
"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<DiceException>(() => die.ValidateDie());
}
}
}
}