diff --git a/src/DelegateDecompiler.Tests/DecompilerTestsBase.cs b/src/DelegateDecompiler.Tests/DecompilerTestsBase.cs index b8b74355..6c8a1cfe 100644 --- a/src/DelegateDecompiler.Tests/DecompilerTestsBase.cs +++ b/src/DelegateDecompiler.Tests/DecompilerTestsBase.cs @@ -18,7 +18,8 @@ private static Func BuildDebugView() protected static void Test(Expression 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); @@ -31,7 +32,8 @@ protected static void Test(Expression expected, T compiled) protected static void Test(Expression 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); @@ -44,7 +46,8 @@ protected static void Test(Expression expected, MethodInfo compiled) protected static void Test(Expression expected1, Expression 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); diff --git a/src/DelegateDecompiler.Tests/Issue135.cs b/src/DelegateDecompiler.Tests/Issue135.cs new file mode 100644 index 00000000..173e1629 --- /dev/null +++ b/src/DelegateDecompiler.Tests/Issue135.cs @@ -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 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> expected = b => b.HasBar ? b.Posts.Any(x => x.IsActive) : (b.HasBaz && b.Posts.Any(x => x.IsActive)); + Func compiled = b => b.HasFoo; + + Test(expected, compiled); + } + + [Test] + public void Test2() + { + Expression> expected = b => b.Posts.Any(x => x.IsActive) && (b.HasBar ? true : b.HasBaz); + Func compiled = b => b.HasFoo3; + + Test(expected, compiled); + } + } +} \ No newline at end of file