Skip to content

Commit 496f06d

Browse files
committed
Broke AttributeValueCollection tests into more granular units
Previously, there was a 1:1 mapping between most methods and their tests. Now, different states (e.g., invalid value, invalid key) 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., `GetValue_IncorrectKey_ReturnsDefault()`).
1 parent 48f7c5c commit 496f06d

1 file changed

Lines changed: 175 additions & 55 deletions

File tree

OnTopic.Tests/AttributeValueCollectionTest.cs

Lines changed: 175 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,157 +21,238 @@ namespace OnTopic.Tests {
2121
public class AttributeValueCollectionTest {
2222

2323
/*==========================================================================================================================
24-
| TEST: GET VALUE
24+
| TEST: GET VALUE: CORRECT VALUE: IS RETURNED
2525
\-------------------------------------------------------------------------------------------------------------------------*/
2626
/// <summary>
2727
/// Creates a new topic and ensures that the key can be returned as an attribute.
2828
/// </summary>
2929
[TestMethod]
30-
public void GetValue() {
30+
public void GetValue_CorrectValue_IsReturned() {
3131
var topic = TopicFactory.Create("Test", "Container");
3232
Assert.AreEqual<string>("Test", topic.Attributes.GetValue("Key"));
3333
}
3434

3535
/*==========================================================================================================================
36-
| TEST: GET INTEGER
36+
| TEST: GET VALUE: MISSING VALUE: RETURNS DEFAULT
37+
\-------------------------------------------------------------------------------------------------------------------------*/
38+
/// <summary>
39+
/// Creates a new topic and requests an invalid attribute; ensures falls back to the default.
40+
/// </summary>
41+
[TestMethod]
42+
public void GetValue_MissingValue_ReturnsDefault() {
43+
var topic = TopicFactory.Create("Test", "Container");
44+
Assert.AreEqual<string>("Foo", topic.Attributes.GetValue("InvalidAttribute", "Foo"));
45+
}
46+
47+
/*==========================================================================================================================
48+
| TEST: GET INTEGER: CORRECT VALUE: IS RETURNED
3749
\-------------------------------------------------------------------------------------------------------------------------*/
3850
/// <summary>
3951
/// Ensures that integer values can be set and retrieved as expected.
4052
/// </summary>
4153
[TestMethod]
42-
public void GetInteger() {
54+
public void GetInteger_CorrectValue_IsReturned() {
4355

4456
var topic = TopicFactory.Create("Test", "Container");
4557

4658
topic.Attributes.SetInteger("Number1", 1);
47-
topic.Attributes.SetInteger("Number2", 2);
48-
topic.Attributes.SetValue("Number3", "Invalid");
4959

5060
Assert.AreEqual<int>(1, topic.Attributes.GetInteger("Number1", 5));
51-
Assert.AreEqual<int>(2, topic.Attributes.GetInteger("Number2", 5));
61+
62+
}
63+
64+
/*==========================================================================================================================
65+
| TEST: GET INTEGER: INCORRECT VALUE: RETURNS DEFAULT
66+
\-------------------------------------------------------------------------------------------------------------------------*/
67+
/// <summary>
68+
/// Ensures that invalid values return the default.
69+
/// </summary>
70+
[TestMethod]
71+
public void GetInteger_IncorrectValue_ReturnsDefault() {
72+
73+
var topic = TopicFactory.Create("Test", "Container");
74+
75+
topic.Attributes.SetValue("Number3", "Invalid");
76+
5277
Assert.AreEqual<int>(5, topic.Attributes.GetInteger("Number3", 5));
78+
79+
}
80+
81+
/*==========================================================================================================================
82+
| TEST: GET INTEGER: INCORRECT KEY: RETURNS DEFAULT
83+
\-------------------------------------------------------------------------------------------------------------------------*/
84+
/// <summary>
85+
/// Ensures that incorrect key names return the default.
86+
/// </summary>
87+
[TestMethod]
88+
public void GetInteger_IncorrectKey_ReturnsDefault() {
89+
90+
var topic = TopicFactory.Create("Test", "Container");
91+
5392
Assert.AreEqual<int>(5, topic.Attributes.GetInteger("InvalidKey", 5));
5493

5594
}
5695

5796
/*==========================================================================================================================
58-
| TEST: GET DATETIME
97+
| TEST: GET DATETIME: CORRECT VALUE: IS RETURNED
5998
\-------------------------------------------------------------------------------------------------------------------------*/
6099
/// <summary>
61100
/// Ensures that integer values can be set and retrieved as expected.
62101
/// </summary>
63102
[TestMethod]
64-
public void GetDateTime() {
103+
public void GetDateTime_CorrectValue_IsReturned() {
65104

66105
var topic = TopicFactory.Create("Test", "Container");
67106
var dateTime1 = new DateTime(1976, 10, 15);
68-
var dateTime2 = new DateTime(1981, 06, 03);
69107

70108
topic.Attributes.SetDateTime("DateTime1", dateTime1);
71-
topic.Attributes.SetDateTime("DateTime2", dateTime2);
72-
topic.Attributes.SetValue("DateTime3", "Invalid");
73109

74110
Assert.AreEqual<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime1", DateTime.MinValue));
75-
Assert.AreEqual<DateTime>(dateTime2, topic.Attributes.GetDateTime("DateTime2", DateTime.MinValue));
111+
112+
}
113+
114+
/*==========================================================================================================================
115+
| TEST: GET DATETIME: INCORRECT VALUE: RETURNS DEFAULT
116+
\-------------------------------------------------------------------------------------------------------------------------*/
117+
/// <summary>
118+
/// Ensures that invalid values return the default.
119+
/// </summary>
120+
[TestMethod]
121+
public void GetDateTime_IncorrectValue_ReturnsDefault() {
122+
123+
var topic = TopicFactory.Create("Test", "Container");
124+
var dateTime1 = new DateTime(1976, 10, 15);
125+
var dateTime2 = new DateTime(1981, 06, 03);
126+
127+
topic.Attributes.SetDateTime("DateTime2", dateTime2);
128+
129+
Assert.AreEqual<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime3", dateTime1));
130+
131+
}
132+
133+
/*==========================================================================================================================
134+
| TEST: GET DATETIME: INCORRECT KEY: RETURNS DEFAULT
135+
\-------------------------------------------------------------------------------------------------------------------------*/
136+
/// <summary>
137+
/// Ensures that incorrect key names return the default.
138+
/// </summary>
139+
[TestMethod]
140+
public void GetDateTime_IncorrectKey_ReturnsDefault() {
141+
142+
var topic = TopicFactory.Create("Test", "Container");
143+
var dateTime1 = new DateTime(1976, 10, 15);
144+
var dateTime2 = new DateTime(1981, 06, 03);
145+
146+
topic.Attributes.SetDateTime("DateTime2", dateTime2);
147+
76148
Assert.AreEqual<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime3", dateTime1));
77-
Assert.AreEqual<DateTime>(dateTime2, topic.Attributes.GetDateTime("InvalidKey", dateTime2));
78149

79150
}
80151

81152
/*==========================================================================================================================
82-
| TEST: GET BOOLEAN
153+
| TEST: GET BOOLEAN: CORRECT VALUE: IS RETURNED
83154
\-------------------------------------------------------------------------------------------------------------------------*/
84155
/// <summary>
85156
/// Ensures that boolean values can be set and retrieved as expected.
86157
/// </summary>
87158
[TestMethod]
88-
public void GetBoolean() {
159+
public void GetBoolean_CorrectValue_IsReturned() {
89160

90161
var topic = TopicFactory.Create("Test", "Container");
91162

92163
topic.Attributes.SetBoolean("IsValue1", true);
93164
topic.Attributes.SetBoolean("IsValue2", false);
94-
topic.Attributes.SetValue("IsValue3", "Invalid");
95165

96166
Assert.IsTrue(topic.Attributes.GetBoolean("IsValue1", false));
97167
Assert.IsFalse(topic.Attributes.GetBoolean("IsValue2", true));
98-
Assert.IsTrue(topic.Attributes.GetBoolean("IsValue3", true));
99-
Assert.IsFalse(topic.Attributes.GetBoolean("IsValue3", false));
100-
Assert.IsTrue(topic.Attributes.GetBoolean("InvalidKey", true));
101-
Assert.IsFalse(topic.Attributes.GetBoolean("InvalidKey", false));
102168

103169
}
104170

105171
/*==========================================================================================================================
106-
| TEST: DEFAULT VALUE
172+
| TEST: GET BOOLEAN: INCORRECT VALUE: RETURN DEFAULT
107173
\-------------------------------------------------------------------------------------------------------------------------*/
108174
/// <summary>
109-
/// Creates a new topic and requests an invalid attribute; ensures falls back to the default.
175+
/// Ensures that invalid values return the default.
110176
/// </summary>
111177
[TestMethod]
112-
public void DefaultValue() {
178+
public void GetBoolean_IncorrectValue_ReturnDefault() {
179+
113180
var topic = TopicFactory.Create("Test", "Container");
114-
Assert.AreEqual<string>("Foo", topic.Attributes.GetValue("InvalidAttribute", "Foo"));
181+
182+
topic.Attributes.SetValue("IsValue", "Invalid");
183+
184+
Assert.IsTrue(topic.Attributes.GetBoolean("IsValue", true));
185+
Assert.IsFalse(topic.Attributes.GetBoolean("IsValue", false));
186+
115187
}
116188

117189
/*==========================================================================================================================
118-
| TEST: SET VALUE
190+
| TEST: GET BOOLEAN: INCORRECT KEY: RETURN DEFAULT
191+
\-------------------------------------------------------------------------------------------------------------------------*/
192+
/// <summary>
193+
/// Ensures that incorrect key names return the default.
194+
/// </summary>
195+
[TestMethod]
196+
public void GetBoolean_IncorrectKey_ReturnDefault() {
197+
198+
var topic = TopicFactory.Create("Test", "Container");
199+
200+
Assert.IsTrue(topic.Attributes.GetBoolean("InvalidKey", true));
201+
Assert.IsFalse(topic.Attributes.GetBoolean("InvalidKey", false));
202+
203+
}
204+
205+
/*==========================================================================================================================
206+
| TEST: SET VALUE: CORRECT VALUE: IS RETURNED
119207
\-------------------------------------------------------------------------------------------------------------------------*/
120208
/// <summary>
121209
/// Sets a custom attribute on a topic and ensures it can be retrieved.
122210
/// </summary>
123211
[TestMethod]
124-
public void SetValue() {
212+
public void SetValue_CorrectValue_IsReturned() {
125213
var topic = TopicFactory.Create("Test", "Container");
126214
topic.Attributes.SetValue("Foo", "Bar");
127215
Assert.AreEqual<string>("Bar", topic.Attributes.GetValue("Foo"));
128216
}
129217

130218
/*==========================================================================================================================
131-
| TEST: SET VALUE: IS DIRTY?
219+
| TEST: SET VALUE: VALUE CHANGED: IS DIRTY?
132220
\-------------------------------------------------------------------------------------------------------------------------*/
133221
/// <summary>
134222
/// Modifies the value of a custom attribute on a topic and ensures it is marked as IsDirty.
135223
/// </summary>
136224
[TestMethod]
137-
public void SetValue_IsDirtyTest() {
225+
public void SetValue_ValueChanged_IsDirty() {
138226

139227
var topic = TopicFactory.Create("Test", "Container");
140228

141229
topic.Attributes.SetValue("Foo", "Bar", false);
142-
topic.Attributes.SetValue("Fah", "Bar", false);
143230
topic.Attributes.SetValue("Foo", "Baz");
144-
topic.Attributes.SetValue("Fah", "Bar");
145231

146-
Assert.AreEqual<string>("Baz", topic.Attributes.GetValue("Foo"));
147-
Assert.AreEqual<string>("Bar", topic.Attributes.GetValue("Fah"));
148-
Assert.AreEqual<bool>(true, topic.Attributes["Foo"].IsDirty);
149-
Assert.AreEqual<bool>(false, topic.Attributes["Fah"].IsDirty);
232+
Assert.IsTrue(topic.Attributes["Foo"].IsDirty);
150233

151234
}
152235

153236
/*==========================================================================================================================
154-
| TEST: SET VALUE BACKDOOR
237+
| TEST: SET VALUE: VALUE UNCHANGED: IS NOT DIRTY?
155238
\-------------------------------------------------------------------------------------------------------------------------*/
156239
/// <summary>
157-
/// Sets a custom attribute on a topic by directly adding an <see cref="AttributeValue"/> instance; ensures it can be
158-
/// retrieved.
240+
/// Sets the value of a custom attribute to the existing value and ensures it is <i>not</i> marked as IsDirty.
159241
/// </summary>
160242
[TestMethod]
161-
public void SetValue_BackdoorTest() {
243+
public void SetValue_ValueUnchanged_IsNotDirty() {
162244

163245
var topic = TopicFactory.Create("Test", "Container");
164246

165-
topic.Attributes.SetValue("Key", "NewKey");
166-
topic.Attributes.SetValue("Foo", "Bar");
247+
topic.Attributes.SetValue("Fah", "Bar", false);
248+
topic.Attributes.SetValue("Fah", "Bar");
167249

168-
Assert.AreEqual<string>("NewKey", topic.Key);
169-
Assert.AreEqual<string>("Bar", topic.Attributes.GetValue("Foo"));
250+
Assert.IsFalse(topic.Attributes["Fah"].IsDirty);
170251

171252
}
172253

173254
/*==========================================================================================================================
174-
| TEST: ENFORCE BUSINESS LOGIC
255+
| TEST: SET VALUE: INVALID VALUE: THROWS EXCEPTION
175256
\-------------------------------------------------------------------------------------------------------------------------*/
176257
/// <summary>
177258
/// Attempts to violate the business logic by bypassing the property setter; ensures that business logic is enforced.
@@ -181,13 +262,32 @@ public void SetValue_BackdoorTest() {
181262
typeof(TargetInvocationException),
182263
"The topic allowed a key to be set via a back door, without routing it through the Key property."
183264
)]
184-
public void EnforceBusinessLogic() {
265+
public void SetValue_InvalidValue_ThrowsException() {
185266
var topic = TopicFactory.Create("Test", "Container");
186267
topic.Attributes.SetValue("Key", "# ?");
187268
}
188269

189270
/*==========================================================================================================================
190-
| TEST: ENFORCE BUSINESS LOGIC BACKDOOR
271+
| TEST: ADD: VALID ATTRIBUTE VALUE: IS RETURNED
272+
\-------------------------------------------------------------------------------------------------------------------------*/
273+
/// <summary>
274+
/// Sets a custom attribute on a topic by directly adding an <see cref="AttributeValue"/> instance; ensures it can be
275+
/// retrieved.
276+
/// </summary>
277+
[TestMethod]
278+
public void Add_ValidAttributeValue_IsReturned() {
279+
280+
var topic = TopicFactory.Create("Test", "Container");
281+
282+
topic.Attributes.Remove("Key");
283+
topic.Attributes.Add(new AttributeValue("Key", "NewKey", false));
284+
285+
Assert.AreEqual<string>("NewKey", topic.Key);
286+
287+
}
288+
289+
/*==========================================================================================================================
290+
| TEST: SET VALUE: INSERT INVALID ATTRIBUTE VALUE: THROWS EXCEPTION
191291
\-------------------------------------------------------------------------------------------------------------------------*/
192292
/// <summary>
193293
/// Attempts to violate the business logic by bypassing SetValue() entirely; ensures that business logic is enforced.
@@ -197,20 +297,20 @@ public void EnforceBusinessLogic() {
197297
typeof(TargetInvocationException),
198298
"The topic allowed a key to be set via a back door, without routing it through the Key property."
199299
)]
200-
public void EnforceBusinessLogic_Backdoor() {
300+
public void Add_InvalidAttributeValue_ThrowsException() {
201301
var topic = TopicFactory.Create("Test", "Container");
202302
topic.Attributes.Remove("Key");
203303
topic.Attributes.Add(new AttributeValue("Key", "# ?"));
204304
}
205305

206306
/*==========================================================================================================================
207-
| TEST: INHERIT FROM PARENT
307+
| TEST: GET VALUE: INHERIT FROM PARENT: RETURNS PARENT VALUE
208308
\-------------------------------------------------------------------------------------------------------------------------*/
209309
/// <summary>
210310
/// Sets an attribute on the parent of a topic and ensures it can be retrieved using inheritance.
211311
/// </summary>
212312
[TestMethod]
213-
public void InheritFromParent() {
313+
public void GetValue_InheritFromParent_ReturnsParentValue() {
214314

215315
var topics = new Topic[8];
216316

@@ -229,13 +329,36 @@ public void InheritFromParent() {
229329
}
230330

231331
/*==========================================================================================================================
232-
| TEST: MAX HOPS
332+
| TEST: GET VALUE: INHERIT FROM DERIVED: RETURNS DERIVED VALUE
333+
\-------------------------------------------------------------------------------------------------------------------------*/
334+
/// <summary>
335+
/// Establishes a long tree of derives topics, and ensures that the derived value is returned.
336+
/// </summary>
337+
[TestMethod]
338+
public void GetValue_InheritFromDerived_ReturnsDerivedValue() {
339+
340+
var topics = new Topic[5];
341+
342+
for (var i = 0; i <= 4; i++) {
343+
var topic = TopicFactory.Create("Topic" + i, "Container");
344+
if (i > 0) topics[i - 1].DerivedTopic = topic;
345+
topics[i] = topic;
346+
}
347+
348+
topics[4].Attributes.SetValue("Foo", "Bar");
349+
350+
Assert.AreEqual<string>("Bar", topics[0].Attributes.GetValue("Foo", null, true, true));
351+
352+
}
353+
354+
/*==========================================================================================================================
355+
| TEST: GET VALUE: EXCEEDS MAX HOPS: RETURNS DEFAULT
233356
\-------------------------------------------------------------------------------------------------------------------------*/
234357
/// <summary>
235358
/// Establishes a long tree of derives topics, and ensures that inheritance will pursue no more than five hops.
236359
/// </summary>
237360
[TestMethod]
238-
public void MaxHops() {
361+
public void GetValue_ExceedsMaxHops_ReturnsDefault() {
239362

240363
var topics = new Topic[8];
241364

@@ -247,10 +370,7 @@ public void MaxHops() {
247370

248371
topics[7].Attributes.SetValue("Foo", "Bar");
249372

250-
Assert.IsNull(topics[0].Attributes.GetValue("Foo", null, true, false));
251-
Assert.AreEqual<string>("Bar", topics[2].Attributes.GetValue("Foo", null, true, true));
252-
Assert.AreNotEqual<string>("Bar", topics[1].Attributes.GetValue("Foo", null, true, true));
253-
Assert.AreNotEqual<string>("Bar", topics[0].Attributes.GetValue("Foo", null, true, true));
373+
Assert.IsNull(topics[0].Attributes.GetValue("Foo", null, true, true));
254374

255375
}
256376

0 commit comments

Comments
 (0)