Skip to content

Commit

Permalink
Added more unit tests to cover dice notation cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris3606 committed Feb 4, 2024
1 parent 1ee5d50 commit 84e07c4
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GoRogue.DiceNotation;
using System;
using GoRogue.DiceNotation;
using JetBrains.Annotations;
using Xunit;
using XUnit.ValueTuples;
Expand All @@ -17,14 +18,21 @@ private static void AssertMinMaxValues(DiceExpression expr, int min, int max)
[AssertionMethod]
private static void AssertReturnedInRange(DiceExpression expr, int min, int max)
{
// Necessary if we're using negative numbers as multipliers
int lMin = Math.Min(min, max);
int lMax = Math.Max(min, max);

for (var i = 0; i < 100; i++)
{
var result = expr.Roll();
Assert.True(result >= min && result <= max);
Assert.True(result >= lMin && result <= lMax);
}
}

public static (string expr, int min, int max)[] DiceExpressions = new[]
// NOTE: The test case min and max values are the expected min and max values for the expression
// assuming the MINIMUM roll of the dice, and assuming the MAXIMUM roll of the dice, in that order.
// This means that they min and max values may be reversed if the expression contains a negative multiplier.
public static (string expr, int min, int max)[] DiceExpressions =
{
// Advanced Dice Expression
("1d(1d12+4)+3", 4, 19),
Expand All @@ -46,12 +54,23 @@ public static (string expr, int min, int max)[] DiceExpressions = new[]
("3*(1d6+2)", 9, 24),
// Single dice with multiply
("1d6*3", 3, 18),
// Unary negation
// Unary negation (only constant)
("-1", -1, -1),
// Negative combined with other operators
("1d6+-1", 0, 5),
// Negative combined with minus
("1d6--1", 2, 7)
("1d6--1", 2, 7),
// Negative combined with multiply
("1d6*-1", -1, -6),
// Negative combined with multiply and paren
("-2*(1d6+7)", -16, -26)
};

public static string[] InvalidDiceExpressions =
{
"1d6*/3",
"1d6-/3",
"1d6-)3"
};

[Theory]
Expand All @@ -62,5 +81,12 @@ public void DiceExpressionValues(string expression, int min, int max)
AssertMinMaxValues(expr, min, max);
AssertReturnedInRange(expr, min, max);
}

[Theory]
[MemberDataEnumerable(nameof(InvalidDiceExpressions))]
public void DiceExpressionInvalid(string expression)
{
Assert.Throws<InvalidOperationException>(() => Dice.Parse(expression));
}
}
}

0 comments on commit 84e07c4

Please sign in to comment.