From cdb09425de8d0883c8bf9117bff59c5e9856912c Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Thu, 2 May 2024 14:45:50 +0200 Subject: [PATCH] Add extra unit test for calling methods on a string property --- .../DynamicExpressionParserTests.cs | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs b/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs index 024dc08a..d03cd692 100644 --- a/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs @@ -48,7 +48,9 @@ public void Bar() Name = nameof(Foo); } - public string Name { get; set; } + public string? Name { get; set; } + + public string? Description { get; set; } public MyClass Child { get; set; } } @@ -1683,7 +1685,7 @@ public void DynamicExpressionParser_ParseLambda_StringEquals_WithCombinedConditi // #803 [Fact] - public void DynamicExpressionParser_ParseLambda_StringEquals_WithConstantString() + public void DynamicExpressionParser_ParseLambda_Calling_Equals_On_ConstantString() { // Arrange var parameters = new[] @@ -1691,16 +1693,45 @@ public void DynamicExpressionParser_ParseLambda_StringEquals_WithConstantString( Expression.Parameter(typeof(MyClass), "myClass") }; - var invokerArguments = new List + var invokerArguments = new object[] { new MyClass { Name = "Foo" } }; // Act - var expression = "Name == \"test\" || \"foo\".Equals(it.Name, StringComparison.OrdinalIgnoreCase)"; + var expression = "Name == \"test\" || Name.ToLower() == \"abc\" or \"foo\".Equals(it.Name, StringComparison.OrdinalIgnoreCase)"; var lambdaExpression = DynamicExpressionParser.ParseLambda(parameters, null, expression); var del = lambdaExpression.Compile(); - var result = del.DynamicInvoke(invokerArguments.ToArray()); + var result = del.DynamicInvoke(invokerArguments); + + // Assert + result.Should().Be(true); + } + + // #810 + [Fact] + public void DynamicExpressionParser_ParseLambda_Calling_ToLower_On_String() + { + // Arrange + var parameters = new[] + { + Expression.Parameter(typeof(MyClass), "myClass") + }; + + var invokerArguments = new object[] + { + new MyClass + { + Name = "Foo", + Description = "Bar" + } + }; + + // Act + var expression = "(Name != null && Convert.ToString(Name).ToLower().Contains(\"foo\")) or (Description != null && Convert.ToString(Description).ToLower().Contains(\"bar\"))"; + var lambdaExpression = DynamicExpressionParser.ParseLambda(parameters, null, expression); + var del = lambdaExpression.Compile(); + var result = del.DynamicInvoke(invokerArguments); // Assert result.Should().Be(true);