Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to PEG parser. Introduce boolean operators and constants. #2182

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ public sealed class MathOperator
/// </summary>
/// <param name="name">Name</param>
/// <param name="numericCount">Number of Numerals</param>
/// <param name="precedence">Math Operator Preference</param>
/// <param name="calculateFunc">Calculation Function</param>
public MathOperator(
string name,
int numericCount,
MathOperatorPrecedence precedence,
Func<double[], double> calculateFunc)
Func<object?[], object?> calculateFunc)
{
Name = name;
CalculateFunc = calculateFunc;
Precedence = precedence;
NumericCount = numericCount;
}

Expand All @@ -59,5 +56,5 @@ public MathOperator(
/// <summary>
/// Calculation Function
/// </summary>
public Func<double[], double> CalculateFunc { get; }
public Func<object?[], object?> CalculateFunc { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,30 @@ public void MathExpressionConverter_ReturnsCorrectResult(string expression, doub
var convertFromResult = mathExpressionConverter.ConvertFrom(x, expression);

Assert.True(Math.Abs((double)convertResult - expectedResult) < tolerance);
Assert.True(Math.Abs(convertFromResult - expectedResult) < tolerance);
Assert.True(Math.Abs((double)convertFromResult - expectedResult) < tolerance);
}

[Theory]
[InlineData("3 < x", 2d, false)]
[InlineData("x > 3", 2d, false)]
[InlineData("3 < x == x > 3", 2d, true)]
[InlineData("3 <= x != 3 >= x", 2d, true)]
[InlineData("x >= 1", 2d, true)]
[InlineData("x <= 3", 2d, true)]
[InlineData("x >= 1 && (x <= 3 || x >= 0)", 2d, true)]
[InlineData("true", 2d, true)]
[InlineData("false", 2d, false)]
[InlineData("-x > 2", 3d, false)]
[InlineData("!!! (---x > 2)", 3d, true)]
public void MathExpressionConverter_ReturnsCorrectBooleanResult(string expression, double x, bool expectedResult)
{
var mathExpressionConverter = new MathExpressionConverter();

var convertResult = ((ICommunityToolkitValueConverter)mathExpressionConverter).Convert(x, mathExpressionTargetType, expression, cultureInfo) ?? throw new NullReferenceException();
var convertFromResult = mathExpressionConverter.ConvertFrom(x, expression);

Assert.True((bool)convertResult == expectedResult);
Assert.True((bool)convertFromResult == expectedResult);
}

[Theory]
Expand All @@ -47,6 +70,22 @@ public void MathExpressionConverter_WithMultiplyVariable_ReturnsCorrectResult(st
Assert.True(Math.Abs((double)result - expectedResult) < tolerance);
}

[Theory]
[InlineData("x == x1", new object?[] { 2d, 2d }, true)]
[InlineData("x == x1", new object?[] { 2d, null }, false)]
[InlineData("x == x1", new object?[] { null, 2d}, false)]
[InlineData("x == x1", new object?[] { null, null }, true)]
[InlineData("(x ? x1 : x2) == null", new object?[] { true, null, 2d }, true)]
public void MathExpressionConverter_WithMultiplyVariable_ReturnsCorrectBooleanResult(string expression, object[] variables, bool expectedResult)
{
var mathExpressionConverter = new MultiMathExpressionConverter();

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

Assert.True(result is not null);
Assert.Equal(expectedResult, result);
}

[Theory]
[InlineData("1 + 3 + 5 + (3 - 2))")]
[InlineData("1 + 2) + (9")]
Expand Down Expand Up @@ -74,7 +113,7 @@ public void MultiMathExpressionConverterInvalidParameterThrowsArgumentException(
public void MultiMathExpressionConverterInvalidValuesReturnsNull()
{
var mathExpressionConverter = new MultiMathExpressionConverter();
var result = mathExpressionConverter.Convert([0d, null], mathExpressionTargetType, "x", cultureInfo);
var result = mathExpressionConverter.Convert([0d, null], mathExpressionTargetType, "x + x1", cultureInfo);
result.Should().BeNull();
}

Expand All @@ -85,9 +124,9 @@ public void MathExpressionConverterNullInputTest()
Assert.Throws<ArgumentNullException>(() => ((ICommunityToolkitValueConverter)new MathExpressionConverter()).Convert(0.0, null, "x", null));
Assert.Throws<ArgumentNullException>(() => ((ICommunityToolkitValueConverter)new MathExpressionConverter()).ConvertBack(0.0, null, null, null));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => ((ICommunityToolkitValueConverter)new MathExpressionConverter()).Convert(null, typeof(bool), "x", null));
//Assert.Throws<ArgumentNullException>(() => ((ICommunityToolkitValueConverter)new MathExpressionConverter()).Convert(null, typeof(bool), "x", null));
stephenquan marked this conversation as resolved.
Show resolved Hide resolved
Assert.Throws<ArgumentNullException>(() => ((ICommunityToolkitValueConverter)new MathExpressionConverter()).Convert(null, typeof(bool), null, null));
Assert.Throws<ArgumentNullException>(() => ((ICommunityToolkitValueConverter)new MathExpressionConverter()).ConvertBack(null, typeof(bool), null, null));
//Assert.Throws<ArgumentNullException>(() => ((ICommunityToolkitValueConverter)new MathExpressionConverter()).ConvertBack(null, typeof(bool), null, null));
stephenquan marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
Expand Down
Loading
Loading