| 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 FieldBuilderSetOffset |
| 10 |
{ |
| 11 |
[Theory] |
| 12 |
[InlineData(0)] |
| 13 |
[InlineData(1)] |
| 14 |
[InlineData(8)] |
| 15 |
public void SetOffset(int offset) |
| 16 |
{ |
| 17 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 18 |
|
| 19 |
FieldBuilder field = type.DefineField("Field_PosTest2", typeof(int), FieldAttributes.Public); |
| 20 |
field.SetOffset(offset); |
| 21 |
} |
| 22 |
|
| 23 |
[Fact] |
| 24 |
public void SetOffset_DifferentForTwoProperties() |
| 25 |
{ |
| 26 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 27 |
|
| 28 |
FieldBuilder field1 = type.DefineField("TestField1", typeof(int), FieldAttributes.Public); |
| 29 |
field1.SetOffset(0); |
| 30 |
|
| 31 |
FieldBuilder field2 = type.DefineField("TestField2", typeof(int), FieldAttributes.Public); |
| 32 |
field2.SetOffset(4); |
| 33 |
|
| 34 |
FieldBuilder field3 = type.DefineField("TestField3", typeof(int), FieldAttributes.Public); |
| 35 |
field3.SetOffset(4); |
| 36 |
} |
| 37 |
|
| 38 |
[Fact] |
| 39 |
public void SetOffset_SameForTwoProperties() |
| 40 |
{ |
| 41 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 42 |
|
| 43 |
FieldBuilder field1 = type.DefineField("TestField1", typeof(int), FieldAttributes.Public); |
| 44 |
field1.SetOffset(0); |
| 45 |
|
| 46 |
FieldBuilder field2 = type.DefineField("TestField2", typeof(int), FieldAttributes.Public); |
| 47 |
field2.SetOffset(0); |
| 48 |
} |
| 49 |
|
| 50 |
[Fact] |
| 51 |
public void SetOffset_TypeCreated_ThrowsInvalidOperationException() |
| 52 |
{ |
| 53 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 54 |
FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Public); |
| 55 |
type.CreateTypeInfo().AsType(); |
| 56 |
|
| 57 |
Assert.Throws<InvalidOperationException>(() => field.SetOffset(0)); |
| 58 |
} |
| 59 |
|
| 60 |
[Fact] |
| 61 |
public void SetOffset_NegativeOffset_ThrowsArgumentException() |
| 62 |
{ |
| 63 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 64 |
|
| 65 |
FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Public); |
| 66 |
AssertExtensions.Throws<ArgumentException>(null, () => field.SetOffset(-1)); |
| 67 |
} |
| 68 |
} |
| 69 |
} |