From aa26ea2733104a706f22b324e7f5b429513cc75d Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 30 Jul 2018 19:46:29 +0200 Subject: [PATCH] Add CSharpLanguageTests. --- ILSpy.Tests/ILSpy.Tests.csproj | 70 ++++++++++ ILSpy.Tests/Languages/CSharpLanguageTests.cs | 129 +++++++++++++++++++ ILSpy.Tests/Stub.cs | 11 ++ ILSpy.sln | 8 +- appveyor.yml | 1 + 5 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 ILSpy.Tests/ILSpy.Tests.csproj create mode 100644 ILSpy.Tests/Languages/CSharpLanguageTests.cs create mode 100644 ILSpy.Tests/Stub.cs diff --git a/ILSpy.Tests/ILSpy.Tests.csproj b/ILSpy.Tests/ILSpy.Tests.csproj new file mode 100644 index 000000000..a9341a552 --- /dev/null +++ b/ILSpy.Tests/ILSpy.Tests.csproj @@ -0,0 +1,70 @@ + + + + + net46 + 7.3 + + True + + 1701;1702;1705,67,169,1058,728,1720,649,168,251 + + False + + false + + Exe + ILSpy.Tests.Stub + True + + True + ..\ICSharpCode.Decompiler\ICSharpCode.Decompiler.snk + + + + full + true + + + + pdbonly + true + + + + TRACE;DEBUG;NET46;ROSLYN;CS60;CS70 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ILSpy.Tests/Languages/CSharpLanguageTests.cs b/ILSpy.Tests/Languages/CSharpLanguageTests.cs new file mode 100644 index 000000000..8d3f7617d --- /dev/null +++ b/ILSpy.Tests/Languages/CSharpLanguageTests.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.Decompiler.Tests.TypeSystem; +using ICSharpCode.Decompiler.TypeSystem; +using ICSharpCode.Decompiler.TypeSystem.Implementation; +using ICSharpCode.ILSpy; +using NUnit.Framework; + +namespace ILSpy.Tests.Languages +{ + [TestFixture, Parallelizable(ParallelScope.All)] + public class CSharpLanguageTests + { + const string ns = "ICSharpCode.Decompiler.Tests.TypeSystem"; + + static PEFile LoadAssembly(string filename) + { + return new PEFile(filename, new FileStream(filename, FileMode.Open, FileAccess.Read)); + } + + static readonly Lazy mscorlib = new Lazy( + delegate { + return LoadAssembly(typeof(object).Assembly.Location); + }); + + static readonly Lazy systemCore = new Lazy( + delegate { + return LoadAssembly(typeof(System.Linq.Enumerable).Assembly.Location); + }); + + static readonly Lazy testAssembly = new Lazy( + delegate { + return LoadAssembly(typeof(CSharpLanguageTests).Assembly.Location); + }); + + public static PEFile Mscorlib { get { return mscorlib.Value; } } + public static PEFile SystemCore { get { return systemCore.Value; } } + public static PEFile TestAssembly { get { return testAssembly.Value; } } + + [OneTimeSetUp] + public void FixtureSetUp() + { + compilation = new SimpleCompilation(TestAssembly, + Mscorlib.WithOptions(TypeSystemOptions.Default)); + language = new CSharpLanguage(); + } + + ICompilation compilation; + CSharpLanguage language; + + ITypeDefinition GetTypeDefinition(Type type) + { + return compilation.FindType(type).GetDefinition(); + } + + void TestType(Type t, string ns, string name) + { + var type = GetTypeDefinition(t); + Assert.AreEqual(name, language.TypeToString(type, includeNamespace: false)); + Assert.AreEqual(ns + "." + name, language.TypeToString(type, includeNamespace: true)); + } + + void TestMethod(Type t, Predicate filter, string ns, string typeName, string name, string paramListReturnType, string longParamListReturnType = null) + { + var type = GetTypeDefinition(t); + var method = type.GetMembers(filter, GetMemberOptions.IgnoreInheritedMembers).Single() as IMethod; + if (method == null) + throw new ArgumentNullException(); + if (longParamListReturnType == null) + longParamListReturnType = paramListReturnType; + Assert.AreEqual(name + paramListReturnType, language.MethodToString(method, includeDeclaringTypeName: false, includeNamespace: false, includeNamespaceOfDeclaringTypeName: false)); + Assert.AreEqual(typeName + "." + name + paramListReturnType, language.MethodToString(method, includeDeclaringTypeName: true, includeNamespace: false, includeNamespaceOfDeclaringTypeName: false)); + Assert.AreEqual(name + longParamListReturnType, language.MethodToString(method, includeDeclaringTypeName: false, includeNamespace: true, includeNamespaceOfDeclaringTypeName: false)); + Assert.AreEqual(typeName + "." + name + longParamListReturnType, language.MethodToString(method, includeDeclaringTypeName: true, includeNamespace: true, includeNamespaceOfDeclaringTypeName: false)); + Assert.AreEqual(name + paramListReturnType, language.MethodToString(method, includeDeclaringTypeName: false, includeNamespace: false, includeNamespaceOfDeclaringTypeName: true)); + Assert.AreEqual(ns + "." + typeName + "." + name + paramListReturnType, language.MethodToString(method, includeDeclaringTypeName: true, includeNamespace: false, includeNamespaceOfDeclaringTypeName: true)); + Assert.AreEqual(name + longParamListReturnType, language.MethodToString(method, includeDeclaringTypeName: false, includeNamespace: true, includeNamespaceOfDeclaringTypeName: true)); + Assert.AreEqual(ns + "." + typeName + "." + name + longParamListReturnType, language.MethodToString(method, includeDeclaringTypeName: true, includeNamespace: true, includeNamespaceOfDeclaringTypeName: true)); + } + + [Test] + public void PrimitiveTypes() + { + TestType(typeof(object), "System", "Object"); + TestType(typeof(string), "System", "String"); + TestType(typeof(int), "System", "Int32"); + } + + [Test] + public void ClassTests() + { + TestType(typeof(SimplePublicClass), ns, "SimplePublicClass"); + TestType(typeof(GenericClass<,>), ns, "GenericClass"); + TestType(typeof(OuterGeneric<>), ns, "OuterGeneric"); + TestType(typeof(OuterGeneric<>.Inner), ns + ".OuterGeneric", "Inner"); + } + + [Test] + public void InterfaceTests() + { + TestType(typeof(IBase1), ns, "IBase1"); + TestType(typeof(IGenericInterface<>), ns, "IGenericInterface"); + } + + [Test] + public void EnumTests() + { + TestType(typeof(MyEnum), ns, "MyEnum"); + TestType(typeof(GenericClass<,>.NestedEnum), ns + ".GenericClass", "NestedEnum"); + } + + [Test] + public void DelegateTests() + { + TestType(typeof(GenericDelegate<,>), ns, "GenericDelegate"); + } + + [Test] + public void MethodTests() + { + TestMethod(typeof(IMarshalAsTests), x => x.Name == "QueryApplicationFile", ns, "IMarshalAsTests", "QueryApplicationFile", "(string, out string, out string, out bool, out bool, out object[]) : void"); + TestMethod(typeof(MyClassWithCtor), x => x is IMethod m && m.IsConstructor, ns, "MyClassWithCtor", "MyClassWithCtor", "(int)"); + TestMethod(typeof(OuterGeneric<>), x => x is IMethod m && m.IsConstructor, ns, "OuterGeneric", "OuterGeneric", "()"); + } + } +} diff --git a/ILSpy.Tests/Stub.cs b/ILSpy.Tests/Stub.cs new file mode 100644 index 000000000..94f083677 --- /dev/null +++ b/ILSpy.Tests/Stub.cs @@ -0,0 +1,11 @@ +using System; + +namespace ILSpy.Tests +{ + class Stub + { + static void Main(string[] args) + { + } + } +} diff --git a/ILSpy.sln b/ILSpy.sln index e46598f7d..39a095e23 100644 --- a/ILSpy.sln +++ b/ILSpy.sln @@ -31,7 +31,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Rebracer.xml = Rebracer.xml EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.Decompiler.PdbProvider.Cecil", "ICSharpCode.Decompiler.PdbProvider.Cecil\ICSharpCode.Decompiler.PdbProvider.Cecil.csproj", "{B85A155A-9DD6-43BC-A624-2D8EC773D71F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ICSharpCode.Decompiler.PdbProvider.Cecil", "ICSharpCode.Decompiler.PdbProvider.Cecil\ICSharpCode.Decompiler.PdbProvider.Cecil.csproj", "{B85A155A-9DD6-43BC-A624-2D8EC773D71F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILSpy.Tests", "ILSpy.Tests\ILSpy.Tests.csproj", "{B51C6636-B8D1-4200-9869-08F2689DE6C2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -75,6 +77,10 @@ Global {B85A155A-9DD6-43BC-A624-2D8EC773D71F}.Debug|Any CPU.Build.0 = Debug|Any CPU {B85A155A-9DD6-43BC-A624-2D8EC773D71F}.Release|Any CPU.ActiveCfg = Release|Any CPU {B85A155A-9DD6-43BC-A624-2D8EC773D71F}.Release|Any CPU.Build.0 = Release|Any CPU + {B51C6636-B8D1-4200-9869-08F2689DE6C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B51C6636-B8D1-4200-9869-08F2689DE6C2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B51C6636-B8D1-4200-9869-08F2689DE6C2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B51C6636-B8D1-4200-9869-08F2689DE6C2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/appveyor.yml b/appveyor.yml index 69468dc04..94021c08e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -19,6 +19,7 @@ after_build: test: assemblies: - 'ICSharpCode.Decompiler.Tests\bin\%configuration%\net46\ICSharpCode.Decompiler.Tests.exe' + - 'ILSpy.Tests\bin\%configuration%\net46\ILSpy.Tests.exe' - 'ILSpy.BamlDecompiler.Tests\bin\%configuration%\net46\ILSpy.BamlDecompiler.Tests.dll' after_test: - python BuildTools\tidy.py