Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,11 @@ Expression Invoke(Expression? instance, MethodInfo? method)

if (instance is not null)
{
Debug.Assert(method is null && typeof(Delegate).IsAssignableFrom(instance.Type)
|| method is not null && !typeof(Delegate).IsAssignableFrom(instance.Type));
//
// Without a method, the instance is a delegate to invoke. A method can also be called
// on a delegate, as in 'func.Invoke(1)' or 'func.ToString()'.
//
Debug.Assert(method is not null || typeof(Delegate).IsAssignableFrom(instance.Type));

if (method is null)
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ public void Evaluate_ArrayForParamArray_PassesArray(string expression, object ex
public void Evaluate_ElementsForParamArray_CreatesArray(string expression, object expected) =>
Assert.That(ExpressionParser.Evaluate(expression, new ParamArrayContext()), Is.EqualTo(expected));

[TestCase("Doubler(2)", 4)]
[TestCase("Doubler.Invoke(2)", 4)]
[TestCase("Doubler.ToString()", "System.Func`2[System.Int32,System.Int32]")]
public void Evaluate_DelegateMember_CallsDelegateAndItsMethods(string expression, object expected) =>
Assert.That(ExpressionParser.Evaluate(expression, new DelegateContext()), Is.EqualTo(expected));

[TestCase("Defaults()", "1 Monday True null")]
[TestCase("""Defaults(5, DayOfWeek.Friday, Guid.NewGuid(), "a")""", "5 Friday False a")]
[TestCase("Mixed(1)", "1 2 0")]
Expand Down Expand Up @@ -491,6 +497,11 @@ public class ParamArrayContext
public bool IsWords(params string[] values) => ReferenceEquals(values, Words);
}

public class DelegateContext
{
public Func<int, int> Doubler { get; } = x => x * 2;
}

public class OptionalContext
{
public static string Defaults(int number = 1, DayOfWeek? day = DayOfWeek.Monday, Guid id = default, string? text = null) =>
Expand Down
Loading