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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public override T Accept<T>(ExprVisitor<T> visitor) =>

/// <inheritdoc />
public override string ToString() =>
$"{Test} ? {IfTrue} : {IfFalse}";
$"({Test} ? {IfTrue} : {IfFalse})";
}
}
4 changes: 2 additions & 2 deletions src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public override T Accept<T>(ExprVisitor<T> visitor) =>

/// <inheritdoc />
/// <returns>
/// A string representation of the unary expression, e.g., <c>-(x)</c> for operators or <c>x :> int</c> for conversions.
/// A string representation of the unary expression, e.g., <c>-(x)</c> for operators or <c>(x :> int)</c> for conversions.
/// </returns>
public override string ToString() => UnaryType switch
{
UnaryType.Convert => $"{Operand} :> {Operator}",
UnaryType.Convert => $"({Operand} :> {Operator})",
_ => $"{Operator}({Operand})"
};
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,48 @@ public void StringConcatTest(string source, string expected)
Assert.That(result.ToString(), Is.EqualTo(expected));
}

[TestCase("1 :> long + 1", "((1 :> long) + 1)")]
[TestCase("1 + 2 :> long", "(1 + (2 :> long))")]
[TestCase("a == b :> bool", "(a == (b :> bool))")]
[TestCase("-x :> int", "-((x :> int))")]
[TestCase("a ? b : c :> int", "(a ? b : (c :> int))")]
public void ToString_ConversionOperand_ParenthesizesConversion(string source, string expected) =>
Assert.That(ExpressionParser.Parser.Parse(source).Value!.ToString(), Is.EqualTo(expected));

[TestCaseSource(nameof(BuiltOperandSource))]
public void ToString_BuiltTreeOperand_ParenthesizesOperand(Expr expr, string expected) =>
Assert.That(expr.ToString(), Is.EqualTo(expected));

private static IEnumerable<TestCaseData> BuiltOperandSource
{
get
{
//
// The parser wraps such operands in Expr.Parenthesized, but a tree can be built without it.
//
var conditional = new Expr.Conditional(Reference("a"), Reference("b"), Reference("c"));
var conversion = new Expr.Unary(new Identifier("object"), UnaryType.Convert, Reference("x"));

yield return Case("conditional in binary",
new Expr.Binary(new Identifier("+"), conditional, new Expr.Literal(1)),
"((a ? b : c) + 1)");

yield return Case("conditional in conversion",
new Expr.Unary(new Identifier("int"), UnaryType.Convert, conditional),
"((a ? b : c) :> int)");

yield return Case("conversion in member access",
new Expr.Call(new Expr.MemberAccess(conversion, Reference("ToString")), []),
"(x :> object).ToString()");

static Expr Reference(string name) =>
new Expr.Reference(new Identifier(name));

static TestCaseData Case(string name, Expr expr, string expected) =>
new TestCaseData(expr, expected).SetArgDisplayNames(name);
}
}

private static void AssertNullableResult(string expression, Type type, object? expected) =>
AssertResult(new NullableContext(), expression, type, expected);

Expand Down