PageRenderTime 26ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/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
19using System;
20using System.CodeDom.Compiler;
21using System.Collections.Generic;
22using System.IO;
23using System.Linq;
24using System.Text;
25
26using ICSharpCode.Decompiler.Ast;
27using ICSharpCode.Decompiler.Tests.Helpers;
28using Microsoft.CSharp;
29using Mono.Cecil;
30using NUnit.Framework;
31
32namespace ICSharpCode.Decompiler.Tests
33{
34	public abstract class DecompilerTestBase
35	{
36		protected static void ValidateFileRoundtrip(string samplesFileName)
37		{
38			var fullPath = Path.Combine(@"..\..\Tests", samplesFileName);
39			AssertRoundtripCode(fullPath);
40		}
41
42		static string RemoveIgnorableLines(IEnumerable<string> lines)
43		{
44			return CodeSampleFileParser.ConcatLines(lines.Where(l => !CodeSampleFileParser.IsCommentOrBlank(l)));
45		}
46
47		protected static void AssertRoundtripCode(string fileName, bool optimize = false, bool useDebug = false, int compilerVersion = 4)
48		{
49			var code = RemoveIgnorableLines(File.ReadLines(fileName));
50			AssemblyDefinition assembly = CompileLegacy(code, optimize, useDebug, compilerVersion);
51
52			AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule));
53			decompiler.AddAssembly(assembly);
54			new Helpers.RemoveCompilerAttribute().Run(decompiler.SyntaxTree);
55
56			StringWriter output = new StringWriter();
57			decompiler.GenerateCode(new PlainTextOutput(output));
58			CodeAssert.AreEqual(code, output.ToString());
59		}
60
61		protected static AssemblyDefinition CompileLegacy(string code, bool optimize, bool useDebug, int compilerVersion)
62		{
63			CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v" + new Version(compilerVersion, 0) } });
64			CompilerParameters options = new CompilerParameters();
65			options.CompilerOptions = "/unsafe /o" + (optimize ? "+" : "-") + (useDebug ? " /debug" : "");
66			if (compilerVersion >= 4)
67				options.ReferencedAssemblies.Add("System.Core.dll");
68			CompilerResults results = provider.CompileAssemblyFromSource(options, code);
69			try
70			{
71				if (results.Errors.Count > 0)
72				{
73					StringBuilder b = new StringBuilder("Compiler error:");
74					foreach (var error in results.Errors)
75					{
76						b.AppendLine(error.ToString());
77					}
78					throw new Exception(b.ToString());
79				}
80				return AssemblyDefinition.ReadAssembly(results.PathToAssembly);
81			}
82			finally
83			{
84				File.Delete(results.PathToAssembly);
85				results.TempFiles.Delete();
86			}
87		}
88	}
89}