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

[Feature] Adds support for decimal qualifiers. Resolves #91 #92

Merged
merged 3 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
20 changes: 19 additions & 1 deletion src/System.Linq.Dynamic.Core/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ Expression ParseIntegerLiteral()
bool isHexadecimal = text.StartsWith(text[0] == '-' ? "-0x" : "0x", StringComparison.CurrentCultureIgnoreCase);
char[] qualifierLetters = isHexadecimal
? new[] { 'U', 'u', 'L', 'l' }
: new[] { 'U', 'u', 'L', 'l', 'F', 'f', 'D', 'd' };
: new[] { 'U', 'u', 'L', 'l', 'F', 'f', 'D', 'd', 'M', 'm' };

if (qualifierLetters.Contains(last))
{
Expand Down Expand Up @@ -1007,6 +1007,9 @@ Expression ParseIntegerLiteral()
if (qualifier == "D" || qualifier == "d")
return TryParseAsDouble(text, qualifier[0]);

if (qualifier == "M" || qualifier == "m")
return TryParseAsDecimal(text, qualifier[0]);

throw ParseError(Res.MinusCannotBeAppliedToUnsignedInteger);
}

Expand Down Expand Up @@ -1038,6 +1041,21 @@ Expression TryParseAsFloat(string text, char qualifier)
}
}

// not possible to find float qualifier, so try to parse as double
return TryParseAsDecimal(text, qualifier);
}

Expression TryParseAsDecimal(string text, char qualifier)
{
if (qualifier == 'M' || qualifier == 'm')
{
decimal d;
if (decimal.TryParse(text.Substring(0, text.Length - 1), NumberStyles.Number, CultureInfo.InvariantCulture, out d))
{
return CreateLiteral(d, text);
}
}

// not possible to find float qualifier, so try to parse as double
return TryParseAsDouble(text, qualifier);
}
Expand Down
1 change: 1 addition & 0 deletions src/System.Linq.Dynamic.Core/Tokenizer/TextParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public void NextToken()

if (_ch == 'F' || _ch == 'f') NextChar();
if (_ch == 'D' || _ch == 'd') NextChar();
if (_ch == 'M' || _ch == 'm') NextChar();
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ public void ParseLambda_ParameterName_Null()
Assert.Equal(typeof(bool), expression.Body.Type);
}

[Fact]
public void ParseLambda_RealNumbers()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please order the test alphabetically in the file.

{
var parameters = new ParameterExpression[0];

var result1 = DynamicExpressionParser.ParseLambda(parameters, typeof(double), "0.10");
var result2 = DynamicExpressionParser.ParseLambda(parameters, typeof(double), "0.10d");
var result3 = DynamicExpressionParser.ParseLambda(parameters, typeof(float), "0.10f");
var result4 = DynamicExpressionParser.ParseLambda(parameters, typeof(decimal), "0.10m");

// Assert
Assert.Equal(0.10d, result1.Compile().DynamicInvoke());
Assert.Equal(0.10d, result2.Compile().DynamicInvoke());
Assert.Equal(0.10f, result3.Compile().DynamicInvoke());
Assert.Equal(0.10m, result4.Compile().DynamicInvoke());
}

[Fact]
public void ParseLambda_ParameterExpressionMethodCall_ReturnsIntExpression()
{
Expand Down
32 changes: 32 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,38 @@ public void ExpressionTests_FloatQualifiers_Negative()
Assert.Equal(resultValues.ToArray(), result2.ToArray());
}

[Fact]
public void ExpressionTests_DecimalQualifiers()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please order the test alphabetically in the file.

{
//Arrange
var values = new[] { 1m, 2M, 3M }.AsQueryable();
var resultValues = new[] { 2m, 3m }.AsQueryable();

//Act
var result1 = values.Where("it == 2M or it == 3m");
var result2 = values.Where("it == 2.0M or it == 3.00m");

//Assert
Assert.Equal(resultValues.ToArray(), result1.ToArray());
Assert.Equal(resultValues.ToArray(), result2.ToArray());
}

[Fact]
public void ExpressionTests_DecimalQualifiers_Negative()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please order the test alphabetically in the file.

{
//Arrange
var values = new[] { -1m, -2M, -3M }.AsQueryable();
var resultValues = new[] { -2m, -3m }.AsQueryable();

//Act
var result1 = values.Where("it == -2M or it == -3m");
var result2 = values.Where("it == -2.0M or it == -3.0m");

//Assert
Assert.Equal(resultValues.ToArray(), result1.ToArray());
Assert.Equal(resultValues.ToArray(), result2.ToArray());
}

[Fact]
public void ExpressionTests_Guid_CompareTo_String()
{
Expand Down
24 changes: 24 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/TextParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ public void TextParser_Parse_RealLiteralPlus()
Check.That(textParser.CurrentToken.Text).Equals("1.0E+25");
}

[Fact]
public void TextParser_Parse_RealLiteralFloatQualifier()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please order the test alphabetically in the file.

{
// Assign + Act
var textParser = new TextParser(" 12.5f ");

// Assert
Check.That(textParser.CurrentToken.Id).Equals(TokenId.RealLiteral);
Check.That(textParser.CurrentToken.Pos).Equals(1);
Check.That(textParser.CurrentToken.Text).Equals("12.5f");
}

[Fact]
public void TextParser_Parse_RealLiteralDecimalQualifier()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please order the test alphabetically in the file.

{
// Assign + Act
var textParser = new TextParser(" 12.5m ");

// Assert
Check.That(textParser.CurrentToken.Id).Equals(TokenId.RealLiteral);
Check.That(textParser.CurrentToken.Pos).Equals(1);
Check.That(textParser.CurrentToken.Text).Equals("12.5m");
}

[Fact]
public void TextParser_Parse_Percent()
{
Expand Down