Browse Source

Add RoundtripAssembly.RunWithOutput for TestCase-1.exe

pull/728/head
Siegfried Pammer 9 years ago
parent
commit
7d12dd9e42
  1. 54
      ICSharpCode.Decompiler/Tests/Helpers/Tester.cs
  2. 29
      ICSharpCode.Decompiler/Tests/RoundtripAssembly.cs
  3. 29
      ICSharpCode.Decompiler/Tests/TestRunner.cs

54
ICSharpCode.Decompiler/Tests/Helpers/Tester.cs

@ -1,4 +1,22 @@ @@ -1,4 +1,22 @@
using System;
// Copyright (c) 2015 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
@ -12,6 +30,7 @@ using ICSharpCode.Decompiler.CSharp.Transforms; @@ -12,6 +30,7 @@ using ICSharpCode.Decompiler.CSharp.Transforms;
using ICSharpCode.NRefactory.CSharp;
using Microsoft.CSharp;
using Mono.Cecil;
using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests.Helpers
{
@ -86,5 +105,38 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -86,5 +105,38 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
return fileName;
}
public static void RunAndCompareOutput(string testFileName, string outputFile, string decompiledOutputFile, string decompiledCodeFile = null)
{
string output1, output2, error1, error2;
int result1 = Tester.Run(outputFile, out output1, out error1);
int result2 = Tester.Run(decompiledOutputFile, out output2, out error2);
if (result1 != result2 || output1 != output2 || error1 != error2) {
Console.WriteLine("Test {0} failed.", testFileName);
if (decompiledCodeFile != null)
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(0, result1, "Exit code != 0; did the test case crash?" + Environment.NewLine + error1);
Assert.AreEqual(0, result2, "Exit code != 0; did the decompiled code crash?" + Environment.NewLine + error2);
Assert.AreEqual(error1, error2);
Assert.AreEqual(output1, output2);
}
}
}

29
ICSharpCode.Decompiler/Tests/RoundtripAssembly.cs

@ -21,6 +21,7 @@ using System.Diagnostics; @@ -21,6 +21,7 @@ using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Tests.Helpers;
using ICSharpCode.NRefactory.Utils;
using Mono.Cecil;
using NUnit.Framework;
@ -37,13 +38,35 @@ namespace ICSharpCode.Decompiler.Tests @@ -37,13 +38,35 @@ namespace ICSharpCode.Decompiler.Tests
public void Cecil_net45()
{
try {
Run("Mono.Cecil-net45", "Mono.Cecil.dll", "Mono.Cecil.Tests.dll");
RunWithTest("Mono.Cecil-net45", "Mono.Cecil.dll", "Mono.Cecil.Tests.dll");
} catch (CompilationFailedException ex) {
Assert.Ignore(ex.Message);
}
}
[Test]
public void Random_Tests_TestCases()
{
try {
RunWithOutput("Random Tests\\TestCases", "TestCase-1.exe", "TestCase-1.out");
} catch (CompilationFailedException ex) {
Assert.Ignore(ex.Message);
}
}
void Run(string dir, string fileToRoundtrip, string fileToTest)
void RunWithTest(string dir, string fileToRoundtrip, string fileToTest)
{
RunInternal(dir, fileToRoundtrip, () => RunTest(Path.Combine(testDir, dir) + "-output", fileToTest));
}
void RunWithOutput(string dir, string fileToRoundtrip, string outputFile)
{
string inputDir = Path.Combine(testDir, dir);
string outputDir = inputDir + "-output";
RunInternal(dir, fileToRoundtrip, () => Tester.RunAndCompareOutput(fileToRoundtrip, Path.Combine(inputDir, fileToRoundtrip), Path.Combine(outputDir, fileToRoundtrip)));
}
void RunInternal(string dir, string fileToRoundtrip, Action testAction)
{
if (!Directory.Exists(testDir)) {
Assert.Ignore($"Assembly-roundtrip test ignored: test directory '{testDir}' needs to be checked out separately." + Environment.NewLine +
@ -79,7 +102,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -79,7 +102,7 @@ namespace ICSharpCode.Decompiler.Tests
Assert.IsNotNull(projectFile, $"Could not find {fileToRoundtrip}");
Compile(projectFile, outputDir);
RunTest(outputDir, fileToTest);
testAction();
}
static void ClearDirectory(string dir)

29
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -119,40 +119,13 @@ namespace ICSharpCode.Decompiler.Tests @@ -119,40 +119,13 @@ namespace ICSharpCode.Decompiler.Tests
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(0, result1, "Exit code != 0; did the test case crash?" + Environment.NewLine + error1);
Assert.AreEqual(0, result2, "Exit code != 0; did the decompiled code crash?" + Environment.NewLine + error2);
Assert.AreEqual(error1, error2);
Assert.AreEqual(output1, output2);
Tester.RunAndCompareOutput(testFileName, outputFile.PathToAssembly, decompiledOutputFile.PathToAssembly, decompiledCodeFile);
File.Delete(decompiledCodeFile);
File.Delete(outputFile.PathToAssembly);

Loading…
Cancel
Save