From 6085b60fc227a3bf5266ca36946372774ba8c286 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Thu, 17 Feb 2011 23:53:39 +0100 Subject: [PATCH] Add dumb testing framework. --- .../Tests/DelegateConstruction.cs | 41 +++++++ .../Tests/ICSharpCode.Decompiler.Tests.csproj | 66 ++++++++++++ ICSharpCode.Decompiler/Tests/Loops.cs | 30 ++++++ ICSharpCode.Decompiler/Tests/TestRunner.cs | 101 ++++++++++++++++++ ILSpy.sln | 10 ++ 5 files changed, 248 insertions(+) create mode 100644 ICSharpCode.Decompiler/Tests/DelegateConstruction.cs create mode 100644 ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj create mode 100644 ICSharpCode.Decompiler/Tests/Loops.cs create mode 100644 ICSharpCode.Decompiler/Tests/TestRunner.cs diff --git a/ICSharpCode.Decompiler/Tests/DelegateConstruction.cs b/ICSharpCode.Decompiler/Tests/DelegateConstruction.cs new file mode 100644 index 000000000..34657126e --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/DelegateConstruction.cs @@ -0,0 +1,41 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + +using System; + +public static class DelegateConstruction +{ + public static void Test(this string a) + { + } + + public static Action ExtensionMethodUnbound() + { + return new Action(DelegateConstruction.Test); + } + + public static Action ExtensionMethodBound() + { + return new Action("abc".Test); + } + + public static Action ExtensionMethodBoundOnNull() + { + return new Action(((string)null).Test); + } + + public static object StaticMethod() + { + return new Func(DelegateConstruction.ExtensionMethodBound); + } + + public static object InstanceMethod() + { + return new Func("hello".ToUpper); + } + + public static object InstanceMethodOnNull() + { + return new Func(((string)null).ToUpper); + } +} diff --git a/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj new file mode 100644 index 000000000..8d0896ef5 --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj @@ -0,0 +1,66 @@ + + + + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22} + Debug + x86 + Exe + ICSharpCode.Decompiler.Tests + ICSharpCode.Decompiler.Tests + v4.0 + Properties + False + False + 4 + false + + + x86 + False + Auto + 4194304 + 4096 + + + ..\bin\Debug\ + true + Full + False + True + DEBUG;TRACE + + + ..\bin\Release\ + false + None + True + False + TRACE + + + + + 3.5 + + + + 3.5 + + + + + + + + + + {D68133BD-1E63-496E-9EDE-4FBDBF77B486} + Mono.Cecil + + + {984CC812-9470-4A13-AFF9-CC44068D666C} + ICSharpCode.Decompiler + + + + \ No newline at end of file diff --git a/ICSharpCode.Decompiler/Tests/Loops.cs b/ICSharpCode.Decompiler/Tests/Loops.cs new file mode 100644 index 000000000..838a6e886 --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/Loops.cs @@ -0,0 +1,30 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + +using System; +using System.Collections.Generic; + +public class Loops +{ + public void ForEach(IEnumerable enumerable) + { + foreach (string text in enumerable) { + text.ToLower(); + } + } + + public void ForEachOverArray(string[] array) + { + foreach (string text in array) { + text.ToLower(); + } + } + + public void ForOverArray(string[] array) + { + for (int i = 0; i < array.Length; i++) { + array[i].ToLower(); + } + } +} + diff --git a/ICSharpCode.Decompiler/Tests/TestRunner.cs b/ICSharpCode.Decompiler/Tests/TestRunner.cs new file mode 100644 index 000000000..9c3ba5c9d --- /dev/null +++ b/ICSharpCode.Decompiler/Tests/TestRunner.cs @@ -0,0 +1,101 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under MIT X11 license (for details please see \doc\license.txt) + +using System; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.IO; +using System.Text; +using Decompiler; +using Microsoft.CSharp; +using Mono.Cecil; + +namespace ICSharpCode.Decompiler.Tests +{ + public class TestRunner + { + public static void Main() + { + Test(@"..\..\Tests\DelegateConstruction.cs"); + + Console.ReadKey(); + } + + + + static void Test(string fileName) + { + string code = File.ReadAllText(fileName); + AssemblyDefinition assembly = Compile(code); + AstBuilder decompiler = new AstBuilder(); + decompiler.AddAssembly(assembly); + 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()); + } + } + + 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) || line1.StartsWith("using ", StringComparison.Ordinal)) { + diff.WriteLine(" " + line1); + continue; + } + line2 = r2.ReadLine(); + while (line2 != null && (line2.StartsWith("using ", StringComparison.Ordinal) || line2.Trim().Length == 0)) + line2 = r2.ReadLine(); + if (line2 == null) { + ok = false; + diff.WriteLine("-" + line1); + continue; + } + if (line1 != line2) { + 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("+" + line1); + } + return ok; + } + + static AssemblyDefinition Compile(string code) + { + CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary {{ "CompilerVersion", "v4.0" }}); + CompilerParameters options = new CompilerParameters(); + options.ReferencedAssemblies.Add("System.Core.dll"); + CompilerResults results = provider.CompileAssemblyFromSource(options, code); + try { + if (results.Errors.Count > 0) { + StringBuilder b = new StringBuilder("Compiler error:"); + foreach (var error in results.Errors) { + b.AppendLine(error.ToString()); + } + throw new Exception(b.ToString()); + } + return AssemblyDefinition.ReadAssembly(results.PathToAssembly); + } finally { + File.Delete(results.PathToAssembly); + results.TempFiles.Delete(); + } + } + } +} diff --git a/ILSpy.sln b/ILSpy.sln index 449ef4139..409cbd730 100644 --- a/ILSpy.sln +++ b/ILSpy.sln @@ -14,6 +14,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.Decompiler", "I EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.NRefactory", "NRefactory\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj", "{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.Decompiler.Tests", "ICSharpCode.Decompiler\Tests\ICSharpCode.Decompiler.Tests.csproj", "{FEC0DA52-C4A6-4710-BE36-B484A20C5E22}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x86 = Debug|x86 @@ -70,5 +72,13 @@ Global {3B2A5653-EC97-4001-BB9B-D90F1AF2C371}.Release|x86.ActiveCfg = Release|Any CPU {3B2A5653-EC97-4001-BB9B-D90F1AF2C371}.Release|Any CPU.Build.0 = Release|Any CPU {3B2A5653-EC97-4001-BB9B-D90F1AF2C371}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22}.Debug|x86.Build.0 = Debug|x86 + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22}.Debug|x86.ActiveCfg = Debug|x86 + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22}.Debug|Any CPU.Build.0 = Debug|x86 + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22}.Debug|Any CPU.ActiveCfg = Debug|x86 + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22}.Release|x86.Build.0 = Release|x86 + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22}.Release|x86.ActiveCfg = Release|x86 + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22}.Release|Any CPU.Build.0 = Release|x86 + {FEC0DA52-C4A6-4710-BE36-B484A20C5E22}.Release|Any CPU.ActiveCfg = Release|x86 EndGlobalSection EndGlobal