Skip to content

Commit 80bb593

Browse files
committed
Adding parentheses around equality test condition operands - re: issue #22
1 parent d9f0c07 commit 80bb593

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

ReadableExpressions.UnitTests/WhenTranslatingConditionals.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,22 @@ public void ShouldTranslateAssignmentOutcomeTests()
483483

484484
translated.ShouldBe(EXPECTED.TrimStart());
485485
}
486+
487+
// See https://github.com/agileobjects/ReadableExpressions/issues/22
488+
[Fact]
489+
public void ShouldTranslateEnumComparisonTests()
490+
{
491+
var flagParameter = Expression.Parameter(typeof(bool), "flag");
492+
var one = Expression.Constant(Test.One);
493+
var two = Expression.Constant(Test.Two);
494+
var oneOrTwo = Expression.Condition(flagParameter, one, two);
495+
var oneOrTwoEqualsTwo = Expression.Equal(oneOrTwo, two);
496+
var testLambda = Expression.Lambda<Func<bool, bool>>(oneOrTwoEqualsTwo, flagParameter);
497+
498+
var translated = ToReadableString(testLambda);
499+
500+
translated.ShouldBe("flag => (flag ? Test.One : Test.Two) == Test.Two");
501+
}
486502
}
487503

488504
#region Helpers
@@ -495,5 +511,7 @@ public bool MultipleParameterMethod(string stringValue, int intValue)
495511
}
496512
}
497513

514+
internal enum Test { One, Two };
515+
498516
#endregion
499517
}

ReadableExpressions/Translators/BinaryExpressionTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private static string TranslateOperand(Expression expression, TranslationContext
9090

9191
var translation = context.Translate(expression);
9292

93-
return expression.IsAssignment()
93+
return (expression.NodeType == ExpressionType.Conditional) || expression.IsAssignment()
9494
? translation.WithSurroundingParentheses()
9595
: translation;
9696
}

0 commit comments

Comments
 (0)