Skip to content

Commit 286044a

Browse files
txavierhazzik
authored andcommitted
Unit test and fixes from #53
1 parent 4ce1803 commit 286044a

File tree

2 files changed

+87
-43
lines changed

2 files changed

+87
-43
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Contributed by @JonPSmith (GitHub) www.thereformedprogrammer.com
2+
3+
using System.Linq;
4+
using DelegateDecompiler.EntityFramework.Tests.Helpers;
5+
using NUnit.Framework;
6+
using DelegateDecompiler.EntityFramework.Tests.EfItems;
7+
using System.Data.Entity;
8+
using System.Collections.Generic;
9+
10+
namespace DelegateDecompiler.EntityFramework.Tests.TestGroup05BasicFeatures
11+
{
12+
class Test01Include
13+
{
14+
private ClassEnvironment classEnv;
15+
16+
[OneTimeSetUp]
17+
public void SetUpFixture()
18+
{
19+
classEnv = new ClassEnvironment();
20+
}
21+
22+
[Computed]
23+
private static bool ComputedSample() { return true; }
24+
25+
[Test]
26+
public void TestInclude()
27+
{
28+
using (var db = new EfTestDbContext())
29+
using (var env = new MethodEnvironment(classEnv))
30+
{
31+
//SETUP
32+
var linq = db.EfParents.Where(p => true).Include(p => p.Children).ToList();
33+
34+
//ATTEMPT
35+
env.AboutToUseDelegateDecompiler();
36+
var dd = env.Db.EfParents.Where(p => ComputedSample()).Include(p => p.Children).Decompile().ToList();
37+
38+
//VERIFY
39+
env.CompareAndLogList(linq, dd);
40+
}
41+
}
42+
43+
}
44+
}

src/DelegateDecompiler/DecompileExpressionVisitor.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,50 @@ public static Expression Decompile(Expression expression)
1212
return new DecompileExpressionVisitor().Visit(expression);
1313
}
1414

15-
private static readonly object NULL = new object(); // for use as a dictionary key
16-
private readonly Dictionary<object, Expression> visitedConstants;
17-
18-
private bool hasAnyChanges = false;
19-
public override Expression Visit(Expression node)
20-
{
21-
var result = base.Visit(node);
22-
if (result != node)
23-
hasAnyChanges = true;
24-
return result;
25-
}
26-
27-
private DecompileExpressionVisitor(Dictionary<object, Expression> sharedVisitedConstants = null)
28-
{
29-
this.visitedConstants = sharedVisitedConstants ?? new Dictionary<object, Expression>();
30-
}
31-
32-
protected override Expression VisitConstant(ConstantExpression node)
33-
{
34-
Expression result;
35-
if (visitedConstants.TryGetValue(node.Value ?? NULL, out result))
36-
{
37-
return result; // avoid infinite recursion
38-
}
39-
40-
if (typeof(IQueryable).IsAssignableFrom(node.Type))
41-
{
42-
visitedConstants.Add(node.Value ?? NULL, node);
43-
44-
var value = (IQueryable)node.Value;
45-
var childVisitor = new DecompileExpressionVisitor(visitedConstants);
46-
result = childVisitor.Visit(value.Expression);
47-
48-
if (childVisitor.hasAnyChanges)
49-
{
50-
result = Expression.Constant(value.Provider.CreateQuery(result), node.Type);
51-
visitedConstants[node.Value ?? NULL] = result;
52-
return result;
53-
}
54-
}
55-
56-
return node;
57-
}
15+
private static readonly object NULL = new object(); // for use as a dictionary key
16+
private readonly Dictionary<object, Expression> visitedConstants;
5817

18+
private bool hasAnyChanges = false;
19+
public override Expression Visit(Expression node)
20+
{
21+
var result = base.Visit(node);
22+
if (result != node)
23+
hasAnyChanges = true;
24+
return result;
25+
}
26+
27+
private DecompileExpressionVisitor(Dictionary<object, Expression> sharedVisitedConstants = null)
28+
{
29+
this.visitedConstants = sharedVisitedConstants ?? new Dictionary<object, Expression>();
30+
}
31+
32+
protected override Expression VisitConstant(ConstantExpression node)
33+
{
34+
Expression result;
35+
if (visitedConstants.TryGetValue(node.Value ?? NULL, out result))
36+
{
37+
return result; // avoid infinite recursion
38+
}
39+
40+
if (typeof(IQueryable).IsAssignableFrom(node.Type))
41+
{
42+
visitedConstants.Add(node.Value ?? NULL, node);
43+
44+
var value = (IQueryable)node.Value;
45+
var childVisitor = new DecompileExpressionVisitor(visitedConstants);
46+
result = childVisitor.Visit(value.Expression);
47+
48+
if (childVisitor.hasAnyChanges)
49+
{
50+
result = Expression.Constant(value.Provider.CreateQuery(result), node.Type);
51+
visitedConstants[node.Value ?? NULL] = result;
52+
return result;
53+
}
54+
}
55+
56+
return node;
57+
}
58+
5959
protected override Expression VisitMember(MemberExpression node)
6060
{
6161
if (ShouldDecompile(node.Member) && node.Member is PropertyInfo property)

0 commit comments

Comments
 (0)