Skip to content

Commit 0ef487c

Browse files
committed
support Properties, support modifier
1 parent 29e96ba commit 0ef487c

File tree

9 files changed

+287
-99
lines changed

9 files changed

+287
-99
lines changed

ExpressionDebugger.Tests/DebugInfoInjectorTest.cs

Lines changed: 100 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void TestBinary()
1919
public int Main(int a, int b)
2020
{
2121
return a + b;
22-
}"
22+
}".Trim()
2323
, str);
2424
}
2525

@@ -40,7 +40,7 @@ public void TestBinary_ArrayIndex()
4040
public int Main(int[] a)
4141
{
4242
return a[0];
43-
}"
43+
}".Trim()
4444
, str);
4545
}
4646

@@ -53,7 +53,7 @@ public void TestBlock()
5353
Assert.AreEqual(@"
5454
int p1;
5555
56-
p1 + p1;", str);
56+
p1 + p1;".Trim(), str);
5757
}
5858

5959
[TestMethod]
@@ -65,7 +65,7 @@ public void Test_Conditional()
6565
public int Main(int a)
6666
{
6767
return a < 0 ? a - 1 : a + 1;
68-
}"
68+
}".Trim()
6969
, str);
7070
}
7171

@@ -86,7 +86,7 @@ public void TestConditional_Block()
8686
else
8787
{
8888
3;
89-
}"
89+
}".Trim()
9090
, str);
9191
}
9292

@@ -115,7 +115,7 @@ public void TestConditional_Block_Chain()
115115
else
116116
{
117117
2;
118-
}"
118+
}".Trim()
119119
, str);
120120
}
121121

@@ -128,7 +128,7 @@ public void TestConstants()
128128
public char Main(string s)
129129
{
130130
return s == ""x"" || s == @""\"" || s == null || s.IsNormalized() == false || s.GetType() == typeof(string) ? 'x' : s[0];
131-
}"
131+
}".Trim()
132132
, str);
133133

134134
Expression<Func<string>> fn2 = () => 1f.ToString() + 2m.ToString() + ((byte)1).ToString() + DayOfWeek.Friday.ToString() + default(DateTime).ToString();
@@ -137,7 +137,7 @@ public char Main(string s)
137137
public string Main()
138138
{
139139
return 1f.ToString() + 2m.ToString() + ((byte)1).ToString() + DayOfWeek.Friday.ToString() + default(DateTime).ToString();
140-
}"
140+
}".Trim()
141141
, str2);
142142
}
143143

@@ -149,7 +149,7 @@ public void TestConstant_DateTime()
149149
var script = expr.ToScript();
150150
Assert.AreEqual(@"
151151
private DateTime DateTime1;
152-
DateTime1", script);
152+
DateTime1".Trim(), script);
153153
}
154154

155155
// [TestMethod]
@@ -224,7 +224,7 @@ public void TestDefault()
224224
{
225225
var exp = Expression.Default(typeof(int));
226226
var str = exp.ToScript();
227-
Assert.AreEqual(@"default(int)", str);
227+
Assert.AreEqual("default(int)", str);
228228
}
229229

230230
[TestMethod]
@@ -236,7 +236,7 @@ public void TestGroup()
236236
public int Main(int x)
237237
{
238238
return -(-x) + 1 + x - (1 - x);
239-
}"
239+
}".Trim()
240240
, str);
241241
}
242242

@@ -287,7 +287,7 @@ public void TestLambda()
287287
private int func1(int p1)
288288
{
289289
return p1 + 1;
290-
}"
290+
}".Trim()
291291
, str);
292292
}
293293

@@ -300,7 +300,7 @@ public void TestLambda_Inline()
300300
public IQueryable<int> Main(IQueryable<int> q)
301301
{
302302
return q.Where<int>(it => it > 0);
303-
}"
303+
}".Trim()
304304
, str);
305305
}
306306

@@ -313,7 +313,7 @@ public void TestListInit()
313313
public List<int> Main()
314314
{
315315
return new List<int>() {1, 2, 3};
316-
}"
316+
}".Trim()
317317
, str);
318318
}
319319

@@ -330,7 +330,7 @@ public void TestListInit_Dictionary()
330330
public Dictionary<int, int> Main()
331331
{
332332
return new Dictionary<int, int>() {{1, 2}, {3, 4}};
333-
}"
333+
}".Trim()
334334
, str);
335335
}
336336

@@ -367,7 +367,7 @@ public void TestLoop()
367367
p1--;
368368
continue;
369369
}
370-
}"
370+
}".Trim()
371371
, str);
372372
}
373373

@@ -384,7 +384,7 @@ public void TestLoop2()
384384
{
385385
goto label1;
386386
}
387-
label1:"
387+
label1:".Trim()
388388
, str);
389389
}
390390

@@ -397,7 +397,7 @@ public void TestMemberAccess()
397397
public int Main()
398398
{
399399
return DateTime.Now.Day;
400-
}"
400+
}".Trim()
401401
, str);
402402
}
403403

@@ -427,7 +427,7 @@ public DebugInfoInjectorTest.Poco Main()
427427
Parent = {Name = ""2""},
428428
Children = {new DebugInfoInjectorTest.Poco(), new DebugInfoInjectorTest.Poco()}
429429
};
430-
}"
430+
}".Trim()
431431
, str);
432432
}
433433

@@ -440,7 +440,7 @@ public void TestMethodCall_Default()
440440
public int Main(Dictionary<int, int> dict)
441441
{
442442
return dict[0];
443-
}"
443+
}".Trim()
444444
, str);
445445
}
446446

