mirror of https://github.com/icsharpcode/ILSpy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.5 KiB
159 lines
4.5 KiB
using System; |
|
using System.CodeDom.Compiler; |
|
using System.Collections.Generic; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
using ICSharpCode.Decompiler.Tests.Helpers; |
|
using NUnit.Framework; |
|
|
|
namespace ICSharpCode.Decompiler.Tests |
|
{ |
|
[TestFixture] |
|
public class TestRunner |
|
{ |
|
const string TestCasePath = @"../../Tests/TestCases"; |
|
|
|
[Test] |
|
public void AllFilesHaveTests() |
|
{ |
|
var testNames = typeof(TestRunner).GetMethods() |
|
.Where(m => m.GetCustomAttributes(typeof(TestAttribute), false).Any()) |
|
.Select(m => m.Name) |
|
.ToArray(); |
|
foreach (var file in new DirectoryInfo(TestCasePath).EnumerateFiles()) { |
|
var testName = Path.GetFileNameWithoutExtension(file.Name); |
|
Assert.Contains(testName, testNames); |
|
} |
|
} |
|
|
|
[Test] |
|
public void Comparisons() |
|
{ |
|
TestCompileDecompileCompileOutputAll("Comparisons.cs"); |
|
} |
|
|
|
[Test] |
|
public void Conversions() |
|
{ |
|
TestCompileDecompileCompileOutputAll("Conversions.cs"); |
|
} |
|
|
|
[Test] |
|
public void HelloWorld() |
|
{ |
|
TestCompileDecompileCompileOutputAll("HelloWorld.cs"); |
|
} |
|
|
|
[Test] |
|
public void ControlFlow() |
|
{ |
|
TestCompileDecompileCompileOutputAll("ControlFlow.cs"); |
|
} |
|
|
|
[Test] |
|
public void CompoundAssignment() |
|
{ |
|
TestCompileDecompileCompileOutputAll("CompoundAssignment.cs"); |
|
} |
|
|
|
[Test] |
|
public void PropertiesAndEvents() |
|
{ |
|
TestCompileDecompileCompileOutputAll("PropertiesAndEvents.cs"); |
|
} |
|
|
|
[Test] |
|
public void Switch() |
|
{ |
|
TestCompileDecompileCompileOutputAll("Switch.cs"); |
|
} |
|
|
|
[Test] |
|
public void Generics() |
|
{ |
|
TestCompileDecompileCompileOutputAll("Generics.cs"); |
|
} |
|
|
|
[Test] |
|
public void ValueTypeCall() |
|
{ |
|
TestCompileDecompileCompileOutputAll("ValueTypeCall.cs"); |
|
} |
|
|
|
[Test] |
|
public void InitializerTests() |
|
{ |
|
TestCompileDecompileCompileOutputAll("InitializerTests.cs"); |
|
} |
|
|
|
[Test] |
|
public void DecimalFields() |
|
{ |
|
TestCompileDecompileCompileOutputAll("DecimalFields.cs"); |
|
} |
|
|
|
[Test] |
|
public void UndocumentedExpressions() |
|
{ |
|
TestCompileDecompileCompileOutputAll("UndocumentedExpressions.cs"); |
|
} |
|
|
|
void TestCompileDecompileCompileOutputAll(string testFileName) |
|
{ |
|
TestCompileDecompileCompileOutput(testFileName, CompilerOptions.None); |
|
TestCompileDecompileCompileOutput(testFileName, CompilerOptions.UseDebug); |
|
TestCompileDecompileCompileOutput(testFileName, CompilerOptions.Optimize); |
|
TestCompileDecompileCompileOutput(testFileName, CompilerOptions.UseDebug | CompilerOptions.Optimize); |
|
} |
|
|
|
void TestCompileDecompileCompileOutput(string testFileName, CompilerOptions options = CompilerOptions.UseDebug) |
|
{ |
|
CompilerResults outputFile = null, decompiledOutputFile = null; |
|
string output1, output2, error1, error2; |
|
|
|
try { |
|
outputFile = Tester.CompileCSharp(Path.Combine(TestCasePath, testFileName), options); |
|
string decompiledCodeFile = Tester.DecompileCSharp(outputFile.PathToAssembly); |
|
decompiledOutputFile = Tester.CompileCSharp(decompiledCodeFile, options); |
|
int result1 = Tester.Run(outputFile.PathToAssembly, out output1, out error1); |
|
int result2 = Tester.Run(decompiledOutputFile.PathToAssembly, out output2, out error2); |
|
|
|
if (result1 != result2 || output1 != output2 || error1 != error2) { |
|
Console.WriteLine("Test {0} failed.", testFileName); |
|
Console.WriteLine("Decompiled code in {0}:line 1", decompiledCodeFile); |
|
if (error1 == "" && error2 != "") { |
|
Console.WriteLine(error2); |
|
} else { |
|
string outputFileName = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(testFileName)); |
|
File.WriteAllText(outputFileName + ".original.out", output1); |
|
File.WriteAllText(outputFileName + ".decompiled.out", output2); |
|
int diffLine = 0; |
|
foreach (var pair in output1.Split('\n').Zip(output2.Split('\n'), Tuple.Create)) { |
|
diffLine++; |
|
if (pair.Item1 != pair.Item2) { |
|
break; |
|
} |
|
} |
|
Console.WriteLine("Output: {0}.original.out:line {1}", outputFileName, diffLine); |
|
Console.WriteLine("Output: {0}.decompiled.out:line {1}", outputFileName, diffLine); |
|
} |
|
} |
|
|
|
Assert.AreEqual(result1, result2, "Exit codes differ; did the decompiled code crash?"); |
|
Assert.AreEqual(error1, error2); |
|
Assert.AreEqual(output1, output2); |
|
|
|
File.Delete(decompiledCodeFile); |
|
File.Delete(outputFile.PathToAssembly); |
|
File.Delete(decompiledOutputFile.PathToAssembly); |
|
} finally { |
|
if (outputFile != null) |
|
outputFile.TempFiles.Delete(); |
|
if (decompiledOutputFile != null) |
|
decompiledOutputFile.TempFiles.Delete(); |
|
} |
|
} |
|
} |
|
}
|
|
|