Skip to content

Commit 018ec2c

Browse files
authored
Merge pull request #119 - Expanding RequiresNotNull args
Expanding RequiresNotNull args
2 parents 099d299 + f89065c commit 018ec2c

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

DesignContracts/Core/Contract.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,26 @@ public static class Contract
2424
/// Specifies a precondition that must hold true when the enclosing method is called.
2525
/// </summary>
2626
/// <param name="precondition">The precondition that is required to be <c>true</c>.</param>
27-
/// <param name="conditionDescription">An optional English description of what the precondition is.</param>
28-
/// <param name="conditionCode">An optional source code representation of the condition expression.</param>
27+
/// <param name="userMessage">Optional English description of what the precondition is.</param>
28+
/// <param name="conditionText">Optional pseudo-code representation of the condition expression.</param>
2929
/// <exception cref="ContractException">
3030
/// Thrown when <paramref name="precondition"/> is <c>false</c>.
3131
/// </exception>
32-
public static void Requires(bool precondition, string? conditionDescription = null, string? conditionCode = null)
32+
public static void Requires(bool precondition, string? userMessage = null, string? conditionText = null)
3333
{
34-
if (!precondition) ReportFailure(ContractFailureKind.Precondition, conditionDescription, conditionCode);
34+
if (!precondition) ReportFailure(ContractFailureKind.Precondition, userMessage, conditionText);
3535
}
3636

3737
/// <summary>
38-
/// Argument not null precondition.
38+
/// Requires that argument be not null. If it is, raises an ArgumentNullException.
3939
/// </summary>
4040
/// <param name="argument"></param>
41-
public static void RequiresNotNull(object? argument)
41+
/// <param name="userMessage">Defaults to 'Argument must not be null'</param>
42+
/// <param name="conditionText">Optional pseudo-code representation of the not null expression.</param>
43+
public static void RequiresNotNull(object? argument, string? userMessage = "Argument must not be null"
44+
, string? conditionText = null)
4245
{
43-
Requires<ArgumentNullException>(argument != null, "Argument should not be null");
46+
Requires<ArgumentNullException>(argument != null, userMessage, conditionText);
4447
}
4548

4649
/// <summary>
@@ -52,20 +55,22 @@ public static void RequiresNotNull(object? argument)
5255
/// The type must have a public constructor that accepts a single <see cref="string"/> parameter.
5356
/// </typeparam>
5457
/// <param name="precondition">The condition that must be <c>true</c>.</param>
55-
/// <param name="conditionDescription">An optional message describing the precondition.</param>
58+
/// <param name="userMessage">Optional user readable message describing the precondition.</param>
59+
/// <param name="conditionText">Optional user readable message describing the precondition.</param>
5660
/// <exception cref="ContractException">
5761
/// Thrown when the specified exception type cannot be constructed.
5862
/// </exception>
5963
/// <exception cref="Exception">
6064
/// An instance of <typeparamref name="TException"/> when <paramref name="precondition"/> is <c>false</c>.
6165
/// </exception>
62-
public static void Requires<TException>(bool precondition, string? conditionDescription = null)
66+
public static void Requires<TException>(bool precondition, string? userMessage = null,
67+
string? conditionText = null)
6368
where TException : Exception
6469
{
6570
if (precondition) return;
6671

6772
// Try to honor the requested exception type first.
68-
string message = BuildFailureMessage(ContractFailureKind.Precondition, conditionDescription, conditionText: null);
73+
string message = BuildFailureMessage(ContractFailureKind.Precondition, userMessage, conditionText);
6974

7075
Exception? exception = null;
7176
try
@@ -83,7 +88,7 @@ public static void Requires<TException>(bool precondition, string? conditionDesc
8388
}
8489

8590
// Fall back to standard handling if we cannot construct TException.
86-
ReportFailure(ContractFailureKind.Precondition, conditionDescription, conditionText: null);
91+
ReportFailure(ContractFailureKind.Precondition, userMessage, conditionText: null);
8792
}
8893

8994

DesignContracts/Tests/ContractTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public sealed class ContractTests
1818
[TestCase(null, "", "Precondition failed.")]
1919
[TestCase(null, " ", "Precondition failed.")]
2020
[TestCase(null, "(arg==0)", "Precondition failed: (arg==0)")]
21-
public void Requires_throws_exception_with_correct_message_on_precondition_failure(string conditionDescription, string? conditionCode, string expectedExceptionMessage)
21+
public void Requires_throws_exception_with_correct_message_on_precondition_failure(string conditionDescription, string? conditionText, string expectedExceptionMessage)
2222
{
23-
ContractException? ex = Assert.Throws<ContractException>(() => Contract.Requires(false, conditionDescription,conditionCode));
23+
ContractException? ex = Assert.Throws<ContractException>(() => Contract.Requires(false, conditionDescription,conditionText));
2424
Assert.That(ex, Is.Not.Null);
2525
Assert.That(ex!.Message, Is.EqualTo(expectedExceptionMessage), "Exception message is incorrect");
2626
}

0 commit comments

Comments
 (0)