| 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 Xunit; |
| 6 |
|
| 7 |
namespace System.Reflection.Emit.Tests |
| 8 |
{ |
| 9 |
public class ModuleBuilderDefineInitializedData |
| 10 |
{ |
| 11 |
[Theory] |
| 12 |
[InlineData(FieldAttributes.Static | FieldAttributes.Public)] |
| 13 |
[InlineData(FieldAttributes.Static | FieldAttributes.Private)] |
| 14 |
[InlineData( FieldAttributes.Private)] |
| 15 |
public void TestWithStaticAndPublic(FieldAttributes attributes) |
| 16 |
{ |
| 17 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 18 |
FieldBuilder field = module.DefineInitializedData("MyField", new byte[] { 01, 00, 01 }, attributes); |
| 19 |
Assert.True(field.IsStatic); |
| 20 |
Assert.Equal((attributes & FieldAttributes.Public) != 0 , field.IsPublic); |
| 21 |
Assert.Equal((attributes & FieldAttributes.Private) != 0, field.IsPrivate); |
| 22 |
Assert.Equal(field.Name, "MyField"); |
| 23 |
} |
| 24 |
|
| 25 |
[Fact] |
| 26 |
public void DefineInitializedData_EmptyName_ThrowsArgumentException() |
| 27 |
{ |
| 28 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 29 |
AssertExtensions.Throws<ArgumentException>("name", () => module.DefineInitializedData("", new byte[] { 1, 0, 1 }, FieldAttributes.Private)); |
| 30 |
} |
| 31 |
|
| 32 |
[Theory] |
| 33 |
[InlineData(0)] |
| 34 |
[InlineData(0x3f0000)] |
| 35 |
public void DefineInitializedData_InvalidDataLength_ThrowsArgumentException(int length) |
| 36 |
{ |
| 37 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 38 |
AssertExtensions.Throws<ArgumentException>(null, () => module.DefineInitializedData("MyField", new byte[length], FieldAttributes.Public)); |
| 39 |
} |
| 40 |
|
| 41 |
[Fact] |
| 42 |
public void DefineInitializedData_NullName_ThrowsArgumentNullException() |
| 43 |
{ |
| 44 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 45 |
AssertExtensions.Throws<ArgumentNullException>("name", () => module.DefineInitializedData(null, new byte[] { 1, 0, 1 }, FieldAttributes.Public)); |
| 46 |
} |
| 47 |
|
| 48 |
[Fact] |
| 49 |
public void DefineInitializedData_NullData_ThrowsArgumentNullException() |
| 50 |
{ |
| 51 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 52 |
AssertExtensions.Throws<ArgumentNullException>("data", () => module.DefineInitializedData("MyField", null, FieldAttributes.Public)); |
| 53 |
} |
| 54 |
|
| 55 |
[Fact] |
| 56 |
public void DefineInitializedData_CreateGlobalFunctionsCalled_ThrowsInvalidOperationException() |
| 57 |
{ |
| 58 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 59 |
FieldBuilder field = module.DefineInitializedData("MyField", new byte[] { 1, 0, 1 }, FieldAttributes.Public); |
| 60 |
module.CreateGlobalFunctions(); |
| 61 |
Assert.Throws<InvalidOperationException>(() => module.DefineInitializedData("MyField2", new byte[] { 1, 0, 1 }, FieldAttributes.Public)); |
| 62 |
} |
| 63 |
} |
| 64 |
} |