Skip to content

Commit

Permalink
Add more logical and/or unit tests. Add XAML friendly comparator oper…
Browse files Browse the repository at this point in the history
…ators.
  • Loading branch information
stephenquan committed Dec 5, 2024
1 parent 9994c63 commit 6808590
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,53 @@ public void MathExpressionConverter_WithMultipleVariable_ReturnsCorrectResult(st
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 && 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 || 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 })]
public void MultiMathExpressionConverter_WithMultipleVariable_ReturnsCorrectLogicalResult(string expression, object?[] variables, object? expectedResult)
{
var mathExpressionConverter = new MultiMathExpressionConverter();
var result = mathExpressionConverter.Convert(variables, typeof(object), expression);
Assert.NotNull(result);
Assert.Equal(expectedResult, result);
}

[Theory]
[InlineData("x == 3 && x1", new object?[] { 3d, 4d }, 4d)]
[InlineData("x != 3 || x1", new object?[] { 3d, 4d }, 4d)]
Expand Down Expand Up @@ -182,4 +229,4 @@ public void MultiMathExpressionConverterNullInputTest()
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => new MultiMathExpressionConverter().Convert([0.0, 7], typeof(bool), null, null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ internal MathExpression(string expression, IEnumerable<object?>? arguments = nul
new ("/", 2, x => Convert.ToDouble(x[0]) / Convert.ToDouble(x[1])),
new ("%", 2, x => Convert.ToDouble(x[0]) % Convert.ToDouble(x[1])),

new ("&&", 2, x => __bool(x[0]) ? x[1] : x[0]),
new ("||", 2, x => __bool(x[0]) ? x[0] : x[1]),
new ("and", 2, x => __bool(x[0]) ? x[1] : x[0]),
new ("or", 2, x => __bool(x[0]) ? x[0] : x[1]),

new ("==", 2, x => object.Equals(x[0], x[1])),
new ("!=", 2, x => !object.Equals(x[0], x[1])),

new (">=", 2, x => Convert.ToDouble(x[0]) >= Convert.ToDouble(x[1])),
new (">", 2, x => Convert.ToDouble(x[0]) > Convert.ToDouble(x[1])),
new ("<=", 2, x => Convert.ToDouble(x[0]) <= Convert.ToDouble(x[1])),
new ("<", 2, x => Convert.ToDouble(x[0]) < Convert.ToDouble(x[1])),
new ("ge", 2, x => Convert.ToDouble(x[0]) >= Convert.ToDouble(x[1])),
new ("gt", 2, x => Convert.ToDouble(x[0]) > Convert.ToDouble(x[1])),
new ("le", 2, x => Convert.ToDouble(x[0]) <= Convert.ToDouble(x[1])),
new ("lt", 2, x => Convert.ToDouble(x[0]) < Convert.ToDouble(x[1])),
new ("neg", 1, x => -Convert.ToDouble(x[0])),
new ("not", 1, x => !__bool(x[0])),
new ("if", 3, x => __bool(x[0]) ? x[1] : x[2]),
Expand Down Expand Up @@ -287,6 +287,16 @@ bool ParseConditional()
[GeneratedRegex("""^(\-|\!)""")]
private static partial Regex UnaryOperators();

static Dictionary<string, string> binaryMapping { get; } = new Dictionary<string, string>()
{
{ "<", "lt" },
{ "<=", "le" },
{ ">", "gt" },
{ ">=", "ge" },
{ "&&", "and" },
{ "||", "or" }
};

static Dictionary<string, string> unaryMapping { get; } = new Dictionary<string, string>()
{
{ "-", "neg" },
Expand All @@ -303,6 +313,10 @@ bool ParseBinaryOperators(Regex BinaryOperators, Func<bool> ParseNext)
while (ParsePattern(BinaryOperators))
{
string _operator = PatternMatch.Groups[1].Value;
if (binaryMapping.ContainsKey(_operator))
{
_operator = binaryMapping[_operator];
}
if (!ParseNext())
{
ExpressionIndex = index;
Expand Down

0 comments on commit 6808590

Please sign in to comment.