Skip to content

Commit

Permalink
Added Unit tests for issue hazzik#135
Browse files Browse the repository at this point in the history
Force DecompilerTestBase to visit decompiled expressions
  • Loading branch information
Max committed Jan 6, 2019
1 parent 5de630e commit ee5422b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/DelegateDecompiler.Tests/DecompilerTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ private static Func<Expression, string> BuildDebugView()
protected static void Test<T>(Expression<T> expected, T compiled)
{
//Double cast required as we can not convert T to Delegate directly
var decompiled = ((Delegate) ((object) compiled)).Decompile();
//var decompiled = ((Delegate)((object)compiled)).Decompile();
var decompiled = (LambdaExpression)DecompileExpressionVisitor.Decompile(((Delegate) ((object) compiled)).Decompile());

var x = expected.Body.ToString();
Console.WriteLine(x);
Expand All @@ -31,7 +32,8 @@ protected static void Test<T>(Expression<T> expected, T compiled)
protected static void Test<T>(Expression<T> expected, MethodInfo compiled)
{
//Double cast required as we can not convert T to Delegate directly
var decompiled = compiled.Decompile();
//var decompiled = compiled.Decompile();
var decompiled = (LambdaExpression)DecompileExpressionVisitor.Decompile(compiled.Decompile());

var x = expected.Body.ToString();
Console.WriteLine(x);
Expand All @@ -44,7 +46,8 @@ protected static void Test<T>(Expression<T> expected, MethodInfo compiled)
protected static void Test<T>(Expression<T> expected1, Expression<T> expected2, T compiled)
{
//Double cast required as we can not convert T to Delegate directly
var decompiled = ((Delegate) ((object) compiled)).Decompile();
//var decompiled = ((Delegate) ((object) compiled)).Decompile();
LambdaExpression decompiled = (LambdaExpression)DecompileExpressionVisitor.Decompile(((Delegate)((object)compiled)).Decompile());

var x1 = expected1.Body.ToString();
Console.WriteLine(x1);
Expand Down
71 changes: 71 additions & 0 deletions src/DelegateDecompiler.Tests/Issue135.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NUnit.Framework;

namespace DelegateDecompiler.Tests
{
[TestFixture]
public class Issue135 : DecompilerTestsBase
{
public class Post {
public bool IsActive { get; set; }
}

public class Blog
{
public bool HasBar { get; }
public bool HasBaz { get; }

public IEnumerable<Post> Posts { get; }


[Decompile]
public bool HasFoo
{
get
{
return (this.HasBar || this.HasBaz) && this.Posts.Any(x => x.IsActive);
}
}

[Decompile]
public bool HasFoo2
{
get
{
return (this.HasBar && this.Posts.Any(x => x.IsActive)) || (this.HasBaz && this.Posts.Any(x => x.IsActive)) ;
}
}

[Decompile]
public bool HasFoo3
{
get
{
return this.Posts.Any(x => x.IsActive) && (this.HasBar || this.HasBaz);
}
}
}


[Test]
public void Test()
{
Expression<Func<Blog, bool>> expected = b => b.HasBar ? b.Posts.Any(x => x.IsActive) : (b.HasBaz && b.Posts.Any(x => x.IsActive));
Func<Blog, bool> compiled = b => b.HasFoo;

Test(expected, compiled);
}

[Test]
public void Test2()
{
Expression<Func<Blog, bool>> expected = b => b.Posts.Any(x => x.IsActive) && (b.HasBar ? true : b.HasBaz);
Func<Blog, bool> compiled = b => b.HasFoo3;

Test(expected, compiled);
}
}
}

0 comments on commit ee5422b

Please sign in to comment.