-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
VirtualPropertyInfo.PropertySetter.Tests.cs
207 lines (177 loc) · 7.65 KB
/
VirtualPropertyInfo.PropertySetter.Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using Xunit;
namespace System.Reflection.Context.Tests
{
public class VirtualPropertyInfo_PropertySetter_Tests
{
// Points to a PropertyInfo instance created by reflection. This doesn't work in a reflection-only context.
private readonly MethodInfo _virtualPropertySetter;
private readonly TestObject _testObject = new TestObject("Age");
public VirtualPropertyInfo_PropertySetter_Tests()
{
var customReflectionContext = new VirtualPropertyInfoCustomReflectionContext();
TypeInfo typeInfo = typeof(TestObject).GetTypeInfo();
TypeInfo customTypeInfo = customReflectionContext.MapType(typeInfo);
PropertyInfo virtualProperty = customTypeInfo.DeclaredProperties.ElementAt(2);
_virtualPropertySetter = virtualProperty.GetSetMethod(false);
}
[Fact]
public void ProjectionTest()
{
Assert.Equal(ProjectionConstants.VirtualPropertyInfoSetter, _virtualPropertySetter.GetType().FullName);
}
[Fact]
public void GetCustomAttributes_WithType_Test()
{
object[] attributes = _virtualPropertySetter.GetCustomAttributes(typeof(TestGetterSetterAttribute), true);
Assert.Single(attributes);
Assert.IsType<TestGetterSetterAttribute>(attributes[0]);
}
[Fact]
public void GetCustomAttributes_NoType_Test()
{
object[] attributes = _virtualPropertySetter.GetCustomAttributes(false);
Assert.Single(attributes);
Assert.IsType<TestGetterSetterAttribute>(attributes[0]);
}
[Fact]
public void GetCustomAttributesDataTest()
{
// This will never return any results as virtual properties never have custom attributes
// defined in code as they are instantiated during runtime. But as the method is overridden
// we call it for code coverage.
IList<CustomAttributeData> customAttributesData = _virtualPropertySetter.GetCustomAttributesData();
Assert.Empty(customAttributesData);
}
[Fact]
public void IsDefinedTest()
{
Assert.True(_virtualPropertySetter.IsDefined(typeof(TestGetterSetterAttribute), true));
Assert.True(_virtualPropertySetter.IsDefined(typeof(TestGetterSetterAttribute), false));
Assert.False(_virtualPropertySetter.IsDefined(typeof(DataContractAttribute), true));
Assert.False(_virtualPropertySetter.IsDefined(typeof(DataContractAttribute), false));
}
[Fact]
public void GetParametersTest()
{
ParameterInfo[] virtualParameters = _virtualPropertySetter.GetParameters();
Assert.Single(virtualParameters);
Assert.Equal(ProjectionConstants.VirtualParameter, virtualParameters[0].GetType().FullName);
}
[Fact]
public void Invoke_NullParameter_Throws()
{
Assert.Throws<TargetParameterCountException>(() =>
_virtualPropertySetter.Invoke(null, BindingFlags.GetProperty, null, null, CultureInfo.InvariantCulture));
}
[Fact]
public void Invoke_NotSingleParameter_Throws()
{
Assert.Throws<TargetParameterCountException>(() =>
_virtualPropertySetter.Invoke(_testObject, BindingFlags.GetProperty, null, new object[] { }, CultureInfo.InvariantCulture));
Assert.Throws<TargetParameterCountException>(() =>
_virtualPropertySetter.Invoke(_testObject, BindingFlags.GetProperty, null, new object[] { "a", 1 }, CultureInfo.InvariantCulture));
}
[Fact]
public void Invoke_NullObject_Throws()
{
Assert.Throws<TargetException>(() =>
_virtualPropertySetter.Invoke(null, BindingFlags.GetProperty, null, new object[] { 2 }, CultureInfo.InvariantCulture));
}
[Fact]
public void Invoke_WrongObject_Throws()
{
Assert.Throws<TargetException>(() =>
_virtualPropertySetter.Invoke("text", BindingFlags.GetProperty, null, new object[] { 2 }, CultureInfo.InvariantCulture));
}
[Fact]
public void Invoke_ValidArguments_Success()
{
object returnVal = _virtualPropertySetter.Invoke(_testObject, BindingFlags.GetProperty, null, new object[] { 2 }, CultureInfo.InvariantCulture);
Assert.Null(returnVal);
}
[Fact]
public void CallingConventionTest()
{
Assert.Equal(CallingConventions.HasThis | CallingConventions.Standard, _virtualPropertySetter.CallingConvention);
}
[Fact]
public void ContainsGenericParametersTest()
{
Assert.False(_virtualPropertySetter.ContainsGenericParameters);
}
[Fact]
public void IsGenericMethodTest()
{
Assert.False(_virtualPropertySetter.IsGenericMethod);
}
[Fact]
public void IsGenericMethodDefinitionTest()
{
Assert.False(_virtualPropertySetter.IsGenericMethodDefinition);
}
[Fact]
public void MethodHandleTest()
{
Assert.Throws<NotSupportedException>(() => _virtualPropertySetter.MethodHandle);
}
[Fact]
public void ModuleTest()
{
Module customModule = _virtualPropertySetter.Module;
Assert.Equal(ProjectionConstants.CustomModule, customModule.GetType().FullName);
}
[Fact]
public void ReturnParameterTest()
{
ParameterInfo virtualReturnParameter = _virtualPropertySetter.ReturnParameter;
Assert.Equal(ProjectionConstants.VirtualReturnParameter, virtualReturnParameter.GetType().FullName);
ParameterInfo clone = _virtualPropertySetter.ReturnParameter;
Assert.Equal(virtualReturnParameter.GetHashCode(), clone.GetHashCode());
Assert.True(virtualReturnParameter.Equals(clone));
}
[Fact]
public void ReturnTypeCustomAttributes()
{
ICustomAttributeProvider virtualReturnParameter = _virtualPropertySetter.ReturnTypeCustomAttributes;
Assert.Equal(ProjectionConstants.VirtualReturnParameter, virtualReturnParameter.GetType().FullName);
}
[Fact]
public void GetBaseDefinition()
{
Assert.Equal(_virtualPropertySetter, _virtualPropertySetter.GetBaseDefinition());
}
[Fact]
public void GetGenericArgumentsTest()
{
object[] attributes = _virtualPropertySetter.GetGenericArguments();
Assert.IsType<Type[]>(attributes);
Assert.Empty(attributes);
}
[Fact]
public void GetGenericMethodDefinitionTest()
{
Assert.Throws<InvalidOperationException>(() => _virtualPropertySetter.GetGenericMethodDefinition());
}
[Fact]
public void GetMethodImplementationFlagsTest()
{
Assert.Equal(MethodImplAttributes.IL, _virtualPropertySetter.GetMethodImplementationFlags());
}
[Fact]
public void MakeGenericMethodTest()
{
Assert.Throws<InvalidOperationException>(() => _virtualPropertySetter.MakeGenericMethod());
}
[Fact]
public void GetCustomAttributesTest()
{
Assert.True(_virtualPropertySetter.Equals(_virtualPropertySetter));
}
}
}