Skip to content

Commit

Permalink
- Replaced simple Test differ with full-blown DiffLib-backed implemen…
Browse files Browse the repository at this point in the history
…tation (pulled in w/ NuGet), to improve failed test messages.
  • Loading branch information
ALyman committed Apr 18, 2011
1 parent a35c4b5 commit f3f5571
Show file tree
Hide file tree
Showing 10 changed files with 777 additions and 54 deletions.
3 changes: 2 additions & 1 deletion ICSharpCode.Decompiler/Tests/DecompilerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;

using ICSharpCode.Decompiler.Ast;
using ICSharpCode.Decompiler.Tests.Helpers;
using Microsoft.CSharp;
using Mono.Cecil;
using NUnit.Framework;
Expand All @@ -19,7 +20,7 @@ protected static void ValidateFileRoundtrip(string samplesFileName)
var lines = File.ReadAllLines(Path.Combine(@"..\..\Tests", samplesFileName));
var testCode = RemoveIgnorableLines(lines);
var decompiledTestCode = RoundtripCode(testCode);
Assert.AreEqual(testCode, decompiledTestCode);
CodeAssert.AreEqual(testCode, decompiledTestCode);
}

static string RemoveIgnorableLines(IEnumerable<string> lines)
Expand Down
108 changes: 108 additions & 0 deletions ICSharpCode.Decompiler/Tests/Helpers/CodeAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DiffLib;
using NUnit.Framework;

