private static void CompareAssemblyAgainstCSharp(string expectedCSharpCode, string asmFilePath) { var module = Utils.OpenModule(asmFilePath); try { try { module.LoadPdb(); } catch { } AstBuilder decompiler = new AstBuilder(DecompilerContext.CreateTestContext(module)); decompiler.AddAssembly(module, false, true, true); new Helpers.RemoveCompilerAttribute().Run(decompiler.SyntaxTree); StringWriter output = new StringWriter(); // the F# assembly contains a namespace `<StartupCode$tmp6D55>` where the part after tmp is randomly generated. // remove this from the ast to simplify the diff var startupCodeNode = decompiler.SyntaxTree.Children.OfType <NamespaceDeclaration>().SingleOrDefault(d => d.Name.StartsWith("<StartupCode$", StringComparison.Ordinal)); if (startupCodeNode != null) { startupCodeNode.Remove(); } decompiler.GenerateCode(new PlainTextOutput(output)); var fullCSharpCode = output.ToString(); CodeAssert.AreEqual(expectedCSharpCode, output.ToString()); } finally { File.Delete(asmFilePath); File.Delete(Path.ChangeExtension(asmFilePath, ".pdb")); } }
void Run(string compiledFile, string expectedOutputFile) { string expectedOutput = File.ReadAllText(Path.Combine(path, expectedOutputFile)); var assembly = Utils.OpenModule(Path.Combine(path, compiledFile)).Assembly; AstBuilder decompiler = new AstBuilder(DecompilerContext.CreateTestContext(assembly.ManifestModule)); decompiler.AddAssembly(assembly); new Helpers.RemoveCompilerAttribute().Run(decompiler.SyntaxTree); StringWriter output = new StringWriter(); decompiler.GenerateCode(new PlainTextOutput(output)); CodeAssert.AreEqual(expectedOutput, output.ToString()); }
protected static void AssertRoundtripCode(string fileName, bool optimize = false, bool useDebug = false, int compilerVersion = 4) { var code = RemoveIgnorableLines(File.ReadLines(fileName)); AssemblyDef assembly = CompileLegacy(code, optimize, useDebug, compilerVersion); AstBuilder decompiler = new AstBuilder(DecompilerContext.CreateTestContext(assembly.ManifestModule)); decompiler.AddAssembly(assembly); new Helpers.RemoveCompilerAttribute().Run(decompiler.SyntaxTree); StringWriter output = new StringWriter(); decompiler.GenerateCode(new PlainTextOutput(output)); CodeAssert.AreEqual(code, output.ToString()); }