Skip to content

Commit 190578e

Browse files
committed
add unit tests
1 parent 2337bd0 commit 190578e

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

ExpressionDebugger.Console/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ static void Main(string[] args)
99
{
1010
var p1 = Expression.Parameter(typeof(int));
1111
var p2 = Expression.Parameter(typeof(int));
12-
var body = Expression.Add(p1, p2);
12+
var body = Expression.Add(p1, Expression.Block(
13+
new Expression[] {
14+
Expression.Call(typeof(System.Console).GetMethod("WriteLine", new [] { typeof(int) }), p2),
15+
p2,
16+
}));
1317
var lambda = Expression.Lambda<Func<int, int, int>>(body, p1, p2);
1418
var fun = lambda.CompileWithDebugInfo();
1519
var result = fun(1, 2);

ExpressionDebugger.Tests/DebugInfoInjectorTest.cs

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Linq.Expressions;
5+
using System.Reflection;
56
using Microsoft.VisualStudio.TestTools.UnitTesting;
67

78
namespace 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(@"
119128
public 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;
134152
DateTime1", 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()
597635
public 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
}

ExpressionTranslator/ExpressionTranslator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private Expression VisitGroup(Expression node, ExpressionType parentNodeType, bo
281281
Indent(true);
282282
result = VisitMultiline(node, true);
283283
Outdent();
284-
Write(")()");
284+
Write("))()");
285285
}
286286
else if (ShouldGroup(node, parentNodeType, isRightNode))
287287
{
@@ -773,7 +773,7 @@ private void WriteValue(object value)
773773
}
774774
else if (value is byte || value is sbyte || value is short || value is ushort)
775775
{
776-
Write("(", Translate(value.GetType()), ")", value.ToString());
776+
Write("((", Translate(value.GetType()), ")", value.ToString(), ")");
777777
}
778778
else if (value.GetType().GetTypeInfo().IsEnum)
779779
{

0 commit comments

Comments
 (0)