forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyHelperTest.cs
More file actions
246 lines (197 loc) · 7.66 KB
/
Copy pathPropertyHelperTest.cs
File metadata and controls
246 lines (197 loc) · 7.66 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Reflection;
using Microsoft.TestCommon;
namespace System.Web.WebPages.Test
{
public class PropertyHelperTest
{
[Fact]
public void PropertyHelperReturnsNameCorrectly()
{
// Arrange
var anonymous = new { foo = "bar" };
PropertyInfo property = anonymous.GetType().GetProperties().First();
// Act
PropertyHelper helper = new PropertyHelper(property);
// Assert
Assert.Equal("foo", property.Name);
Assert.Equal("foo", helper.Name);
}
[Fact]
public void PropertyHelperReturnsValueCorrectly()
{
// Arrange
var anonymous = new { bar = "baz" };
PropertyInfo property = anonymous.GetType().GetProperties().First();
// Act
PropertyHelper helper = new PropertyHelper(property);
// Assert
Assert.Equal("bar", helper.Name);
Assert.Equal("baz", helper.GetValue(anonymous));
}
[Fact]
public void PropertyHelperReturnsValueCorrectlyForValueTypes()
{
// Arrange
var anonymous = new { foo = 32 };
PropertyInfo property = anonymous.GetType().GetProperties().First();
// Act
PropertyHelper helper = new PropertyHelper(property);
// Assert
Assert.Equal("foo", helper.Name);
Assert.Equal(32, helper.GetValue(anonymous));
}
[Fact]
public void PropertyHelperReturnsCachedPropertyHelper()
{
// Arrange
var anonymous = new { foo = "bar" };
// Act
PropertyHelper[] helpers1 = PropertyHelper.GetProperties(anonymous);
PropertyHelper[] helpers2 = PropertyHelper.GetProperties(anonymous);
// Assert
Assert.Single(helpers1);
Assert.ReferenceEquals(helpers1, helpers2);
Assert.ReferenceEquals(helpers1[0], helpers2[0]);
}
[Fact]
public void PropertyHelperDoesNotChangeUnderscores()
{
// Arrange
var anonymous = new { bar_baz2 = "foo" };
// Act + Assert
PropertyHelper helper = Assert.Single(PropertyHelper.GetProperties(anonymous));
Assert.Equal("bar_baz2", helper.Name);
}
private class PrivateProperties
{
public int Prop1 { get; set; }
protected int Prop2 { get; set; }
private int Prop3 { get; set; }
}
[Fact]
public void PropertyHelperDoesNotFindPrivateProperties()
{
// Arrange
var anonymous = new PrivateProperties();
// Act + Assert
PropertyHelper helper = Assert.Single(PropertyHelper.GetProperties(anonymous));
Assert.Equal("Prop1", helper.Name);
}
private class Static
{
public static int Prop2 { get; set; }
public int Prop5 { get; set; }
}
[Fact]
public void PropertyHelperDoesNotFindStaticProperties()
{
// Arrange
var anonymous = new Static();
// Act + Assert
PropertyHelper helper = Assert.Single(PropertyHelper.GetProperties(anonymous));
Assert.Equal("Prop5", helper.Name);
}
private class SetOnly
{
public int Prop2 { set { } }
public int Prop6 { get; set; }
}
[Fact]
public void PropertyHelperDoesNotFindSetOnlyProperties()
{
// Arrange
var anonymous = new SetOnly();
// Act + Assert
PropertyHelper helper = Assert.Single(PropertyHelper.GetProperties(anonymous));
Assert.Equal("Prop6", helper.Name);
}
private struct MyProperties
{
public int IntProp { get; set; }
public string StringProp { get; set; }
}
[Fact]
public void PropertyHelperWorksForStruct()
{
// Arrange
var anonymous = new MyProperties();
anonymous.IntProp = 3;
anonymous.StringProp = "Five";
// Act + Assert
PropertyHelper helper1 = Assert.Single(PropertyHelper.GetProperties(anonymous), prop => prop.Name == "IntProp");
PropertyHelper helper2 = Assert.Single(PropertyHelper.GetProperties(anonymous), prop => prop.Name == "StringProp");
Assert.Equal(3, helper1.GetValue(anonymous));
Assert.Equal("Five", helper2.GetValue(anonymous));
}
public class BaseClass
{
public string PropA { get; set; }
protected string PropProtected { get; set; }
}
public class DerivedClass : BaseClass
{
public string PropB { get; set; }
}
public class BaseClassWithVirtual
{
public virtual string PropA { get; set; }
public string PropB { get; set; }
}
public class DerivedClassWithNew : BaseClassWithVirtual
{
public new string PropB { get { return "Newed"; } }
}
public class DerivedClassWithOverride : BaseClassWithVirtual
{
public override string PropA { get { return "Overriden"; } }
}
[Fact]
public void PropertyHelperForDerivedClass()
{
// Arrange
object derived = new DerivedClass { PropA = "propAValue", PropB = "propBValue" };
// Act
PropertyHelper[] helpers = PropertyHelper.GetProperties(derived).ToArray();
// Assert
Assert.NotNull(helpers);
Assert.Equal(2, helpers.Length);
PropertyHelper propAHelper = Assert.Single(helpers, h => h.Name == "PropA");
PropertyHelper propBHelper = Assert.Single(helpers, h => h.Name == "PropB");
Assert.Equal("propAValue", propAHelper.GetValue(derived));
Assert.Equal("propBValue", propBHelper.GetValue(derived));
}
[Fact]
public void PropertyHelperForDerivedClassWithNew()
{
// Arrange
object derived = new DerivedClassWithNew { PropA = "propAValue" };
// Act
PropertyHelper[] helpers = PropertyHelper.GetProperties(derived).ToArray();
// Assert
Assert.NotNull(helpers);
Assert.Equal(2, helpers.Length);
PropertyHelper propAHelper = Assert.Single(helpers, h => h.Name == "PropA");
PropertyHelper propBHelper = Assert.Single(helpers, h => h.Name == "PropB");
Assert.Equal("propAValue", propAHelper.GetValue(derived));
Assert.Equal("Newed", propBHelper.GetValue(derived));
}
[Fact]
public void PropertyHelperForDerivedWithVirtual()
{
// Arrange
object derived = new DerivedClassWithOverride { PropA = "propAValue", PropB = "propBValue" };
// Act
PropertyHelper[] helpers = PropertyHelper.GetProperties(derived).ToArray();
// Assert
Assert.NotNull(helpers);
Assert.Equal(2, helpers.Length);
PropertyHelper propAHelper = Assert.Single(helpers, h => h.Name == "PropA");
PropertyHelper propBHelper = Assert.Single(helpers, h => h.Name == "PropB");
Assert.Equal("Overriden", propAHelper.GetValue(derived));
Assert.Equal("propBValue", propBHelper.GetValue(derived));
}
}
}