Skip to content

Commit

Permalink
Improve unit test coverage on alternate operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenquan committed Dec 5, 2024
1 parent 6808590 commit c96e628
Showing 1 changed file with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,49 +69,30 @@ public void MathExpressionConverter_WithMultipleVariable_ReturnsCorrectResult(st

var result = mathExpressionConverter.Convert(variables, mathExpressionTargetType, expression);

Assert.NotNull(result);
Assert.NotNull(result);
Assert.True(Math.Abs((double)result - expectedResult) < tolerance);
}

[Theory]
[InlineData("x && x1", new object?[] { true, true }, true)]
[InlineData("x && x1", new object?[] { true, false }, false)]
[InlineData("x && x1", new object?[] { false, true }, false)]
[InlineData("x && 3 == 4", new object?[] { false } , false)]
[InlineData("x && 3 == 4", new object?[] { false }, false)]
[InlineData("x && x1", new object?[] { "Cat", "Dog" }, "Dog")]
[InlineData("x && x1", new object?[] { false, "Cat" }, false)]
[InlineData("x && x1", new object?[] { "Cat", false }, false)]
[InlineData("x && x1", new object?[] { "", false }, "")]
[InlineData("x && x1", new object?[] { false, "" }, false)]
[InlineData("x || x1", new object?[] { true, true }, true)]
[InlineData("x || x1", new object?[] { false, true }, true)]
[InlineData("x || x1", new object?[] { true, false}, true)]
[InlineData("x || x1", new object?[] { true, false }, true)]
[InlineData("x || 3 == 4", new object?[] { false }, false)]
[InlineData("x || x1", new object?[] { "Cat", "Dog" }, "Cat")]
[InlineData("x || x1", new object?[] { false, "Cat" }, "Cat")]
[InlineData("x || x1", new object?[] { "Cat", false }, "Cat")]
[InlineData("x || x1", new object?[] { "", false }, false)]
[InlineData("x || x1", new object?[] { false, "" }, "")]
[InlineData("x || x1", new object?[] { false, new int[] { 1, 2, 3 } }, new int[] { 1, 2, 3 })]
[InlineData("x and x1", new object?[] { true, true }, true)]
[InlineData("x and x1", new object?[] { true, false }, false)]
[InlineData("x and x1", new object?[] { false, true }, false)]
[InlineData("x and 3 == 4", new object?[] { false }, false)]
[InlineData("x and x1", new object?[] { "Cat", "Dog" }, "Dog")]
[InlineData("x and x1", new object?[] { false, "Cat" }, false)]
[InlineData("x and x1", new object?[] { "Cat", false }, false)]
[InlineData("x and x1", new object?[] { "", false }, "")]
[InlineData("x and x1", new object?[] { false, "" }, false)]
[InlineData("x or x1", new object?[] { true, true }, true)]
[InlineData("x or x1", new object?[] { false, true }, true)]
[InlineData("x or x1", new object?[] { true, false }, true)]
[InlineData("x or 3 == 4", new object?[] { false }, false)]
[InlineData("x or x1", new object?[] { "Cat", "Dog" }, "Cat")]
[InlineData("x or x1", new object?[] { false, "Cat" }, "Cat")]
[InlineData("x or x1", new object?[] { "Cat", false }, "Cat")]
[InlineData("x or x1", new object?[] { "", false }, false)]
[InlineData("x or x1", new object?[] { false, "" }, "")]
[InlineData("x or x1", new object?[] { false, new int[] { 1, 2, 3 } }, new int[] { 1, 2, 3 })]
[InlineData("x || x1", new object?[] { false, new int[] { 1, 2, 3 } }, new int[] { 1, 2, 3 })]
public void MultiMathExpressionConverter_WithMultipleVariable_ReturnsCorrectLogicalResult(string expression, object?[] variables, object? expectedResult)
{
var mathExpressionConverter = new MultiMathExpressionConverter();
Expand All @@ -120,6 +101,56 @@ public void MultiMathExpressionConverter_WithMultipleVariable_ReturnsCorrectLogi
Assert.Equal(expectedResult, result);
}

[Theory]
[InlineData("x && x1", "x and x1")]
[InlineData("x || x1", "x or x1")]
public void MultiMathExpressionConverter_WithAlternateEqualityOperators_ReturnsSameEvaluation(string expression, string alternateExpression)
{
var mathExpressionConverter = new MultiMathExpressionConverter();
var listOfVariables = new object?[][]
{
new object?[] { true, true },
new object?[] { true, false },
new object?[] { false, true },
new object?[] { false, false },
new object?[] { "Cat", "Dog" },
new object?[] { false, "Cat" },
new object?[] { "Cat", false },
new object?[] { "", false },
new object?[] { false, "" },
};
foreach (var variables in listOfVariables)
{
var result = mathExpressionConverter.Convert(variables, typeof(object), expression);
var alternateResult = mathExpressionConverter.Convert(variables, typeof(object), alternateExpression);
Assert.NotNull(result);
Assert.NotNull(alternateResult);
Assert.Equal(result, alternateResult);
}
}

[Theory]
[InlineData("x >= x1", "x ge x1")]
[InlineData("x > x1", "x gt x1")]
[InlineData("x <= x1", "x le x1")]
[InlineData("x < x1", "x lt x1")]
public void MultiMathExpressionConverter_WithAlternateCompareOperators_ReturnsSameEvaluation(string expression, string alternateExpression)
{
var mathExpressionConverter = new MultiMathExpressionConverter();
for (var i = 0; i <= 2; i++)
{
for (var j = 0; j <= 2; j++)
{
var variables = new object?[] { i, j };
var result = mathExpressionConverter.Convert(variables, typeof(object), expression);
var alternateResult = mathExpressionConverter.Convert(variables, typeof(object), alternateExpression);
Assert.NotNull(result);
Assert.NotNull(alternateResult);
Assert.Equal(result, alternateResult);
}
}
}

[Theory]
[InlineData("x == 3 && x1", new object?[] { 3d, 4d }, 4d)]
[InlineData("x != 3 || x1", new object?[] { 3d, 4d }, 4d)]
Expand Down

0 comments on commit c96e628

Please sign in to comment.