| 1 |
// Licensed to the .NET Foundation under one or more agreements. |
| 2 |
// The .NET Foundation licenses this file to you under the MIT license. |
| 3 |
// See the LICENSE file in the project root for more information. |
| 4 |
|
| 5 |
using System.Collections.Generic; |
| 6 |
using Xunit; |
| 7 |
|
| 8 |
namespace System.Reflection.Emit.Tests |
| 9 |
{ |
| 10 |
public struct MBTestStruct |
| 11 |
{ |
| 12 |
public int IntVal; |
| 13 |
public string StrVal; |
| 14 |
} |
| 15 |
|
| 16 |
public class ModuleBuilderGetArrayMethod |
| 17 |
{ |
| 18 |
public static IEnumerable<object[]> CallingConventions_TestData() |
| 19 |
{ |
| 20 |
yield return new object[] { CallingConventions.Any }; |
| 21 |
yield return new object[] { CallingConventions.ExplicitThis }; |
| 22 |
yield return new object[] { CallingConventions.HasThis }; |
| 23 |
yield return new object[] { CallingConventions.Standard }; |
| 24 |
yield return new object[] { CallingConventions.VarArgs }; |
| 25 |
} |
| 26 |
|
| 27 |
[Theory] |
| 28 |
[MemberData(nameof(CallingConventions_TestData))] |
| 29 |
public void GetArrayMethod_ValidArrayValues_VoidReturnType(CallingConventions callingConvention) |
| 30 |
{ |
| 31 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 32 |
VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[]), callingConvention.ToString(), callingConvention, typeof(void), new Type[0]); |
| 33 |
} |
| 34 |
|
| 35 |
[Theory] |
| 36 |
[MemberData(nameof(CallingConventions_TestData))] |
| 37 |
public void GetArrayMethod_ValidArrayValues_ValueReturnType(CallingConventions callingConvention) |
| 38 |
{ |
| 39 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 40 |
VerifyGetArrayMethod(module, typeof(int[]), callingConvention.ToString(), callingConvention, typeof(int), new Type[0]); |
| 41 |
} |
| 42 |
|
| 43 |
[Theory] |
| 44 |
[MemberData(nameof(CallingConventions_TestData))] |
| 45 |
public void GetArrayMethod_ValidArrayValues_ReferenceReturnType(CallingConventions callingConvention) |
| 46 |
{ |
| 47 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 48 |
VerifyGetArrayMethod(module, typeof(object[]), callingConvention.ToString(), callingConvention, typeof(object), new Type[0]); |
| 49 |
} |
| 50 |
|
| 51 |
[Theory] |
| 52 |
[MemberData(nameof(CallingConventions_TestData))] |
| 53 |
public void GetArrayMethod_ValidArrayValues_ValueParameterType(CallingConventions callingConvention) |
| 54 |
{ |
| 55 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 56 |
VerifyGetArrayMethod(module, typeof(object[]), callingConvention.ToString() + "1", callingConvention, typeof(int), new Type[] { typeof(int) }); |
| 57 |
|
| 58 |
VerifyGetArrayMethod(module, typeof(object[]), callingConvention.ToString() + "2", callingConvention, typeof(int), new Type[] { typeof(int), typeof(MBTestStruct) }); |
| 59 |
} |
| 60 |
|
| 61 |
[Theory] |
| 62 |
[MemberData(nameof(CallingConventions_TestData))] |
| 63 |
public void GetArrayMethod_ValidArrayValues_ReferenceParameterType(CallingConventions callingConvention) |
| 64 |
{ |
| 65 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 66 |
VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[]), callingConvention.ToString() + "1", callingConvention, typeof(int), new Type[] { typeof(object) }); |
| 67 |
|
| 68 |
VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[]), callingConvention.ToString() + "2", callingConvention, typeof(int), new Type[] { typeof(object), typeof(string), typeof(ModuleBuilderGetArrayMethod) }); |
| 69 |
} |
| 70 |
|
| 71 |
[Theory] |
| 72 |
[MemberData(nameof(CallingConventions_TestData))] |
| 73 |
public void GetArrayMethod_JaggedArray(CallingConventions callingConvention) |
| 74 |
{ |
| 75 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 76 |
VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[][]), callingConvention.ToString() + "1", callingConvention, typeof(int), new Type[] { typeof(object) }); |
| 77 |
|
| 78 |
VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[][]), callingConvention.ToString() + "2", callingConvention, typeof(int), new Type[] { typeof(object), typeof(int), typeof(ModuleBuilderGetArrayMethod) }); |
| 79 |
} |
| 80 |
|
| 81 |
[Theory] |
| 82 |
[MemberData(nameof(CallingConventions_TestData))] |
| 83 |
public void GetArrayMethod_MultiDimensionalArray(CallingConventions callingConvention) |
| 84 |
{ |
| 85 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 86 |
VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[,]), callingConvention.ToString() + "1", callingConvention, typeof(int), new Type[] { typeof(object) }); |
| 87 |
|
| 88 |
VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[,]), callingConvention.ToString() + "2", callingConvention, typeof(int), new Type[] { typeof(object), typeof(int), typeof(ModuleBuilderGetArrayMethod) }); |
| 89 |
} |
| 90 |
|
| 91 |
[Theory] |
| 92 |
[MemberData(nameof(CallingConventions_TestData))] |
| 93 |
public void GetArrayMethod_NullParameters(CallingConventions callingConvention) |
| 94 |
{ |
| 95 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 96 |
VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[]), callingConvention.ToString(), callingConvention, typeof(void), null); |
| 97 |
} |
| 98 |
|
| 99 |
[Theory] |
| 100 |
[InlineData(typeof(ModuleBuilderGetArrayMethod))] |
| 101 |
[InlineData(typeof(int))] |
| 102 |
[InlineData(typeof(Array))] |
| 103 |
[InlineData(typeof(void))] |
| 104 |
public void GetArrayMethod_ArrayClassNotArray_ThrowsArgumentException(Type arrayClass) |
| 105 |
{ |
| 106 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 107 |
AssertExtensions.Throws<ArgumentException>(null, () => module.GetArrayMethod(arrayClass, "TestMethod", CallingConventions.Standard, typeof(void), new Type[0])); |
| 108 |
} |
| 109 |
|
| 110 |
[Fact] |
| 111 |
public void GetArrayMethod_InvalidArgument_ThrowsArgumentException() |
| 112 |
{ |
| 113 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 114 |
|
| 115 |
AssertExtensions.Throws<ArgumentNullException>("arrayClass", () => module.GetArrayMethod(null, "TestMethod", CallingConventions.Standard, typeof(void), new Type[0])); |
| 116 |
|
| 117 |
AssertExtensions.Throws<ArgumentNullException>("methodName", () => module.GetArrayMethod(typeof(string[]), null, CallingConventions.Standard, typeof(void), new Type[0])); |
| 118 |
AssertExtensions.Throws<ArgumentException>("methodName", () => module.GetArrayMethod(typeof(string[]), "", CallingConventions.Standard, typeof(void), new Type[0])); |
| 119 |
|
| 120 |
AssertExtensions.Throws<ArgumentNullException>("argument", () => module.GetArrayMethod(typeof(string[]), "TestMethod", CallingConventions.Standard, typeof(void), new Type[] { null })); |
| 121 |
} |
| 122 |
|
| 123 |
private void VerifyGetArrayMethod(ModuleBuilder module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) |
| 124 |
{ |
| 125 |
MethodInfo method = module.GetArrayMethod(arrayClass, methodName, callingConvention, returnType, parameterTypes); |
| 126 |
|
| 127 |
Assert.Equal(arrayClass, method.DeclaringType); |
| 128 |
Assert.Equal(methodName, method.Name); |
| 129 |
Assert.Equal(callingConvention, method.CallingConvention); |
| 130 |
Assert.Equal(returnType, method.ReturnType); |
| 131 |
} |
| 132 |
} |
| 133 |
} |