Skip to content

Commit

Permalink
Refactor method calls and add new tests (#851)
Browse files Browse the repository at this point in the history
Modified `#if` directive in `NotNullWhenAttribute.cs` to include `NETSTANDARD2_0` and remove `NETSTANDARD1_3_OR_GREATER`. Updated `GenerateStaticMethodCall` in `ExpressionHelper.cs` to use `TryGetStaticMethod` instead of `GetStaticMethod`, throwing an `ArgumentException` if the method is not found. Replaced `GetStaticMethod` with `TryGetStaticMethod` in `ExpressionHelper.cs`. Added `MyView` class with nullable `Properties` dictionary to `ExpressionParserTests.cs`. Added `Parse_InvalidExpressionShouldThrowArgumentException` test to verify `ArgumentException` is thrown for invalid expressions.
  • Loading branch information
StefH authored Oct 31, 2024
1 parent 83010f8 commit f42dfb6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// SOFTWARE.
#endregion

#if NETSTANDARD1_3_OR_GREATER || NET35 || NET40 || NET45 || NET452 || NET46 || NETCOREAPP2_1 || UAP10_0
#if NETSTANDARD1_3 || NETSTANDARD2_0 || NET35 || NET40 || NET45 || NET452 || NET46 || NETCOREAPP2_1 || UAP10_0

// ReSharper disable once CheckNamespace
namespace System.Diagnostics.CodeAnalysis;
Expand Down
17 changes: 8 additions & 9 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,11 @@ private void TryConvertTypes(ref Expression left, ref Expression right)

private static Expression GenerateStaticMethodCall(string methodName, Expression left, Expression right)
{
var methodInfo = GetStaticMethod(methodName, left, right);
if (!TryGetStaticMethod(methodName, left, right, out var methodInfo))
{
throw new ArgumentException($"Method '{methodName}' not found on type '{left.Type}' or '{right.Type}'");
}

var parameters = methodInfo.GetParameters();
var parameterTypeLeft = parameters[0].ParameterType;
var parameterTypeRight = parameters[1].ParameterType;
Expand All @@ -495,15 +499,10 @@ private static Expression GenerateStaticMethodCall(string methodName, Expression
return Expression.Call(null, methodInfo, [left, right]);
}

private static MethodInfo GetStaticMethod(string methodName, Expression left, Expression right)
private static bool TryGetStaticMethod(string methodName, Expression left, Expression right, [NotNullWhen(true)] out MethodInfo? methodInfo)
{
var methodInfo = left.Type.GetMethod(methodName, [left.Type, right.Type]);
if (methodInfo == null)
{
methodInfo = right.Type.GetMethod(methodName, [left.Type, right.Type])!;
}

return methodInfo;
methodInfo = left.Type.GetMethod(methodName, [left.Type, right.Type]) ?? right.Type.GetMethod(methodName, [left.Type, right.Type]);
return methodInfo != null;
}

private static Expression? GetMethodCallExpression(MethodCallExpression methodCallExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public enum ExampleFlags
D = 8,
};

public class MyView
{
public Dictionary<string, string>? Properties { get; set; }
}

public ExpressionParserTests()
{
Expand Down Expand Up @@ -421,8 +425,18 @@ public void Parse_StringConcat(string expression, string result)

// Act
var parsedExpression = parser.Parse(typeof(string)).ToString();

// Assert
parsedExpression.Should().Be(result);
}

[Fact]
public void Parse_InvalidExpressionShouldThrowArgumentException()
{
// Arrange & Act
Action act = () => DynamicExpressionParser.ParseLambda<MyView, bool>(ParsingConfig.Default, false, "Properties[\"foo\"] > 2", Array.Empty<object>());

// Assert
act.Should().Throw<ArgumentException>().WithMessage("Method 'Compare' not found on type 'System.String' or 'System.Int32'");
}
}

0 comments on commit f42dfb6

Please sign in to comment.