/ICSharpCode.Decompiler/Tests/DecompilerTestBase.cs

http://github.com/icsharpcode/ILSpy · C# · 89 lines · 64 code · 8 blank · 17 comment · 2 complexity · 0638d26e65534a8032285c2443f1781e MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.CodeDom.Compiler;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Text;
  24. using ICSharpCode.Decompiler.Ast;
  25. using ICSharpCode.Decompiler.Tests.Helpers;
  26. using Microsoft.CSharp;
  27. using Mono.Cecil;
  28. using NUnit.Framework;
  29. namespace ICSharpCode.Decompiler.Tests
  30. {
  31. public abstract class DecompilerTestBase
  32. {
  33. protected static void ValidateFileRoundtrip(string samplesFileName)
  34. {
  35. var fullPath = Path.Combine(@"..\..\Tests", samplesFileName);
  36. AssertRoundtripCode(fullPath);
  37. }
  38. static string RemoveIgnorableLines(IEnumerable<string> lines)
  39. {
  40. return CodeSampleFileParser.ConcatLines(lines.Where(l => !CodeSampleFileParser.IsCommentOrBlank(l)));
  41. }
  42. protected static void AssertRoundtripCode(string fileName, bool optimize = false, bool useDebug = false, int compilerVersion = 4)
  43. {
  44. var code = RemoveIgnorableLines(File.ReadLines(fileName));
  45. AssemblyDefinition assembly = CompileLegacy(code, optimize, useDebug, compilerVersion);
  46. AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule));
  47. decompiler.AddAssembly(assembly);
  48. new Helpers.RemoveCompilerAttribute().Run(decompiler.SyntaxTree);
  49. StringWriter output = new StringWriter();
  50. decompiler.GenerateCode(new PlainTextOutput(output));
  51. CodeAssert.AreEqual(code, output.ToString());
  52. }
  53. protected static AssemblyDefinition CompileLegacy(string code, bool optimize, bool useDebug, int compilerVersion)
  54. {
  55. CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v" + new Version(compilerVersion, 0) } });
  56. CompilerParameters options = new CompilerParameters();
  57. options.CompilerOptions = "/unsafe /o" + (optimize ? "+" : "-") + (useDebug ? " /debug" : "");
  58. if (compilerVersion >= 4)
  59. options.ReferencedAssemblies.Add("System.Core.dll");
  60. CompilerResults results = provider.CompileAssemblyFromSource(options, code);
  61. try
  62. {
  63. if (results.Errors.Count > 0)
  64. {
  65. StringBuilder b = new StringBuilder("Compiler error:");
  66. foreach (var error in results.Errors)
  67. {
  68. b.AppendLine(error.ToString());
  69. }
  70. throw new Exception(b.ToString());
  71. }
  72. return AssemblyDefinition.ReadAssembly(results.PathToAssembly);
  73. }
  74. finally
  75. {
  76. File.Delete(results.PathToAssembly);
  77. results.TempFiles.Delete();
  78. }
  79. }
  80. }
  81. }