| 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 System.Runtime.InteropServices; |
| 7 |
using Xunit; |
| 8 |
|
| 9 |
namespace System.Reflection.Emit.Tests |
| 10 |
{ |
| 11 |
public class ModuleBuilderDefineEnum |
| 12 |
{ |
| 13 |
public static IEnumerable<object[]> DefineEnum_TestData() |
| 14 |
{ |
| 15 |
yield return new object[] { "TestEnum", TypeAttributes.Public, typeof(byte) }; |
| 16 |
yield return new object[] { "testenum", TypeAttributes.NotPublic, typeof(sbyte) }; |
| 17 |
yield return new object[] { "enum", TypeAttributes.Public, typeof(short) }; |
| 18 |
yield return new object[] { "\uD800\uDC00", TypeAttributes.Public, typeof(ushort) }; |
| 19 |
yield return new object[] { "a\0b\0c", TypeAttributes.Public, typeof(int) }; |
| 20 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(uint) }; |
| 21 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(long) }; |
| 22 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(char) }; |
| 23 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(bool) }; |
| 24 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(ulong) }; |
| 25 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(float) }; |
| 26 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(double) }; |
| 27 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(IntPtr) }; |
| 28 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(UIntPtr) }; |
| 29 |
yield return new object[] { "Name", TypeAttributes.Public, typeof(Int32Enum) }; |
| 30 |
} |
| 31 |
|
| 32 |
[Theory] |
| 33 |
[MemberData(nameof(DefineEnum_TestData))] |
| 34 |
public void DefineEnum(string name, TypeAttributes visibility, Type underlyingType) |
| 35 |
{ |
| 36 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 37 |
EnumBuilder enumBuilder = module.DefineEnum(name, visibility, underlyingType); |
| 38 |
Assert.True(enumBuilder.IsEnum); |
| 39 |
|
| 40 |
Assert.Equal(module.Assembly, enumBuilder.Assembly); |
| 41 |
Assert.Equal(module, enumBuilder.Module); |
| 42 |
|
| 43 |
Assert.Equal(name, enumBuilder.Name); |
| 44 |
Assert.Equal(Helpers.GetFullName(name), enumBuilder.FullName); |
| 45 |
Assert.Equal(enumBuilder.FullName + ", " + module.Assembly.FullName, enumBuilder.AssemblyQualifiedName); |
| 46 |
|
| 47 |
Assert.Equal(typeof(Enum), enumBuilder.BaseType); |
| 48 |
Assert.Null(enumBuilder.DeclaringType); |
| 49 |
|
| 50 |
Assert.Equal(visibility | TypeAttributes.Sealed, enumBuilder.Attributes); |
| 51 |
|
| 52 |
Assert.Equal("value__", enumBuilder.UnderlyingField.Name); |
| 53 |
Assert.Equal(underlyingType, enumBuilder.UnderlyingField.FieldType); |
| 54 |
Assert.Equal(FieldAttributes.Public | FieldAttributes.SpecialName, enumBuilder.UnderlyingField.Attributes); |
| 55 |
|
| 56 |
// Verify we can create the Enum properly |
| 57 |
TypeInfo createdEnum = enumBuilder.CreateTypeInfo(); |
| 58 |
Assert.True(createdEnum.IsEnum); |
| 59 |
|
| 60 |
Assert.Equal(module.Assembly.ToString(), createdEnum.Assembly.ToString()); |
| 61 |
Assert.Equal(module.ToString(), createdEnum.Module.ToString()); |
| 62 |
|
| 63 |
Assert.Equal(Helpers.GetFullName(name), createdEnum.Name); |
| 64 |
Assert.Equal(Helpers.GetFullName(name), enumBuilder.FullName); |
| 65 |
Assert.Equal(enumBuilder.FullName + ", " + module.Assembly.FullName, enumBuilder.AssemblyQualifiedName); |
| 66 |
|
| 67 |
Assert.Equal(typeof(Enum), createdEnum.BaseType); |
| 68 |
Assert.Null(createdEnum.DeclaringType); |
| 69 |
|
| 70 |
Assert.Equal(visibility | TypeAttributes.Sealed, createdEnum.Attributes); |
| 71 |
Type expectedUnderlyingType = underlyingType.GetTypeInfo().IsEnum ? Enum.GetUnderlyingType(underlyingType) : underlyingType; |
| 72 |
Assert.Equal(expectedUnderlyingType, Enum.GetUnderlyingType(createdEnum.AsType())); |
| 73 |
|
| 74 |
// There should be a field called "value__" created |
| 75 |
FieldInfo createdUnderlyingField = createdEnum.AsType().GetField("value__", Helpers.AllFlags); |
| 76 |
Assert.Equal(FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName, createdUnderlyingField.Attributes); |
| 77 |
} |
| 78 |
|
| 79 |
[Fact] |
| 80 |
public void DefineEnum_DynamicUnderlyingType_Works() |
| 81 |
{ |
| 82 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 83 |
EnumBuilder underlyingEnumTypeBuilder = module.DefineEnum("Enum1", TypeAttributes.Public, typeof(int)); |
| 84 |
Type underlyingEnumType = underlyingEnumTypeBuilder.CreateTypeInfo().AsType(); |
| 85 |
|
| 86 |
EnumBuilder enumBuilder = module.DefineEnum("Enum2", TypeAttributes.Public, underlyingEnumType); |
| 87 |
Type enumType = enumBuilder.CreateTypeInfo().AsType(); |
| 88 |
|
| 89 |
Assert.Equal(typeof(int), Enum.GetUnderlyingType(enumType)); |
| 90 |
} |
| 91 |
|
| 92 |
[Fact] |
| 93 |
public void DefineEnum_EnumWithSameNameExists_ThrowsArgumentException() |
| 94 |
{ |
| 95 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 96 |
module.DefineEnum("Name", TypeAttributes.Public, typeof(int)); |
| 97 |
AssertExtensions.Throws<ArgumentException>(null, () => module.DefineEnum("Name", TypeAttributes.Public, typeof(int))); |
| 98 |
} |
| 99 |
|
| 100 |
[Fact] |
| 101 |
public void DefineEnum_NullName_ThrowsArgumentNullException() |
| 102 |
{ |
| 103 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 104 |
AssertExtensions.Throws<ArgumentNullException>("fullname", () => module.DefineEnum(null, TypeAttributes.Public, typeof(object))); |
| 105 |
} |
| 106 |
|
| 107 |
[Theory] |
| 108 |
[InlineData("")] |
| 109 |
[InlineData("\0")] |
| 110 |
[InlineData("\0abc")] |
| 111 |
public void DefineEnum_EmptyName_ThrowsArgumentNullException(string name) |
| 112 |
{ |
| 113 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 114 |
AssertExtensions.Throws<ArgumentException>("fullname", () => module.DefineEnum(name, TypeAttributes.Public, typeof(object))); |
| 115 |
} |
| 116 |
|
| 117 |
[Theory] |
| 118 |
[InlineData((TypeAttributes)(-1), "name")] |
| 119 |
[InlineData(TypeAttributes.Abstract, "name")] |
| 120 |
[InlineData(TypeAttributes.AutoClass, "name")] |
| 121 |
[InlineData(TypeAttributes.BeforeFieldInit, "name")] |
| 122 |
[InlineData(TypeAttributes.ClassSemanticsMask, "name")] |
| 123 |
[InlineData(TypeAttributes.CustomFormatClass, "name")] |
| 124 |
[InlineData(TypeAttributes.CustomFormatMask, "name")] |
| 125 |
[InlineData(TypeAttributes.ExplicitLayout, "name")] |
| 126 |
[InlineData(TypeAttributes.HasSecurity, "name")] |
| 127 |
[InlineData(TypeAttributes.Import, "name")] |
| 128 |
[InlineData(TypeAttributes.ClassSemanticsMask, "name")] |
| 129 |
[InlineData(TypeAttributes.LayoutMask, "name")] |
| 130 |
[InlineData(TypeAttributes.RTSpecialName, "name")] |
| 131 |
[InlineData(TypeAttributes.Sealed, "name")] |
| 132 |
[InlineData(TypeAttributes.SequentialLayout, "name")] |
| 133 |
[InlineData(TypeAttributes.Serializable, "name")] |
| 134 |
[InlineData(TypeAttributes.SpecialName, "name")] |
| 135 |
[InlineData(TypeAttributes.CustomFormatClass, "name")] |
| 136 |
[InlineData(TypeAttributes.UnicodeClass, "name")] |
| 137 |
[InlineData(TypeAttributes.NestedAssembly, null)] |
| 138 |
[InlineData(TypeAttributes.NestedFamANDAssem, null)] |
| 139 |
[InlineData(TypeAttributes.NestedFamily, null)] |
| 140 |
[InlineData(TypeAttributes.NestedFamORAssem, null)] |
| 141 |
[InlineData(TypeAttributes.NestedPrivate, null)] |
| 142 |
[InlineData(TypeAttributes.NestedPublic, null)] |
| 143 |
public void DefineEnum_IncorrectVisibilityAttributes_ThrowsArgumentException(TypeAttributes visibility, string paramName) |
| 144 |
{ |
| 145 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 146 |
AssertExtensions.Throws<ArgumentException>(paramName, () => module.DefineEnum("Enum", visibility, typeof(int))); |
| 147 |
} |
| 148 |
|
| 149 |
[Fact] |
| 150 |
public void DefineEnum_NullUnderlyingType_ThrowsArgumentNullException() |
| 151 |
{ |
| 152 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 153 |
AssertExtensions.Throws<ArgumentNullException>("type", () => module.DefineEnum("Name", TypeAttributes.Public, null)); |
| 154 |
} |
| 155 |
|
| 156 |
[Fact] |
| 157 |
public void DefineEnum_VoidUnderlyingType_ThrowsArgumentException() |
| 158 |
{ |
| 159 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 160 |
AssertExtensions.Throws<ArgumentException>(null, () => module.DefineEnum("Name", TypeAttributes.Public, typeof(void))); |
| 161 |
} |
| 162 |
|
| 163 |
[Fact] |
| 164 |
public void DefineEnum_ByRefUnderlyingType_ThrowsCOMExceptionOnCreation() |
| 165 |
{ |
| 166 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 167 |
EnumBuilder enumBuilder = module.DefineEnum("Name", TypeAttributes.Public, typeof(int).MakeByRefType()); |
| 168 |
Assert.Throws<COMException>(() => enumBuilder.CreateTypeInfo()); |
| 169 |
} |
| 170 |
|
| 171 |
[Theory] |
| 172 |
[InlineData(typeof(string))] |
| 173 |
[InlineData(typeof(Guid))] |
| 174 |
[InlineData(typeof(int*))] |
| 175 |
[InlineData(typeof(int[]))] |
| 176 |
[InlineData(typeof(List<>))] |
| 177 |
[InlineData(typeof(List<string>))] |
| 178 |
[InlineData(typeof(object))] |
| 179 |
[InlineData(typeof(ValueType))] |
| 180 |
[InlineData(typeof(Enum))] |
| 181 |
[InlineData(typeof(int?))] |
| 182 |
public void DefineEnum_InvalidUnderlyingType_ThrowsTypeLoadExceptionOnCreation(Type underlyingType) |
| 183 |
{ |
| 184 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 185 |
EnumBuilder enumBuilder = module.DefineEnum("Name", TypeAttributes.Public, underlyingType); |
| 186 |
Assert.Equal(underlyingType, enumBuilder.UnderlyingField.FieldType); |
| 187 |
Assert.Throws<TypeLoadException>(() => enumBuilder.CreateTypeInfo().AsType()); |
| 188 |
} |
| 189 |
|
| 190 |
protected enum Int32Enum { } |
| 191 |
} |
| 192 |
} |