22using System . Collections . Generic ;
33using System . Linq ;
44using System . Linq . Expressions ;
5+ using System . Reflection ;
56using Microsoft . VisualStudio . TestTools . UnitTesting ;
67
78namespace ExpressionDebugger . Tests
@@ -22,6 +23,14 @@ public int Main(int a, int b)
2223 , str ) ;
2324 }
2425
26+ [ TestMethod ]
27+ public void TestBinary_PowerAssign ( )
28+ {
29+ var exp = Expression . PowerAssign ( Expression . Variable ( typeof ( double ) , "d" ) , Expression . Constant ( 2d ) ) ;
30+ var str = exp . ToScript ( ) ;
31+ Assert . AreEqual ( "d = Math.Pow(d, 2d)" , str ) ;
32+ }
33+
2534 [ TestMethod ]
2635 public void TestBinary_ArrayIndex ( )
2736 {
@@ -111,16 +120,25 @@ public void TestConditional_Block_Chain()
111120 }
112121
113122 [ TestMethod ]
114- public void TestConstant ( )
123+ public void TestConstants ( )
115124 {
116- Expression < Func < string , char > > fn = s => s == "x" || s == null || s . IsNormalized ( ) == false || s . GetType ( ) == typeof ( string ) ? 'x' : s [ 0 ] ;
125+ Expression < Func < string , char > > fn = s => s == "x" || s == @"\" || s == null || s . IsNormalized ( ) == false || s . GetType ( ) == typeof ( string ) ? 'x' : s [ 0 ] ;
117126 var str = fn . ToScript ( ) ;
118127 Assert . AreEqual ( @"
119128public char Main(string s)
120129{
121- return s == ""x"" || s == null || s.IsNormalized() == false || s.GetType() == typeof(string) ? 'x' : s[0];
130+ return s == ""x"" || s == @""\"" || s == null || s.IsNormalized() == false || s.GetType() == typeof(string) ? 'x' : s[0];
122131}"
123132 , str ) ;
133+
134+ Expression < Func < string > > fn2 = ( ) => 1f . ToString ( ) + 2m . ToString ( ) + ( ( byte ) 1 ) . ToString ( ) + DayOfWeek . Friday . ToString ( ) + default ( DateTime ) . ToString ( ) ;
135+ var str2 = fn2 . ToScript ( ) ;
136+ Assert . AreEqual ( @"
137+ public string Main()
138+ {
139+ return 1f.ToString() + 2m.ToString() + ((byte)1).ToString() + DayOfWeek.Friday.ToString() + default(DateTime).ToString();
140+ }"
141+ , str2 ) ;
124142 }
125143
126144 [ TestMethod ]
@@ -130,7 +148,7 @@ public void TestConstant_DateTime()
130148 var expr = Expression . Constant ( now ) ;
131149 var script = expr . ToScript ( ) ;
132150 Assert . AreEqual ( @"
133- public DateTime DateTime1;
151+ private DateTime DateTime1;
134152DateTime1" , script ) ;
135153 }
136154
@@ -222,6 +240,26 @@ public int Main(int x)
222240 , str ) ;
223241 }
224242
243+ [ TestMethod ]
244+ public void TestGroup_MultiLine ( )
245+ {
246+ var p = Expression . Variable ( typeof ( int ) , "p" ) ;
247+ var exp = Expression . Add (
248+ p ,
249+ Expression . Block (
250+ new Expression [ ] {
251+ Expression . Call ( typeof ( Console ) . GetMethod ( nameof ( Console . WriteLine ) , new [ ] { typeof ( int ) } ) , p ) ,
252+ p ,
253+ }
254+ ) ) ;
255+ var str = exp . ToScript ( ) ;
256+ Assert . AreEqual ( @"p + (new Func<int>(() => {
257+ Console.WriteLine(p);
258+ return p;
259+ }))()"
260+ , str ) ;
261+ }
262+
225263 [ TestMethod ]
226264 public void TestIndex ( )
227265 {
@@ -597,6 +635,42 @@ public void TestUnary_As()
597635public Expression Main(Expression expr)
598636{
599637 return expr as UnaryExpression;
638+ }"
639+ , str ) ;
640+ }
641+
642+ internal static int GetInternal ( ) => 1 ;
643+
644+ [ TestMethod ]
645+ public void TestToString ( )
646+ {
647+ var call = Expression . Call (
648+ typeof ( DebugInfoInjectorTest ) . GetMethod ( nameof ( GetInternal ) ,
649+ BindingFlags . Static | BindingFlags . NonPublic )
650+ ) ;
651+ var exp = Expression . Lambda < Func < int > > ( call ) ;
652+ var str = exp . ToScript ( new ExpressionDefinitions
653+ {
654+ IsStatic = true ,
655+ MethodName = "Main" ,
656+ Namespace = "ExpressionDebugger.Tests" ,
657+ TypeName = "MockClass"
658+ } ) ;
659+ Assert . AreEqual ( @"
660+ using System;
661+
662+
663+ namespace ExpressionDebugger.Tests
664+ {
665+ public static partial class MockClass
666+ {
667+ private static Func<int> GetInternal1;
668+
669+ public static int Main()
670+ {
671+ return GetInternal1.Invoke();
672+ }
673+ }
600674}"
601675 , str ) ;
602676 }
0 commit comments