Skip to content

Commit

Permalink
Tests: Run all decompiler tests against the Roslyn compiler as well
Browse files Browse the repository at this point in the history
This introduces lots of test failures which will hopefully be fixed
later.
See icsharpcode#502
  • Loading branch information
SLaks committed Nov 12, 2014
1 parent 26780d1 commit ab94aa0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
37 changes: 34 additions & 3 deletions ICSharpCode.Decompiler/Tests/DecompilerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

using ICSharpCode.Decompiler.Ast;
using ICSharpCode.Decompiler.Tests.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CSharp;
using Mono.Cecil;
using NUnit.Framework;
Expand All @@ -36,18 +38,19 @@ public abstract class DecompilerTestBase
protected static void ValidateFileRoundtrip(string samplesFileName)
{
var fullPath = Path.Combine(@"..\..\Tests", samplesFileName);
AssertRoundtripCode(fullPath);
AssertRoundtripCode(fullPath, useRoslyn: false);
AssertRoundtripCode(fullPath, useRoslyn: true);
}

static string RemoveIgnorableLines(IEnumerable<string> lines)
{
return CodeSampleFileParser.ConcatLines(lines.Where(l => !CodeSampleFileParser.IsCommentOrBlank(l)));
}

protected static void AssertRoundtripCode(string fileName, bool optimize = false, bool useDebug = false)
protected static void AssertRoundtripCode(string fileName, bool useRoslyn = false, bool optimize = false, bool useDebug = false)
{
var code = RemoveIgnorableLines(File.ReadLines(fileName));
AssemblyDefinition assembly = CompileLegacy(code, optimize, useDebug);
AssemblyDefinition assembly = useRoslyn ? CompileRoslyn(code, optimize, useDebug) : CompileLegacy(code, optimize, useDebug);

AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule));
decompiler.AddAssembly(assembly);
Expand Down Expand Up @@ -84,5 +87,33 @@ protected static AssemblyDefinition CompileLegacy(string code, bool optimize, bo
results.TempFiles.Delete();
}
}
protected static AssemblyDefinition CompileRoslyn(string code, bool optimize, bool useDebug)
{
var compilation = CSharpCompilation.Create(
"RoslynAssembly-" + Guid.NewGuid(),
new[] { CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(LanguageVersion.CSharp5)) },
new[] { typeof(object).Assembly, typeof(Enumerable).Assembly }
.Select(a => new MetadataFileReference(a.Location, MetadataReferenceProperties.Assembly)),
new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: optimize ? OptimizationLevel.Debug : OptimizationLevel.Release,
allowUnsafe: true
)
);

var peStream = new MemoryStream();
var pdbStream = useDebug ? new MemoryStream() : null;

var emitResult = compilation.Emit(peStream, pdbStream: pdbStream);
if (!emitResult.Success)
throw new ApplicationException(string.Join(Environment.NewLine, emitResult.Diagnostics));
peStream.Position = 0;
if (useDebug)
pdbStream.Position = 0;
return AssemblyDefinition.ReadAssembly(
peStream,
new ReaderParameters { ReadSymbols = useDebug, SymbolStream = pdbStream }
);
}
}
}
27 changes: 8 additions & 19 deletions ICSharpCode.Decompiler/Tests/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DiffLib;
using ICSharpCode.Decompiler.Ast;
using ICSharpCode.Decompiler.Tests.Helpers;
using Microsoft.CSharp;
using Mono.Cecil;
using NUnit.Framework;

namespace ICSharpCode.Decompiler.Tests
Expand Down Expand Up @@ -67,8 +56,8 @@ public void ExpressionTrees()
[Test]
public void ExceptionHandling()
{
AssertRoundtripCode(@"..\..\Tests\ExceptionHandling.cs", optimize: false);
AssertRoundtripCode(@"..\..\Tests\ExceptionHandling.cs", optimize: false);
AssertRoundtripCode(@"..\..\Tests\ExceptionHandling.cs", useRoslyn: false, optimize: false);
AssertRoundtripCode(@"..\..\Tests\ExceptionHandling.cs", useRoslyn: true, optimize: false);
}

[Test]
Expand All @@ -86,8 +75,8 @@ public void CustomShortCircuitOperators()
[Test]
public void ControlFlowWithDebug()
{
AssertRoundtripCode(@"..\..\Tests\ControlFlow.cs", optimize: false, useDebug: true);
AssertRoundtripCode(@"..\..\Tests\ControlFlow.cs", optimize: false, useDebug: true);
AssertRoundtripCode(@"..\..\Tests\ControlFlow.cs", useRoslyn: false, optimize: false, useDebug: true);
AssertRoundtripCode(@"..\..\Tests\ControlFlow.cs", useRoslyn: true, optimize: false, useDebug: true);
}

[Test]
Expand Down Expand Up @@ -182,10 +171,10 @@ public void TypeAnalysis()

static void TestFile(string fileName, bool useDebug = false)
{
AssertRoundtripCode(fileName, optimize: false, useDebug: useDebug);
AssertRoundtripCode(fileName, optimize: true, useDebug: useDebug);
AssertRoundtripCode(fileName, optimize: false, useDebug: useDebug);
AssertRoundtripCode(fileName, optimize: true, useDebug: useDebug);
AssertRoundtripCode(fileName, useRoslyn: false, optimize: false, useDebug: useDebug);
AssertRoundtripCode(fileName, useRoslyn: false, optimize: true, useDebug: useDebug);
AssertRoundtripCode(fileName, useRoslyn: true, optimize: false, useDebug: useDebug);
AssertRoundtripCode(fileName, useRoslyn: true, optimize: true, useDebug: useDebug);
}
}
}

0 comments on commit ab94aa0

Please sign in to comment.