From 54846bd4911d257c735b701ead493f8f06595634 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Sat, 1 Jun 2019 02:24:39 +0300 Subject: [PATCH] Added test cases for Activator.CreateInstance (dotnet/corefx#38032) Commit migrated from https://github.com/dotnet/corefx/commit/0837ff6ca8e494ac7a8c64316b2ce28744f42223 --- .../System.Runtime/System.Runtime.sln | 10 ++ .../tests/System.Runtime.Tests.csproj | 2 + .../tests/System/ActivatorTests.Generic.cs | 100 ++++++++++++++++++ .../tests/System/ActivatorTests.cs | 43 ++------ .../System.Reflection.TestModule.il | 4 + .../tests/TestStructs/Configurations.props | 7 ++ .../tests/TestStructs/System.TestStructs.il | 73 +++++++++++++ .../TestStructs/System.TestStructs.ilproj | 11 ++ 8 files changed, 218 insertions(+), 32 deletions(-) create mode 100644 src/libraries/System.Runtime/tests/System/ActivatorTests.Generic.cs create mode 100644 src/libraries/System.Runtime/tests/TestStructs/Configurations.props create mode 100644 src/libraries/System.Runtime/tests/TestStructs/System.TestStructs.il create mode 100644 src/libraries/System.Runtime/tests/TestStructs/System.TestStructs.ilproj diff --git a/src/libraries/System.Runtime/System.Runtime.sln b/src/libraries/System.Runtime/System.Runtime.sln index d3a46cdb7ef..6937ad02cdf 100644 --- a/src/libraries/System.Runtime/System.Runtime.sln +++ b/src/libraries/System.Runtime/System.Runtime.sln @@ -22,6 +22,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Reflection.TestModul {56B9D0A9-44D3-488E-8B42-C14A6E30CAB2} = {56B9D0A9-44D3-488E-8B42-C14A6E30CAB2} EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.TestStructs", "tests\TestStructs\System.TestStructs.ilproj", "{6B002B34-089C-4BC4-91DD-57D350DEA91C}" + ProjectSection(ProjectDependencies) = postProject + {56B9D0A9-44D3-488E-8B42-C14A6E30CAB2} = {56B9D0A9-44D3-488E-8B42-C14A6E30CAB2} + EndProjectSection +EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Runtime", "src\System.Runtime.csproj", "{56B9D0A9-44D3-488E-8B42-C14A6E30CAB2}" ProjectSection(ProjectDependencies) = postProject {ADBCF120-3454-4A3C-9D1D-AC4293E795D6} = {ADBCF120-3454-4A3C-9D1D-AC4293E795D6} @@ -57,6 +62,10 @@ Global {3B7489C4-65DB-4E69-BE01-F6234133400C}.Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU {3B7489C4-65DB-4E69-BE01-F6234133400C}.Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU {3B7489C4-65DB-4E69-BE01-F6234133400C}.Release|Any CPU.Build.0 = netstandard-Release|Any CPU + {6B002B34-089C-4BC4-91DD-57D350DEA91C}.Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU + {6B002B34-089C-4BC4-91DD-57D350DEA91C}.Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU + {6B002B34-089C-4BC4-91DD-57D350DEA91C}.Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU + {6B002B34-089C-4BC4-91DD-57D350DEA91C}.Release|Any CPU.Build.0 = netstandard-Release|Any CPU {56B9D0A9-44D3-488E-8B42-C14A6E30CAB2}.Debug|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Debug|Any CPU {56B9D0A9-44D3-488E-8B42-C14A6E30CAB2}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU {56B9D0A9-44D3-488E-8B42-C14A6E30CAB2}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU @@ -74,6 +83,7 @@ Global {F9C30DC5-30C1-45DA-9336-F7BE358C367C} = {1A2F9F4A-A032-433E-B914-ADD5992BB178} {71DE1A1F-F8E2-452A-9D54-0385F6099DD3} = {1A2F9F4A-A032-433E-B914-ADD5992BB178} {3B7489C4-65DB-4E69-BE01-F6234133400C} = {1A2F9F4A-A032-433E-B914-ADD5992BB178} + {6B002B34-089C-4BC4-91DD-57D350DEA91C} = {1A2F9F4A-A032-433E-B914-ADD5992BB178} {56B9D0A9-44D3-488E-8B42-C14A6E30CAB2} = {E107E9C1-E893-4E87-987E-04EF0DCEAEFD} {ADBCF120-3454-4A3C-9D1D-AC4293E795D6} = {2E666815-2EDB-464B-9DF6-380BF4789AD4} EndGlobalSection diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj index 40937ac18fb..c556e0eec09 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj @@ -54,6 +54,7 @@ + @@ -329,6 +330,7 @@ TestCollectibleAssembly + diff --git a/src/libraries/System.Runtime/tests/System/ActivatorTests.Generic.cs b/src/libraries/System.Runtime/tests/System/ActivatorTests.Generic.cs new file mode 100644 index 00000000000..7f4ffd42cc9 --- /dev/null +++ b/src/libraries/System.Runtime/tests/System/ActivatorTests.Generic.cs @@ -0,0 +1,100 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Reflection; +using Xunit; + +namespace System.Tests +{ + public partial class ActivatorTests + { + [Fact] + public void CreateInstanceT_Array_ThrowsMissingMethodException() => + Assert.Throws(() => Activator.CreateInstance()); + + [Fact] + public void CreateInstanceT_Interface_ThrowsMissingMethodException() => + Assert.Throws(() => Activator.CreateInstance()); + + [Fact] + public void CreateInstanceT_AbstractClass_ThrowsMissingMethodException() => + Assert.Throws(() => Activator.CreateInstance()); + + [Fact] + public void CreateInstanceT_ClassWithDeefaultConstructor_InvokesConstructor() => + Activator.CreateInstance(); + + [Fact] + public void CreateInstanceT_ClassWithPublicConstructor_InvokesConstructor() => + Assert.True(Activator.CreateInstance().ConstructorInvoked); + + [Fact] + public void CreateInstanceT_ClassWithPrivateConstructor_ThrowsMissingMethodException() => + Assert.Throws(() => Activator.CreateInstance()); + + [Fact] + public void CreateInstanceT_ClassWithoutDefaultConstructor_ThrowsMissingMethodException() => + Assert.Throws(() => Activator.CreateInstance()); + + [Fact] + public void CreateInstanceT_ClassWithDefaultConstructorThatThrows_ThrowsTargetInvocationException() => + Assert.Throws(() => Activator.CreateInstance()); + + [Fact] + public void CreateInstanceT_StructWithDefaultConstructor_InvokesConstructor() => + Activator.CreateInstance(); + + [Fact] + public void CreateInstanceT_StructWithPublicDefaultConstructor_InvokesConstructor() => + Assert.True(Activator.CreateInstance().ConstructorInvoked); + + [Fact] + public void CreateInstanceT_StructWithPrivateDefaultConstructor_ThrowsMissingMethodException() => + Assert.Throws(() => Activator.CreateInstance()); + + [Fact] + public void CreateInstanceT_StructWithoutDefaultConstructor_ThrowsMissingMethodException() => + Activator.CreateInstance(); + + [Fact] + public void CreateInstanceT_StructWithDefaultConstructorThatThrows_ThrowsTargetInvocationException() => + Assert.Throws(() => Activator.CreateInstance()); + + private interface IInterface + { + } + + private abstract class AbstractClass + { + } + + public class ClassWithDefaultConstructor + { + } + + private class ClassWithPublicDefaultConstructor + { + public readonly bool ConstructorInvoked; + + public ClassWithPublicDefaultConstructor() => + ConstructorInvoked = true; + } + + private class ClassWithPrivateDefaultConstructor + { + private ClassWithPrivateDefaultConstructor() { } + } + + private class ClassWithoutDefaultConstructor + { + public ClassWithoutDefaultConstructor(int value) { } + } + + private class ClassWithDefaultConstructorThatThrows + { + public ClassWithDefaultConstructorThatThrows() => + throw new Exception(); + } + } +} diff --git a/src/libraries/System.Runtime/tests/System/ActivatorTests.cs b/src/libraries/System.Runtime/tests/System/ActivatorTests.cs index 5ee7803271d..a657c294cf4 100644 --- a/src/libraries/System.Runtime/tests/System/ActivatorTests.cs +++ b/src/libraries/System.Runtime/tests/System/ActivatorTests.cs @@ -76,28 +76,28 @@ namespace System.Tests } [Fact] - public void CreateInstance_NonPublicClassWithPrivateDefaultConstructor_Success() + public void CreateInstance_NonPublicTypeWithPrivateDefaultConstructor_Success() { // Activator holds a cache of constructors and the types to which they belong. // Test caching behaviour by activating multiple times. - ClassWithPrivateDefaultConstructor c1 = (ClassWithPrivateDefaultConstructor)Activator.CreateInstance(typeof(ClassWithPrivateDefaultConstructor), nonPublic: true); + TypeWithPrivateDefaultConstructor c1 = (TypeWithPrivateDefaultConstructor)Activator.CreateInstance(typeof(TypeWithPrivateDefaultConstructor), nonPublic: true); Assert.Equal(-1, c1.Property); - ClassWithPrivateDefaultConstructor c2 = (ClassWithPrivateDefaultConstructor)Activator.CreateInstance(typeof(ClassWithPrivateDefaultConstructor), nonPublic: true); + TypeWithPrivateDefaultConstructor c2 = (TypeWithPrivateDefaultConstructor)Activator.CreateInstance(typeof(TypeWithPrivateDefaultConstructor), nonPublic: true); Assert.Equal(-1, c2.Property); } [Fact] - public void CreateInstance_PublicOnlyClassWithPrivateDefaultConstructor_ThrowsMissingMethodException() + public void CreateInstance_PublicOnlyTypeWithPrivateDefaultConstructor_ThrowsMissingMethodException() { - Assert.Throws(() => Activator.CreateInstance(typeof(ClassWithPrivateDefaultConstructor))); - Assert.Throws(() => Activator.CreateInstance(typeof(ClassWithPrivateDefaultConstructor), nonPublic: false)); + Assert.Throws(() => Activator.CreateInstance(typeof(TypeWithPrivateDefaultConstructor))); + Assert.Throws(() => Activator.CreateInstance(typeof(TypeWithPrivateDefaultConstructor), nonPublic: false)); // Put the private default constructor into the cache and make sure we still throw if public only. - Assert.NotNull(Activator.CreateInstance(typeof(ClassWithPrivateDefaultConstructor), nonPublic: true)); + Assert.NotNull(Activator.CreateInstance(typeof(TypeWithPrivateDefaultConstructor), nonPublic: true)); - Assert.Throws(() => Activator.CreateInstance(typeof(ClassWithPrivateDefaultConstructor))); - Assert.Throws(() => Activator.CreateInstance(typeof(ClassWithPrivateDefaultConstructor), nonPublic: false)); + Assert.Throws(() => Activator.CreateInstance(typeof(TypeWithPrivateDefaultConstructor))); + Assert.Throws(() => Activator.CreateInstance(typeof(TypeWithPrivateDefaultConstructor), nonPublic: false)); } [Fact] @@ -266,27 +266,6 @@ namespace System.Tests Assert.Throws(() => Activator.CreateInstance(type, null, new object[] { 1, 2 } )); } - [Fact] - public static void CreateInstance_Generic() - { - Choice1 c = Activator.CreateInstance(); - Assert.Equal(1, c.I); - - Activator.CreateInstance(); - Activator.CreateInstance(); - } - - [Fact] - public static void CreateInstance_Generic_Invalid() - { - Assert.ThrowsAny(() => Activator.CreateInstance()); // Cannot create array type - - Assert.ThrowsAny(() => Activator.CreateInstance()); // Type has no default constructor - Assert.Throws(() => Activator.CreateInstance()); // Type has a default constructor that throws - Assert.ThrowsAny(() => Activator.CreateInstance()); // Type is abstract - Assert.ThrowsAny(() => Activator.CreateInstance()); // Type is an interface - } - [Fact] public static void TestActivatorOnNonActivatableFinalizableTypes() { @@ -388,11 +367,11 @@ namespace System.Tests { } - public class ClassWithPrivateDefaultConstructor + public class TypeWithPrivateDefaultConstructor { public int Property { get; } - private ClassWithPrivateDefaultConstructor() + private TypeWithPrivateDefaultConstructor() { Property = -1; } diff --git a/src/libraries/System.Runtime/tests/TestModule/System.Reflection.TestModule.il b/src/libraries/System.Runtime/tests/TestModule/System.Reflection.TestModule.il index 4c9082dc546..18291a0d0cb 100644 --- a/src/libraries/System.Runtime/tests/TestModule/System.Reflection.TestModule.il +++ b/src/libraries/System.Runtime/tests/TestModule/System.Reflection.TestModule.il @@ -1,3 +1,7 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + .assembly extern System.Runtime { .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A) diff --git a/src/libraries/System.Runtime/tests/TestStructs/Configurations.props b/src/libraries/System.Runtime/tests/TestStructs/Configurations.props new file mode 100644 index 00000000000..581054d46db --- /dev/null +++ b/src/libraries/System.Runtime/tests/TestStructs/Configurations.props @@ -0,0 +1,7 @@ + + + + netstandard; + + + \ No newline at end of file diff --git a/src/libraries/System.Runtime/tests/TestStructs/System.TestStructs.il b/src/libraries/System.Runtime/tests/TestStructs/System.TestStructs.il new file mode 100644 index 00000000000..3de7ab041c1 --- /dev/null +++ b/src/libraries/System.Runtime/tests/TestStructs/System.TestStructs.il @@ -0,0 +1,73 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +.assembly extern System.Runtime +{ + .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A) + .ver 4:0:0:0 +} + +.assembly System.TestStructs +{ + .ver 1:0:0:0 +} + +.module System.TestStructs.dll + +.namespace System.Tests +{ + .class public sequential sealed StructWithDefaultConstructor + extends [System.Runtime]System.ValueType + { + .size 1 + } + + .class public sequential sealed StructWithPublicDefaultConstructor + extends [System.Runtime]System.ValueType + { + .field public initonly bool ConstructorInvoked + + .method public hidebysig specialname rtspecialname instance void .ctor () cil managed + { + ldarg.0 + ldc.i4.1 + stfld bool System.Tests.StructWithPublicDefaultConstructor::ConstructorInvoked + ret + } + } + + .class public sequential sealed StructWithPrivateDefaultConstructor + extends [System.Runtime]System.ValueType + { + .size 1 + + .method private hidebysig specialname rtspecialname instance void .ctor () cil managed + { + ret + } + } + + .class public sequential sealed StructWithoutDefaultConstructor + extends [System.Runtime]System.ValueType + { + .size 1 + + .method public hidebysig specialname rtspecialname instance void .ctor (int32 v) cil managed + { + ret + } + } + + .class public sequential sealed StructWithDefaultConstructorThatThrows + extends [System.Runtime]System.ValueType + { + .size 1 + + .method public hidebysig specialname rtspecialname instance void .ctor () cil managed + { + newobj instance void [System.Runtime]System.Exception::.ctor() + throw + } + } +} diff --git a/src/libraries/System.Runtime/tests/TestStructs/System.TestStructs.ilproj b/src/libraries/System.Runtime/tests/TestStructs/System.TestStructs.ilproj new file mode 100644 index 00000000000..bc89c4f2858 --- /dev/null +++ b/src/libraries/System.Runtime/tests/TestStructs/System.TestStructs.ilproj @@ -0,0 +1,11 @@ + + + 1.0.0.0 + {6B002B34-089C-4BC4-91DD-57D350DEA91C} + netstandard-Debug;netstandard-Release + Microsoft + + + + + -- 2.34.1