1
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.CodeDom.Compiler;
21
using System.Collections.Generic;
26
using ICSharpCode.Decompiler.Ast;
27
using ICSharpCode.Decompiler.Tests.Helpers;
28
using Microsoft.CSharp;
30
using NUnit.Framework;
32
namespace ICSharpCode.Decompiler.Tests
35
public class TestRunner
40
TestFile(@"..\..\Tests\Async.cs");
43
[Test, Ignore("disambiguating overloads is not yet implemented")]
44
public void CallOverloadedMethod()
46
TestFile(@"..\..\Tests\CallOverloadedMethod.cs");
49
[Test, Ignore("unncessary primitive casts")]
50
public void CheckedUnchecked()
52
TestFile(@"..\..\Tests\CheckedUnchecked.cs");
55
[Test, Ignore("Missing cast on null")]
56
public void DelegateConstruction()
58
TestFile(@"..\..\Tests\DelegateConstruction.cs");
61
[Test, Ignore("Not yet implemented")]
62
public void ExpressionTrees()
64
TestFile(@"..\..\Tests\ExpressionTrees.cs");
68
public void ExceptionHandling()
70
TestFile(@"..\..\Tests\ExceptionHandling.cs", optimize: false);
74
public void Generics()
76
TestFile(@"..\..\Tests\Generics.cs");
80
public void CustomShortCircuitOperators()
82
TestFile(@"..\..\Tests\CustomShortCircuitOperators.cs");
86
public void ControlFlowWithDebug()
88
TestFile(@"..\..\Tests\ControlFlow.cs", optimize: false, useDebug: true);
92
public void IncrementDecrement()
94
TestFile(@"..\..\Tests\IncrementDecrement.cs");
98
public void InitializerTests()
100
TestFile(@"..\..\Tests\InitializerTests.cs");
104
public void LiftedOperators()
106
TestFile(@"..\..\Tests\LiftedOperators.cs");
112
TestFile(@"..\..\Tests\Loops.cs");
116
public void MultidimensionalArray()
118
TestFile(@"..\..\Tests\MultidimensionalArray.cs");
122
public void PInvoke()
124
TestFile(@"..\..\Tests\PInvoke.cs");
128
public void PropertiesAndEvents()
130
TestFile(@"..\..\Tests\PropertiesAndEvents.cs");
134
public void QueryExpressions()
136
TestFile(@"..\..\Tests\QueryExpressions.cs");
139
[Test, Ignore("switch transform doesn't recreate the exact original switch")]
142
TestFile(@"..\..\Tests\Switch.cs");
146
public void UndocumentedExpressions()
148
TestFile(@"..\..\Tests\UndocumentedExpressions.cs");
151
[Test, Ignore("has incorrect casts to IntPtr")]
152
public void UnsafeCode()
154
TestFile(@"..\..\Tests\UnsafeCode.cs");
158
public void ValueTypes()
160
TestFile(@"..\..\Tests\ValueTypes.cs");
163
[Test, Ignore("Redundant yield break; not removed")]
164
public void YieldReturn()
166
TestFile(@"..\..\Tests\YieldReturn.cs");
170
public void TypeAnalysis()
172
TestFile(@"..\..\Tests\TypeAnalysisTests.cs");
175
static void TestFile(string fileName, bool useDebug = false)
177
TestFile(fileName, false, useDebug);
178
TestFile(fileName, true, useDebug);
181
static void TestFile(string fileName, bool optimize, bool useDebug = false)
183
string code = File.ReadAllText(fileName);
184
AssemblyDefinition assembly = Compile(code, optimize, useDebug);
185
AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule));
186
decompiler.AddAssembly(assembly);
187
new Helpers.RemoveCompilerAttribute().Run(decompiler.CompilationUnit);
188
StringWriter output = new StringWriter();
189
decompiler.GenerateCode(new PlainTextOutput(output));
190
CodeAssert.AreEqual(code, output.ToString());
193
static AssemblyDefinition Compile(string code, bool optimize, bool useDebug)
195
CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
196
CompilerParameters options = new CompilerParameters();
197
options.CompilerOptions = "/unsafe /o" + (optimize ? "+" : "-") + (useDebug ? " /debug": "");
198
options.ReferencedAssemblies.Add("System.Core.dll");
199
CompilerResults results = provider.CompileAssemblyFromSource(options, code);
201
if (results.Errors.Count > 0) {
202
StringBuilder b = new StringBuilder("Compiler error:");
203
foreach (var error in results.Errors) {
204
b.AppendLine(error.ToString());
206
throw new Exception(b.ToString());
208
return AssemblyDefinition.ReadAssembly(results.PathToAssembly);
210
File.Delete(results.PathToAssembly);
211
results.TempFiles.Delete();