namespace ICSharpCode.Decompiler.Tests.Helpers
{
public class CodeAssert
{
public static void AreEqual(string input1, string input2)
{
var diff = new StringWriter();
if (!Compare(input1, input2, diff)) {
Assert.Fail(diff.ToString());
}
}

static bool Compare(string input1, string input2, StringWriter diff)
{
var differ = new AlignedDiff<string>(
NormalizeAndSplitCode(input1),
NormalizeAndSplitCode(input2),
new CodeLineEqualityComparer(),
new StringSimilarityComparer(),
new StringAlignmentFilter());

bool result = true, ignoreChange;

int line1 = 0, line2 = 0;

foreach (var change in differ.Generate()) {
switch (change.Change) {
case ChangeType.Same:
diff.Write("{0,4} {1,4} ", ++line1, ++line2);
diff.Write(" ");
diff.WriteLine(change.Element1);
break;
case ChangeType.Added:
diff.Write(" {1,4} ", line1, ++line2);
result &= ignoreChange = ShouldIgnoreChange(change.Element2);
diff.Write(ignoreChange ? " " : " + ");
diff.WriteLine(change.Element2);
break;
case ChangeType.Deleted:
diff.Write("{0,4} ", ++line1, line2);
result &= ignoreChange = ShouldIgnoreChange(change.Element1);
diff.Write(ignoreChange ? " " : " - ");
diff.WriteLine(change.Element1);
break;
case ChangeType.Changed:
diff.Write("{0,4} ", ++line1, line2);
result = false;
diff.Write("(-) ");
diff.WriteLine(change.Element1);
diff.Write(" {1,4} ", line1, ++line2);
diff.Write("(+) ");
diff.WriteLine(change.Element2);
break;
}
}

return result;
}

class CodeLineEqualityComparer : IEqualityComparer<string>
{
private IEqualityComparer<string> baseComparer = EqualityComparer<string>.Default;

public bool Equals(string x, string y)
{
return baseComparer.Equals(
NormalizeLine(x),
NormalizeLine(y)
);
}

public int GetHashCode(string obj)
{
return baseComparer.GetHashCode(NormalizeLine(obj));
}
}

private static string NormalizeLine(string line)
{
line = line.Trim();
var index = line.IndexOf("//");
if (index >= 0) {
return line.Substring(0, index);
} else {
return line;
}
}

private static bool ShouldIgnoreChange(string line)
{
// for the result, we should ignore blank lines and added comments
return NormalizeLine(line) == string.Empty;
}

private static IEnumerable<string> NormalizeAndSplitCode(string input)
{
return input.Split(new[] { "\r\n", "\n\r", "\n", "\r" }, StringSplitOptions.None);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="DiffLib">
<HintPath>..\..\packages\DiffLib.1.0.0.55\lib\net35-Client\DiffLib.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\nunit.framework.dll</HintPath>
Expand All @@ -54,12 +57,14 @@
<ItemGroup>
<Compile Include="CallOverloadedMethod.cs" />
<Compile Include="CheckedUnchecked.cs" />
<Compile Include="Helpers\CodeAssert.cs" />
<Compile Include="IncrementDecrement.cs" />
<Compile Include="QueryExpressions.cs" />
<Compile Include="Switch.cs" />
<Compile Include="UnsafeCode.cs" />
<Compile Include="Types\S_TypeDeclarations.cs" />
<Compile Include="YieldReturn.cs" />
<None Include="packages.config" />
<None Include="Types\S_EnumSamples.cs" />
<None Include="CustomAttributes\S_AssemblyCustomAttribute.cs" />
<Compile Include="Helpers\RemoveCompilerAttribute.cs" />
Expand Down Expand Up @@ -101,4 +106,4 @@
<None Include="StackTests\StackTests.il" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project>
</Project>
12 changes: 6 additions & 6 deletions ICSharpCode.Decompiler/Tests/IncrementDecrement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ public int PostIncrementInstanceField(IncrementDecrement.MutableClass m)
// {
// return this.M()[name]++;
// }


// public unsafe int PostIncrementOfPointer(int* ptr)
// {
// return *(ptr++);
// }

public int PostIncrementInstanceField()
{
return this.M().Field--;
Expand All @@ -225,9 +230,4 @@ public unsafe int PostIncrementByPointer()
{
return (*this.GetPointer())++;
}

// public unsafe int PostIncrementOfPointer(int* ptr)
// {
// return *(ptr++);
// }
}
52 changes: 6 additions & 46 deletions ICSharpCode.Decompiler/Tests/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DiffLib;
using ICSharpCode.Decompiler.Ast;
using ICSharpCode.Decompiler.Tests.Helpers;
using Microsoft.CSharp;
using Mono.Cecil;
using NUnit.Framework;
Expand Down Expand Up @@ -82,7 +85,7 @@ public void UnsafeCode()
TestFile(@"..\..\Tests\UnsafeCode.cs");
}

[Test, Ignore("IncrementArrayLocation not yet supported")]
[Test]
public void ValueTypes()
{
TestFile(@"..\..\Tests\ValueTypes.cs");
Expand All @@ -109,52 +112,9 @@ static void TestFile(string fileName)
new Helpers.RemoveCompilerAttribute().Run(decompiler.CompilationUnit);
StringWriter output = new StringWriter();
decompiler.GenerateCode(new PlainTextOutput(output));
StringWriter diff = new StringWriter();
if (!Compare(code, output.ToString(), diff)) {
throw new Exception("Test failure." + Environment.NewLine + diff.ToString());
}
CodeAssert.AreEqual(code, output.ToString());
}

static bool Compare(string input1, string input2, StringWriter diff)
{
bool ok = true;
int numberOfContinuousMistakes = 0;
StringReader r1 = new StringReader(input1);
StringReader r2 = new StringReader(input2);
string line1, line2;
while ((line1 = r1.ReadLine()) != null) {
string trimmed = line1.Trim();
if (trimmed.Length == 0 || trimmed.StartsWith("//", StringComparison.Ordinal) | trimmed.StartsWith("#", StringComparison.Ordinal)) {
diff.WriteLine(" " + line1);
continue;
}
line2 = r2.ReadLine();
while (line2 != null && string.IsNullOrWhiteSpace(line2))
line2 = r2.ReadLine();
if (line2 == null) {
ok = false;
diff.WriteLine("-" + line1);
continue;
}
if (line1.Trim() != line2.Trim()) {
ok = false;
if (numberOfContinuousMistakes++ > 5)
return false;
diff.WriteLine("-" + line1);
diff.WriteLine("+" + line2);
} else {
if (numberOfContinuousMistakes > 0)
numberOfContinuousMistakes--;
diff.WriteLine(" " + line1);
}
}
while ((line2 = r2.ReadLine()) != null) {
ok = false;
diff.WriteLine("+" + line2);
}
return ok;
}


static AssemblyDefinition Compile(string code)
{
CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
Expand Down
4 changes: 4 additions & 0 deletions ICSharpCode.Decompiler/Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DiffLib" version="1.0.0.55" />
</packages>
Binary file added packages/DiffLib.1.0.0.55/DiffLib.1.0.0.55.nupkg
Binary file not shown.
Loading

0 comments on commit f3f5571

Please sign in to comment.