Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetConstant.cs

raw
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 class PropertyBuilderTest11
11 {
12 private enum Colors
13 {
14 Red = 0,
15 Green = 1,
16 Blue = 2
17 }
18
19 public static IEnumerable<object[]> SetConstant_TestData()
20 {
21 yield return new object[] { typeof(int), 10 };
22 yield return new object[] { typeof(bool), true };
23 yield return new object[] { typeof(sbyte), (sbyte)10 };
24 yield return new object[] { typeof(short), (short)10 };
25 yield return new object[] { typeof(long), (long)10 };
26
27 yield return new object[] { typeof(byte), (byte)10 };
28 yield return new object[] { typeof(ushort), (ushort)10 };
29 yield return new object[] { typeof(uint), (uint)10 };
30 yield return new object[] { typeof(ulong), (ulong)10 };
31
32 yield return new object[] { typeof(float), (float)10 };
33 yield return new object[] { typeof(double), (double)10 };
34
35 yield return new object[] { typeof(DateTime), DateTime.Now };
36 yield return new object[] { typeof(char), 'a' };
37 yield return new object[] { typeof(string), "a" };
38
39 yield return new object[] { typeof(Colors), Colors.Blue };
40 yield return new object[] { typeof(object), null };
41 yield return new object[] { typeof(object), "a" };
42 }
43
44 [Theory]
45 [MemberData(nameof(SetConstant_TestData))]
46 public void SetConstant(Type returnType, object defaultValue)
47 {
48 MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
49 BindingFlags bindingAttributes = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static;
50 Type[] paramTypes = new Type[0];
51
52 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
53
54 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, returnType, null);
55 property.SetConstant(defaultValue);
56
57 MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, returnType, paramTypes);
58 ILGenerator methodILGenerator = method.GetILGenerator();
59 methodILGenerator.Emit(OpCodes.Ldarg_0);
60 methodILGenerator.Emit(OpCodes.Ret);
61
62 property.SetGetMethod(method);
63
64 Type createdType = type.CreateTypeInfo().AsType();
65 PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingAttributes);
66 Assert.Equal(defaultValue, createdProperty.GetConstantValue());
67 }
68
69 [Fact]
70 public void SetConstant_TypeAlreadyCreated_ThrowsInvalidOperationException()
71 {
72 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
73 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]);
74 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, CallingConventions.HasThis, typeof(int), new Type[] { typeof(int) });
75
76 ILGenerator methodILGenerator = method.GetILGenerator();
77 methodILGenerator.Emit(OpCodes.Ldarg_0);
78 methodILGenerator.Emit(OpCodes.Ldarg_1);
79 methodILGenerator.Emit(OpCodes.Ret);
80
81 type.CreateTypeInfo().AsType();
82 Assert.Throws<InvalidOperationException>(() => property.SetConstant(1));
83 }
84
85 [Fact]
86 public void SetConstant_TypeNotConstant_ThrowsArgumentException()
87 {
88 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
89
90 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(decimal), null);
91 AssertExtensions.Throws<ArgumentException>(null, () => property.SetConstant((decimal)10));
92 }
93 }
94 }