|
1 |
| -using Newtonsoft.Json.Linq; |
| 1 | +using FluentAssertions; |
| 2 | +using Newtonsoft.Json.Linq; |
2 | 3 | using NFluent;
|
3 | 4 | using System.Collections.Generic;
|
4 | 5 | using System.Dynamic;
|
@@ -49,6 +50,17 @@ public class TestObjectIdClass
|
49 | 50 | public long ObjectId { get; set; }
|
50 | 51 | }
|
51 | 52 |
|
| 53 | + public class Foo |
| 54 | + { |
| 55 | + public Foo FooValue { get; set; } |
| 56 | + |
| 57 | + public string Zero() => null; |
| 58 | + |
| 59 | + public string One(int x) => null; |
| 60 | + |
| 61 | + public string Two(int x, int y) => null; |
| 62 | + } |
| 63 | + |
52 | 64 | [Fact]
|
53 | 65 | public void ExpressionTests_Add_Number()
|
54 | 66 | {
|
@@ -1394,6 +1406,72 @@ public void ExpressionTests_NullPropagating(string test, string query)
|
1394 | 1406 | Check.That(queryAsString).Equals(query);
|
1395 | 1407 | }
|
1396 | 1408 |
|
| 1409 | + [Fact] |
| 1410 | + public void ExpressionTests_NullPropagating_InstanceMethod_Zero_Arguments() |
| 1411 | + { |
| 1412 | + // Arrange 1 |
| 1413 | + var expression = "np(FooValue.Zero().Length)"; |
| 1414 | + |
| 1415 | + // Act 1 |
| 1416 | + var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo()); |
| 1417 | + |
| 1418 | + // Assert 1 |
| 1419 | + 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)"); |
| 1420 | + |
| 1421 | + // Arrange 2 |
| 1422 | + var q = new[] { new Foo { FooValue = new Foo() } }.AsQueryable(); |
| 1423 | + |
| 1424 | + // Act 2 |
| 1425 | + var result = q.Select(expression).FirstOrDefault() as string; |
| 1426 | + |
| 1427 | + // Assert 2 |
| 1428 | + result.Should().BeNull(); |
| 1429 | + } |
| 1430 | + |
| 1431 | + [Fact] |
| 1432 | + public void ExpressionTests_NullPropagating_InstanceMethod_One_Argument() |
| 1433 | + { |
| 1434 | + // Arrange 1 |
| 1435 | + var expression = "np(FooValue.One(1).Length)"; |
| 1436 | + |
| 1437 | + // Act 1 |
| 1438 | + var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo()); |
| 1439 | + |
| 1440 | + // Assert 1 |
| 1441 | + 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)"); |
| 1442 | + |
| 1443 | + // Arrange 2 |
| 1444 | + var q = new[] { new Foo { FooValue = new Foo() } }.AsQueryable(); |
| 1445 | + |
| 1446 | + // Act 2 |
| 1447 | + var result = q.Select(expression).FirstOrDefault() as string; |
| 1448 | + |
| 1449 | + // Assert 2 |
| 1450 | + result.Should().BeNull(); |
| 1451 | + } |
| 1452 | + |
| 1453 | + [Fact] |
| 1454 | + public void ExpressionTests_NullPropagating_InstanceMethod_Two_Arguments() |
| 1455 | + { |
| 1456 | + // Arrange 1 |
| 1457 | + var expression = "np(FooValue.Two(1, 42).Length)"; |
| 1458 | + |
| 1459 | + // Act 1 |
| 1460 | + var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(Foo), null, expression, new Foo()); |
| 1461 | + |
| 1462 | + // Assert 1 |
| 1463 | + 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)"); |
| 1464 | + |
| 1465 | + // Arrange 2 |
| 1466 | + var q = new[] { new Foo { FooValue = new Foo() } }.AsQueryable(); |
| 1467 | + |
| 1468 | + // Act 2 |
| 1469 | + var result = q.Select(expression).FirstOrDefault() as string; |
| 1470 | + |
| 1471 | + // Assert 2 |
| 1472 | + result.Should().BeNull(); |
| 1473 | + } |
| 1474 | + |
1397 | 1475 | [Fact]
|
1398 | 1476 | public void ExpressionTests_NullPropagation_Method()
|
1399 | 1477 | {
|
|
0 commit comments