Skip to content

Commit 13673dd

Browse files
committed
Broke TypeMemberInfoCollectionTest tests into more granular units
Previously, each test evaluated quite a few states, both successful and unsuccessful. Now, different states (e.g., invalid property name, invalid property value) are broken out as distinct tests in order to provide more granular units. This dramatically increases the number of tests. This also necessitated adopting a more expressive test naming convention: `METHOD_CONDITION_RESULT` (e.g., `Constructor_ValidType_IdentifiedProperty()`).
1 parent 706434a commit 13673dd

1 file changed

Lines changed: 188 additions & 35 deletions

File tree

OnTopic.Tests/TypeMemberInfoCollectionTest.cs

Lines changed: 188 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,63 @@ namespace OnTopic.Tests {
2626
public class TypeMemberInfoCollectionTest {
2727

2828
/*==========================================================================================================================
29-
| TEST: PROPERTY INFO COLLECTION CONSTRUCTOR
29+
| TEST: CONSTRUCTOR: VALID TYPE: IDENTIFIES PROPERTY
3030
\-------------------------------------------------------------------------------------------------------------------------*/
3131
/// <summary>
32-
/// Establishes a <see cref="MemberInfoCollection"/> based on a type, and confirms that the property collection is
33-
/// returning expected types.
32+
/// Establishes a <see cref="MemberInfoCollection"/> based on a type, and confirms that the property collection returns
33+
/// properties of the type.
3434
/// </summary>
3535
[TestMethod]
36-
public void Constructor() {
36+
public void Constructor_ValidType_IdentifiesProperty() {
3737

3838
var properties = new MemberInfoCollection<PropertyInfo>(typeof(ContentTypeDescriptor));
3939

40-
Assert.IsTrue(properties.Contains("Key")); //Inherited string property
4140
Assert.IsTrue(properties.Contains("AttributeDescriptors")); //First class collection property
42-
Assert.IsFalse(properties.Contains("IsTypeOf")); //This is a method, not a property
4341
Assert.IsFalse(properties.Contains("InvalidPropertyName")); //Invalid property
4442

4543
}
4644

4745
/*==========================================================================================================================
48-
| TEST: GET TYPE
46+
| TEST: CONSTRUCTOR: VALID TYPE: IDENTIFIES DERIVED PROPERTY
47+
\-------------------------------------------------------------------------------------------------------------------------*/
48+
/// <summary>
49+
/// Establishes a <see cref="MemberInfoCollection"/> based on a type, and confirms that the property collection returns
50+
/// properties derived from the base class.
51+
/// </summary>
52+
[TestMethod]
53+
public void Constructor_ValidType_IdentifiesDerivedProperty() {
54+
55+
var properties = new MemberInfoCollection<PropertyInfo>(typeof(ContentTypeDescriptor));
56+
57+
Assert.IsTrue(properties.Contains("Key")); //Inherited string property
58+
59+
}
60+
61+
/*==========================================================================================================================
62+
| TEST: CONSTRUCTOR: VALID TYPE: IDENTIFIES METHOD
63+
\-------------------------------------------------------------------------------------------------------------------------*/
64+
/// <summary>
65+
/// Establishes a <see cref="MemberInfoCollection"/> based on a type, and confirms that the property collection returns
66+
/// methods of the type.
67+
/// </summary>
68+
[TestMethod]
69+
public void Constructor_ValidType_IdentifiesMethod() {
70+
71+
var properties = new MemberInfoCollection<PropertyInfo>(typeof(ContentTypeDescriptor));
72+
73+
Assert.IsFalse(properties.Contains("IsTypeOf")); //This is a method, not a property
74+
75+
}
76+
77+
/*==========================================================================================================================
78+
| TEST: GET MEMBERS: PROPERTY INFO: RETURNS PROPERTIES
4979
\-------------------------------------------------------------------------------------------------------------------------*/
5080
/// <summary>
5181
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that <see
5282
/// cref="TypeMemberInfoCollection.GetMembers{T}(Type)"/> functions.
5383
/// </summary>
5484
[TestMethod]
55-
public void GetProperties() {
85+
public void GetMembers_PropertyInfo_ReturnsProperties() {
5686

5787
var types = new TypeMemberInfoCollection();
5888

@@ -66,91 +96,214 @@ public void GetProperties() {
6696
}
6797

6898
/*==========================================================================================================================
69-
| TEST: GET MEMBER
99+
| TEST: GET MEMBER: PROPERTY INFO BY KEY: RETURNS VALUE
70100
\-------------------------------------------------------------------------------------------------------------------------*/
71101
/// <summary>
72102
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that <see
73-
/// cref="TypeMemberInfoCollection.GetMember(Type, String)"/> correctly returns the expected properties.
103+
/// cref="TypeMemberInfoCollection.GetMember{T}(Type, String)"/> correctly returns the expected properties.
74104
/// </summary>
75105
[TestMethod]
76-
public void GetMember() {
106+
public void GetMember_PropertyInfoByKey_ReturnsValue() {
77107

78108
var types = new TypeMemberInfoCollection();
79109

80-
Assert.IsTrue(types.GetMember<PropertyInfo>(typeof(ContentTypeDescriptor), "Key") != null);
81-
Assert.IsTrue(types.GetMember<PropertyInfo>(typeof(ContentTypeDescriptor), "AttributeDescriptors") != null);
82-
Assert.IsFalse(types.GetMember<PropertyInfo>(typeof(ContentTypeDescriptor), "IsTypeOf") != null);
83-
Assert.IsFalse(types.GetMember<PropertyInfo>(typeof(ContentTypeDescriptor), "InvalidPropertyName") != null);
84-
Assert.IsTrue(types.GetMember<MethodInfo>(typeof(ContentTypeDescriptor), "GetWebPath") != null);
85-
Assert.IsFalse(types.GetMember<MethodInfo>(typeof(ContentTypeDescriptor), "AttributeDescriptors") != null);
110+
Assert.IsNotNull(types.GetMember<PropertyInfo>(typeof(ContentTypeDescriptor), "Key"));
111+
Assert.IsNotNull(types.GetMember<PropertyInfo>(typeof(ContentTypeDescriptor), "AttributeDescriptors"));
112+
Assert.IsNull(types.GetMember<PropertyInfo>(typeof(ContentTypeDescriptor), "InvalidPropertyName"));
86113

87114
}
88115

89116
/*==========================================================================================================================
90-
| TEST: SET PROPERTY
117+
| TEST: GET MEMBER: METHOD INFO BY KEY: RETURNS VALUE
91118
\-------------------------------------------------------------------------------------------------------------------------*/
92119
/// <summary>
93-
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that a value can be properly set using the
120+
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that <see
121+
/// cref="TypeMemberInfoCollection.GetMember{T}(Type, String)"/> correctly returns the expected methods.
122+
/// </summary>
123+
[TestMethod]
124+
public void GetMember_MethodInfoByKey_ReturnsValue() {
125+
126+
var types = new TypeMemberInfoCollection();
127+
128+
Assert.IsNotNull(types.GetMember<MethodInfo>(typeof(ContentTypeDescriptor), "GetWebPath"));
129+
Assert.IsNull(types.GetMember<MethodInfo>(typeof(ContentTypeDescriptor), "AttributeDescriptors"));
130+
131+
}
132+
133+
/*==========================================================================================================================
134+
| TEST: GET MEMBER: GENERIC TYPE MISMATCH: RETURNS NULL
135+
\-------------------------------------------------------------------------------------------------------------------------*/
136+
/// <summary>
137+
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that <see
138+
/// cref="TypeMemberInfoCollection.GetMember{T}(Type, string)"/> does not return values if the
139+
/// </summary>
140+
[TestMethod]
141+
public void GetMember_GenericTypeMismatch_ReturnsNull() {
142+
143+
var types = new TypeMemberInfoCollection();
144+
145+
Assert.IsNull(types.GetMember<PropertyInfo>(typeof(ContentTypeDescriptor), "IsTypeOf"));
146+
Assert.IsNull(types.GetMember<MethodInfo>(typeof(ContentTypeDescriptor), "AttributeDescriptors"));
147+
148+
}
149+
150+
/*==========================================================================================================================
151+
| TEST: SET PROPERTY VALUE: KEY: SETS VALUE
152+
\-------------------------------------------------------------------------------------------------------------------------*/
153+
/// <summary>
154+
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that a key value can be properly set using the
155+
/// <see cref="TypeMemberInfoCollection.SetPropertyValue(Object, String, String)"/> method.
156+
/// </summary>
157+
[TestMethod]
158+
public void SetPropertyValue_Key_SetsValue() {
159+
160+
var types = new TypeMemberInfoCollection();
161+
var topic = TopicFactory.Create("Test", "ContentType");
162+
163+
var isKeySet = types.SetPropertyValue(topic, "Key", "NewKey");
164+
165+
var key = types.GetPropertyValue(topic, "Key", typeof(string)).ToString();
166+
167+
Assert.IsTrue(isKeySet);
168+
Assert.AreEqual<string>("NewKey", topic.Key);
169+
Assert.AreEqual<string>("NewKey", key);
170+
171+
}
172+
173+
174+
/*==========================================================================================================================
175+
| TEST: SET PROPERTY VALUE: BOOLEAN: SETS VALUE
176+
\-------------------------------------------------------------------------------------------------------------------------*/
177+
/// <summary>
178+
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that a boolean value can be properly set using the
94179
/// <see cref="TypeMemberInfoCollection.SetPropertyValue(Object, String, String)"/> method.
95180
/// </summary>
96181
[TestMethod]
97-
public void SetProperty() {
182+
public void SetPropertyValue_Boolean_SetsValue() {
98183

99184
var types = new TypeMemberInfoCollection();
100185
var topic = TopicFactory.Create("Test", "ContentType");
101186

102187
types.SetPropertyValue(topic, "IsHidden", "1");
103188

189+
Assert.IsTrue(topic.IsHidden);
190+
191+
}
192+
193+
/*==========================================================================================================================
194+
| TEST: SET PROPERTY VALUE: DATE/TIME: SETS VALUE
195+
\-------------------------------------------------------------------------------------------------------------------------*/
196+
/// <summary>
197+
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that a date/time value can be properly set using the
198+
/// <see cref="TypeMemberInfoCollection.SetPropertyValue(Object, String, String)"/> method.
199+
/// </summary>
200+
[TestMethod]
201+
public void SetPropertyValue_DateTime_SetsValue() {
202+
203+
var types = new TypeMemberInfoCollection();
204+
var topic = TopicFactory.Create("Test", "ContentType");
205+
104206
var isDateSet = types.SetPropertyValue(topic, "LastModified", "June 3, 2008");
105207
isDateSet = types.SetPropertyValue(topic, "LastModified", "2008-06-03") && isDateSet;
106208
isDateSet = types.SetPropertyValue(topic, "LastModified", "06/03/2008") && isDateSet;
107-
var isKeySet = types.SetPropertyValue(topic, "Key", "NewKey");
108-
var isInvalidPropertySet = types.SetPropertyValue(topic, "InvalidProperty", "Invalid");
109209

110210
var lastModified = DateTime.Parse(
111211
types.GetPropertyValue(topic, "LastModified", typeof(DateTime)).ToString(),
112212
CultureInfo.InvariantCulture
113213
);
114-
var key = types.GetPropertyValue(topic, "Key", typeof(string)).ToString();
115214

116215
Assert.IsTrue(isDateSet);
117-
Assert.IsTrue(isKeySet);
118-
Assert.IsFalse(isInvalidPropertySet);
119-
Assert.AreEqual<string>("NewKey", topic.Key);
120-
Assert.AreEqual<string>("NewKey", key);
121216
Assert.AreEqual<DateTime>(new DateTime(2008, 6, 3), topic.LastModified);
122217
Assert.AreEqual<DateTime>(new DateTime(2008, 6, 3), lastModified);
123-
Assert.IsTrue(topic.IsHidden);
218+
219+
}
220+
221+
222+
/*==========================================================================================================================
223+
| TEST: SET PROPERTY VALUE: INVALID PROPERTY: RETURNS FALSE
224+
\-------------------------------------------------------------------------------------------------------------------------*/
225+
/// <summary>
226+
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that an invalid property being set via the
227+
/// <see cref="TypeMemberInfoCollection.SetPropertyValue(Object, String, String)"/> method returns false.
228+
/// </summary>
229+
[TestMethod]
230+
public void SetPropertyValue_InvalidProperty_ReturnsFalse() {
231+
232+
var types = new TypeMemberInfoCollection();
233+
var topic = TopicFactory.Create("Test", "ContentType");
234+
235+
var isInvalidPropertySet = types.SetPropertyValue(topic, "InvalidProperty", "Invalid");
236+
237+
Assert.IsFalse(isInvalidPropertySet);
124238

125239
}
126240

127241
/*==========================================================================================================================
128-
| TEST: SET METHOD
242+
| TEST: SET METHOD: VALID VALUE: SETS VALUE
129243
\-------------------------------------------------------------------------------------------------------------------------*/
130244
/// <summary>
131245
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that a value can be properly set using the
132246
/// <see cref="TypeMemberInfoCollection.SetMethodValue(Object, String, String)"/> method.
133247
/// </summary>
134248
[TestMethod]
135-
public void SetMethod() {
249+
public void SetMethod_ValidValue_SetsValue() {
136250

137251
var types = new TypeMemberInfoCollection();
138252
var source = new MethodBasedViewModel();
139253

140254
var isValueSet = types.SetMethodValue(source, "SetMethod", "123");
141-
var isInvalidSet = types.SetMethodValue(source, "BogusMethod", "123");
142-
143255
var value = types.GetMethodValue(source, "GetMethod")?? 0;
144256

145257
Assert.IsTrue(isValueSet);
146-
Assert.IsFalse(isInvalidSet);
147258
Assert.IsTrue(value is int);
148259
Assert.AreEqual<int>(123, (int)value);
149260

150261
}
151262

152263
/*==========================================================================================================================
153-
| TEST: REFLECTION PERFORMANCE
264+
| TEST: SET METHOD: INVALID VALUE: DOESN'T SET VALUE
265+
\-------------------------------------------------------------------------------------------------------------------------*/
266+
/// <summary>
267+
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that a value set with an invalid value using the
268+
/// <see cref="TypeMemberInfoCollection.SetMethodValue(Object, String, String)"/> method returns an exception.
269+
/// </summary>
270+
[TestMethod]
271+
public void SetMethod_InvalidValue_DoesNotSetValue() {
272+
273+
var types = new TypeMemberInfoCollection();
274+
var source = new MethodBasedViewModel();
275+
276+
var isValueSet = types.SetMethodValue(source, "SetMethod", "ABC");
277+
var value = types.GetMethodValue(source, "GetMethod")?? 0;
278+
279+
Assert.IsFalse(isValueSet);
280+
Assert.IsTrue(value is int);
281+
Assert.AreEqual<int>(0, (int)value);
282+
283+
}
284+
285+
286+
/*==========================================================================================================================
287+
| TEST: SET METHOD: INVALID MEMBER: RETURNS FALSE
288+
\-------------------------------------------------------------------------------------------------------------------------*/
289+
/// <summary>
290+
/// Establishes a <see cref="TypeMemberInfoCollection"/> and confirms that setting an invalid property name using the
291+
/// <see cref="TypeMemberInfoCollection.SetMethodValue(Object, String, String)"/> method returns false.
292+
/// </summary>
293+
[TestMethod]
294+
public void SetMethod_Integer_SetsValue() {
295+
296+
var types = new TypeMemberInfoCollection();
297+
var source = new MethodBasedViewModel();
298+
299+
var isInvalidSet = types.SetMethodValue(source, "BogusMethod", "123");
300+
301+
Assert.IsFalse(isInvalidSet);
302+
303+
}
304+
305+
/*==========================================================================================================================
306+
| TEST: SET PROPERTY VALUE: REFLECTION PERFORMANCE
154307
\-------------------------------------------------------------------------------------------------------------------------*/
155308
/// <summary>
156309
/// Sets properties via reflection n number of times; quick way of evaluating the relative performance impact of changes.
@@ -162,7 +315,7 @@ public void SetMethod() {
162315
/// the number of iterations, simply increment the "totalIterations" variable.
163316
/// </remarks>
164317
[TestMethod]
165-
public void ReflectionPerformance() {
318+
public void SetPropertyValue_ReflectionPerformance() {
166319

167320
var totalIterations = 1;
168321
var types = new TypeMemberInfoCollection();

0 commit comments

Comments
 (0)