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
2 changes: 1 addition & 1 deletion src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private object ResolveSymbol(Expr.Reference expr)
if (TypeUtils.CanConvertPrimitive(expression.Type, conversionType))
return Expression.Convert(expression, conversionType);

var conversion = TypeUtils.FindConversionOperator(expression.Type, typeof(bool));
var conversion = TypeUtils.FindConversionOperator(expression.Type, conversionType);
if (conversion is not null)
return Expression.Convert(expression, conversionType, conversion);

Expand Down
43 changes: 43 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ public void Evaluate_StaticDelegateMemberOfType_CallsDelegate(string expression)
Assert.That(value, Is.EqualTo(6));
}

[TestCase("Missing ?? StringOnly", "string")]
[TestCase("Missing ?? StringAndBool", "string")]
[TestCase("""Flag ? StringOnly : "x" """, "string")]
[TestCase("StringAndBool && Flag", true)]
public void Evaluate_UserDefinedImplicitConversion_UsesOperatorForTargetType(string expression, object expected) =>
Assert.That(ExpressionParser.Evaluate(expression, new ConversionContext()), Is.EqualTo(expected));

[Test]
public void Parse_NoUserDefinedImplicitConversionToTargetType_ReturnsError()
{
var parameter = Expression.Parameter(typeof(ConversionContext), "c");
var result = ExpressionParser.Parse("Missing ?? BoolOnly", new ContextAwareBinder(parameter));

Assert.That(result.Success, Is.False);
Assert.That(result.ErrorMessage, Is.EqualTo(
$"Operator '??' cannot be applied to operands of type 'System.String' and '{typeof(ConvertibleToBool)}'."));
}

[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 @@ -521,6 +539,31 @@ public static class StaticDelegates
public static Func<int, int> Tripler { get; } = x => x * 3;
}

public class ConversionContext
{
public string? Missing { get; }
public bool Flag { get; } = true;
public ConvertibleToString StringOnly { get; } = new();
public ConvertibleToStringAndBool StringAndBool { get; } = new();
public ConvertibleToBool BoolOnly { get; } = new();
}

public class ConvertibleToString
{
public static implicit operator string(ConvertibleToString _) => "string";
}

public class ConvertibleToStringAndBool
{
public static implicit operator string(ConvertibleToStringAndBool _) => "string";
public static implicit operator bool(ConvertibleToStringAndBool _) => true;
}

public class ConvertibleToBool
{
public static implicit operator bool(ConvertibleToBool _) => true;
}

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