@@ -698,6 +698,48 @@ public void StringConcatTest(string source, string expected)
698698 Assert . That ( result . ToString ( ) , Is . EqualTo ( expected ) ) ;
699699 }
700700
701+ [ TestCase ( "1 :> long + 1" , "((1 :> long) + 1)" ) ]
702+ [ TestCase ( "1 + 2 :> long" , "(1 + (2 :> long))" ) ]
703+ [ TestCase ( "a == b :> bool" , "(a == (b :> bool))" ) ]
704+ [ TestCase ( "-x :> int" , "-((x :> int))" ) ]
705+ [ TestCase ( "a ? b : c :> int" , "(a ? b : (c :> int))" ) ]
706+ public void ToString_ConversionOperand_ParenthesizesConversion ( string source , string expected ) =>
707+ Assert . That ( ExpressionParser . Parser . Parse ( source ) . Value ! . ToString ( ) , Is . EqualTo ( expected ) ) ;
708+
709+ [ TestCaseSource ( nameof ( BuiltOperandSource ) ) ]
710+ public void ToString_BuiltTreeOperand_ParenthesizesOperand ( Expr expr , string expected ) =>
711+ Assert . That ( expr . ToString ( ) , Is . EqualTo ( expected ) ) ;
712+
713+ private static IEnumerable < TestCaseData > BuiltOperandSource
714+ {
715+ get
716+ {
717+ //
718+ // The parser wraps such operands in Expr.Parenthesized, but a tree can be built without it.
719+ //
720+ var conditional = new Expr . Conditional ( Reference ( "a" ) , Reference ( "b" ) , Reference ( "c" ) ) ;
721+ var conversion = new Expr . Unary ( new Identifier ( "object" ) , UnaryType . Convert , Reference ( "x" ) ) ;
722+
723+ yield return Case ( "conditional in binary" ,
724+ new Expr . Binary ( new Identifier ( "+" ) , conditional , new Expr . Literal ( 1 ) ) ,
725+ "((a ? b : c) + 1)" ) ;
726+
727+ yield return Case ( "conditional in conversion" ,
728+ new Expr . Unary ( new Identifier ( "int" ) , UnaryType . Convert , conditional ) ,
729+ "((a ? b : c) :> int)" ) ;
730+
731+ yield return Case ( "conversion in member access" ,
732+ new Expr . Call ( new Expr . MemberAccess ( conversion , Reference ( "ToString" ) ) , [ ] ) ,
733+ "(x :> object).ToString()" ) ;
734+
735+ static Expr Reference ( string name ) =>
736+ new Expr . Reference ( new Identifier ( name ) ) ;
737+
738+ static TestCaseData Case ( string name , Expr expr , string expected ) =>
739+ new TestCaseData ( expr , expected ) . SetArgDisplayNames ( name ) ;
740+ }
741+ }
742+
701743 private static void AssertNullableResult ( string expression , Type type , object ? expected ) =>
702744 AssertResult ( new NullableContext ( ) , expression , type , expected ) ;
703745
0 commit comments