From cee373e6afd7ba402298a1fc50171ac9a2b656a2 Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Fri, 24 Jun 2016 17:35:31 +0100 Subject: [PATCH] Cleanup MethodBuilder tests Commit migrated from https://github.com/dotnet/corefx/commit/fcad7f89c2e12de50cf41e85faf62e980bfd36ee --- .../ConstructorBuilderDefineParameter.cs | 6 +- .../tests/MethodBuilder/MethodBuilderAttributes.cs | 108 ++---- .../MethodBuilderCallingConvention.cs | 163 ++------ .../MethodBuilderContainsGenericParameters.cs | 37 +- .../MethodBuilder/MethodBuilderDeclaringType.cs | 73 +--- .../MethodBuilderDefineGenericParameters.cs | 161 ++++---- .../MethodBuilderDefineGenericParameters1b.cs | 175 --------- .../MethodBuilder/MethodBuilderDefineParameter.cs | 205 ++++++----- .../MethodBuilderDefineParameter1b.cs | 260 ------------- .../tests/MethodBuilder/MethodBuilderEquals.cs | 155 +++----- .../tests/MethodBuilder/MethodBuilderEquals1.cs | 225 ------------ .../MethodBuilderGetGenericArguments.cs | 120 ++---- .../MethodBuilderGetGenericMethodDefinition.cs | 52 +-- .../MethodBuilder/MethodBuilderGetHashCode.cs | 78 +--- .../MethodBuilder/MethodBuilderGetILGenerator.cs | 120 +++--- .../MethodBuilder/MethodBuilderGetILGenerator1.cs | 349 ------------------ .../tests/MethodBuilder/MethodBuilderInitLocals.cs | 70 +--- .../MethodBuilder/MethodBuilderIsGenericMethod.cs | 108 +----- .../MethodBuilderIsGenericMethodDefinition.cs | 110 ------ .../MethodBuilderMakeGenericMethod.cs | 110 ++---- .../tests/MethodBuilder/MethodBuilderName.cs | 64 +--- .../MethodBuilder/MethodBuilderReturnParameter.cs | 169 ++------- .../MethodBuilderSetCustomAttribute.cs | 147 +++++--- .../MethodBuilderSetCustomAttribute1.cs | 151 -------- .../MethodBuilderSetImplementationFlags.cs | 282 ++------------ .../MethodBuilder/MethodBuilderSetParameters.cs | 298 ++++----------- .../MethodBuilder/MethodBuilderSetReturnType.cs | 260 +++++-------- .../MethodBuilder/MethodBuilderSetSignature.cs | 408 ++++++--------------- .../tests/MethodBuilder/MethodBuilderToString.cs | 131 +++---- .../tests/MethodBuilder/MethodBuilderToString2.cs | 66 ---- .../tests/System.Reflection.Emit.Tests.csproj | 7 - 31 files changed, 995 insertions(+), 3673 deletions(-) delete mode 100644 src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineGenericParameters1b.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter1b.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderEquals1.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator1.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderIsGenericMethodDefinition.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute1.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderToString2.cs diff --git a/src/libraries/System.Reflection.Emit/tests/ConstructorBuilder/ConstructorBuilderDefineParameter.cs b/src/libraries/System.Reflection.Emit/tests/ConstructorBuilder/ConstructorBuilderDefineParameter.cs index ff14dff..92184bc 100644 --- a/src/libraries/System.Reflection.Emit/tests/ConstructorBuilder/ConstructorBuilderDefineParameter.cs +++ b/src/libraries/System.Reflection.Emit/tests/ConstructorBuilder/ConstructorBuilderDefineParameter.cs @@ -59,7 +59,7 @@ namespace System.Reflection.Emit.Tests ILGenerator ilGenerator = constructor.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); - ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { })); + ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); ilGenerator.Emit(OpCodes.Ret); type.CreateTypeInfo().AsType(); @@ -83,7 +83,7 @@ namespace System.Reflection.Emit.Tests ILGenerator ilGenerator = constructor.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); - ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { })); + ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); ilGenerator.Emit(OpCodes.Ret); type.CreateTypeInfo().AsType(); @@ -108,7 +108,7 @@ namespace System.Reflection.Emit.Tests ILGenerator ilGenerator = constructor.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); - ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { })); + ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); ilGenerator.Emit(OpCodes.Ret); type.CreateTypeInfo().AsType(); diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderAttributes.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderAttributes.cs index f46a5e7..6345ff5 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderAttributes.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderAttributes.cs @@ -2,91 +2,43 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderAttributes { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - - [Fact] - public void TestForMethodAttributes() - { - PosTestHelp(MethodAttributes.Abstract); - PosTestHelp(MethodAttributes.Assembly); - PosTestHelp(MethodAttributes.CheckAccessOnOverride); - PosTestHelp(MethodAttributes.FamANDAssem); - PosTestHelp(MethodAttributes.Family); - PosTestHelp(MethodAttributes.FamORAssem); - PosTestHelp(MethodAttributes.Final); - PosTestHelp(MethodAttributes.HasSecurity); - PosTestHelp(MethodAttributes.HideBySig); - PosTestHelp(MethodAttributes.MemberAccessMask); - PosTestHelp(MethodAttributes.NewSlot); - PosTestHelp(MethodAttributes.PinvokeImpl); - PosTestHelp(MethodAttributes.Private); - PosTestHelp(MethodAttributes.PrivateScope); - PosTestHelp(MethodAttributes.Public); - PosTestHelp(MethodAttributes.RequireSecObject); - PosTestHelp(MethodAttributes.ReuseSlot); - PosTestHelp(MethodAttributes.RTSpecialName); - PosTestHelp(MethodAttributes.SpecialName); - PosTestHelp(MethodAttributes.Static); - PosTestHelp(MethodAttributes.UnmanagedExport); - PosTestHelp(MethodAttributes.Virtual); - PosTestHelp(MethodAttributes.VtableLayoutMask); - } - - [Fact] - public void TestForCombinationOfMethodAttributes() + [Theory] + [InlineData(MethodAttributes.Abstract)] + [InlineData(MethodAttributes.Assembly)] + [InlineData(MethodAttributes.CheckAccessOnOverride)] + [InlineData(MethodAttributes.FamANDAssem)] + [InlineData(MethodAttributes.Family)] + [InlineData(MethodAttributes.FamORAssem)] + [InlineData(MethodAttributes.Final)] + [InlineData(MethodAttributes.HasSecurity)] + [InlineData(MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.MemberAccessMask)] + [InlineData(MethodAttributes.NewSlot)] + [InlineData(MethodAttributes.PinvokeImpl)] + [InlineData(MethodAttributes.Private)] + [InlineData(MethodAttributes.PrivateScope)] + [InlineData(MethodAttributes.Public)] + [InlineData(MethodAttributes.RequireSecObject)] + [InlineData(MethodAttributes.ReuseSlot)] + [InlineData(MethodAttributes.RTSpecialName)] + [InlineData(MethodAttributes.SpecialName)] + [InlineData(MethodAttributes.Static)] + [InlineData(MethodAttributes.UnmanagedExport)] + [InlineData(MethodAttributes.Virtual)] + [InlineData(MethodAttributes.VtableLayoutMask)] + [InlineData(MethodAttributes.Abstract | MethodAttributes.Public | MethodAttributes.NewSlot | MethodAttributes.Virtual)] + [InlineData(MethodAttributes.Final | MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.Static)] + public void Attributes(MethodAttributes attributes) { - PosTestHelp( - MethodAttributes.Abstract | - MethodAttributes.Public | - MethodAttributes.NewSlot | - MethodAttributes.Virtual); - PosTestHelp( - MethodAttributes.Final | - MethodAttributes.Private | - MethodAttributes.SpecialName | - MethodAttributes.Static); - } - - private void PosTestHelp(MethodAttributes desiredAttribute) - { - string methodName = null; - MethodAttributes actualAttribute = (MethodAttributes)(-1); - methodName = _generator.GetString(false, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - desiredAttribute); - actualAttribute = builder.Attributes; - - Assert.Equal(desiredAttribute, actualAttribute); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", attributes); + Assert.Equal(attributes, method.Attributes); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderCallingConvention.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderCallingConvention.cs index c07b12b..3819d70 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderCallingConvention.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderCallingConvention.cs @@ -2,144 +2,43 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; +using System.Collections.Generic; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderCallingConvention { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - - } - - [Fact] - public void TestforStaticMethods() - { - PosTestStaticHelp(CallingConventions.Any); - PosTestStaticHelp(CallingConventions.ExplicitThis); - PosTestStaticHelp(CallingConventions.HasThis); - PosTestStaticHelp(CallingConventions.Standard); - PosTestStaticHelp(CallingConventions.VarArgs); - } - - [Fact] - public void TestCombinationForStaticMethods() - { - PosTestStaticHelp(CallingConventions.Any | CallingConventions.Standard); - PosTestStaticHelp(CallingConventions.Any | CallingConventions.VarArgs); - PosTestStaticHelp(CallingConventions.HasThis | CallingConventions.Standard); - PosTestStaticHelp(CallingConventions.HasThis | CallingConventions.ExplicitThis); - } - - [Fact] - public void TestInstanceMethods() - { - PosTestInstanceHelp(CallingConventions.Any); - PosTestInstanceHelp(CallingConventions.ExplicitThis); - PosTestInstanceHelp(CallingConventions.HasThis); - PosTestInstanceHelp(CallingConventions.Standard); - PosTestInstanceHelp(CallingConventions.VarArgs); - } - - [Fact] - public void TestCombinationForInstanceMethods() - { - PosTestInstanceHelp(CallingConventions.Any | CallingConventions.Standard); - PosTestInstanceHelp(CallingConventions.Any | CallingConventions.VarArgs); - PosTestInstanceHelp(CallingConventions.HasThis | CallingConventions.Standard); - PosTestInstanceHelp(CallingConventions.HasThis | CallingConventions.ExplicitThis); - } - - [Fact] - public void TestCorrectValueForNegativeOneForInstanceMethods() - { - string methodName = null; - CallingConventions actualCallingConventions = (CallingConventions)0; - CallingConventions desiredCallingConventions = (CallingConventions)(-1); - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public, - desiredCallingConventions); - actualCallingConventions = builder.CallingConvention; - // Instance method should have HasThis calling convention - desiredCallingConventions |= desiredCallingConventions | CallingConventions.HasThis; - - Assert.Equal(desiredCallingConventions, actualCallingConventions); - } - - [Fact] - public void TestCorrectValueForNegativeOneForStaticMethods() - { - string methodName = null; - CallingConventions actualCallingConventions = (CallingConventions)0; - CallingConventions desiredCallingConventions = (CallingConventions)(-1); - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public, - desiredCallingConventions); - actualCallingConventions = builder.CallingConvention; - - Assert.Equal(desiredCallingConventions, actualCallingConventions); - } - - private void PosTestStaticHelp(CallingConventions desiredCallingConventions) - { - string methodName = null; - CallingConventions actualCallingConventions = (CallingConventions)(-1); - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Static, - desiredCallingConventions); - actualCallingConventions = builder.CallingConvention; - - Assert.Equal(desiredCallingConventions, actualCallingConventions); - } - - private void PosTestInstanceHelp(CallingConventions desiredCallingConventions) - { - string methodName = null; - CallingConventions actualCallingConventions = (CallingConventions)(-1); - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public, - desiredCallingConventions); - actualCallingConventions = builder.CallingConvention; - // Instance method should have HasThis calling convention - desiredCallingConventions |= desiredCallingConventions | CallingConventions.HasThis; - - Assert.Equal(desiredCallingConventions, actualCallingConventions); + public static IEnumerable CallingConventions_TestData() + { + yield return new object[] { CallingConventions.Any }; + yield return new object[] { CallingConventions.ExplicitThis }; + yield return new object[] { CallingConventions.HasThis }; + yield return new object[] { CallingConventions.Standard }; + yield return new object[] { CallingConventions.VarArgs }; + yield return new object[] { CallingConventions.Any | CallingConventions.Standard }; + yield return new object[] { CallingConventions.Any | CallingConventions.VarArgs }; + yield return new object[] { CallingConventions.HasThis | CallingConventions.Standard }; + yield return new object[] { CallingConventions.HasThis | CallingConventions.ExplicitThis }; + yield return new object[] { (CallingConventions)(-1) }; + } + + [Theory] + [MemberData(nameof(CallingConventions_TestData))] + public void CallingConvention_StaticMethods(CallingConventions callingConvention) + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod(callingConvention.ToString(), MethodAttributes.Static, callingConvention); + Assert.Equal(callingConvention, method.CallingConvention); + } + + [Theory] + [MemberData(nameof(CallingConventions_TestData))] + public void CallingConvention_InstanceMethods(CallingConventions callingConvention) + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod(callingConvention.ToString(), MethodAttributes.Public, callingConvention); + Assert.Equal(callingConvention | CallingConventions.HasThis, method.CallingConvention); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderContainsGenericParameters.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderContainsGenericParameters.cs index b5eed9d..fd60298 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderContainsGenericParameters.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderContainsGenericParameters.cs @@ -2,48 +2,19 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderContainsGenericParameters { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - [Fact] - public void TestThrowsExceptionForNotSupported() + public void ContainsGenericParameters_ThrowsNotSupportedException() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - Assert.Throws(() => { bool ret = builder.ContainsGenericParameters; }); + Assert.Throws(() => method.ContainsGenericParameters); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDeclaringType.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDeclaringType.cs index 7a50d330..4fedebe 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDeclaringType.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDeclaringType.cs @@ -2,81 +2,30 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderDeclaringType { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - private readonly byte[] _defaultILArray = new byte[] - { - 0x00, - 0x72, - 0x01, - 0x00, - 0x00, - 0x70, - 0x28, - 0x04, - 0x00, - 0x00, - 0x0a, - 0x00, - 0x2a - }; - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - [Fact] - public void TestDeclaringTypeProperty() + public void DeclaringType_TypeNotCreated() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - Assert.True(builder.DeclaringType.Equals(typeBuilder)); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + Assert.True(method.DeclaringType.Equals(type)); } [Fact] - public void TestDeclaringTypePropertyAfterTypeCreated() + public void DeclaringType_TypeCreated() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - - Type type = typeBuilder.CreateTypeInfo().AsType(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - Assert.True(builder.DeclaringType.Equals(type)); + Type resultType = type.CreateTypeInfo().AsType(); + Assert.True(method.DeclaringType.Equals(resultType)); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineGenericParameters.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineGenericParameters.cs index b34bb7a..bd31ebb 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineGenericParameters.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineGenericParameters.cs @@ -2,112 +2,137 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderDefineGenericParameters { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const string PosTestDynamicMethodName = "PosDynamicMethod"; - private const string NegTestDynamicMethodName = "NegDynamicMethod"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - - private ModuleBuilder modHelper(AssemblyBuilder asmbuild) + [Fact] + public void DefineGenericParameters_SingleTypeParameter() { - return TestLibrary.Utilities.GetModuleBuilder(asmbuild, TestDynamicModuleName); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + string[] typeParamNames = new string[] { "T" }; + GenericTypeParameterBuilder[] parameters = method.DefineGenericParameters(typeParamNames); + + Assert.True(method.IsGenericMethod); + Assert.True(method.IsGenericMethodDefinition); } - private TypeBuilder GetTestTypeBuilder() + [Fact] + public void DefineGenericParameters_TwoTypeParameters() { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, "Module1"); - return moduleBuilder.DefineType(TestDynamicTypeName); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + string[] typeParamNames = new string[] { "T", "U" }; + GenericTypeParameterBuilder[] parameters = method.DefineGenericParameters(typeParamNames); + + Assert.True(method.IsGenericMethod); + Assert.True(method.IsGenericMethodDefinition); } [Fact] - public void TestWithSingleTypeParam() + public void DefineGenericParameter_MultipleParameters() { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(PosTestDynamicMethodName, - MethodAttributes.Public); - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); - - Assert.True(builder.IsGenericMethod); - Assert.True(builder.IsGenericMethodDefinition); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, typeof(int), new Type[0]); + method.DefineGenericParameters(new string[] { "T", "U", "V" }); + + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ldc_I4, 100); + ilGenerator.Emit(OpCodes.Ret); + + Type resultType = type.CreateTypeInfo().AsType(); + Type[] typeArguments = { typeof(int), typeof(string), typeof(object) }; + MethodInfo constructedMethod = resultType.GetMethod("TestMethod").MakeGenericMethod(typeArguments); + Assert.Equal(typeArguments, constructedMethod.GetGenericArguments()); } [Fact] - public void TestWithMultipleTypeParam() + public void DefineGenericParameters_SingleTypeParameter_SetImplementationFlagsCalled_ThrowsInvalidOperationException() { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(PosTestDynamicMethodName, - MethodAttributes.Public); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); - - Assert.True(builder.IsGenericMethod); - Assert.True(builder.IsGenericMethodDefinition); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + method.SetImplementationFlags(MethodImplAttributes.Managed); + Assert.Throws(() => method.DefineGenericParameters("T")); } [Fact] - public void TestThrowsExceptionOnSetImplementationFlagsCalledPreviously() + public void DefineGenericParameters_TwoTypeParameters_SetImplementationFlagsCalled_ThrowsInvalidOperationException() { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(PosTestDynamicMethodName, - MethodAttributes.Public); - builder.SetImplementationFlags(MethodImplAttributes.Managed); - Assert.Throws(() => { GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters("T"); }); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static); + + method.SetImplementationFlags(MethodImplAttributes.IL | MethodImplAttributes.Managed | MethodImplAttributes.Synchronized | MethodImplAttributes.NoInlining); + + Assert.Throws(() => method.DefineGenericParameters(new string[] { "T", "U" })); } [Fact] - public void TestThrowsExceptionOnMethodCompleted() + public void DefineGenericParameters_SingleTypeParameter_AlreadyDefined_ThrowsInvalidOperationException() { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(PosTestDynamicMethodName, - MethodAttributes.Public); - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); - Assert.Throws(() => { builder.DefineGenericParameters(typeParamNames); }); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + string[] typeParamNames = new string[] { "T" }; + GenericTypeParameterBuilder[] parameters = method.DefineGenericParameters(typeParamNames); + Assert.Throws(() => method.DefineGenericParameters(typeParamNames)); } + [Fact] - public void TestThrowsExceptionForNull() + public void DefineGenericParameters_TwoTypeParameters_AlreadyDefined_ThrowsInvalidOperationException() { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(PosTestDynamicMethodName, - MethodAttributes.Public); - Assert.Throws(() => { GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(null); }); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static); + + method.DefineGenericParameters(new string[] { "T", "U" }); + Assert.Throws(() => method.DefineGenericParameters(new string[] { "M", "K" })); } - [Fact] - public void TestThrowsExceptionForNullMember() + [Theory] + [InlineData(TypeAttributes.NotPublic, MethodAttributes.Public)] + [InlineData(TypeAttributes.Public, MethodAttributes.Public | MethodAttributes.Static)] + public void DefineGenericParameters_NullNames_ThrowsArgumentNullException(TypeAttributes typeAttributes, MethodAttributes methodAttributes) + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes); + Assert.Throws("names", () => method.DefineGenericParameters(null)); + } + + [Theory] + [InlineData(TypeAttributes.NotPublic, MethodAttributes.Public)] + [InlineData(TypeAttributes.Public, MethodAttributes.Public | MethodAttributes.Static)] + public void DefineGenericParameters_NamesContainsNull_ThrowsArgumentNullException(TypeAttributes typeAttributes, MethodAttributes methodAttributes) { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(PosTestDynamicMethodName, - MethodAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("Test", methodAttributes); string[] typeParamNames = new string[] { "T", null, "U" }; - Assert.Throws(() => { GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); }); + Assert.Throws("names", () => method.DefineGenericParameters(typeParamNames)); + } + + [Theory] + [InlineData(TypeAttributes.NotPublic, MethodAttributes.Public)] + [InlineData(TypeAttributes.Public, MethodAttributes.Public | MethodAttributes.Static)] + public void DefineGenericParameters_EmptyNames_ThrowsArgumentException(TypeAttributes typeAttributes, MethodAttributes methodAttributes) + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder builder = type.DefineMethod("TestMethod", methodAttributes); + string[] typeParamNames = new string[0]; + Assert.Throws("names", () => builder.DefineGenericParameters(typeParamNames)); } [Fact] - public void TestThrowsExceptionForEmptyArray() + public void DefineGenericParameters_TypeCreated_ThrowsInvalidOperationException() { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(PosTestDynamicMethodName, - MethodAttributes.Public); - string[] typeParamNames = new string[] { }; - Assert.Throws(() => { GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); }); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); + + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); + + Type resultType = type.CreateTypeInfo().AsType(); + Assert.Throws(() => method.DefineGenericParameters(new string[] { "T", "U" })); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineGenericParameters1b.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineGenericParameters1b.cs deleted file mode 100644 index 4a2378b..0000000 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineGenericParameters1b.cs +++ /dev/null @@ -1,175 +0,0 @@ -// 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; - -using TestLibrary; -using System.Reflection; -using System.Reflection.Emit; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class MethodBuilderDefineGenericParameters1b - { - [Fact] - public void TestForDifferentTypes() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(name); - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - - TypeBuilder tb = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder mbuild = tb.DefineMethod("meth1", MethodAttributes.Public, typeof(int), new Type[] { }); - mbuild.DefineGenericParameters(new string[] { "T", "U", "V" }); - - ILGenerator ilgen = mbuild.GetILGenerator(); - ilgen.Emit(OpCodes.Ldc_I4, 100); - ilgen.Emit(OpCodes.Ret); - - Type tp = tb.CreateTypeInfo().AsType(); - MethodInfo mi = tp.GetMethod("meth1"); - - Type[] methArgs = { typeof(int), typeof(string), typeof(object) }; - - MethodInfo miConstructed = mi.MakeGenericMethod(methArgs); - Type[] args = miConstructed.GetGenericArguments(); - - Assert.Equal("System.Int32", args[0].ToString()); - Assert.Equal("System.String", args[1].ToString()); - Assert.Equal("System.Object", args[2].ToString()); - } - - [Fact] - public void TestThrowsExceptionForEmptyArray() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); - - - string[] typeParamNames = { }; - Assert.Throws(() => - { - GenericTypeParameterBuilder[] typeParameters = methbuild.DefineGenericParameters(typeParamNames); - }); - } - - [Fact] - public void TestThrowsExceptionForNull() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); - - Assert.Throws(() => - { - GenericTypeParameterBuilder[] typeParameters = methbuild.DefineGenericParameters(null); - }); - } - - [Fact] - public void TestThrowsExceptionForNullMember() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); - - string s = null; - string[] typeParamNames = { "T", s }; - Assert.Throws(() => - { - GenericTypeParameterBuilder[] typeParameters = methbuild.DefineGenericParameters(typeParamNames); - }); - } - - [Fact] - public void TestThrowsExceptionForTypeCreated() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); - - ILGenerator ilgen = methbuild.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - - Type tp = tpbuild.CreateTypeInfo().AsType(); - MethodInfo mi = tp.GetMethod("method1"); - - string[] typeParamNames = { "T", "U" }; - Assert.Throws(() => - { - GenericTypeParameterBuilder[] typeParameters = methbuild.DefineGenericParameters(typeParamNames); - }); - } - - [Fact] - public void TestThrowsExceptionForSetImplementationFlagsCalledPreviously() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); - - - methbuild.SetImplementationFlags(MethodImplAttributes.IL | - MethodImplAttributes.Managed | - MethodImplAttributes.Synchronized | - MethodImplAttributes.NoInlining); - - string[] typeParamNames = { "T", "U" }; - Assert.Throws(() => - { - GenericTypeParameterBuilder[] typeParameters = methbuild.DefineGenericParameters(typeParamNames); - }); - } - - [Fact] - public void TestThrowsExceptionForParametersDefinedAlready() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); - - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - methbuild.DefineGenericParameters(typeParamNames); - - string[] typeParamNames2 = { "M", "K" }; - Assert.Throws(() => { methbuild.DefineGenericParameters(typeParamNames2); }); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter.cs index 70baddf..02853d7 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter.cs @@ -2,137 +2,168 @@ // 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; - -using TestLibrary; -using System.Reflection; -using System.Reflection.Emit; +using System.Linq; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderDefineParameter { + private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; + [Fact] - public void TestDefineParameter() + public void DefineParameter_TwoParameters() { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); Type[] paramTypes = new Type[] { typeof(string), typeof(object) }; - string[] paramNames = new string[] { "Param1", "Param2" }; - ParameterAttributes[] paramAttrs = new ParameterAttributes[] { ParameterAttributes.In, ParameterAttributes.Out }; Type returnType = typeof(int); - ParameterAttributes returnAttrs = ParameterAttributes.None; - MethodBuilder methbuild = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, - returnType, paramTypes); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, returnType, paramTypes); - methbuild.DefineParameter(0, returnAttrs, "returnName"); - methbuild.DefineParameter(1, paramAttrs[0], paramNames[0]); - methbuild.DefineParameter(2, paramAttrs[1], paramNames[1]); + method.DefineParameter(0, ParameterAttributes.None, "returnName"); + method.DefineParameter(1, ParameterAttributes.In, "Param1"); + method.DefineParameter(2, ParameterAttributes.Out, "Param2"); - int expectedRet = 3; - ILGenerator ilgen = methbuild.GetILGenerator(); - ilgen.Emit(OpCodes.Ldc_I4, expectedRet); - ilgen.Emit(OpCodes.Ret); + int expectedReturn = 3; + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ldc_I4, expectedReturn); + ilGenerator.Emit(OpCodes.Ret); - Type tp = tpbuild.CreateTypeInfo().AsType(); + Type resultType = type.CreateTypeInfo().AsType(); - ParameterInfo[] paramArray = methbuild.GetParameters(); + ParameterInfo[] parameters = method.GetParameters(); + Assert.Equal("System.String Param1", parameters[0].ToString()); + Assert.Equal("System.Object Param2", parameters[1].ToString()); - Assert.Equal("System.String Param1", paramArray[0].ToString()); - Assert.Equal("System.Object Param2", paramArray[1].ToString()); - // invoke method to verify it still works correctly - MethodInfo mi = tp.GetMethod("meth1"); - int ret = (int)mi.Invoke(null, new object[] { "hello", new object() }); - Assert.Equal(expectedRet, ret); + // Invoke the method to verify it works correctly + MethodInfo resultMethod = resultType.GetMethod("TestMethod"); + Assert.Equal(expectedReturn, resultMethod.Invoke(null, new object[] { "hello", new object() })); } [Fact] - public void TestThrowsExceptionForTypeCreated() + public void DefineParameter_HasDefaultAttribute() { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - - Type[] paramTypes = new Type[] { typeof(string), typeof(object) }; - string[] paramNames = new string[] { "Param1", "Param2" }; - ParameterAttributes[] paramAttrs = new ParameterAttributes[] { ParameterAttributes.In, ParameterAttributes.Out }; - Type returnType = typeof(int); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + Type[] paramTypes = new Type[] { typeof(int) }; + MethodBuilder method = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), paramTypes); - MethodBuilder methbuild = tpbuild.DefineMethod("meth1", MethodAttributes.Public, returnType, paramTypes); - ILGenerator ilgen = methbuild.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - - Type tp = tpbuild.CreateTypeInfo().AsType(); + ParameterBuilder parameter = method.DefineParameter(1, ParameterAttributes.HasDefault, "TestParam"); + VerifyParameterBuilder(parameter, "TestParam", ParameterAttributes.HasDefault, 1); + } - Assert.Throws(() => { methbuild.DefineParameter(1, ParameterAttributes.Retval, "param1"); }); + [Fact] + public void DefineParameter_AllAttributes() + { + ParameterAttributes[] attributes = new ParameterAttributes[] + { + ParameterAttributes.HasDefault, + ParameterAttributes.HasFieldMarshal, + ParameterAttributes.In, + ParameterAttributes.None, + ParameterAttributes.Optional, + ParameterAttributes.Out, + ParameterAttributes.Retval + }; + + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + Type[] paramTypes = Enumerable.Repeat(typeof(int), attributes.Length).ToArray(); + MethodBuilder method = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), paramTypes); + + for (int i = 1; i < attributes.Length; ++i) + { + ParameterBuilder parameter = method.DefineParameter(i, attributes[i], "TestParam"); + VerifyParameterBuilder(parameter, "TestParam", attributes[i], i); + } } [Fact] - public void TestThrowsExceptionForNegativePosition() + public void DefineParameter_AllAttributesCombined() { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + Type[] paramTypes = new Type[] { typeof(int) }; + MethodBuilder method = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), paramTypes); + + ParameterAttributes attribute = + ParameterAttributes.HasDefault | + ParameterAttributes.HasFieldMarshal | + ParameterAttributes.In | + ParameterAttributes.None | + ParameterAttributes.Optional | + ParameterAttributes.Out | + ParameterAttributes.Retval; + ParameterBuilder parameter = method.DefineParameter(1, attribute, "TestParam"); + VerifyParameterBuilder(parameter, "TestParam", attribute, 1); + } - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); + [Fact] + public void DefineParameter_NegativeAttribute() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + Type[] paramTypes = new Type[] { typeof(int) }; + MethodBuilder method = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), paramTypes); + ParameterBuilder parameter = method.DefineParameter(1, (ParameterAttributes)(-1), "TestParam"); + } - Type[] paramTypes = new Type[] { typeof(string), typeof(object) }; - string[] paramNames = new string[] { "Param1", "Param2" }; - ParameterAttributes[] paramAttrs = new ParameterAttributes[] { ParameterAttributes.In, ParameterAttributes.Out }; + [Fact] + public void DefineParameter_TypeAlreadyCreated_ThrowsInvalidOperationException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); Type returnType = typeof(int); + Type[] paramTypes = new Type[] { typeof(string), typeof(object) }; + + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, returnType, paramTypes); + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - MethodBuilder methbuild = tpbuild.DefineMethod("meth1", MethodAttributes.Public, returnType, paramTypes); + Type resultType = type.CreateTypeInfo().AsType(); - Assert.Throws(() => { methbuild.DefineParameter(-1, ParameterAttributes.None, "Param1"); }); + Assert.Throws(() => method.DefineParameter(1, ParameterAttributes.Retval, "param1")); } [Fact] - public void TestThrowsExceptionForNoParameters() + public void DefineParameter_SingleParameter_TypeAlreadyCreated_ThrowsInvalidOperationException() { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder builder = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), new Type[] { typeof(int) }); - MethodBuilder methbuild = tpbuild.DefineMethod("meth1", MethodAttributes.Public); + type.CreateTypeInfo().AsType(); - Assert.Throws(() => { methbuild.DefineParameter(1, ParameterAttributes.None, "Param1"); }); + Assert.Throws(() => builder.DefineParameter(1, ParameterAttributes.HasDefault, "TestParam")); } - [Fact] - public void TestThrowsExceptionForPositionGreaterThanNumberOfParameters() + [Theory] + [InlineData(TypeAttributes.Public, MethodAttributes.Public, ParameterAttributes.None, new Type[] { typeof(string), typeof(object) }, typeof(int))] + [InlineData(TypeAttributes.Abstract, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, ParameterAttributes.HasDefault, new Type[] { typeof(int) }, typeof(void))] + public void DefineParameter_InvalidPosition_ThrowsArgumentOutOfRangeException(TypeAttributes typeAttributes, MethodAttributes methodAttributes, ParameterAttributes parameterAttributes, Type[] paramTypes, Type returnType) { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); + TypeBuilder type = Helpers.DynamicType(typeAttributes); + MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes, returnType, paramTypes); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); + Assert.Throws(() => method.DefineParameter(-1, parameterAttributes, "Param1")); + Assert.Throws(() => method.DefineParameter(paramTypes.Length + 1, parameterAttributes, "Param1")); + } - Type[] paramTypes = new Type[] { typeof(string), typeof(object) }; - string[] paramNames = new string[] { "Param1", "Param2" }; - ParameterAttributes[] paramAttrs = new ParameterAttributes[] { ParameterAttributes.In, ParameterAttributes.Out }; - Type returnType = typeof(int); + [Theory] + [InlineData(TypeAttributes.Public, MethodAttributes.Public, ParameterAttributes.None)] + [InlineData(TypeAttributes.Abstract, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, ParameterAttributes.HasDefault)] + public void DefineParameter_NoParameters_NonZeroPosition_ThrowsArgumentOutOfRangeException(TypeAttributes typeAttributes, MethodAttributes methodAttributes, ParameterAttributes parameterAttributes) + { + TypeBuilder type = Helpers.DynamicType(typeAttributes); + MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes); - MethodBuilder methbuild = tpbuild.DefineMethod("meth1", MethodAttributes.Public, returnType, paramTypes); + Assert.Throws(() => method.DefineParameter(1, parameterAttributes, "Param1")); + Assert.Throws(() => method.DefineParameter(-1, parameterAttributes, "Param1")); + } + + private static void VerifyParameterBuilder(ParameterBuilder parameter, string expectedName, ParameterAttributes expectedAttributes, int expectedPosition) + { + // This constant maps to ParameterAttributes.ReservedMask that is not available in the contract. + const int ReservedMaskParameterAttribute = 0xF000; + Assert.Equal(expectedName, parameter.Name); - Assert.Throws(() => { methbuild.DefineParameter(3, ParameterAttributes.None, "Param1"); }); + int removedReservedAttributes = (int)expectedAttributes & ~ReservedMaskParameterAttribute; + Assert.Equal(removedReservedAttributes, (parameter.Attributes & removedReservedAttributes)); + Assert.Equal(expectedPosition, parameter.Position); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter1b.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter1b.cs deleted file mode 100644 index 23440b7..0000000 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter1b.cs +++ /dev/null @@ -1,260 +0,0 @@ -// 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class MethodBuilderDefineParameter1b - { - private const int MinStringLength = 1; - private const int MaxStringLength = 256; - - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const string PosTestDynamicMethodName = "PosDynamicMethod"; - private const string NegTestDynamicMethodName = "NegDynamicMethod"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TypeAttributes.Abstract); - } - - [Fact] - public void TestWithHasDefault() - { - string strParamName = null; - - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - PosTestDynamicMethodName, - TestMethodAttributes, - typeof(void), - paramTypes); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - strParamName); - - Assert.NotNull(paramBuilder); - VerifyParameterBuilder(paramBuilder, strParamName, ParameterAttributes.HasDefault, 1); - } - - [Fact] - public void TestForEveryParameterAtributeFlag() - { - string strParamName = null; - - ParameterAttributes[] attributes = new ParameterAttributes[] { - ParameterAttributes.HasDefault, - ParameterAttributes.HasFieldMarshal, - ParameterAttributes.In, - ParameterAttributes.None, - ParameterAttributes.Optional, - ParameterAttributes.Out, - ParameterAttributes.Retval - }; - - Type[] paramTypes = new Type[attributes.Length]; - for (int i = 0; i < paramTypes.Length; ++i) - { - paramTypes[i] = typeof(int); - } - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - PosTestDynamicMethodName, - TestMethodAttributes, - typeof(void), - paramTypes); - - for (int i = 1; i < attributes.Length; ++i) - { - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - ParameterBuilder paramBuilder = builder.DefineParameter(i, attributes[i], strParamName); - - Assert.NotNull(paramBuilder); - VerifyParameterBuilder(paramBuilder, strParamName, attributes[i], i); - } - } - - [Fact] - public void TestForCombinationOfParamterAttributeFlags() - { - string strParamName = null; - - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - PosTestDynamicMethodName, - TestMethodAttributes, - typeof(void), - paramTypes); - ParameterAttributes attribute = - ParameterAttributes.HasDefault | - ParameterAttributes.HasFieldMarshal | - ParameterAttributes.In | - ParameterAttributes.None | - ParameterAttributes.Optional | - ParameterAttributes.Out | - ParameterAttributes.Retval; - - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - attribute, - strParamName); - - Assert.NotNull(paramBuilder); - VerifyParameterBuilder(paramBuilder, strParamName, attribute, 1); - } - - [Fact] - public void TestForNegativeFlag() - { - string strParamName = null; - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - PosTestDynamicMethodName, - TestMethodAttributes, - typeof(void), - paramTypes); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - (ParameterAttributes)(-1), - strParamName); - } - - - - [Fact] - public void TestThrowsExceptionWithNoParameter() - { - string strParamName = null; - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - PosTestDynamicMethodName, - TestMethodAttributes); - Assert.Throws(() => - { - ParameterBuilder paramBuilder = builder.DefineParameter(1, ParameterAttributes.HasDefault, strParamName); - }); - } - - [Fact] - public void TestThrowsExceptionForNegativePosition() - { - string strParamName = null; - int paramPos = 0; - - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - paramPos = _generator.GetInt32(); - if (paramPos > 0) - { - paramPos = 0 - paramPos; - } - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - PosTestDynamicMethodName, - TestMethodAttributes); - Assert.Throws(() => - { - ParameterBuilder paramBuilder = builder.DefineParameter(paramPos, ParameterAttributes.HasDefault, strParamName); - }); - } - - [Fact] - public void TestThrowsExceptionForPositionGreaterThanNumberOfParameters() - { - string strParamName = null; - int paramPos = 0; - - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] paramTypes = new Type[] { - typeof(int) - }; - while (paramPos < paramTypes.Length) - { - paramPos = _generator.GetInt32(); - } - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - - MethodBuilder builder = typeBuilder.DefineMethod( - PosTestDynamicMethodName, - TestMethodAttributes, - typeof(void), - paramTypes); - Assert.Throws(() => - { - ParameterBuilder paramBuilder = builder.DefineParameter(paramPos, ParameterAttributes.HasDefault, strParamName); - }); - } - - [Fact] - public void TestThrowsExceptionForCreateTypeCalled() - { - string strParamName = null; - - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] paramTypes = new Type[] { - typeof(int) - }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - - MethodBuilder builder = typeBuilder.DefineMethod( - PosTestDynamicMethodName, - TestMethodAttributes, - typeof(void), - paramTypes); - - typeBuilder.CreateTypeInfo().AsType(); - - Assert.Throws(() => - { - ParameterBuilder paramBuilder = builder.DefineParameter(1, ParameterAttributes.HasDefault, strParamName); - }); - } - - private void VerifyParameterBuilder( - ParameterBuilder actualBuilder, - string desiredName, - ParameterAttributes desiredAttributes, - int desiredPosition) - { - const int ReservedMaskParameterAttribute = 0xF000; // This constant maps to ParameterAttributes.ReservedMask that is not available in the contract. - Assert.Equal(desiredName, actualBuilder.Name); - - int removedReservedAttributes = (int)desiredAttributes & ~ReservedMaskParameterAttribute; - Assert.Equal((int)removedReservedAttributes, (actualBuilder.Attributes & (int)removedReservedAttributes)); - Assert.Equal(desiredPosition, actualBuilder.Position); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderEquals.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderEquals.cs index 43ed05c..e5c0de8 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderEquals.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderEquals.cs @@ -2,137 +2,80 @@ // 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; - -using TestLibrary; -using System.Reflection; -using System.Reflection.Emit; +using System.Collections.Generic; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderEquals { - [Fact] - public void TestRandomObject() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - - MethodBuilder methbuild = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, - typeof(int), new Type[] { }); - - Assert.False(methbuild.Equals(tpbuild)); - } - - [Fact] - public void TestWithDifferentNames() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - - MethodBuilder methbuild1 = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, - typeof(int), new Type[] { }); - - MethodBuilder methbuild2 = tpbuild.DefineMethod("meth11", MethodAttributes.Public | MethodAttributes.Static, - typeof(int), new Type[] { }); - - Assert.False(methbuild1.Equals(methbuild2)); - } - - [Fact] - public void TestWithDifferentAttributes() + public static IEnumerable Equals_TestData() { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + MethodBuilder method1 = type.DefineMethod("TestMethod1", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]); + MethodBuilder method2 = type.DefineMethod("TestMethod2", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]); + MethodBuilder method3 = type.DefineMethod("TestMethod1", MethodAttributes.Public, typeof(int), new Type[0]); - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); + MethodBuilder method4 = type.DefineMethod("TestMethod3", MethodAttributes.Public | MethodAttributes.Static); + method4.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); - MethodBuilder methbuild1 = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, - typeof(int), new Type[] { }); + MethodBuilder method5 = type.DefineMethod("TestMethod3", MethodAttributes.Public | MethodAttributes.Static); + method5.SetSignature(typeof(int), new Type[] { typeof(object) }, null, null, null, null); - MethodBuilder methbuild2 = tpbuild.DefineMethod("meth1", MethodAttributes.Public, - typeof(int), new Type[] { }); + MethodBuilder method6 = type.DefineMethod("TestMethod3", MethodAttributes.Public | MethodAttributes.Static); + method6.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); + MethodBuilder method7 = type.DefineMethod("TestMethod4", MethodAttributes.Public | MethodAttributes.Static); + method7.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); + method7.DefineGenericParameters(new string[] { "T", "U", "V" }); - Assert.False(methbuild1.Equals(methbuild2)); - } + MethodBuilder method8 = type.DefineMethod("TestMethod4", MethodAttributes.Public | MethodAttributes.Static); + method8.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); + method8.DefineGenericParameters(new string[] { "T", "U" }); - [Fact] - public void TestWithDifferentSignatures() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; + MethodBuilder method9 = type.DefineMethod("TestMethod5", MethodAttributes.Public); + MethodBuilder method10 = type.DefineMethod("TestMethod5", MethodAttributes.Public); + MethodBuilder method11 = type.DefineMethod("TestMethod6", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.NewSlot); + MethodBuilder method12 = type.DefineMethod("TestMethod6", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.NewSlot); + + MethodBuilder method13 = type.DefineMethod("TestMethod7", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(string) }); + MethodBuilder method14 = type.DefineMethod("TestMethod7", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(string) }); - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); + MethodBuilder method15 = type.DefineMethod("TestMethod8", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(string) }); + MethodBuilder method16 = type.DefineMethod("TestMethod8", MethodAttributes.Public, typeof(int), new Type[] { typeof(int), typeof(string) }); - MethodBuilder methbuild1 = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static); - methbuild1.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); + MethodBuilder method17 = type.DefineMethod("TestMethod9", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(float) }); + MethodBuilder method18 = type.DefineMethod("TestMethod9", MethodAttributes.Public, typeof(void), new Type[] { typeof(string), typeof(float) }); - MethodBuilder methbuild2 = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static); - methbuild2.SetSignature(typeof(int), new Type[] { typeof(object) }, null, null, null, null); + MethodBuilder method19 = type.DefineMethod("TestMethod10", MethodAttributes.Public, typeof(void), new Type[] { typeof(int), typeof(string) }); + MethodBuilder method20 = type.DefineMethod("TestMethod10", MethodAttributes.Public, typeof(void), new Type[] { typeof(string), typeof(int) }); - Assert.False(methbuild1.Equals(methbuild2)); - } - - [Fact] - public void TestWithEqualObjects() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; + yield return new object[] { method1, type, false }; // Random object + yield return new object[] { method1, null, false }; // Null + yield return new object[] { method1, method2, false }; // Different name + yield return new object[] { method1, method3, false }; // Different attributes + yield return new object[] { method4, method5, false }; // Different signature + yield return new object[] { method4, method6, true }; // Same signature + yield return new object[] { method7, method8, false }; // Different generic parameters - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); + yield return new object[] { method9, method9, true }; // Same instance + yield return new object[] { method9, method10, true }; // Same name + yield return new object[] { method11, method12, true }; // Same name, same attributes + yield return new object[] { method13, method14, true }; // Same name, same attributes, same parameters - MethodBuilder methbuild1 = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static); - methbuild1.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); + yield return new object[] { method15, method16, false }; // Different return type + yield return new object[] { method17, method18, false }; // Different parameters + yield return new object[] { method19, method20, false }; // Different parameter ordering - MethodBuilder methbuild2 = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static); - methbuild2.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); - - Assert.True(methbuild1.Equals(methbuild2)); } - [Fact] - public void TestWithDifferentGenericArguments() + [Theory] + [MemberData(nameof(Equals_TestData))] + public void Equals(MethodBuilder builder, object obj, bool expected) { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - - MethodBuilder methbuild1 = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static); - methbuild1.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); - methbuild1.DefineGenericParameters(new string[] { "T", "U", "V" }); - - MethodBuilder methbuild2 = tpbuild.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static); - methbuild2.SetSignature(typeof(int), new Type[] { typeof(string) }, null, null, null, null); - methbuild2.DefineGenericParameters(new string[] { "T", "U" }); - - Assert.False(methbuild1.Equals(methbuild2)); + Assert.Equal(expected, builder.Equals(obj)); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderEquals1.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderEquals1.cs deleted file mode 100644 index 72b87f0..0000000 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderEquals1.cs +++ /dev/null @@ -1,225 +0,0 @@ -// 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class MethodBuilderEquals1 - { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const string PosTestDynamicMethodName = "PosDynamicMethod"; - private const string NegTestDynamicMethodName = "NegDynamicMethod"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName); - } - - [Fact] - public void TestWithItself() - { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(NegTestDynamicMethodName, - MethodAttributes.Public); - - Assert.True(builder.Equals(builder)); - } - - [Fact] - public void TestWithSameName() - { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - MethodAttributes.Public); - MethodBuilder builder2 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - MethodAttributes.Public); - - Assert.True(builder1.Equals(builder2)); - } - - [Fact] - public void TestWithSameNameAndAttributes() - { - MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.NewSlot; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes); - MethodBuilder builder2 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes); - - Assert.True(builder1.Equals(builder2)); - } - - [Fact] - public void TestWithSameNameAttributeReturnTypeParameterType() - { - MethodAttributes attributes = MethodAttributes.Public; - Type returnType = typeof(void); - Type[] paramTypes = new Type[] { - typeof(int), - typeof(string) - }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - - MethodBuilder builder1 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes, - returnType, - paramTypes); - MethodBuilder builder2 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes, - returnType, - paramTypes); - - Assert.True(builder1.Equals(builder2)); - } - - [Fact] - public void TestWithDifferentReturnType() - { - MethodAttributes attributes = MethodAttributes.Public; - Type returnType1 = typeof(void); - Type returnType2 = typeof(int); - Type[] paramTypes = new Type[] { - typeof(int), - typeof(string) - }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - - MethodBuilder builder1 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes, - returnType1, - paramTypes); - MethodBuilder builder2 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes, - returnType2, - paramTypes); - - Assert.False(builder1.Equals(builder2)); - } - - [Fact] - public void TestWithDifferentName() - { - string methodName1 = null; - string methodName2 = null; - - methodName1 = _generator.GetString(false, MinStringLength, MaxStringLength); - methodName2 = _generator.GetString(false, MinStringLength, MaxStringLength); - - // Avoid generate same method name - if (methodName1.Length == methodName2.Length) - { - methodName1 += _generator.GetChar(); - } - - MethodAttributes attributes = MethodAttributes.Public; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(methodName1, - attributes); - MethodBuilder builder2 = typeBuilder.DefineMethod(methodName2, - attributes); - - Assert.False(builder1.Equals(builder2)); - } - - [Fact] - public void TestWithDifferentAttributes() - { - MethodAttributes attributes1 = MethodAttributes.Public; - MethodAttributes attributes2 = MethodAttributes.Private; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes1); - MethodBuilder builder2 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes2); - - Assert.False(builder1.Equals(builder2)); - } - - [Fact] - public void TestWithDifferentParameterList() - { - MethodAttributes attributes = MethodAttributes.Public; - Type returnType = typeof(void); - Type[] paramTypes1 = new Type[] { - typeof(int), - typeof(float) - }; - Type[] paramTypes2 = new Type[] { - typeof(string), - typeof(float) - }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes, - returnType, - paramTypes1); - MethodBuilder builder2 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes, - returnType, - paramTypes2); - - Assert.False(builder1.Equals(builder2)); - } - - [Fact] - public void TestWithDifferentParameterOrder() - { - MethodAttributes attributes = MethodAttributes.Public; - Type returnType = typeof(void); - Type[] paramTypes1 = new Type[] { - typeof(int), - typeof(string) - }; - Type[] paramTypes2 = new Type[] { - typeof(string), - typeof(int) - }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes, - returnType, - paramTypes1); - MethodBuilder builder2 = typeBuilder.DefineMethod(NegTestDynamicMethodName, - attributes, - returnType, - paramTypes2); - - Assert.False(builder1.Equals(builder2)); - } - - [Fact] - public void TestWithNull() - { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(NegTestDynamicMethodName, - MethodAttributes.Public); - - Assert.False(builder.Equals(null)); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetGenericArguments.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetGenericArguments.cs index 33f5ad4..a722b06 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetGenericArguments.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetGenericArguments.cs @@ -2,120 +2,66 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; +using System.Collections.Generic; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderGetGenericArguments { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - [Fact] - public void TestWithNonGenericMethod() + public void GetGenericArguments_NonGenericMethod_ReturnsNull() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod1", MethodAttributes.Public); - Assert.Null(builder.GetGenericArguments()); + VerifyGenericArguments(method, null); } [Fact] - public void TestWithGenericMethod() + public void GetGenericArguments_GenericMethod() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] desiredParameters = builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod2", MethodAttributes.Public); + GenericTypeParameterBuilder[] genericParams = method.DefineGenericParameters("T", "U"); - Type[] actualParameters = builder.GetGenericArguments(); - VerificationHelper(desiredParameters, actualParameters); + VerifyGenericArguments(method, genericParams); } [Fact] - public void TestWithSingleParameter() + public void GetGenericArguments_GenericMethod_GenericParameters() { - string methodName = null; - string paramName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - paramName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod4", MethodAttributes.Public); + GenericTypeParameterBuilder[] genericParams = method.DefineGenericParameters("T", "U"); + method.SetReturnType(genericParams[0].AsType()); - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - methodName, - MethodAttributes.Public, - typeof(void), - paramTypes); - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] desiredParameters = builder.DefineGenericParameters(typeParamNames); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - paramName); - - Type[] actualParameters = builder.GetGenericArguments(); - VerificationHelper(desiredParameters, actualParameters); + VerifyGenericArguments(method, genericParams); } - + [Fact] - public void TestWithGenericReturnType() + public void GetGenericArguments_SingleParameters() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - methodName, - MethodAttributes.Public); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] desiredParameters = builder.DefineGenericParameters(typeParamNames); - - builder.SetReturnType(desiredParameters[0].AsType()); - - Type[] actualParameters = builder.GetGenericArguments(); - VerificationHelper(desiredParameters, actualParameters); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod3", MethodAttributes.Public, typeof(void), new Type[] { typeof(int) }); + GenericTypeParameterBuilder[] genericParams = method.DefineGenericParameters("T"); + method.DefineParameter(1, ParameterAttributes.HasDefault, "TestParam"); + VerifyGenericArguments(method, genericParams); } - private void VerificationHelper(GenericTypeParameterBuilder[] desiredParameters, Type[] actualParameters) + private static void VerifyGenericArguments(MethodBuilder method, GenericTypeParameterBuilder[] expected) { - if (null == desiredParameters) - Assert.Null(actualParameters); - if (null != desiredParameters) + Type[] genericArguments = method.GetGenericArguments(); + if (expected == null) + { + Assert.Null(genericArguments); + } + else { - Assert.NotNull(actualParameters); - Assert.Equal(desiredParameters.Length, actualParameters.Length); - for (int i = 0; i < actualParameters.Length; ++i) + Assert.Equal(expected.Length, genericArguments.Length); + for (int i = 0; i < genericArguments.Length; ++i) { - Assert.True(desiredParameters[i].Equals(actualParameters[i])); + Assert.True(expected[i].Equals(genericArguments[i])); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetGenericMethodDefinition.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetGenericMethodDefinition.cs index de33dc7..4624b54 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetGenericMethodDefinition.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetGenericMethodDefinition.cs @@ -2,62 +2,28 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderGetGenericMethodDefinition { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - [Fact] - public void TestGetGenericMethodDefinition() + public void GetGenericMethodDefinition() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] desiredParameters = builder.DefineGenericParameters(typeParamNames); - - Assert.True(builder.GetGenericMethodDefinition().Equals(builder)); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + method.DefineGenericParameters("T", "U"); + Assert.True(method.GetGenericMethodDefinition().Equals(method)); } [Fact] - public void TestThrowsExceptionOnNonGenericMethod() + public void GetGenericMethodDefinition_NonGenericMethod_ThrowsInvalidOperationException() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - Assert.Throws(() => { MethodInfo methodInfo = builder.GetGenericMethodDefinition(); }); + Assert.Throws(() => method.GetGenericMethodDefinition()); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetHashCode.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetHashCode.cs index aefee4c..52695ff 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetHashCode.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetHashCode.cs @@ -2,82 +2,38 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; +using System.Collections.Generic; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderGetHashCode { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() + public static IEnumerable GetHashCode_TestData() { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } + MethodBuilder method1 = type.DefineMethod("TestMethod1", MethodAttributes.Public); + MethodBuilder method2 = type.DefineMethod("TestMethod1", MethodAttributes.Public); - [Fact] - public void TestForEqualObjects1() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - MethodBuilder builder2 = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); + MethodBuilder method3 = type.DefineMethod("TestMethod1", MethodAttributes.Public); + method3.DefineGenericParameters("T", "U"); - Assert.Equal(builder1.GetHashCode(), builder2.GetHashCode()); - } + MethodBuilder method4 = type.DefineMethod("TestMethod1", MethodAttributes.Public); + method4.DefineGenericParameters("T", "U"); - [Fact] - public void TestForEqualObjects2() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] Parameters = builder1.DefineGenericParameters(typeParamNames); - MethodBuilder builder2 = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - Parameters = builder2.DefineGenericParameters(typeParamNames); + MethodBuilder method5 = type.DefineMethod("TestMethod2", MethodAttributes.Public); - Assert.Equal(builder1.GetHashCode(), builder2.GetHashCode()); + yield return new object[] { method1, method2, true }; // Non-generic + yield return new object[] { method3, method4, true }; // Generic + yield return new object[] { method1, method5, false }; // Different names } - [Fact] - public void TestForNonEqualObjects() + [Theory] + [MemberData(nameof(GetHashCode_TestData))] + public void GetHashCode(MethodBuilder method1, MethodBuilder method2, bool expected) { - string methodName1 = null; - string methodName2 = null; - methodName1 = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - methodName2 = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder1 = typeBuilder.DefineMethod(methodName1, - MethodAttributes.Public); - MethodBuilder builder2 = typeBuilder.DefineMethod(methodName2, - MethodAttributes.Public); - - Assert.NotEqual(builder1.GetHashCode(), builder2.GetHashCode()); + Assert.Equal(expected, method1.GetHashCode().Equals(method2.GetHashCode())); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs index e44b112..6720d90 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator.cs @@ -2,86 +2,78 @@ // 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; - -using TestLibrary; -using System.Reflection; -using System.Reflection.Emit; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderGetILGenerator { - [Fact] - public void TestWithSizeGreaterThanDefaultSize() + [Theory] + [InlineData(20)] + [InlineData(-10)] + public void GetILGenerator_Int(int size) { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static, - typeof(int), new Type[] { }); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]); - ILGenerator ilgen = methbuild.GetILGenerator(20); - int expectedRet = 5; - ilgen.Emit(OpCodes.Ldc_I4, expectedRet); - ilgen.Emit(OpCodes.Ret); + ILGenerator ilGenerator = method.GetILGenerator(size); + int expectedReturn = 5; + ilGenerator.Emit(OpCodes.Ldc_I4, expectedReturn); + ilGenerator.Emit(OpCodes.Ret); - Type tp = tpbuild.CreateTypeInfo().AsType(); - MethodInfo mi = tp.GetMethod("method1"); - int ret = (int)mi.Invoke(null, null); - - Assert.Equal(expectedRet, ret); + Type createdType = type.CreateTypeInfo().AsType(); + MethodInfo createedMethod = createdType.GetMethod("TestMethod"); + Assert.Equal(expectedReturn, createedMethod.Invoke(null, null)); } - [Fact] - public void TestWithSizeLessThanDefaultSize() + [Theory] + [InlineData(TypeAttributes.Public, MethodAttributes.Public | MethodAttributes.PinvokeImpl)] + [InlineData(TypeAttributes.Abstract, MethodAttributes.PinvokeImpl)] + [InlineData(TypeAttributes.Abstract, MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)] + public void GetILGenerator_NoMethodBody_ThrowsInvalidOperationException(TypeAttributes typeAttributes, MethodAttributes methodAttributes) { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); + TypeBuilder type = Helpers.DynamicType(typeAttributes); + MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static, - typeof(int), new Type[] { }); - - // runtime will use default size and ignore this value - ILGenerator ilgen = methbuild.GetILGenerator(-10); - int expectedRet = 5; - ilgen.Emit(OpCodes.Ldc_I4, expectedRet); - ilgen.Emit(OpCodes.Ret); - - Type tp = tpbuild.CreateTypeInfo().AsType(); - MethodInfo mi = tp.GetMethod("method1"); - int ret = (int)mi.Invoke(null, null); - - Assert.Equal(expectedRet, ret); + Assert.Throws(() => method.GetILGenerator()); + Assert.Throws(() => method.GetILGenerator(10)); } - [Fact] - public void TestThrowsExceptionForMethodWithNoBody() + [Theory] + [InlineData(MethodAttributes.Abstract)] + [InlineData(MethodAttributes.Assembly)] + [InlineData(MethodAttributes.CheckAccessOnOverride)] + [InlineData(MethodAttributes.FamANDAssem)] + [InlineData(MethodAttributes.Family)] + [InlineData(MethodAttributes.FamORAssem)] + [InlineData(MethodAttributes.Final)] + [InlineData(MethodAttributes.HasSecurity)] + [InlineData(MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.MemberAccessMask)] + [InlineData(MethodAttributes.NewSlot)] + [InlineData(MethodAttributes.Private)] + [InlineData(MethodAttributes.PrivateScope)] + [InlineData(MethodAttributes.Public)] + [InlineData(MethodAttributes.RequireSecObject)] + [InlineData(MethodAttributes.ReuseSlot)] + [InlineData(MethodAttributes.RTSpecialName)] + [InlineData(MethodAttributes.SpecialName)] + [InlineData(MethodAttributes.Static)] + [InlineData(MethodAttributes.UnmanagedExport)] + [InlineData(MethodAttributes.Virtual)] + [InlineData(MethodAttributes.VtableLayoutMask)] + [InlineData(MethodAttributes.Assembly | MethodAttributes.CheckAccessOnOverride | + MethodAttributes.FamORAssem | MethodAttributes.Final | + MethodAttributes.HasSecurity | MethodAttributes.HideBySig | MethodAttributes.MemberAccessMask | + MethodAttributes.NewSlot | MethodAttributes.Private | + MethodAttributes.PrivateScope | MethodAttributes.RequireSecObject | + MethodAttributes.RTSpecialName | MethodAttributes.SpecialName | + MethodAttributes.Static | MethodAttributes.UnmanagedExport)] + public void GetILGenerator_DifferentAttributes(MethodAttributes attributes) { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.PinvokeImpl); - - Assert.Throws(() => { ILGenerator ilgen = methbuild.GetILGenerator(10); }); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod(attributes.ToString(), attributes); + Assert.NotNull(method.GetILGenerator()); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator1.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator1.cs deleted file mode 100644 index c54f5b5..0000000 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderGetILGenerator1.cs +++ /dev/null @@ -1,349 +0,0 @@ -// 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class MethodBuilderGetILGenerator1 - { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - - [Fact] - public void TestWithPublicAttribute() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithAssemblyAttribute() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Assembly); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithCheckAccessOnOverride() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.CheckAccessOnOverride); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithFamAndAssem() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.FamANDAssem); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithFamily() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Family); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithFamOrAssem() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.FamORAssem); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithFinal() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Final); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithHasSecurity() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.HasSecurity); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithHideBySig() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.HideBySig); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithMemberAccessMask() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.MemberAccessMask); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithNewSlot() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.NewSlot); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithPrivate() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Private); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithPrivateScope() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.PrivateScope); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithRequireSecObject() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.RequireSecObject); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithReuseSlot() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.ReuseSlot); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithRTSpecialName() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.RTSpecialName); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithSpecialName() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.SpecialName); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithStatic() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Static); - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithUnManagedExport() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.UnmanagedExport); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithVirtual() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Virtual); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithVTableLayoutMask() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.VtableLayoutMask); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestWithCombinations() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Assembly | - MethodAttributes.CheckAccessOnOverride | - MethodAttributes.FamORAssem | - MethodAttributes.Final | - MethodAttributes.HasSecurity | - MethodAttributes.HideBySig | - MethodAttributes.MemberAccessMask | - MethodAttributes.NewSlot | - MethodAttributes.Private | - MethodAttributes.PrivateScope | - MethodAttributes.RequireSecObject | - MethodAttributes.RTSpecialName | - MethodAttributes.SpecialName | - MethodAttributes.Static | - MethodAttributes.UnmanagedExport); - - Assert.NotNull(builder.GetILGenerator()); - } - - [Fact] - public void TestThrowsExceptionForPInvoke() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.PinvokeImpl); - - Assert.Throws(() => { ILGenerator generator = builder.GetILGenerator(); }); - } - - [Fact] - public void TestThrowsExceptionOnInvalidMethodAttributeSet() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Abstract | - MethodAttributes.PinvokeImpl); - - Assert.Throws(() => { ILGenerator generator = builder.GetILGenerator(); }); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderInitLocals.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderInitLocals.cs index c63015b..63761c0 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderInitLocals.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderInitLocals.cs @@ -2,76 +2,30 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderInitLocals { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - - [Fact] - public void TestWithDefault() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - Assert.True(builder.InitLocals); - } - [Fact] - public void TestWithTrue() + public void InitLocals_Get_ReturnsTrue() { - string methodName = null; - bool desiredValue = true; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - builder.InitLocals = desiredValue; - - Assert.Equal(desiredValue, builder.InitLocals); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + Assert.True(method.InitLocals); } - [Fact] - public void TestWithFalse() + [Theory] + [InlineData(true)] + [InlineData(false)] + public void InitLocals_Set(bool newInitLocals) { - string methodName = null; - bool desiredValue = false; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - builder.InitLocals = desiredValue; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - Assert.Equal(desiredValue, builder.InitLocals); + method.InitLocals = newInitLocals; + Assert.Equal(newInitLocals, method.InitLocals); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderIsGenericMethod.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderIsGenericMethod.cs index 204520c..6b811a8 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderIsGenericMethod.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderIsGenericMethod.cs @@ -2,109 +2,41 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; +using System.Collections.Generic; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderIsGenericMethod { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() + public static IEnumerable IsGenericMethod_TestData() { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } + MethodBuilder method1 = type.DefineMethod("TestMethod1", MethodAttributes.Public); - [Fact] - public void TestWithNonGenericMethod() - { - string methodName = null; + MethodBuilder method2 = type.DefineMethod("TestMethod2", MethodAttributes.Public); + method2.DefineGenericParameters("T"); - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); + MethodBuilder method3 = type.DefineMethod("TestMethod3", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), new Type[] { typeof(int) }); + method3.DefineGenericParameters("T"); + method3.DefineParameter(1, ParameterAttributes.HasDefault, "TestParam"); - Assert.False(builder.IsGenericMethod); - } - - [Fact] - public void TestWithGenericMethod() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); + MethodBuilder method4 = type.DefineMethod("TestMethod4", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), new Type[] { typeof(int) }); + method4.DefineParameter(1, ParameterAttributes.HasDefault, "TestParam"); - Assert.True(builder.IsGenericMethod); + yield return new object[] { method1, false }; // Non-generic + yield return new object[] { method2, true }; // Generic method + yield return new object[] { method3, true }; // Generic parameter + yield return new object[] { method4, false }; // Non-generic parameter } - [Fact] - public void TestWithCombinationOfGenericAndNonGeneric() + [Theory] + [MemberData(nameof(IsGenericMethod_TestData))] + public void IsGenericMethod(MethodBuilder method, bool expected) { - string methodName = null; - string strParamName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - methodName, - TestMethodAttributes, - typeof(void), - paramTypes); - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - strParamName); - - Assert.True(builder.IsGenericMethod); - } - - [Fact] - public void TestWithNonGenericParameters() - { - string methodName = null; - string strParamName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - methodName, - TestMethodAttributes, - typeof(void), - paramTypes); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - strParamName); - - Assert.False(builder.IsGenericMethod); + Assert.Equal(expected, method.IsGenericMethod); + Assert.Equal(expected, method.IsGenericMethodDefinition); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderIsGenericMethodDefinition.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderIsGenericMethodDefinition.cs deleted file mode 100644 index 586c463..0000000 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderIsGenericMethodDefinition.cs +++ /dev/null @@ -1,110 +0,0 @@ -// 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class MethodBuilderIsGenericMethodDefinition - { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - - [Fact] - public void TestWithNonGenericMethod() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - Assert.False(builder.IsGenericMethodDefinition); - } - - [Fact] - public void TestWithGenericMethod() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); - - Assert.True(builder.IsGenericMethodDefinition); - } - - [Fact] - public void TestWithGenericAndNonGenericParameters() - { - string methodName = null; - string strParamName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - methodName, - TestMethodAttributes, - typeof(void), - paramTypes); - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] parameters = builder.DefineGenericParameters(typeParamNames); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - strParamName); - - Assert.True(builder.IsGenericMethodDefinition); - } - - [Fact] - public void TestWithNonGenericParameters() - { - string methodName = null; - string strParamName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - methodName, - TestMethodAttributes, - typeof(void), - paramTypes); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - strParamName); - - Assert.False(builder.IsGenericMethodDefinition); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderMakeGenericMethod.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderMakeGenericMethod.cs index f999327..20facea 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderMakeGenericMethod.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderMakeGenericMethod.cs @@ -2,11 +2,7 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; +using System.Collections.Generic; using System.Linq; using Xunit; @@ -14,82 +10,34 @@ namespace System.Reflection.Emit.Tests { public class MethodBuilderMakeGenericMethod { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Static; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() + public static IEnumerable MakeGenericMethod_TestData() { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); + yield return new object[] { new string[] { "T" }, new Type[] { typeof(string) }, typeof(void) }; + yield return new object[] { new string[] { "T", "U" }, new Type[] { typeof(string), typeof(int) }, typeof(int) }; } - [Fact] - public void TestWithSingleGenericParameter() + [Theory] + [MemberData(nameof(MakeGenericMethod_TestData))] + public void MakeGenericMethod(string[] names, Type[] typeArguments, Type returnType) { - string methodName = null; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type returnType = typeof(void); - Type[] desiredType = new Type[] { - typeof(string) - }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - Type[] typeParameters = - builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray(); + MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static); + Type[] typeParameters = builder.DefineGenericParameters(names).Select(a => a.AsType()).ToArray(); builder.SetParameters(typeParameters); builder.SetReturnType(returnType); - MethodInfo methodInfo = builder.MakeGenericMethod(desiredType); - VerifyMethodInfo(methodInfo, builder, returnType); - } - [Fact] - public void TestWithMultipleGenericParameters() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type returnType = typeof(int); - Type[] desiredType = new Type[] { - typeof(string), - typeof(int) - }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - Type[] typeParameters = - builder.DefineGenericParameters(new string[] { "T", "U" }).Select(a => a.AsType()).ToArray(); - builder.SetParameters(typeParameters); - builder.SetReturnType(returnType); - MethodInfo methodInfo = builder.MakeGenericMethod(desiredType); + MethodInfo methodInfo = builder.MakeGenericMethod(typeArguments); VerifyMethodInfo(methodInfo, builder, returnType); } [Fact] public void TestNotThrowsExceptionOnNull() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); Type returnType = typeof(void); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - Type[] typeParameters = - builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static); + Type[] typeParameters = builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray(); builder.SetParameters(typeParameters); builder.SetReturnType(returnType); @@ -99,33 +47,25 @@ namespace System.Reflection.Emit.Tests [Fact] public void TestNotThrowsExceptionOnEmptyArray1() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); Type returnType = typeof(void); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - Type[] typeParameters = - builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static); + Type[] typeParameters = builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray(); builder.SetParameters(typeParameters); builder.SetReturnType(returnType); - MethodInfo methodInfo = builder.MakeGenericMethod(new Type[] { }); + MethodInfo methodInfo = builder.MakeGenericMethod(new Type[0]); } [Fact] public void TestNotThrowsExceptionOnEmptyArray2() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); Type returnType = typeof(void); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - Type[] typeParameters = - builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder builder = type.DefineMethod("Test", MethodAttributes.Public | MethodAttributes.Static); + Type[] typeParameters = builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray(); builder.SetParameters(typeParameters); builder.SetReturnType(returnType); @@ -135,13 +75,13 @@ namespace System.Reflection.Emit.Tests private void VerifyMethodInfo(MethodInfo methodInfo, MethodBuilder builder, Type returnType) { if (methodInfo == null) + { Assert.Null(builder); - - if (methodInfo != null) + } + else { - Assert.NotNull(builder); Assert.Equal(methodInfo.Name, builder.Name); - Assert.True(methodInfo.ReturnType.Equals(returnType)); + Assert.Equal(methodInfo.ReturnType, returnType); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderName.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderName.cs index 5a1d167..e0f9ab7 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderName.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderName.cs @@ -2,70 +2,22 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderName { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() + [Theory] + [InlineData("TestMethod")] + public void Name(string name) { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - - [Fact] - public void TestNameWithNoParameter() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - string actualName = builder.Name; - - Assert.True(actualName.Equals(methodName)); - } - - [Fact] - public void TestNameWithMultipleParameter() - { - string methodName = null; - string strParamName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - Type[] paramTypes = new Type[] { typeof(int) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - methodName, - TestMethodAttributes, - typeof(void), - paramTypes); - string actualName = builder.Name; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method1 = type.DefineMethod(name, MethodAttributes.Public); + Assert.Equal(name, method1.Name); - Assert.True(actualName.Equals(methodName)); + MethodBuilder method2 = type.DefineMethod(name, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), new Type[] { typeof(int) }); + Assert.Equal(name, method2.Name); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderReturnParameter.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderReturnParameter.cs index 0857e49..eed0b31 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderReturnParameter.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderReturnParameter.cs @@ -2,172 +2,59 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderReturnParameter { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const string TestDynamicMethodName = "TestDynamicMethod"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - private readonly byte[] _defaultILArray = new byte[] { - 0x00, - 0x72, - 0x01, - 0x00, - 0x00, - 0x70, - 0x28, - 0x04, - 0x00, - 0x00, - 0x0a, - 0x00, - 0x2a - }; - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - - [Fact] - public void TestWithNotSetReturnType() - { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - TestDynamicMethodName, - MethodAttributes.Public); - - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - Type ret = typeBuilder.CreateTypeInfo().AsType(); - ParameterInfo actualReturnParameter = builder.ReturnParameter; - - Assert.True(actualReturnParameter.ParameterType.Equals(typeof(void))); - } - [Fact] - public void TestWithVoidReturnType() + public void ReturnParameter_NoSetReturnParameter_ReturnsVoid() { - string strParamName = null; - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - Type[] paramTypes = new Type[] { typeof(int) }; - Type expectedReturnType = typeof(void); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - TestDynamicMethodName, - MethodAttributes.Public, - expectedReturnType, - paramTypes); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - strParamName); + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - Type ret = typeBuilder.CreateTypeInfo().AsType(); - ParameterInfo actualReturnParameter = builder.ReturnParameter; - - Assert.True(actualReturnParameter.ParameterType.Equals(expectedReturnType)); - } - - [Fact] - public void TestWithValueTypeReturnType() - { - string strParamName = null; - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - Type[] paramTypes = new Type[] { typeof(int) }; - Type expectedReturnType = typeof(int); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - TestDynamicMethodName, - MethodAttributes.Public, - expectedReturnType, - paramTypes); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - strParamName); - - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - Type ret = typeBuilder.CreateTypeInfo().AsType(); - ParameterInfo actualReturnParameter = builder.ReturnParameter; - - Assert.True(actualReturnParameter.ParameterType.Equals(expectedReturnType)); + type.CreateTypeInfo().AsType(); + Assert.Equal(typeof(void), method.ReturnParameter.ParameterType); } - [Fact] - public void TestWithReferenceTypeReturnType() + [Theory] + [InlineData(typeof(void))] + [InlineData(typeof(int))] + [InlineData(typeof(string))] + public void ReturnParameter(Type returnType) { - string strParamName = null; - strParamName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - Type[] paramTypes = new Type[] { typeof(int) }; - Type expectedReturnType = typeof(string); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - TestDynamicMethodName, - MethodAttributes.Public, - expectedReturnType, - paramTypes); - ParameterBuilder paramBuilder = builder.DefineParameter( - 1, - ParameterAttributes.HasDefault, - strParamName); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod( "TestMethod", MethodAttributes.Public, returnType, new Type[] { typeof(int) }); + ParameterBuilder paramBuilder = method.DefineParameter(1, ParameterAttributes.HasDefault, "TestParam"); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - Type ret = typeBuilder.CreateTypeInfo().AsType(); - ParameterInfo actualReturnParameter = builder.ReturnParameter; + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - Assert.True(actualReturnParameter.ParameterType.Equals(expectedReturnType)); + type.CreateTypeInfo().AsType(); + Assert.Equal(returnType, method.ReturnParameter.ParameterType); } [Fact] - public void TestThrowsExceptionForDeclaringTypeNotCreated() + public void ReturnParameter_TypeNotCreated_ThrowsInvalidOperationException() { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - TestDynamicMethodName, - MethodAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - Assert.Throws(() => { ParameterInfo actualReturnParameter = builder.ReturnParameter; }); + Assert.Throws(() => method.ReturnParameter); } [Fact] - public void TestThrowsExceptionWithNoMethodBodyDefined() + public void ReturnParameter_NoBody_ThrowsInvalidOperationException() { - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod( - TestDynamicMethodName, - MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - Type ret = typeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { ParameterInfo actualReturnParameter = builder.ReturnParameter; }); + type.CreateTypeInfo().AsType(); + Assert.Throws(() => method.ReturnParameter); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute.cs index 5ccc406..b097084 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute.cs @@ -2,83 +2,136 @@ // 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; - -using TestLibrary; -using System.Reflection; -using System.Reflection.Emit; -using System.Runtime.CompilerServices; -using Xunit; using System.Linq; +using Xunit; namespace System.Reflection.Emit.Tests { - public class MBMyAttribute3 : Attribute + public class MethodBuilderCustomIntAttribute : Attribute { - public MBMyAttribute3(int mc) + public MethodBuilderCustomIntAttribute(int intField) { - m_ctorType2 = mc; + _intField = intField; } public string Field12345; - public int m_ctorType2; + public int _intField; } - public class MethodBuilderSetCustomAttribute + public class MethodBuilderCustomAttribute : Attribute { - [Fact] - public void TestSetCustomAttribute() + public string TestString + { + get { return TestStringField; } + set { TestStringField = value; } + } + + public int TestInt32 + { + get { return TestInt; } + set { TestInt = value; } + } + + public string GetOnlyString + { + get { return GetString; } + } + + public int GetOnlyInt32 + { + get { return GetInt; } + } + + public string TestStringField; + public int TestInt; + public string GetString; + public int GetInt; + + public MethodBuilderCustomAttribute() { } + + public MethodBuilderCustomAttribute(string getOnlyString, int getOnlyInt32) + { + GetString = getOnlyString; + GetInt = getOnlyInt32; + } + + public MethodBuilderCustomAttribute(string testString, int testInt32, string getOnlyString, int getOnlyInt32) { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; + TestStringField = testString; + TestInt = testInt32; + GetString = getOnlyString; + GetInt = getOnlyInt32; + } + } - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); + public class MethodBuilderSetCustomAttribute + { + private const int ByteArraySize = 128; + private static readonly RandomDataGenerator s_randomDataGenerator = new RandomDataGenerator(); - TypeBuilder tpbuild = modbuild.DefineType("C1"); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); + [Fact] + public void SetCustomAttribute_CustomAttributeBuilder() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static); - ILGenerator ilgen = methbuild.GetILGenerator(); + ILGenerator ilgen = method.GetILGenerator(); ilgen.Emit(OpCodes.Ldc_I4, 100); ilgen.Emit(OpCodes.Ret); - Type attrType = typeof(MBMyAttribute3); - ConstructorInfo ci = attrType.GetConstructors()[0]; - FieldInfo fi = attrType.GetField("Field12345"); + Type attributeType = typeof(MethodBuilderCustomIntAttribute); + ConstructorInfo constructor = attributeType.GetConstructors()[0]; + FieldInfo field = attributeType.GetField("Field12345"); + CustomAttributeBuilder attribute = new CustomAttributeBuilder(constructor, new object[] { 4 }, new FieldInfo[] { field }, new object[] { "hello" }); + method.SetCustomAttribute(attribute); + Type createdType = type.CreateTypeInfo().AsType(); + + object[] attributes = createdType.GetMethod("TestMethod").GetCustomAttributes(false).ToArray(); + Assert.Equal(1, attributes.Length); - CustomAttributeBuilder cab = new CustomAttributeBuilder(ci, - new object[] { 4 }, - new FieldInfo[] { fi }, - new object[] { "hello" }); - methbuild.SetCustomAttribute(cab); - Type tp = tpbuild.CreateTypeInfo().AsType(); - MethodInfo md = tp.GetMethod("method1"); + MethodBuilderCustomIntAttribute obj = (MethodBuilderCustomIntAttribute)attributes[0]; + Assert.Equal("hello", obj.Field12345); + Assert.Equal(4, obj._intField); + } - // VERIFY - object[] attribs = md.GetCustomAttributes(false).Select(a => (object)a).ToArray(); + [Fact] + public void SetCustomAttribute_CustomAttributeBuilder_NullBuilder_ThrowsArgumentNullException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); - Assert.Equal(1, attribs.Length); - MBMyAttribute3 obj = (MBMyAttribute3)attribs[0]; + Assert.Throws("customBuilder", () => method.SetCustomAttribute(null)); + } - Assert.Equal("hello", obj.Field12345); - Assert.Equal(4, obj.m_ctorType2); + [Fact] + public void SetCustomAttribute_ConstructorInfo_ByteArray() + { + byte[] binaryAttribute = new byte[ByteArraySize]; + s_randomDataGenerator.GetBytes(binaryAttribute); + + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + + method.SetCustomAttribute(typeof(MethodBuilderCustomAttribute).GetConstructor(new Type[0]), binaryAttribute); } [Fact] - public void TestThrowsExceptionOnNull() + public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructor_ThrowsArgumentNullException() { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + + Assert.Throws("con", () => method.SetCustomAttribute(null, new byte[0])); + } - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); + [Fact] + public void SetCustomAttribute_ConstructorInfo_ByteArray_NullBinaryAttribute_ThrowsArgumentNullException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public); - Assert.Throws(() => { methbuild.SetCustomAttribute(null); }); + Assert.Throws("binaryAttribute", () => builder.SetCustomAttribute(typeof(MethodBuilderCustomAttribute).GetConstructor(new Type[0]), null)); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute1.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute1.cs deleted file mode 100644 index df66607..0000000 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute1.cs +++ /dev/null @@ -1,151 +0,0 @@ -// 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; -using System.Security; -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class CustomAttributeBuilderTest : Attribute - { - public string TestString - { - get - { - return TestStringField; - } - set - { - TestStringField = value; - } - } - - public int TestInt32 - { - get - { - return TestInt; - } - set - { - TestInt = value; - } - } - - public string GetOnlyString - { - get - { - return GetString; - } - } - - public int GetOnlyInt32 - { - get - { - return GetInt; - } - } - - public string TestStringField; - public int TestInt; - public string GetString; - public int GetInt; - - public CustomAttributeBuilderTest() - { - } - - public CustomAttributeBuilderTest(string getOnlyString, int getOnlyInt32) - { - GetString = getOnlyString; - GetInt = getOnlyInt32; - } - - public CustomAttributeBuilderTest(string testString, int testInt32, string getOnlyString, int getOnlyInt32) - { - TestStringField = testString; - TestInt = testInt32; - GetString = getOnlyString; - GetInt = getOnlyInt32; - } - } - - public class MethodBuilderSetCustomAttribute1 - { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Static; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private const int ByteArraySize = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - - [Fact] - public void TestSetCustomAttribute() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] ctorParams = new Type[] { }; - byte[] binaryAttribute = new byte[ByteArraySize]; - - _generator.GetBytes(binaryAttribute); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetCustomAttribute(typeof(CustomAttributeBuilderTest).GetConstructor(ctorParams), binaryAttribute); - } - - [Fact] - public void TestThrowsExceptionOnNullConstructorInfo() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - byte[] binaryAttribute = new byte[ByteArraySize]; - - _generator.GetBytes(binaryAttribute); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - Assert.Throws(() => { builder.SetCustomAttribute(null, binaryAttribute); }); - } - - [Fact] - public void TestThrowsExceptionOnNullByteArray() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] ctorParams = new Type[] { }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - Assert.Throws(() => { builder.SetCustomAttribute(typeof(CustomAttributeBuilderTest).GetConstructor(ctorParams), null); }); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetImplementationFlags.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetImplementationFlags.cs index e51d17c..cd1bc56 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetImplementationFlags.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetImplementationFlags.cs @@ -2,277 +2,43 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderSetImplementationFlags { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Static; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private const int ByteArraySize = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - - [Fact] - public void TestWithCodeTypeMask() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.CodeTypeMask; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithForwardRef() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.ForwardRef; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithIL() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.IL; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithInternalCall() + [Theory] + [InlineData(MethodImplAttributes.CodeTypeMask)] + [InlineData(MethodImplAttributes.ForwardRef)] + [InlineData(MethodImplAttributes.IL)] + [InlineData(MethodImplAttributes.InternalCall)] + [InlineData(MethodImplAttributes.Managed)] + [InlineData(MethodImplAttributes.ManagedMask)] + [InlineData(MethodImplAttributes.Native)] + [InlineData(MethodImplAttributes.NoInlining)] + [InlineData(MethodImplAttributes.OPTIL)] + [InlineData(MethodImplAttributes.PreserveSig)] + [InlineData(MethodImplAttributes.Runtime)] + [InlineData(MethodImplAttributes.Synchronized)] + [InlineData(MethodImplAttributes.Unmanaged)] + public void SetImplementationFlags(MethodImplAttributes implementationFlags) { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.InternalCall; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); + method.SetImplementationFlags(implementationFlags); + Assert.Equal(implementationFlags, method.MethodImplementationFlags); } [Fact] - public void TestWithManaged() + public void SetImplementationFlags_TypeCreated_ThrowsInvalidOperationException() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.Managed; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithManagedMask() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.ManagedMask; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - - [Fact] - public void TestWithNative() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.Native; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithNoInlining() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.NoInlining; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithOPTIL() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.OPTIL; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithPreserveSig() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.PreserveSig; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithRuntime() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.Runtime; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithSynchronized() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.Synchronized; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestWithUnmanaged() - { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.Unmanaged; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetImplementationFlags(desiredFlags); - - MethodImplAttributes actualFlags = builder.MethodImplementationFlags; - Assert.Equal(desiredFlags, actualFlags); - } - - [Fact] - public void TestThrowsExceptionOnTypeCreated() - { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - MethodImplAttributes desiredFlags = MethodImplAttributes.Unmanaged; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - - Type type = typeBuilder.CreateTypeInfo().AsType(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - Assert.Throws(() => { builder.SetImplementationFlags(desiredFlags); }); + type.CreateTypeInfo().AsType(); + Assert.Throws(() => method.SetImplementationFlags(MethodImplAttributes.Unmanaged)); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetParameters.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetParameters.cs index e5e5a75..2623bc7 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetParameters.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetParameters.cs @@ -2,294 +2,144 @@ // 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; +using System.Collections.Generic; using System.Linq; -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderSetParameters { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly byte[] _defaultILArray = new byte[] + public static IEnumerable SetParameters_TestData() { - 0x00, - 0x72, - 0x01, - 0x00, - 0x00, - 0x70, - 0x28, - 0x04, - 0x00, - 0x00, - 0x0a, - 0x00, - 0x2a - }; - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); + yield return new object[] { new Type[0], new string[] { "T" } }; + yield return new object[] { new Type[0], new string[] { "T", "U" } }; + yield return new object[] { new Type[] { typeof(int) }, new string[] { "T" } }; } - [Fact] - public void TestWithSingleGenericParameter() + [Theory] + [MemberData(nameof(SetParameters_TestData))] + public void SetParameters(Type[] parameterTypes, string[] typeParamNames) { - string methodName = null; - - methodName = "PosTest1"; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - Type[] typeParameters = - builder.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray(); - - builder.SetParameters(typeParameters); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - - Type type = typeBuilder.CreateTypeInfo().AsType(); - - MethodInfo method = type.GetMethod(builder.Name); - ParameterInfo[] parameters = method.GetParameters(); - VerificationHelper(parameters, typeParameters, typeParamNames); - } - - [Fact] - public void TestWithMultipleGenericParameters() - { - string methodName = null; - - methodName = "PosTest2"; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T", "U" }; - Type[] typeParameters = - builder.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray(); - - builder.SetParameters(typeParameters); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - - Type type = typeBuilder.CreateTypeInfo().AsType(); - - MethodInfo method = type.GetMethod(builder.Name); - ParameterInfo[] parameters = method.GetParameters(); - VerificationHelper(parameters, typeParameters, typeParamNames); - } - - [Fact] - public void TestWithSingleGenericAndNonGenericParameter() - { - string methodName = null; - - methodName = "PosTest3"; - Type[] parameterTypes = new Type[] { typeof(int) }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes, - typeof(void), - parameterTypes); - - string[] typeParamNames = { "T" }; - Type[] typeParameters = - builder.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray(); - - builder.SetParameters(typeParameters); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - - Type type = typeBuilder.CreateTypeInfo().AsType(); - - MethodInfo method = type.GetMethod(builder.Name); - ParameterInfo[] parameters = method.GetParameters(); - VerificationHelper(parameters, typeParameters, typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, typeof(void), parameterTypes); + + Type[] typeParameters = method.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray(); + method.SetParameters(typeParameters); + + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); + + Type createdType = type.CreateTypeInfo().AsType(); + MethodInfo createdMethod = createdType.GetMethod(method.Name); + VerifyParameters(createdMethod.GetParameters(), typeParameters, typeParamNames); } [Fact] - public void TestAfterTypeCreated() + public void SetParameters_WorksAfterTypeCreated() { - string methodName = null; - methodName = "PosTest4"; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - string[] typeParamNames = { "T" }; - Type[] typeParameters = - builder.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + Type[] typeParameters = method.DefineGenericParameters("T").Select(a => a.AsType()).ToArray(); - builder.SetParameters(typeParameters); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); + method.SetParameters(typeParameters); + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - Type type = typeBuilder.CreateTypeInfo().AsType(); - - builder.SetParameters(typeParameters); + type.CreateTypeInfo().AsType(); + method.SetParameters(typeParameters); } [Fact] - public void TestOnNull() + public void SetParameters_NullParameterTypes() { - string methodName = null; - methodName = "NegTest1"; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - builder.SetParameters(null); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); + method.SetParameters(null); + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - Type type = typeBuilder.CreateTypeInfo().AsType(); - - MethodInfo method = type.GetMethod(builder.Name); - ParameterInfo[] parameters = method.GetParameters(); - VerificationHelper(parameters, new Type[] { }, null); + Type createdType = type.CreateTypeInfo().AsType(); + MethodInfo createdMethod = createdType.GetMethod(method.Name); + ParameterInfo[] parameters = createdMethod.GetParameters(); + VerifyParameters(parameters, new Type[0], null); } [Fact] - public void TestOnEmptyArray() + public void SetParameters_EmptyParameterTypes() { - string methodName = null; - methodName = "NegTest2"; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + builder.SetParameters(new Type[0]); + ILGenerator ilGenerator = builder.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - builder.SetParameters(new Type[] { }); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); + Type createdType = type.CreateTypeInfo().AsType(); - Type type = typeBuilder.CreateTypeInfo().AsType(); - - MethodInfo method = type.GetMethod(builder.Name); - ParameterInfo[] parameters = method.GetParameters(); - VerificationHelper(parameters, new Type[] { }, null); + MethodInfo method = createdType.GetMethod(builder.Name); + ParameterInfo[] parameters = builder.GetParameters(); + VerifyParameters(parameters, new Type[0], null); } [Fact] - public void TestWithNoParameters() + public void SetParameters_NoParameterTypes() { - string methodName = null; - methodName = "NegTest3"; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public); builder.SetParameters(); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); + ILGenerator ilGenerator = builder.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - Type type = typeBuilder.CreateTypeInfo().AsType(); + Type createdType = type.CreateTypeInfo().AsType(); - MethodInfo method = type.GetMethod(builder.Name); + MethodInfo method = createdType.GetMethod(builder.Name); ParameterInfo[] parameters = method.GetParameters(); - VerificationHelper(parameters, new Type[] { }, null); + VerifyParameters(parameters, new Type[0], null); } [Fact] - public void TestForNonGenericMethods() + public void SetParameters_NullParameter_ThrowsArgumentNullException() { - string methodName = null; - methodName = "NegTest4"; - Type[] parameterTypes = new Type[] { typeof(int) }; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public, typeof(void), new Type[] { typeof(int) }); + Type[] typeParameters = builder.DefineGenericParameters("T").Select(a => a.AsType()).ToArray(); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes, - typeof(void), - parameterTypes); - string[] typeParamNames = { "T" }; - Type[] typeParameters = - builder.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray(); - - builder.SetParameters(typeParameters); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - - Type type = typeBuilder.CreateTypeInfo().AsType(); - - MethodInfo method = type.GetMethod(builder.Name); - ParameterInfo[] parameters = method.GetParameters(); - VerificationHelper(parameters, typeParameters, typeParamNames); - } - - [Fact] - public void TestWithParametersToNull() - { - string methodName = null; - - methodName = "NegTest6"; - Type[] parameterTypes = new Type[] { typeof(int) }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes, - typeof(void), - parameterTypes); - string[] typeParamNames = { "T" }; - Type[] typeParameters = - builder.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray(); - - Type[] desiredParameters = new Type[typeParameters.Length + 1]; + Type[] parameterTypes = new Type[typeParameters.Length + 1]; for (int i = 0; i < typeParameters.Length; ++i) { - desiredParameters[i] = typeParameters[i]; + parameterTypes[i] = typeParameters[i]; } - desiredParameters[typeParameters.Length] = null; + parameterTypes[typeParameters.Length] = null; - builder.SetParameters(desiredParameters); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); + builder.SetParameters(parameterTypes); + ILGenerator ilGenerator = builder.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); - Assert.Throws(() => { Type type = typeBuilder.CreateTypeInfo().AsType(); }); + Assert.Throws("argument", () => type.CreateTypeInfo().AsType()); } - private void VerificationHelper(ParameterInfo[] parameters, Type[] parameterTypes, string[] parameterName) + private void VerifyParameters(ParameterInfo[] parameters, Type[] parameterTypes, string[] parameterName) { if (parameterTypes == null) + { Assert.Null(parameters); - if (parameterTypes != null) + } + else { Assert.NotNull(parameters); - Assert.Equal(parameterTypes.Length, parameters.Length); for (int i = 0; i < parameters.Length; ++i) { ParameterInfo parameter = parameters[i]; - if (null != parameter.Name) + if (parameter.Name != null) { Assert.Equal(parameterName[i], parameter.Name); } else { - Assert.True(parameter.ParameterType.Name.Equals(parameterName[i])); + Assert.Equal(parameterName[i], parameter.ParameterType.Name); } Assert.Equal(i, parameter.Position); diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetReturnType.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetReturnType.cs index 029c616..26e5e07 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetReturnType.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetReturnType.cs @@ -2,238 +2,160 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderSetReturnType { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - [Fact] - public void TestWithSingleGenericParameter() + public void SetReturnType_SingleGenericParameter() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - Type desiredReturnType = typeParameters[0].AsType(); - - builder.SetReturnType(desiredReturnType); - - typeBuilder.CreateTypeInfo().AsType(); - - VerifyReturnType(builder, desiredReturnType); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); + Type returnType = typeParameters[0].AsType(); + method.SetReturnType(returnType); + + type.CreateTypeInfo().AsType(); + VerifyReturnType(method, returnType); } [Fact] - public void TestWithMultipleGenericParameters() + public void SetReturnType_MultipleGenericParameters() { - var methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - Type desiredReturnType = typeParameters[1].AsType(); + string[] typeParamNames = new string[] { "T", "U" }; + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters(typeParamNames); - builder.SetReturnType(desiredReturnType); + Type returnType = typeParameters[1].AsType(); + method.SetReturnType(returnType); - typeBuilder.CreateTypeInfo().AsType(); - - VerifyReturnType(builder, desiredReturnType); + type.CreateTypeInfo().AsType(); + VerifyReturnType(method, returnType); } [Fact] - public void TestWithGenericAndNonGenericParameters() + public void SetReturnType_GenericAndNonGenericParameters() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - Type desiredReturnType = typeof(void); + string[] typeParamNames = new string[] { "T", "U" }; + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters(typeParamNames); - builder.SetReturnType(desiredReturnType); + Type returnType = typeof(void); + method.SetReturnType(returnType); - typeBuilder.CreateTypeInfo().AsType(); - - VerifyReturnType(builder, desiredReturnType); + type.CreateTypeInfo().AsType(); + VerifyReturnType(method, returnType); } [Fact] - public void TestNotSetReturnTypeOnNonGenericMethod() + public void SetReturnType_NonGenericMethod() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] parameterTypes = new Type[] { typeof(int) }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes, - typeof(int), - parameterTypes); - - Type desiredReturnType = typeof(void); - - builder.SetReturnType(desiredReturnType); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(int), new Type[] { typeof(int) }); - typeBuilder.CreateTypeInfo().AsType(); + Type returnType = typeof(void); + method.SetReturnType(returnType); - VerifyReturnType(builder, desiredReturnType); + type.CreateTypeInfo().AsType(); + VerifyReturnType(method, returnType); } [Fact] - public void TestOverwriteGenericReturnTypeWithNonGenericType() + public void SetReturnType_OverwriteGenericReturnTypeWithNonGenericType() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", + MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + string[] typeParamNames = new string[] { "T", "U" }; + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters(typeParamNames); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - Type desiredReturnType = typeof(void); + Type returnType = typeof(void); + method.SetReturnType(typeParameters[0].AsType()); + method.SetReturnType(returnType); - builder.SetReturnType(typeParameters[0].AsType()); - builder.SetReturnType(desiredReturnType); - - typeBuilder.CreateTypeInfo().AsType(); - - VerifyReturnType(builder, desiredReturnType); + type.CreateTypeInfo().AsType(); + VerifyReturnType(method, returnType); } [Fact] - public void TestOverwriteGenericReturnTypeWithGenericType() + public void SetReturnType_OverwriteGenericReturnTypeWithGenericType() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - Type desiredReturnType = typeParameters[1].AsType(); + string[] typeParamNames = new string[] { "T", "U" }; + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters(typeParamNames); - builder.SetReturnType(typeParameters[0].AsType()); - builder.SetReturnType(desiredReturnType); + Type returnType = typeParameters[1].AsType(); + method.SetReturnType(typeParameters[0].AsType()); + method.SetReturnType(returnType); - typeBuilder.CreateTypeInfo().AsType(); - - VerifyReturnType(builder, desiredReturnType); + type.CreateTypeInfo().AsType(); + VerifyReturnType(method, returnType); } [Fact] - public void TestOverwriteNonGenericReturnTypeWithGenericType() + public void SetReturnType_OverwriteNonGenericReturnTypeWithGenericType() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] parameterTypes = new Type[] { typeof(int) }; - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes, - typeof(int), - parameterTypes); - - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - Type desiredReturnType = typeParameters[0].AsType(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(int), new Type[] { typeof(int) }); - builder.SetReturnType(desiredReturnType); + string[] typeParamNames = new string[] { "T", "U" }; + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters(typeParamNames); - typeBuilder.CreateTypeInfo().AsType(); + Type returnType = typeParameters[0].AsType(); + method.SetReturnType(returnType); - VerifyReturnType(builder, desiredReturnType); + type.CreateTypeInfo().AsType(); + VerifyReturnType(method, returnType); } [Fact] - public void TestAfterTypeCreated() + public void SetReturnType_TypeCreated_Works() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - Type[] parameterTypes = new Type[] { typeof(int) }; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(int), new Type[] { typeof(int) }); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes, - typeof(int), - parameterTypes); + string[] typeParamNames = new string[] { "T", "U" }; + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters(typeParamNames); - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - Type desiredReturnType = typeParameters[0].AsType(); - - typeBuilder.CreateTypeInfo().AsType(); - - builder.SetReturnType(desiredReturnType); + Type returnType = typeParameters[0].AsType(); + type.CreateTypeInfo().AsType(); + method.SetReturnType(returnType); } [Fact] - public void TestNotThrowsOnNull() + public void SetReturnType_NullReturnType_ReturnsVoid() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - builder.SetReturnType(null); + method.SetReturnType(null); + type.CreateTypeInfo().AsType(); + VerifyReturnType(method, null); } - private void VerifyReturnType(MethodBuilder builder, Type desiredReturnType) + private void VerifyReturnType(MethodBuilder method, Type expected) { - MethodInfo methodInfo = builder.GetBaseDefinition(); - Type actualReturnType = methodInfo.ReturnType; - - Assert.Equal(desiredReturnType.Name, actualReturnType.Name); - Assert.True(actualReturnType.Equals(desiredReturnType)); + MethodInfo methodInfo = method.GetBaseDefinition(); + Type returnType = methodInfo.ReturnType; + if (expected == null) + { + Assert.Null(returnType); + } + else + { + Assert.Equal(expected.Name, returnType.Name); + Assert.True(returnType.Equals(expected)); + } } } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetSignature.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetSignature.cs index 4905311..e2e2ed5 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetSignature.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetSignature.cs @@ -2,218 +2,115 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; -using System.Runtime.CompilerServices; using Xunit; namespace System.Reflection.Emit.Tests { public class MethodBuilderSetSignature { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTestTypeBuilder() - { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, TestDynamicModuleName); - - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); - } - [Fact] - public void TestGenericMethodWithSingleGenericParameterSet() + public void SetSignature_GenericMethod_SingleGenericParameter() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; - - builder.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); + GenericTypeParameterBuilder returnType = typeParameters[0]; + + method.SetSignature(returnType.AsType(), null, null, null, null, null); + VerifyMethodSignature(type, method, returnType.AsType()); } [Fact] - public void TestGenericMethodWithMultipleGenericParameters() + public void SetSignature_GenericMethod_MultipleGenericParameters() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T", "U"); GenericTypeParameterBuilder desiredReturnType = typeParameters[1]; - builder.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestGenericMethodWithReturnTypeRequiredModifiersSet() + public void SetSignature_GenericMethod_ReturnType_RequiredCustomModifiers() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; - builder.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestGenericMethodWithReturnTypeRequiredAndOptionalModifiersSet() + public void SetSignature_GenericMethod_ReturnType_RequiredModifier_OptionalCustomModifiers() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", + MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - null, - null, - null); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestGenericMethodWithReturnTypeOptionalModifiersSet() + public void SetSignature_GenericMethod_ReturnType_OptionalCustomModifiers() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - null, - null, - null); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestGenericMethodWithParameterTypesSet() + public void SetSignature_GenericMethod_ParameterTypes() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(int) }; - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - desiredParamType, - null, - null); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, null, null); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestGenericMethodWithMultipleParametersSet() + public void SetSignature_GenericMethod_MultipleParameters() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T", "U" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T", "U"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(int), typeParameters[1].AsType() }; - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - desiredParamType, - null, - null); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, null, null); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestGenericMethodWithParameterTypeRequiredModifierSet() + public void SetSignature_GenericMethod_ParameterType_RequiredCustomModifiers() { - string methodName = null; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; + string[] typeParamNames = new string[] { "T" }; GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + method.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(int) }; Type[][] parameterTypeRequiredCustomModifiers = new Type[desiredParamType.Length][]; @@ -222,31 +119,18 @@ namespace System.Reflection.Emit.Tests parameterTypeRequiredCustomModifiers[i] = null; } - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - desiredParamType, - parameterTypeRequiredCustomModifiers, - null); + method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, null); - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestGenericMethodWithParameterTypeRequiredAndOptionModifierSet() + public void SetSignature_GenericMethod_ParameterType_RequiredCustomModifer_OptionalCustomModifiers() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(int) }; Type[][] parameterTypeRequiredCustomModifiers = new Type[desiredParamType.Length][]; @@ -257,31 +141,18 @@ namespace System.Reflection.Emit.Tests parameterTypeOptionalCustomModifiers[i] = null; } - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - desiredParamType, - parameterTypeRequiredCustomModifiers, - parameterTypeOptionalCustomModifiers); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestGenericMethodWithParameterTypeOptionalModifierSet() + public void SetSignature_GenericMethod_ParameterType_OptionalCustomModifiers() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - string[] typeParamNames = { "T" }; GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(int) }; Type[][] parameterTypeOptionalCustomModifiers = new Type[desiredParamType.Length][]; @@ -290,34 +161,21 @@ namespace System.Reflection.Emit.Tests parameterTypeOptionalCustomModifiers[i] = null; } - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - desiredParamType, - null, - parameterTypeOptionalCustomModifiers); - - VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); + method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, null, parameterTypeOptionalCustomModifiers); + VerifyMethodSignature(type, method, desiredReturnType.AsType()); } [Fact] - public void TestWithNonGenericMethod() + public void SetSignature_NonGenericMethod() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); Type[] parameterTypes = new Type[] { typeof(string), typeof(object) }; - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes, - typeof(void), - parameterTypes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), parameterTypes); string[] parameterNames = new string[parameterTypes.Length]; for (int i = 0; i < parameterNames.Length; ++i) { parameterNames[i] = "P" + i.ToString(); - builder.DefineParameter(i + 1, ParameterAttributes.In, parameterNames[i]); + method.DefineParameter(i + 1, ParameterAttributes.In, parameterNames[i]); } Type desiredReturnType = typeof(void); @@ -330,74 +188,40 @@ namespace System.Reflection.Emit.Tests parameterTypeOptionalCustomModifiers[i] = null; } - builder.SetSignature( - desiredReturnType, - null, - null, - desiredParamType, - parameterTypeRequiredCustomModifiers, - parameterTypeOptionalCustomModifiers); + method.SetSignature(desiredReturnType, null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); } [Fact] - public void TestWithNullOnParameters() + public void SetSignature_AllParametersNull() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); - builder.SetSignature(null, null, null, null, null, null); - - VerifyMethodSignature(typeBuilder, builder, null); + method.SetSignature(null, null, null, null, null, null); + VerifyMethodSignature(type, method, null); } [Fact] - public void TestWithNullOnReturnTypeWithModifiersSetToWrongTypes() + public void SetSignature_NullReturnType_CustomModifiersSetToWrongTypes() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; - builder.SetSignature( - null, - null, - null, - null, - null, - null); - - VerifyMethodSignature(typeBuilder, builder, null); + method.SetSignature(null, null, null, null, null, null); + VerifyMethodSignature(type, method, null); } [Fact] - public void TestWithNullOnReturnTypeWithModifiersSetCorrectly() + public void SetSignature_NullOnReturnType_CustomModifiersSetCorrectly() { - string methodName = null; - int arraySize = 0; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - arraySize = _generator.GetByte(); - - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + int arraySize = 10; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = null; Type[][] parameterTypeRequiredCustomModifiers = new Type[arraySize][]; @@ -408,28 +232,16 @@ namespace System.Reflection.Emit.Tests parameterTypeOptionalCustomModifiers[i] = null; } - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - desiredParamType, - parameterTypeRequiredCustomModifiers, - parameterTypeOptionalCustomModifiers); + method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); } [Fact] - public void TestWithNullOnReturnTypeModifiers() + public void SetSignature_NullReturnType_RequiredCustomModifiers_OptionalCustomModifiers() { - string methodName = null; - - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - TestMethodAttributes); - - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(void) }; Type[][] parameterTypeRequiredCustomModifiers = new Type[desiredParamType.Length][]; @@ -440,24 +252,20 @@ namespace System.Reflection.Emit.Tests parameterTypeOptionalCustomModifiers[i] = null; } - builder.SetSignature( - desiredReturnType.AsType(), - null, - null, - desiredParamType, - parameterTypeRequiredCustomModifiers, - parameterTypeOptionalCustomModifiers); + method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); } - private void VerifyMethodSignature(TypeBuilder typeBuilder, MethodBuilder builder, Type desiredReturnType) + private void VerifyMethodSignature(TypeBuilder type, MethodBuilder method, Type desiredReturnType) { - Type ret = typeBuilder.CreateTypeInfo().AsType(); - MethodInfo methodInfo = builder.GetBaseDefinition(); + Type ret = type.CreateTypeInfo().AsType(); + MethodInfo methodInfo = method.GetBaseDefinition(); Type actualReturnType = methodInfo.ReturnType; if (desiredReturnType == null) + { Assert.Null(actualReturnType); - if (desiredReturnType != null) + } + else { Assert.NotNull(actualReturnType); Assert.Equal(desiredReturnType.Name, actualReturnType.Name); diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderToString.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderToString.cs index 0bf4880..65495e8 100644 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderToString.cs +++ b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderToString.cs @@ -2,112 +2,83 @@ // 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; - -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; using Xunit; namespace System.Reflection.Emit.Tests { - public class MethodBuilderToString1 + public class MethodBuilderToString { - private const string TestDynamicAssemblyName = "TestDynamicAssembly"; - private const string TestDynamicModuleName = "TestDynamicModule"; - private const string TestDynamicTypeName = "TestDynamicType"; - private const AssemblyBuilderAccess TestAssemblyBuilderAccess = AssemblyBuilderAccess.Run; - private const TypeAttributes TestTypeAttributes = TypeAttributes.Abstract; - private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual; - private const int MinStringLength = 1; - private const int MaxStringLength = 128; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - private readonly byte[] _defaultILArray = new byte[] { - 0x00, - 0x72, - 0x01, - 0x00, - 0x00, - 0x70, - 0x28, - 0x04, - 0x00, - 0x00, - 0x0a, - 0x00, - 0x2a - }; - - private TypeBuilder GetTestTypeBuilder() + [Fact] + public void ToString_AllFieldsSet() { - AssemblyName assemblyName = new AssemblyName(TestDynamicAssemblyName); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - assemblyName, TestAssemblyBuilderAccess); - ModuleBuilder moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(assemblyBuilder, "Module1"); - return moduleBuilder.DefineType(TestDynamicTypeName, TestTypeAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); + + ILGenerator ilGenerator = method.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ret); + + GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); + GenericTypeParameterBuilder returnType = typeParameters[0]; + + method.SetSignature(returnType.AsType(), null, null, null, null, null); + Assert.Contains(ExpectedToStrin(method), method.ToString()); } [Fact] - public void TestWithAllFieldsSet() + public void ToString_NameAndAttributeSet() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - ILGenerator ilgen = builder.GetILGenerator(); - ilgen.Emit(OpCodes.Ret); - string[] typeParamNames = { "T" }; - GenericTypeParameterBuilder[] typeParameters = - builder.DefineGenericParameters(typeParamNames); - GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; + Assert.Contains(ExpectedToStrin(method), method.ToString()); + } - builder.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); - string actualString = builder.ToString(); - string desiredString = GetDesiredMethodToString(builder); + [Fact] + public void ToString_NameAttributeAndSignatureSetSet() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); - Assert.NotNull(actualString); - Assert.Contains(desiredString, actualString); + method.SetSignature(typeof(void), null, null, null, null, null); + Assert.Contains(ExpectedToStrin(method), method.ToString()); } [Fact] - public void TestWithNameAndAttributeSet() + public void ToString_NonGenericMethod() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, MethodAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]); - string actualString = builder.ToString(); - string desiredString = GetDesiredMethodToString(builder); - - Assert.NotNull(actualString); - Assert.Contains(desiredString, actualString); + string toString = method.ToString(); + Assert.True(toString.LastIndexOf("Name: method1") != -1 && + toString.LastIndexOf("Attributes: 22") != -1 && + toString.LastIndexOf("Method Signature: Length: 3") != -1 && + toString.LastIndexOf("Arguments: 0") != -1 && + toString.LastIndexOf("Signature:") != -1 && + toString.LastIndexOf("0 0 8 0") != -1); } [Fact] - public void TestWithNameAttributeAndSignatureSet() + public void ToString_GenericMethod() { - string methodName = null; - methodName = _generator.GetString(false, false, true, MinStringLength, MaxStringLength); - TypeBuilder typeBuilder = GetTestTypeBuilder(); - MethodBuilder builder = typeBuilder.DefineMethod(methodName, - MethodAttributes.Public); - - builder.SetSignature(typeof(void), null, null, null, null, null); - string actualString = builder.ToString(); - string desiredString = GetDesiredMethodToString(builder); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); + MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public, typeof(int), new Type[0]); + method.DefineGenericParameters("T", "U", "V"); + method.MakeGenericMethod(typeof(string), typeof(int), typeof(object)); - Assert.NotNull(actualString); - Assert.Contains(desiredString, actualString); + string toString = method.ToString(); + Assert.True(toString.LastIndexOf("Name: method1") != -1 && + toString.LastIndexOf("Attributes: 6") != -1 && + toString.LastIndexOf("Method Signature: Length: 4") != -1 && + toString.LastIndexOf("Arguments: 0") != -1 && + toString.LastIndexOf("Signature:") != -1 && + toString.LastIndexOf("48 3 0 8 0") != -1); } - private string GetDesiredMethodToString(MethodBuilder builder) + private static string ExpectedToStrin(MethodBuilder method) { - // Avoid use string.Format or StringBuilder - return "Name: " + builder.Name + " " + Environment.NewLine + - "Attributes: " + ((int)builder.Attributes).ToString() + Environment.NewLine + + return "Name: " + method.Name + " " + Environment.NewLine + + "Attributes: " + ((int)method.Attributes).ToString() + Environment.NewLine + "Method Signature: "; } } diff --git a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderToString2.cs b/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderToString2.cs deleted file mode 100644 index a424eb2..0000000 --- a/src/libraries/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderToString2.cs +++ /dev/null @@ -1,66 +0,0 @@ -// 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; - -using TestLibrary; -using System.Reflection; -using System.Reflection.Emit; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class MethodBuilderToString - { - [Fact] - public void TestOnNonGenericMethod() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static, - typeof(int), new Type[] { }); - - string str = methbuild.ToString(); - - Assert.True(str.LastIndexOf("Name: method1") != -1 && - str.LastIndexOf("Attributes: 22") != -1 && - str.LastIndexOf("Method Signature: Length: 3") != -1 && - str.LastIndexOf("Arguments: 0") != -1 && - str.LastIndexOf("Signature:") != -1 && - str.LastIndexOf("0 0 8 0") != -1); - } - - [Fact] - public void TestOnGenericMethod() - { - string name = "Assembly1"; - AssemblyName asmname = new AssemblyName(); - asmname.Name = name; - - AssemblyBuilder asmbuild = AssemblyBuilder.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run); - ModuleBuilder modbuild = TestLibrary.Utilities.GetModuleBuilder(asmbuild, "Module1"); - - TypeBuilder tpbuild = modbuild.DefineType("C1", TypeAttributes.Public); - MethodBuilder methbuild = tpbuild.DefineMethod("method1", MethodAttributes.Public, - typeof(int), new Type[] { }); - methbuild.DefineGenericParameters(new string[] { "T", "U", "V" }); - methbuild.MakeGenericMethod(typeof(string), typeof(int), typeof(object)); - - string str = methbuild.ToString(); - - Assert.True(str.LastIndexOf("Name: method1") != -1 && - str.LastIndexOf("Attributes: 6") != -1 && - str.LastIndexOf("Method Signature: Length: 4") != -1 && - str.LastIndexOf("Arguments: 0") != -1 && - str.LastIndexOf("Signature:") != -1 && - str.LastIndexOf("48 3 0 8 0") != -1); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj b/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj index b3a7720..05c43e7 100644 --- a/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj +++ b/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj @@ -73,30 +73,23 @@ - - - - - - - -- 2.7.4