|
| 1 | +using System.Linq.Dynamic.Core.Exceptions; |
| 2 | +using System.Linq.Expressions; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace System.Linq.Dynamic.Core.Tests |
| 6 | +{ |
| 7 | + public class DynamicExpressionParserTests |
| 8 | + { |
| 9 | + [Fact] |
| 10 | + public void Parse_ParameterExpressionMethodCall_ReturnsIntExpression() |
| 11 | + { |
| 12 | + var expression = DynamicExpressionParser.ParseLambda(true, |
| 13 | + new[] { Expression.Parameter(typeof(int), "x") }, |
| 14 | + typeof(int), |
| 15 | + "x + 1"); |
| 16 | + Assert.Equal(typeof(int), expression.Body.Type); |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void Parse_TupleToStringMethodCall_ReturnsStringLambdaExpression() |
| 21 | + { |
| 22 | + var expression = DynamicExpressionParser.ParseLambda( |
| 23 | + typeof(Tuple<int>), |
| 24 | + typeof(string), |
| 25 | + "it.ToString()"); |
| 26 | + Assert.Equal(typeof(string), expression.ReturnType); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void Parse_StringLiteral_ReturnsBooleanLambdaExpression() |
| 31 | + { |
| 32 | + var expression = DynamicExpressionParser.ParseLambda(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"test\""); |
| 33 | + Assert.Equal(typeof(Boolean), expression.Body.Type); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void Parse_StringLiteralEmpty_ReturnsBooleanLambdaExpression() |
| 38 | + { |
| 39 | + var expression = DynamicExpressionParser.ParseLambda(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"\""); |
| 40 | + Assert.Equal(typeof(Boolean), expression.Body.Type); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void Parse_StringLiteralEmbeddedQuote_ReturnsBooleanLambdaExpression() |
| 45 | + { |
| 46 | + string expectedRightValue = "\"test \\\"string\""; |
| 47 | + var expression = DynamicExpressionParser.ParseLambda( |
| 48 | + new[] { Expression.Parameter(typeof(string), "Property1") }, |
| 49 | + typeof(Boolean), |
| 50 | + string.Format("Property1 == {0}", expectedRightValue)); |
| 51 | + |
| 52 | + string rightValue = ((BinaryExpression)expression.Body).Right.ToString(); |
| 53 | + Assert.Equal(typeof(Boolean), expression.Body.Type); |
| 54 | + Assert.Equal(expectedRightValue, rightValue); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Parse_StringLiteralStartEmbeddedQuote_ReturnsBooleanLambdaExpression() |
| 59 | + { |
| 60 | + string expectedRightValue = "\"\\\"test\""; |
| 61 | + var expression = DynamicExpressionParser.ParseLambda( |
| 62 | + new[] { Expression.Parameter(typeof(string), "Property1") }, |
| 63 | + typeof(Boolean), |
| 64 | + string.Format("Property1 == {0}", expectedRightValue)); |
| 65 | + |
| 66 | + string rightValue = ((BinaryExpression)expression.Body).Right.ToString(); |
| 67 | + Assert.Equal(typeof(Boolean), expression.Body.Type); |
| 68 | + Assert.Equal(expectedRightValue, rightValue); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void Parse_StringLiteral_MissingClosingQuote() |
| 73 | + { |
| 74 | + string expectedRightValue = "\"test\\\""; |
| 75 | + |
| 76 | + Assert.Throws<ParseException>(() => DynamicExpressionParser.ParseLambda( |
| 77 | + new[] { Expression.Parameter(typeof(string), "Property1") }, |
| 78 | + typeof(Boolean), |
| 79 | + string.Format("Property1 == {0}", expectedRightValue))); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void Parse_StringLiteralEscapedBackslash_ReturnsBooleanLambdaExpression() |
| 84 | + { |
| 85 | + string expectedRightValue = "\"test\\string\""; |
| 86 | + var expression = DynamicExpressionParser.ParseLambda( |
| 87 | + new[] { Expression.Parameter(typeof(string), "Property1") }, |
| 88 | + typeof(Boolean), |
| 89 | + string.Format("Property1 == {0}", expectedRightValue)); |
| 90 | + |
| 91 | + string rightValue = ((BinaryExpression)expression.Body).Right.ToString(); |
| 92 | + Assert.Equal(typeof(Boolean), expression.Body.Type); |
| 93 | + Assert.Equal(expectedRightValue, rightValue); |
| 94 | + } |
| 95 | + |
| 96 | + //[Fact] |
| 97 | + //public void ParseLambda_DelegateTypeMethodCall_ReturnsEventHandlerLambdaExpression() |
| 98 | + //{ |
| 99 | + // var expression = DynamicExpressionParser.ParseLambda(true, |
| 100 | + // typeof(EventHandler), |
| 101 | + // null, |
| 102 | + // new[] { Expression.Parameter(typeof(object), "sender"), Expression.Parameter(typeof(EventArgs), "e") }, |
| 103 | + // "sender.ToString()"); |
| 104 | + |
| 105 | + // Assert.Equal(typeof(void), expression.ReturnType); |
| 106 | + // Assert.Equal(typeof(EventHandler), expression.Type); |
| 107 | + //} |
| 108 | + |
| 109 | + //[Fact] this should fail : not allowed |
| 110 | + //public void ParseLambda_VoidMethodCall_ReturnsActionDelegate() |
| 111 | + //{ |
| 112 | + // var expression = DynamicExpressionParser.ParseLambda( |
| 113 | + // typeof(IO.FileStream), |
| 114 | + // null, |
| 115 | + // "it.Close()"); |
| 116 | + // Assert.Equal(typeof(void), expression.ReturnType); |
| 117 | + // Assert.Equal(typeof(Action<IO.FileStream>), expression.Type); |
| 118 | + //} |
| 119 | + } |
| 120 | +} |
0 commit comments