-
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support embedded quotes in string literal (#65)
- Loading branch information
Showing
9 changed files
with
224 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using System.Linq.Dynamic.Core.Exceptions; | ||
using System.Linq.Expressions; | ||
using Xunit; | ||
|
||
namespace System.Linq.Dynamic.Core.Tests | ||
{ | ||
public class DynamicExpressionParserTests | ||
{ | ||
[Fact] | ||
public void Parse_ParameterExpressionMethodCall_ReturnsIntExpression() | ||
{ | ||
var expression = DynamicExpressionParser.ParseLambda(true, | ||
new[] { Expression.Parameter(typeof(int), "x") }, | ||
typeof(int), | ||
"x + 1"); | ||
Assert.Equal(typeof(int), expression.Body.Type); | ||
} | ||
|
||
[Fact] | ||
public void Parse_TupleToStringMethodCall_ReturnsStringLambdaExpression() | ||
{ | ||
var expression = DynamicExpressionParser.ParseLambda( | ||
typeof(Tuple<int>), | ||
typeof(string), | ||
"it.ToString()"); | ||
Assert.Equal(typeof(string), expression.ReturnType); | ||
} | ||
|
||
[Fact] | ||
public void Parse_StringLiteral_ReturnsBooleanLambdaExpression() | ||
{ | ||
var expression = DynamicExpressionParser.ParseLambda(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"test\""); | ||
Assert.Equal(typeof(Boolean), expression.Body.Type); | ||
} | ||
|
||
[Fact] | ||
public void Parse_StringLiteralEmpty_ReturnsBooleanLambdaExpression() | ||
{ | ||
var expression = DynamicExpressionParser.ParseLambda(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"\""); | ||
Assert.Equal(typeof(Boolean), expression.Body.Type); | ||
} | ||
|
||
[Fact] | ||
public void Parse_StringLiteralEmbeddedQuote_ReturnsBooleanLambdaExpression() | ||
{ | ||
string expectedRightValue = "\"test \\\"string\""; | ||
var expression = DynamicExpressionParser.ParseLambda( | ||
new[] { Expression.Parameter(typeof(string), "Property1") }, | ||
typeof(Boolean), | ||
string.Format("Property1 == {0}", expectedRightValue)); | ||
|
||
string rightValue = ((BinaryExpression)expression.Body).Right.ToString(); | ||
Assert.Equal(typeof(Boolean), expression.Body.Type); | ||
Assert.Equal(expectedRightValue, rightValue); | ||
} | ||
|
||
[Fact] | ||
public void Parse_StringLiteralStartEmbeddedQuote_ReturnsBooleanLambdaExpression() | ||
{ | ||
string expectedRightValue = "\"\\\"test\""; | ||
var expression = DynamicExpressionParser.ParseLambda( | ||
new[] { Expression.Parameter(typeof(string), "Property1") }, | ||
typeof(Boolean), | ||
string.Format("Property1 == {0}", expectedRightValue)); | ||
|
||
string rightValue = ((BinaryExpression)expression.Body).Right.ToString(); | ||
Assert.Equal(typeof(Boolean), expression.Body.Type); | ||
Assert.Equal(expectedRightValue, rightValue); | ||
} | ||
|
||
[Fact] | ||
public void Parse_StringLiteral_MissingClosingQuote() | ||
{ | ||
string expectedRightValue = "\"test\\\""; | ||
|
||
Assert.Throws<ParseException>(() => DynamicExpressionParser.ParseLambda( | ||
new[] { Expression.Parameter(typeof(string), "Property1") }, | ||
typeof(Boolean), | ||
string.Format("Property1 == {0}", expectedRightValue))); | ||
} | ||
|
||
[Fact] | ||
public void Parse_StringLiteralEscapedBackslash_ReturnsBooleanLambdaExpression() | ||
{ | ||
string expectedRightValue = "\"test\\string\""; | ||
var expression = DynamicExpressionParser.ParseLambda( | ||
new[] { Expression.Parameter(typeof(string), "Property1") }, | ||
typeof(Boolean), | ||
string.Format("Property1 == {0}", expectedRightValue)); | ||
|
||
string rightValue = ((BinaryExpression)expression.Body).Right.ToString(); | ||
Assert.Equal(typeof(Boolean), expression.Body.Type); | ||
Assert.Equal(expectedRightValue, rightValue); | ||
} | ||
|
||
//[Fact] | ||
//public void ParseLambda_DelegateTypeMethodCall_ReturnsEventHandlerLambdaExpression() | ||
//{ | ||
// var expression = DynamicExpressionParser.ParseLambda(true, | ||
// typeof(EventHandler), | ||
// null, | ||
// new[] { Expression.Parameter(typeof(object), "sender"), Expression.Parameter(typeof(EventArgs), "e") }, | ||
// "sender.ToString()"); | ||
|
||
// Assert.Equal(typeof(void), expression.ReturnType); | ||
// Assert.Equal(typeof(EventHandler), expression.Type); | ||
//} | ||
|
||
//[Fact] this should fail : not allowed | ||
//public void ParseLambda_VoidMethodCall_ReturnsActionDelegate() | ||
//{ | ||
// var expression = DynamicExpressionParser.ParseLambda( | ||
// typeof(IO.FileStream), | ||
// null, | ||
// "it.Close()"); | ||
// Assert.Equal(typeof(void), expression.ReturnType); | ||
// Assert.Equal(typeof(Action<IO.FileStream>), expression.Type); | ||
//} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
test/System.Linq.Dynamic.Core.Tests/Helpers/Models/ModelWithEnum.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
| ||
namespace System.Linq.Dynamic.Core.Tests.Helpers.Models | ||
{ | ||
public class ModelWithEnum | ||
{ | ||
public string Name { get; set; } | ||
|
||
public TestEnum TestEnum { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters