Skip to content

Commit f5cbd39

Browse files
committed
Broke ContractTest tests into more granular units
Previously, a couple of common overloads, such as passing conditions instead of objects, were not tested. This is likely because those were originally supplied by Microsoft's implementation. Now that they're supplied by Ignia's `Contract` class, they should be subjected to full test coverage. As part of this, I also adopted the more expressive test naming convention recently applied to `AttributeValueCollectionTest` (see #496f06d): `METHOD_CONDITION_RESULT` (e.g., `GetValue_IncorrectKey_ReturnsDefault()`).
1 parent 496f06d commit f5cbd39

1 file changed

Lines changed: 54 additions & 25 deletions

File tree

OnTopic.Tests/ContractTest.cs

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,58 @@ namespace OnTopic.Tests {
1919
public class ContractTest {
2020

2121
/*==========================================================================================================================
22-
| TEST: REQUIRES
22+
| TEST: REQUIRES: CONDITION IS TRUE: THROW NO EXCEPTION
2323
\-------------------------------------------------------------------------------------------------------------------------*/
2424
/// <summary>
25-
/// Tests a non-null argument using the <see cref="Contract"/> class, and validates that it correctly does nothing.
25+
/// Tests a true condition using the <see cref="Contract"/> class, and validates that it correctly does nothing.
2626
/// </summary>
2727
[TestMethod]
28-
public void Requires() {
29-
30-
var argument = new Object();
28+
public void Requires_ConditionIsTrue_ThrowNoException()
29+
=> Contract.Requires(true, "The argument cannot be null");
3130

32-
Contract.Requires(argument, "The argument cannot be null");
31+
/*==========================================================================================================================
32+
| TEST: REQUIRES: CONDITION IS FALSE: THROW ARGUMENT NULL EXCEPTION
33+
\-------------------------------------------------------------------------------------------------------------------------*/
34+
/// <summary>
35+
/// Tests a false condition using the <see cref="Contract"/> class, and validates that it correctly returns an <see
36+
/// cref="ArgumentNullException"/>.
37+
/// </summary>
38+
[TestMethod]
39+
[ExpectedException(typeof(Exception))]
40+
public void Requires_ConditionIsFalse_ThrowArgumentNullException()
41+
=> Contract.Requires(false, "The argument cannot be null");
3342

34-
}
43+
/*==========================================================================================================================
44+
| TEST: REQUIRES: OBJECT EXISTS: THROW NO EXCEPTION
45+
\-------------------------------------------------------------------------------------------------------------------------*/
46+
/// <summary>
47+
/// Tests a non-null argument using the <see cref="Contract"/> class, and validates that it correctly does nothing.
48+
/// </summary>
49+
[TestMethod]
50+
public void Requires_ObjectExists_ThrowNoException() =>
51+
Contract.Requires(new Object(), "The argument cannot be null");
3552

3653
/*==========================================================================================================================
37-
| TEST: REQUIRES (ARGUMENT NULL EXCEPTION)
54+
| TEST: REQUIRES: OBJECT IS NULL: THROW ARGUMENT NULL EXCEPTION
3855
\-------------------------------------------------------------------------------------------------------------------------*/
3956
/// <summary>
4057
/// Tests a null argument using the <see cref="Contract"/> class, and validates that it correctly returns an <see
4158
/// cref="ArgumentNullException"/>.
4259
/// </summary>
4360
[TestMethod]
4461
[ExpectedException(typeof(ArgumentNullException))]
45-
public void RequiresArgumentNullException() {
46-
47-
var argument = (object?)null;
48-
49-
Contract.Requires(argument, "The argument cannot be null");
50-
51-
}
62+
public void Requires_ObjectIsNull_ThrowArgumentNullException() =>
63+
Contract.Requires(null, "The argument cannot be null");
5264

5365
/*==========================================================================================================================
54-
| TEST: REQUIRES (EXCEPTION MESSAGE)
66+
| TEST: REQUIRES: MESSAGE EXISTS: THROW EXCEPTION WITH MESSAGE
5567
\-------------------------------------------------------------------------------------------------------------------------*/
5668
/// <summary>
5769
/// Tests a null argument using the <see cref="Contract"/> class, and validates that it correctly returns an <see
5870
/// cref="ArgumentNullException"/> with the expected <see cref="ArgumentException.Message"/>.
5971
/// </summary>
6072
[TestMethod]
61-
public void RequiresExceptionMessage() {
73+
public void Requires_MessageExists_ThrowExceptionWithMessage() {
6274

6375
var argument = (object?)null;
6476
var errorMessage = "The argument cannot be null";
@@ -73,21 +85,38 @@ public void RequiresExceptionMessage() {
7385
}
7486

7587
/*==========================================================================================================================
76-
| TEST: ASSUME (INVALID OPERATION EXCEPTION)
88+
| TEST: ASSUME: CONDITION IS TRUE: THROW NO EXCEPTION
89+
\-------------------------------------------------------------------------------------------------------------------------*/
90+
/// <summary>
91+
/// Tests a true condition using the <see cref="Contract"/> class, and validates that it correctly does nothing.
92+
/// </summary>
93+
[TestMethod]
94+
public void Assume_ConditionIsTrue_ThrowNoException()
95+
=> Contract.Assume(true, "The argument cannot be null");
96+
97+
/*==========================================================================================================================
98+
| TEST: ASSUME: CONDITION IS FALSE: THROW ARGUMENT NULL EXCEPTION
99+
\-------------------------------------------------------------------------------------------------------------------------*/
100+
/// <summary>
101+
/// Tests a false condition using the <see cref="Contract"/> class, and validates that it correctly returns an <see
102+
/// cref="ArgumentNullException"/>.
103+
/// </summary>
104+
[TestMethod]
105+
[ExpectedException(typeof(Exception))]
106+
public void Assume_ConditionIsFalse_ThrowArgumentNullException()
107+
=> Contract.Assume(false, "The argument cannot be null");
108+
109+
/*==========================================================================================================================
110+
| TEST: ASSUME: OBJECT IS NULL: THROW INVALID OPERATION EXCEPTION
77111
\-------------------------------------------------------------------------------------------------------------------------*/
78112
/// <summary>
79113
/// Tests a null assignment using the <see cref="Contract"/> class, and validates that it correctly returns an <see
80114
/// cref="InvalidOperationException"/>.
81115
/// </summary>
82116
[TestMethod]
83117
[ExpectedException(typeof(InvalidOperationException))]
84-
public void AssumeInvalidOperationException() {
85-
86-
var variable = (object?)null;
87-
88-
Contract.Assume(variable, "The local runtime state is invalid.");
89-
90-
}
118+
public void Assume_ObjectIsNull_ThrowInvalidOperationException()
119+
=> Contract.Assume(null, "The local runtime state is invalid.");
91120

92121
} //Class
93122
} //Namespace

0 commit comments

Comments
 (0)