Skip to content

Commit f5d6428

Browse files
committed
Corrected bug with Contract.Assume(false)
Previously, `Contract.Assume(false)` was _always_ evaluating as _true_, and thus not returning an exception. This is because while there was an overload for `Contract.Assume<T>(bool)` there was not one for `Contract.Assume(bool)`, and so it was getting handled by the `Contract.Assume(object)` overload. And since `false` is not a null object, it wasn't throwing an exceptions. Whoops! A win for expanded test coverage (see #f5cbd39).
1 parent f5cbd39 commit f5d6428

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

OnTopic/Internal/Diagnostics/Contract.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,28 @@ ex is MissingMethodException
110110
/*==========================================================================================================================
111111
| METHOD: ASSUME
112112
\-------------------------------------------------------------------------------------------------------------------------*/
113+
/// <summary>
114+
/// Ensures that a condition is met. If not, an <see cref="Exception"/> is thrown.
115+
/// </summary>
116+
/// <remarks>
117+
/// This is virtually identical to <see cref="Requires(Boolean, String)"/> except that, syntactically, it is expected to
118+
/// live within the body of a method—where as <see cref="Requires(Boolean, String)"/> is expected to live at the beginning
119+
/// of a method. This communicates to readers that <see cref="Assume(Boolean, String)"/> is validating runtime state,
120+
/// whereas <see cref="Requires(Boolean, String)"/> is validating preconditions.
121+
/// </remarks>
122+
/// <param name="isValid">An expression resulting in a boolean value indicating if an exception should be thrown.</param>
123+
/// <param name="errorMessage">Optionally provides an error message in case an exception is thrown.</param>
124+
/// <exception cref="Exception">
125+
/// Thrown when <paramref name="isValid"/> returns <see langword="true"/>.
126+
/// </exception>
127+
public static void Assume(bool isValid, string? errorMessage = null) =>
128+
Requires<Exception>(isValid, errorMessage);
129+
113130
/// <summary>
114131
/// Ensures that a condition is met. If not, the provided exception is thrown.
115132
/// </summary>
116133
/// <remarks>
117-
/// Unlike the standard <c>Assumes()</c> method that ships with .NET, this custom oerload accepts a generic <typeparamref
134+
/// Unlike the standard <c>Assumes()</c> method that ships with .NET, this custom overload accepts a generic <typeparamref
118135
/// name="T"/>, of type <see cref="Exception"/>, which will be thrown if the condition is not met. This is virtually
119136
/// identical to <see cref="Requires{T}(Boolean, String)"/> except that, syntactically, it is expected to live within the
120137
/// body of a method—where as <see cref="Requires{T}(Boolean, String)"/> is expected to live at the beginning of a method.

0 commit comments

Comments
 (0)