|
1 |
| -using NFluent; |
2 |
| -using System.Collections.Generic; |
| 1 | +using System.Collections.Generic; |
3 | 2 | using System.Linq.Dynamic.Core.CustomTypeProviders;
|
4 | 3 | using System.Linq.Dynamic.Core.Exceptions;
|
5 | 4 | using System.Linq.Dynamic.Core.Tests.Helpers.Models;
|
| 5 | +using System.Linq.Dynamic.Core.Tests.TestHelpers; |
6 | 6 | using System.Linq.Expressions;
|
7 | 7 | using System.Reflection;
|
8 | 8 | using System.Runtime.CompilerServices;
|
9 | 9 | using FluentAssertions;
|
| 10 | +using NFluent; |
10 | 11 | using Xunit;
|
11 |
| -using User = System.Linq.Dynamic.Core.Tests.Helpers.Models.User; |
12 |
| -using System.Linq.Dynamic.Core.Tests.TestHelpers; |
13 | 12 |
|
14 | 13 | namespace System.Linq.Dynamic.Core.Tests
|
15 | 14 | {
|
16 | 15 | public class DynamicExpressionParserTests
|
17 | 16 | {
|
| 17 | + public class Foo |
| 18 | + { |
| 19 | + public Foo FooValue { get; set; } |
| 20 | + |
| 21 | + public string Zero() => null; |
| 22 | + |
| 23 | + public string One(int x) => null; |
| 24 | + |
| 25 | + public string Two(int x, int y) => null; |
| 26 | + } |
| 27 | + |
18 | 28 | private class MyClass
|
19 | 29 | {
|
20 | 30 | public List<string> MyStrings { get; set; }
|
@@ -291,15 +301,15 @@ public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQu
|
291 | 301 | dynamic wrappedObj = constantExpression.Value;
|
292 | 302 |
|
293 | 303 | var propertyInfo = wrappedObj.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public);
|
294 |
| - int value = (int) propertyInfo.GetValue(wrappedObj); |
| 304 | + int value = (int)propertyInfo.GetValue(wrappedObj); |
295 | 305 |
|
296 | 306 | Check.That(value).IsEqualTo(42);
|
297 | 307 | }
|
298 | 308 |
|
299 | 309 | [Theory]
|
300 | 310 | [InlineData("NullableIntValue", "42")]
|
301 | 311 | [InlineData("NullableDoubleValue", "42.23")]
|
302 |
| - public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_ForNullableProperty_true(string propName, string valueString) |
| 312 | + public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_ForNullableProperty_true(string propName, string valueString) |
303 | 313 | {
|
304 | 314 | // Assign
|
305 | 315 | var config = new ParsingConfig
|
@@ -1102,21 +1112,73 @@ public void DynamicExpressionParser_ParseLambda_SupportEnumerationStringComparis
|
1102 | 1112 | Check.That(result).IsEqualTo(expectedResult);
|
1103 | 1113 | }
|
1104 | 1114 |
|
| 1115 | + [Fact] |
| 1116 | + public void DynamicExpressionParser_ParseLambda_NullPropagation_InstanceMethod_Zero_Arguments() |
| 1117 | + { |
| 1118 | + // Arrange |
| 1119 | + var expression = "np(FooValue.Zero().Length)"; |
| 1120 | + |
| 1121 | + // Act |
| 1122 | + var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo()); |
| 1123 | + |
| 1124 | + // Assert |
| 1125 | +#if NET452 |
| 1126 | + lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.Zero() != null)), Convert(Param_0.FooValue.Zero().Length), null)"); |
| 1127 | +#else |
| 1128 | + lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.Zero() != null)), Convert(Param_0.FooValue.Zero().Length, Nullable`1), null)"); |
| 1129 | +#endif |
| 1130 | + } |
| 1131 | + |
| 1132 | + [Fact] |
| 1133 | + public void DynamicExpressionParser_ParseLambda_NullPropagation_InstanceMethod_One_Argument() |
| 1134 | + { |
| 1135 | + // Arrange |
| 1136 | + var expression = "np(FooValue.One(1).Length)"; |
| 1137 | + |
| 1138 | + // Act |
| 1139 | + var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo()); |
| 1140 | + |
| 1141 | + // Assert |
| 1142 | +#if NET452 |
| 1143 | + lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.One(1) != null)), Convert(Param_0.FooValue.One(1).Length), null)"); |
| 1144 | +#else |
| 1145 | + lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.One(1) != null)), Convert(Param_0.FooValue.One(1).Length, Nullable`1), null)"); |
| 1146 | +#endif |
| 1147 | + } |
| 1148 | + |
| 1149 | + [Fact] |
| 1150 | + public void DynamicExpressionParser_ParseLambda_NullPropagation_InstanceMethod_Two_Arguments() |
| 1151 | + { |
| 1152 | + // Arrange |
| 1153 | + var expression = "np(FooValue.Two(1, 42).Length)"; |
| 1154 | + |
| 1155 | + // Act |
| 1156 | + var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo()); |
| 1157 | + |
| 1158 | + // Assert |
| 1159 | +#if NET452 |
| 1160 | + lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.Two(1, 42) != null)), Convert(Param_0.FooValue.Two(1, 42).Length), null)"); |
| 1161 | +#else |
| 1162 | + lambdaExpression.ToString().Should().Be("Param_0 => IIF((((Param_0 != null) AndAlso (Param_0.FooValue != null)) AndAlso (Param_0.FooValue.Two(1, 42) != null)), Convert(Param_0.FooValue.Two(1, 42).Length, Nullable`1), null)"); |
| 1163 | +#endif |
| 1164 | + } |
| 1165 | + |
1105 | 1166 | [Fact]
|
1106 | 1167 | public void DynamicExpressionParser_ParseLambda_NullPropagation_MethodCallExpression()
|
1107 | 1168 | {
|
1108 | 1169 | // Arrange
|
1109 |
| - var dataSource = new MyClass(); |
| 1170 | + var myClass = new MyClass(); |
| 1171 | + var dataSource = new { MyClasses = new[] { myClass, null } }; |
1110 | 1172 |
|
1111 | 1173 | var expressionText = "np(MyClasses.FirstOrDefault())";
|
1112 | 1174 |
|
1113 | 1175 | // Act
|
1114 | 1176 | LambdaExpression expression = DynamicExpressionParser.ParseLambda(ParsingConfig.Default, dataSource.GetType(), typeof(MyClass), expressionText);
|
1115 | 1177 | Delegate del = expression.Compile();
|
1116 |
| - MyClass result = del.DynamicInvoke(dataSource) as MyClass; |
| 1178 | + var result = del.DynamicInvoke(dataSource) as MyClass; |
1117 | 1179 |
|
1118 | 1180 | // Assert
|
1119 |
| - result.Should().BeNull(); |
| 1181 | + result.Should().Be(myClass); |
1120 | 1182 | }
|
1121 | 1183 |
|
1122 | 1184 | [Theory]
|
|
0 commit comments