@@ -453,7 +453,7 @@ public void TestMethodCall()
453453
public string Main(List<int> list)
454454
{
455455
return list.ToString();
456-
}"
456+
}".Trim()
457457
, str);
458458
}
459459

@@ -466,7 +466,7 @@ public void TestMethodCall_Static()
466466
public int Main(int a, int b)
467467
{
468468
return Math.Max(a, b);
469-
}"
469+
}".Trim()
470470
, str);
471471
}
472472

@@ -479,7 +479,7 @@ public void TestNewArray()
479479
public int[] Main(int i)
480480
{
481481
return new int[] {i};
482-
}"
482+
}".Trim()
483483
, str);
484484
}
485485

@@ -492,7 +492,7 @@ public void TestNewArray_Bound()
492492
public int[] Main(int i)
493493
{
494494
return new int[i];
495-
}"
495+
}".Trim()
496496
, str);
497497
}
498498

@@ -505,7 +505,7 @@ public void TestParameter_Reserved()
505505
public int Main(int? @null)
506506
{
507507
return @null.Value;
508-
}"
508+
}".Trim()
509509
, str);
510510
}
511511

@@ -527,7 +527,7 @@ public void TestSwitch()
527527
default:
528528
0;
529529
break;
530-
}"
530+
}".Trim()
531531
, str);
532532
}
533533

@@ -559,7 +559,7 @@ public void TestTryCatch()
559559
finally
560560
{
561561
throw new NotSupportedException();
562-
}"
562+
}".Trim()
563563
, str);
564564
}
565565

@@ -583,7 +583,7 @@ public void TestTryFault()
583583
{
584584
""blah"";
585585
}
586-
}"
586+
}".Trim()
587587
, str);
588588
}
589589

@@ -596,7 +596,7 @@ public void TestTypeBinary()
596596
public bool Main(object o)
597597
{
598598
return o is Array;
599-
}"
599+
}".Trim()
600600
, str);
601601
}
602602

@@ -609,7 +609,7 @@ public void TestUnary_Convert()
609609
public int Main(double d)
610610
{
611611
return (int)d;
612-
}"
612+
}".Trim()
613613
, str);
614614
}
615615

@@ -622,7 +622,7 @@ public void TestUnary_ArrayLength()
622622
public int Main(int[] a)
623623
{
624624
return a.Length;
625-
}"
625+
}".Trim()
626626
, str);
627627
}
628628

@@ -635,7 +635,7 @@ public void TestUnary_As()
635635
public Expression Main(Expression expr)
636636
{
637637
return expr as UnaryExpression;
638-
}"
638+
}".Trim()
639639
, str);
640640
}
641641

@@ -659,7 +659,6 @@ public void TestToString()
659659
Assert.AreEqual(@"
660660
using System;
661661
662-
663662
namespace ExpressionDebugger.Tests
664663
{
665664
public static partial class MockClass
@@ -671,7 +670,7 @@ public static int Main()
671670
return GetInternal1.Invoke();
672671
}
673672
}
674-
}"
673+
}".Trim()
675674
, str);
676675
}
677676

@@ -685,7 +684,73 @@ public void TestExpression()
685684
{
686685
Id = data.Id + ""1"",
687686
Records = data.Records.Select<int, int>(it => it + 1)
688-
};", str);
687+
};".Trim(), str);
688+
}
689+
690+
[TestMethod]
691+
public void TestExtensionMethod()
692+
{
693+
var p1 = Expression.Parameter(typeof(int));
694+
var p2 = Expression.Parameter(typeof(int));
695+
var lambda = Expression.Lambda<Func<int, int, int>>(
696+
Expression.Add(p1, p2),
697+
p1, p2);
698+
var translator = new ExpressionTranslator(new TypeDefinitions
699+
{
700+
IsStatic = true,
701+
Namespace = "ExpressionDebugger.Tests",
702+
TypeName = "MockClass"
703+
});
704+
translator.VisitLambda(lambda, ExpressionTranslator.LambdaType.ExtensionMethod, "Add");
705+
var str = translator.ToString();
706+
Assert.AreEqual(@"
707+
namespace ExpressionDebugger.Tests
708+
{
709+
public static partial class MockClass
710+
{
711+
public static int Add(this int p1, int p2)
712+
{
713+
return p1 + p2;
714+
}
715+
}
716+
}".Trim(), str);
717+
}
718+
719+
[TestMethod]
720+
public void TestProperties()
721+
{
722+
var translator = new ExpressionTranslator(new TypeDefinitions
723+
{
724+
IsStatic = false,
725+
Namespace = "ExpressionDebugger.Tests",
726+
TypeName = "MockClass"
727+
});
728+
translator.Properties.Add(new PropertyDefinitions
729+
{
730+
Name = "Prop1",
731+
Type = typeof(string)
732+
});
733+
translator.Properties.Add(new PropertyDefinitions
734+
{
735+
Name = "Prop2",
736+
Type = typeof(string),
737+
IsReadOnly = true
738+
});
739+
var str = translator.ToString();
740+
Assert.AreEqual(@"
741+
namespace ExpressionDebugger.Tests
742+
{
743+
public partial class MockClass
744+
{
745+
public string Prop1 { get; set; }
746+
public string Prop2 { get; }
747+
748+
public MockClass(string prop2)
749+
{
750+
this.Prop2 = prop2;
751+
}
752+
}
753+
}".Trim(), str);
689754
}
690755

691756
public class Data

0 commit comments

Comments
 (0)