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/Internal/TypeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class TypeUtils
/* Boolean */ Primitives.Boolean,
/* Char */ Primitives.Char | Primitives.UInt16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double | Primitives.Decimal,
/* SByte */ Primitives.SByte | Primitives.Int16 | Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double | Primitives.Decimal,
/* Byte */ Primitives.Byte | Primitives.Char | Primitives.UInt16 | Primitives.Int16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double | Primitives.Decimal,
/* Byte */ Primitives.Byte | Primitives.UInt16 | Primitives.Int16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double | Primitives.Decimal,
/* Int16 */ Primitives.Int16 | Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double | Primitives.Decimal,
/* UInt16 */ Primitives.UInt16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double | Primitives.Decimal,
/* Int32 */ Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double | Primitives.Decimal,
Expand Down
3 changes: 3 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ Operator '?:' cannot be applied to operands of type 'System.Int32' and 'System.U
true ? 1 : 1u
Operator '?:' cannot be applied to operands of type 'System.Int32' and 'System.UInt32'.

true ? 1 :> byte : 'c'
Operator '?:' cannot be applied to operands of type 'System.Byte' and 'System.Char'.

true ? 1 : "a"
Operator '?:' cannot be applied to operands of type 'System.Int32' and 'System.String'.

Expand Down
21 changes: 21 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ public void LogicalNotOnOnesComplementOnlyTest()
$"Unary operator '!' cannot be applied to operand of type '{typeof(OnesComplementOperand)}'."));
}

[TestCase("Pick(97 :> byte)", "int")]
[TestCase("Pick('a')", "char")]
public void Evaluate_ByteArgument_DoesNotSelectCharOverload(string expression, string expected) =>
Assert.That(ExpressionParser.Evaluate(expression, new OverloadContext()), Is.EqualTo(expected));

[Test]
public void Parse_ByteArgumentForCharParameter_ReturnsError()
{
var parameter = Expression.Parameter(typeof(OverloadContext), "c");
var result = ExpressionParser.Parse("OnlyChar(97 :> byte)", new ContextAwareBinder(parameter));

Assert.That(result.Success, Is.False);
}

[TestCaseSource(nameof(ErrorsSource))]
public void ParseErrorExpression(string expression, string expected)
{
Expand Down Expand Up @@ -328,4 +342,11 @@ public class OnesComplementOperand
{
public static bool operator ~(OnesComplementOperand _) => true;
}

public class OverloadContext
{
public static string Pick(char _) => "char";
public static string Pick(int _) => "int";
public static string OnlyChar(char _) => "char";
}
}
4 changes: 4 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/TypeUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public void CanConvertPrimitive_IntegerToDecimal_ReturnsTrue(Type source) =>
public void CanConvertPrimitive_ExplicitDecimalConversion_ReturnsFalse(Type source, Type target) =>
Assert.That(TypeUtils.CanConvertPrimitive(source, target), Is.False);

[Test]
public void CanConvertPrimitive_ByteToChar_ReturnsFalse() =>
Assert.That(TypeUtils.CanConvertPrimitive(typeof(byte), typeof(char)), Is.False);

[TestCase(typeof(int), typeof(DayOfWeek))]
[TestCase(typeof(DayOfWeek), typeof(int))]
[TestCase(typeof(DayOfWeek), typeof(DayOfWeek))]
Expand Down
Loading