-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContractTest.cs
166 lines (148 loc) · 8.91 KB
/
ContractTest.cs
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
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/
using Xunit;
namespace OnTopic.Tests {
/*============================================================================================================================
| CLASS: CONTRACT TEST
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Provides unit tests for the <see cref="Contract"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ContractTest {
/*==========================================================================================================================
| TEST: REQUIRES: CONDITION IS TRUE: THROW NO EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a true condition using the <see cref="Contract"/> class, and validates that it correctly does nothing.
/// </summary>
[Fact]
public void Requires_ConditionIsTrue_ThrowNoException()
=> Contract.Requires(true, "The argument cannot be null");
/*==========================================================================================================================
| TEST: REQUIRES: CONDITION IS FALSE: THROW ARGUMENT NULL EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a false condition using the <see cref="Contract"/> class, and validates that it correctly returns an <see
/// cref="ArgumentNullException"/>.
/// </summary>
[Fact]
public void Requires_ConditionIsFalse_ThrowArgumentNullException() =>
Assert.Throws<InvalidOperationException>(() =>
Contract.Requires(false, "The argument cannot be null")
);
/*==========================================================================================================================
| TEST: REQUIRES: OBJECT EXISTS: THROW NO EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a non-null argument using the <see cref="Contract"/> class, and validates that it correctly does nothing.
/// </summary>
[Fact]
public void Requires_ObjectExists_ThrowNoException() =>
Contract.Requires(new object(), "The argument cannot be null");
/*==========================================================================================================================
| TEST: REQUIRES: OBJECT IS NULL: THROW ARGUMENT NULL EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a null argument using the <see cref="Contract"/> class, and validates that it correctly returns an <see
/// cref="ArgumentNullException"/>.
/// </summary>
[Fact]
public void Requires_ObjectIsNull_ThrowArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() =>
Contract.Requires(null, "The argument cannot be null")
);
/*==========================================================================================================================
| TEST: REQUIRES: MESSAGE EXISTS: THROW EXCEPTION WITH MESSAGE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a null argument using the <see cref="Contract"/> class, and validates that it correctly returns an <see
/// cref="ArgumentNullException"/> with the expected <see cref="ArgumentException.Message"/>.
/// </summary>
[Fact]
public void Requires_MessageExists_ThrowExceptionWithMessage() {
var errorMessage = "The argument cannot be null";
try {
Contract.Requires<ArgumentException>(false, errorMessage);
}
catch (ArgumentException ex) {
Assert.Equal(errorMessage, ex.Message);
}
}
/*==========================================================================================================================
| TEST: REQUIRES: INVALID CONSTRUCTOR: THROW ARGUMENT EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a null argument using the <see cref="Contract"/> class, and attempts to throw a custom <see cref="
/// NoMessageException"/> with the expected <see cref="ArgumentException.Message"/>, but fails due to no overload with
/// a single <c>message</c> parameter. In this case, it should throw a <see cref="ArgumentException"/>.
/// </summary>
[Fact]
public void Requires_InvalidConstructor_ThrowArgumentException() {
var errorMessage = "The argument cannot be null";
Assert.Throws<ArgumentException>(() =>
Contract.Requires<NoMessageException>(false, errorMessage)
);
}
/*==========================================================================================================================
| TEST: ASSUME: CONDITION IS TRUE: THROW NO EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a true condition using the <see cref="Contract"/> class, and validates that it correctly does nothing.
/// </summary>
[Fact]
public void Assume_ConditionIsTrue_ThrowNoException()
=> Contract.Assume(true, "The argument cannot be null");
/*==========================================================================================================================
| TEST: ASSUME: CONDITION IS FALSE: THROW ARGUMENT NULL EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a false condition using the <see cref="Contract"/> class, and validates that it correctly returns an <see
/// cref="ArgumentNullException"/>.
/// </summary>
[Fact]
public void Assume_ConditionIsFalse_ThrowArgumentNullException() =>
Assert.Throws<InvalidOperationException>(() =>
Contract.Assume(false, "The argument cannot be null")
);
/*==========================================================================================================================
| TEST: ASSUME: CONDITION IS FALSE: THROW CUSTOM EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a false condition using the <see cref="Contract"/> class, and validates that it correctly throws the specified
/// <see cref="IndexOutOfRangeException"/>.
/// </summary>
[Fact]
public void Assume_ConditionIsFalse_ThrowCustomExpection() =>
Assert.Throws<IndexOutOfRangeException>(() =>
Contract.Assume<IndexOutOfRangeException>(false, "The argument is out of range")
);
/*==========================================================================================================================
| TEST: ASSUME: CONDITION IS FALSE: THROW CUSTOM EXCEPTION WITHOUT MESSAGE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a false condition using the <see cref="Contract"/> class, and validates that it correctly throws the specified
/// <see cref="IndexOutOfRangeException"/> using the empty constructor, since no <c>errorMessage</c> is included.
/// </summary>
[Fact]
public void Assume_ConditionIsFalse_ThrowCustomExpectionWithoutMessage() =>
Assert.Throws<IndexOutOfRangeException>(() =>
Contract.Assume<IndexOutOfRangeException>(false)
);
/*==========================================================================================================================
| TEST: ASSUME: OBJECT IS NULL: THROW INVALID OPERATION EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Tests a null assignment using the <see cref="Contract"/> class, and validates that it correctly returns an <see
/// cref="InvalidOperationException"/>.
/// </summary>
[Fact]
public void Assume_ObjectIsNull_ThrowInvalidOperationException() =>
Assert.Throws<InvalidOperationException>(() =>
Contract.Assume(null, "The local runtime state is invalid.")
);
} //Class
} //Namespace