Skip to content

Commit fa08bfb

Browse files
committed
Introduced test to ensure Clear(), Remove() enforce business logic
Introduced a test to ensure that calling `Clear()` enforces business logic by adding a property that will throw an exception if its set to null. This validates the behavior introduced in abfe213. And since `Clear()` now calls `Remove()`, it also effectively validates the behavior introduced in 0bccf1f. This validates the new functionality introduced as part of #79.
1 parent 7aae9bc commit fa08bfb

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

OnTopic.Tests/AttributeCollectionTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,26 @@ public void SetValue_ValueChanged_IsDirty() {
439439

440440
}
441441

442+
/*==========================================================================================================================
443+
| TEST: CLEAR: NON-NULLABLE VALUE WITH BUSINESS LOGIC: THROWS EXCEPTION
444+
\-------------------------------------------------------------------------------------------------------------------------*/
445+
/// <summary>
446+
/// Sets an attribute on a topic instance with an valid value, and then attempts to clear all attributes. Ensures that the
447+
/// business logic is enforced by throwing an exception on the non-nullable property.
448+
/// </summary>
449+
[Fact]
450+
public void Clear_NonNullableValueWithBusinessLogic_ThrowsException() {
451+
452+
var topic = new CustomTopic("Test", "Page");
453+
454+
topic.NonNullableAttribute = "Test";
455+
456+
Assert.Throws<ArgumentNullException>(() =>
457+
topic.Attributes.Clear()
458+
);
459+
460+
}
461+
442462
/*==========================================================================================================================
443463
| TEST: CLEAR: EXISTING VALUES: IS DIRTY?
444464
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic.Tests/Entities/CustomTopic.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ public int NumericAttribute {
8383
}
8484
}
8585

86+
/*==========================================================================================================================
87+
| NON-NULLABLE ATTRIBUTE
88+
\-------------------------------------------------------------------------------------------------------------------------*/
89+
/// <summary>
90+
/// Provides a property which does not permit null values as a means of ensuring the business logic is enforced when
91+
/// removing values.
92+
/// </summary>
93+
[AttributeSetter]
94+
public string NonNullableAttribute {
95+
get => Attributes.GetValue("NonNullableAttribute")?? "";
96+
set {
97+
Contract.Requires(value, nameof(value));
98+
SetAttributeValue("NonNullableAttribute", value.ToString(CultureInfo.InvariantCulture));
99+
}
100+
}
101+
86102
/*==========================================================================================================================
87103
| DATE/TIME ATTRIBUTE
88104
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)