From 4f392538fb77bad4b85a6edcbd77644ddb9d56cd Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 28 Mar 2018 11:19:49 +0200 Subject: [PATCH] Port test infrastructure --- ICSharpCode.Decompiler.Tests/DataFlowTest.cs | 2 +- .../DecompilerTestBase.cs | 11 +++++------ .../Helpers/TypeSystemHelper.cs | 2 +- .../ICSharpCode.Decompiler.Tests.csproj | 1 - .../RoundtripAssembly.cs | 19 ++++++++----------- .../TypeSystem/TypeSystemLoaderTests.cs | 6 +++--- 6 files changed, 18 insertions(+), 23 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/DataFlowTest.cs b/ICSharpCode.Decompiler.Tests/DataFlowTest.cs index d0277de07..d46f465e5 100644 --- a/ICSharpCode.Decompiler.Tests/DataFlowTest.cs +++ b/ICSharpCode.Decompiler.Tests/DataFlowTest.cs @@ -55,7 +55,7 @@ namespace ICSharpCode.Decompiler.Tests public void TryFinallyWithAssignmentInFinally() { ILVariable v = new ILVariable(VariableKind.Local, SpecialType.UnknownType, 0); - ILFunction f = new ILFunction((IMethod)null, null, new TryFinally( + ILFunction f = new ILFunction((IMethod)null, 0, new TryFinally( new Nop(), new StLoc(v, new LdcI4(0)) )); diff --git a/ICSharpCode.Decompiler.Tests/DecompilerTestBase.cs b/ICSharpCode.Decompiler.Tests/DecompilerTestBase.cs index ba79100e1..4f76e4479 100644 --- a/ICSharpCode.Decompiler.Tests/DecompilerTestBase.cs +++ b/ICSharpCode.Decompiler.Tests/DecompilerTestBase.cs @@ -24,9 +24,9 @@ using System.Linq; using System.Text; using ICSharpCode.Decompiler.Tests.Helpers; using Microsoft.CSharp; -using Mono.Cecil; using ICSharpCode.Decompiler.CSharp; using ICSharpCode.Decompiler.CSharp.OutputVisitor; +using System.Reflection.PortableExecutable; namespace ICSharpCode.Decompiler.Tests { @@ -50,9 +50,9 @@ namespace ICSharpCode.Decompiler.Tests protected static void AssertRoundtripCode(string fileName, bool optimize = false, bool useDebug = false, int compilerVersion = 4) { var code = RemoveIgnorableLines(File.ReadLines(fileName)); - AssemblyDefinition assembly = CompileLegacy(code, optimize, useDebug, compilerVersion); + var assembly = CompileLegacy(code, optimize, useDebug, compilerVersion); - CSharpDecompiler decompiler = new CSharpDecompiler(assembly.MainModule, new DecompilerSettings()); + CSharpDecompiler decompiler = new CSharpDecompiler(assembly, new DecompilerSettings()); decompiler.AstTransforms.Insert(0, new RemoveEmbeddedAtttributes()); decompiler.AstTransforms.Insert(0, new RemoveCompilerAttribute()); @@ -64,7 +64,7 @@ namespace ICSharpCode.Decompiler.Tests CodeAssert.AreEqual(code, syntaxTree.ToString(options)); } - protected static AssemblyDefinition CompileLegacy(string code, bool optimize, bool useDebug, int compilerVersion) + protected static Metadata.PEFile CompileLegacy(string code, bool optimize, bool useDebug, int compilerVersion) { CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary { { "CompilerVersion", "v" + new Version(compilerVersion, 0) } }); CompilerParameters options = new CompilerParameters(); @@ -83,8 +83,7 @@ namespace ICSharpCode.Decompiler.Tests } throw new Exception(b.ToString()); } - return AssemblyDefinition.ReadAssembly(results.PathToAssembly, - new ReaderParameters { InMemory = true }); + return Metadata.UniversalAssemblyResolver.LoadMainModule(results.PathToAssembly, false, true); } finally { diff --git a/ICSharpCode.Decompiler.Tests/Helpers/TypeSystemHelper.cs b/ICSharpCode.Decompiler.Tests/Helpers/TypeSystemHelper.cs index af7ff04a3..61497a4ee 100644 --- a/ICSharpCode.Decompiler.Tests/Helpers/TypeSystemHelper.cs +++ b/ICSharpCode.Decompiler.Tests/Helpers/TypeSystemHelper.cs @@ -31,7 +31,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers { static readonly Lazy decompilerTypeSystem = new Lazy( delegate { - using (var module = ModuleDefinition.ReadModule(typeof(TypeSystem).Module.FullyQualifiedName)) { + using (var module = new PEReader(new FileS tream(typeof(TypeSystem).Module.FullyQualifiedName))) { return new DecompilerTypeSystem(module); } }); diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index da41f0e6d..67032b0ad 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -104,7 +104,6 @@ - diff --git a/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs b/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs index be0848001..76c411228 100644 --- a/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs +++ b/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs @@ -23,9 +23,9 @@ using System.Linq; using System.Text.RegularExpressions; using System.Threading; using ICSharpCode.Decompiler.CSharp; +using ICSharpCode.Decompiler.Metadata; using ICSharpCode.Decompiler.Tests.Helpers; using Microsoft.Win32; -using Mono.Cecil; using NUnit.Framework; namespace ICSharpCode.Decompiler.Tests @@ -142,19 +142,16 @@ namespace ICSharpCode.Decompiler.Tests if (relFile.Equals(fileToRoundtrip, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine($"Decompiling {fileToRoundtrip}..."); Stopwatch w = Stopwatch.StartNew(); - DefaultAssemblyResolver resolver = new DefaultAssemblyResolver(); - resolver.AddSearchDirectory(inputDir); - resolver.RemoveSearchDirectory("."); - var module = ModuleDefinition.ReadModule(file, new ReaderParameters { - AssemblyResolver = resolver, - InMemory = true - }); + PEFile module = UniversalAssemblyResolver.LoadMainModule(file, false, true); + ((UniversalAssemblyResolver)module.AssemblyResolver).AddSearchDirectory(inputDir); + ((UniversalAssemblyResolver)module.AssemblyResolver).RemoveSearchDirectory("."); + var decompiler = new TestProjectDecompiler(inputDir); // use a fixed GUID so that we can diff the output between different ILSpy runs without spurious changes decompiler.ProjectGuid = Guid.Parse("{127C83E4-4587-4CF9-ADCA-799875F3DFE6}"); decompiler.DecompileProject(module, decompiledDir); Console.WriteLine($"Decompiled {fileToRoundtrip} in {w.Elapsed.TotalSeconds:f2}"); - projectFile = Path.Combine(decompiledDir, module.Assembly.Name.Name + ".csproj"); + projectFile = Path.Combine(decompiledDir, module.Name + ".csproj"); } else { File.Copy(file, Path.Combine(outputDir, relFile)); } @@ -263,11 +260,11 @@ namespace ICSharpCode.Decompiler.Tests localAssemblies = new DirectoryInfo(baseDir).EnumerateFiles("*.dll").Select(f => f.FullName).ToArray(); } - protected override bool IsGacAssembly(AssemblyNameReference r, AssemblyDefinition asm) + protected override bool IsGacAssembly(IAssemblyReference r, PEFile asm) { if (asm == null) return false; - return !localAssemblies.Contains(asm.MainModule.FileName); + return !localAssemblies.Contains(asm.FileName); } } diff --git a/ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs b/ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs index 59ec45de0..3869af681 100644 --- a/ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs +++ b/ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs @@ -33,12 +33,12 @@ namespace ICSharpCode.Decompiler.Tests.TypeSystem { static readonly Lazy mscorlib = new Lazy( delegate { - return new CecilLoader().LoadAssemblyFile(typeof(object).Assembly.Location); + return new MetadataLoader().LoadAssemblyFile(typeof(object).Assembly.Location); }); static readonly Lazy systemCore = new Lazy( delegate { - return new CecilLoader().LoadAssemblyFile(typeof(System.Linq.Enumerable).Assembly.Location); + return new MetadataLoader().LoadAssemblyFile(typeof(System.Linq.Enumerable).Assembly.Location); }); public static IUnresolvedAssembly Mscorlib { get { return mscorlib.Value; } } @@ -48,7 +48,7 @@ namespace ICSharpCode.Decompiler.Tests.TypeSystem public void FixtureSetUp() { // use "IncludeInternalMembers" so that Cecil results match C# parser results - CecilLoader loader = new CecilLoader() { IncludeInternalMembers = true }; + MetadataLoader loader = new MetadataLoader() { IncludeInternalMembers = true }; IUnresolvedAssembly asm = loader.LoadAssemblyFile(typeof(SimplePublicClass).Assembly.Location); compilation = new SimpleCompilation(asm, Mscorlib); }