-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
VirtualPropertyInfo.PropertyGetter.Tests.cs
103 lines (90 loc) · 4.14 KB
/
VirtualPropertyInfo.PropertyGetter.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
// 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_PropertyGetter_Tests
{
// Points to a PropertyInfo instance created by reflection. This doesn't work in a reflection-only context.
private readonly MethodInfo _virtualPropertyGetter;
private readonly TestObject _testObject = new TestObject("Age");
public VirtualPropertyInfo_PropertyGetter_Tests()
{
var customReflectionContext = new VirtualPropertyInfoCustomReflectionContext();
TypeInfo typeInfo = typeof(TestObject).GetTypeInfo();
TypeInfo customTypeInfo = customReflectionContext.MapType(typeInfo);
PropertyInfo virtualProperty = customTypeInfo.DeclaredProperties.ElementAt(2);
_virtualPropertyGetter = virtualProperty.GetGetMethod(false);
}
[Fact]
public void ProjectionTest()
{
Assert.Equal(ProjectionConstants.VirtualPropertyInfoGetter, _virtualPropertyGetter.GetType().FullName);
}
[Fact]
public void GetCustomAttributes_WithType_Test()
{
object[] attributes = _virtualPropertyGetter.GetCustomAttributes(typeof(TestGetterSetterAttribute), true);
Assert.Single(attributes);
Assert.IsType<TestGetterSetterAttribute>(attributes[0]);
}
[Fact]
public void GetCustomAttributes_NoType_Test()
{
object[] attributes = _virtualPropertyGetter.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 = _virtualPropertyGetter.GetCustomAttributesData();
Assert.Empty(customAttributesData);
}
[Fact]
public void IsDefinedTest()
{
Assert.True(_virtualPropertyGetter.IsDefined(typeof(TestGetterSetterAttribute), true));
Assert.True(_virtualPropertyGetter.IsDefined(typeof(TestGetterSetterAttribute), false));
Assert.False(_virtualPropertyGetter.IsDefined(typeof(DataContractAttribute), true));
Assert.False(_virtualPropertyGetter.IsDefined(typeof(DataContractAttribute), false));
}
[Fact]
public void Invoke_NotEmptyParameter_Throws()
{
Assert.Throws<TargetParameterCountException>(() =>
_virtualPropertyGetter.Invoke(_testObject, BindingFlags.GetProperty, null, new object[] { 1 }, CultureInfo.InvariantCulture));
}
[Fact]
public void Invoke_NullObject_Throws()
{
Assert.Throws<ArgumentNullException>(() =>
_virtualPropertyGetter.Invoke(null, BindingFlags.GetProperty, null, null, CultureInfo.InvariantCulture));
}
[Fact]
public void Invoke_WrongObject_Throws()
{
Assert.Throws<ArgumentException>(() =>
_virtualPropertyGetter.Invoke("abc", BindingFlags.GetProperty, null, null, CultureInfo.InvariantCulture));
}
[Fact]
public void Invoke_EmptyParameters_Success()
{
object returnVal = _virtualPropertyGetter.Invoke(_testObject, BindingFlags.GetProperty, null, new object[] { }, CultureInfo.InvariantCulture);
Assert.Equal(42, returnVal);
}
[Fact]
public void Invoke_NullParameters_Success()
{
object returnVal = _virtualPropertyGetter.Invoke(_testObject, BindingFlags.GetProperty, null, null, CultureInfo.InvariantCulture);
Assert.Equal(42, returnVal);
}
}
}