Skip to content

Commit 44ef4fc

Browse files
authored
Add extra unittests for NullPropagation / ToString / AllowEqualsAndToStringMethodsOnObject is true (#925)
* Add extra unittests for NullPropagation / ToString / AllowEqualsAndToStringMethodsOnObject is true * ContainInOrder
1 parent 7d39d33 commit 44ef4fc

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/System.Linq.Dynamic.Core/Parser/ExpressionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public bool TryGenerateAndAlsoNotNullExpression(Expression sourceExpression, boo
328328
{
329329
var expressions = CollectExpressions(addSelf, sourceExpression);
330330

331-
if (expressions.Count == 1 && !(expressions[0] is MethodCallExpression))
331+
if (expressions.Count == 1 && expressions[0] is not MethodCallExpression)
332332
{
333333
generatedExpression = sourceExpression;
334334
return false;

test/System.Linq.Dynamic.Core.Tests/Parser/ExpressionParserTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,7 @@ public void Parse_When_PrioritizePropertyOrFieldOverTheType_IsTrue(string expres
390390
CustomTypeProvider = _dynamicTypeProviderMock.Object,
391391
AllowEqualsAndToStringMethodsOnObject = true
392392
};
393-
ParameterExpression[] parameters = [ParameterExpressionHelper.CreateParameterExpression(typeof(Company), "company")
394-
];
393+
ParameterExpression[] parameters = [ ParameterExpressionHelper.CreateParameterExpression(typeof(Company), "company") ];
395394
var sut = new ExpressionParser(parameters, expression, null, config);
396395

397396
// Act

test/System.Linq.Dynamic.Core.Tests/QueryableTests.Where.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,58 @@ public void Where_Dynamic_CompareObjectToInt_ConvertObjectToSupportComparisonIsF
389389
act.Should().Throw<InvalidOperationException>().And.Message.Should().MatchRegex("The binary operator .* is not defined for the types");
390390
}
391391

392+
[Fact]
393+
public void Where_Dynamic_NullPropagation_Test1_On_NullableDoubleToString_When_AllowEqualsAndToStringMethodsOnObject_True()
394+
{
395+
// Arrange
396+
var config = new ParsingConfig
397+
{
398+
AllowEqualsAndToStringMethodsOnObject = true
399+
};
400+
var queryable = new[]
401+
{
402+
new { id = "1", d = (double?) null },
403+
new { id = "2", d = (double?) 5 },
404+
new { id = "3", d = (double?) 50 },
405+
new { id = "4", d = (double?) 40 }
406+
}.AsQueryable();
407+
408+
// Act
409+
var result = queryable
410+
.Where(config, """np(it.d, 0).ToString().StartsWith("5", StringComparison.OrdinalIgnoreCase)""")
411+
.Select<double?>("d")
412+
.ToArray();
413+
414+
// Assert
415+
result.Should().ContainInOrder(5, 50);
416+
}
417+
418+
[Fact]
419+
public void Where_Dynamic_NullPropagation_Test2_On_NullableDoubleToString_When_AllowEqualsAndToStringMethodsOnObject_True()
420+
{
421+
// Arrange
422+
var config = new ParsingConfig
423+
{
424+
AllowEqualsAndToStringMethodsOnObject = true
425+
};
426+
var queryable = new[]
427+
{
428+
new { id = "1", d = (double?) null },
429+
new { id = "2", d = (double?) 5 },
430+
new { id = "3", d = (double?) 50 },
431+
new { id = "4", d = (double?) 40 }
432+
}.AsQueryable();
433+
434+
// Act
435+
var result = queryable
436+
.Where(config, """np(it.d.ToString(), "").StartsWith("5", StringComparison.OrdinalIgnoreCase)""")
437+
.Select<double?>("d")
438+
.ToArray();
439+
440+
// Assert
441+
result.Should().ContainInOrder(5, 50);
442+
}
443+
392444
[ExcludeFromCodeCoverage]
393445
private class PersonWithObject
394446
{

0 commit comments

Comments
 (0)