Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added nullable.OrFail() extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 3acff68 commit 0d1e66a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T?,
name: string [Nullable, NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T [NullableContext, Extension, ContractAnnotation]
- Fail.OrFailIfCollectionEmpty<T>(
collection: T [CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T?,
name: string [Nullable, NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T [NullableContext, Extension, ContractAnnotation]
- Fail.OrFailIfCollectionEmpty<T>(
collection: T [CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T?,
name: string [Nullable, NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T [NullableContext, Extension, ContractAnnotation]
- Fail.OrFailIfCollectionEmpty<T>(
collection: T [CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public List<Contractor> FilterContractors(ContractorFilterParameters paramaters)
{
paramaters.OrFail();
paramaters.EstablishedBetween.FailIfNull(Violation.Of("'{0}' is null and it shouldn't be", nameof(paramaters.EstablishedBetween)));
paramaters.EstablishedBetween.Max.OrFail(nameof(paramaters.EstablishedBetween.Max));
paramaters.EstablishedBetween.Max.OrFail();

if (paramaters.EstablishedBetween == null)
{
Expand Down
16 changes: 16 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ public void NullableOrFail()
// ASSERT
Assert.That(exception.Message, Is.EqualTo("'thisMustBeNull' is null; and it shouldn't be;"));
}

[Test]
public void NullableOrFailCallerArgumentExpression()
{
// ARRANGE
int? thisMustBeNull = null;

// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
// ReSharper disable once ExpressionIsAlwaysNull
() => thisMustBeNull.OrFail());

// ASSERT
Assert.That(exception.Message, Is.EqualTo("'thisMustBeNull' is null; and it shouldn't be;"));
}

[Test]
public void NullableOrFailSuccess()
Expand All @@ -154,6 +169,7 @@ public void NullableOrFailSuccess()

// ACT
thisCannotBeNull.OrFail(nameof(thisCannotBeNull));
thisCannotBeNull.OrFail();
}

#endregion
Expand Down
24 changes: 17 additions & 7 deletions Contracts/Synergy.Contracts/Failures/FailNullability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ static partial class Fail
/// <typeparam name="T">Type of the value to check against nullability.</typeparam>
/// <param name="value">Value to check against nullability.</param>
/// <param name="name">Name of the checked argument / parameter.</param>
[NotNull] [return: System.Diagnostics.CodeAnalysis.NotNull]
[NotNull]
[return: System.Diagnostics.CodeAnalysis.NotNull]
[AssertionMethod]
[ContractAnnotation("value: null => halt; value: notnull => notnull")]
public static T FailIfNull<T>(
Expand All @@ -25,7 +26,7 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumerat
#else
string name
#endif
)
)
{
Fail.RequiresArgumentName(name);
return value.FailIfNull(Violation.WhenVariableIsNull(name));
Expand All @@ -37,13 +38,15 @@ string name
/// <typeparam name="T">Type of the value to check against nullability.</typeparam>
/// <param name="value">Value to check against nullability.</param>
/// <param name="message">Message that will be passed to <see cref="DesignByContractViolationException"/> when the check fails.</param>
[NotNull] [return: System.Diagnostics.CodeAnalysis.NotNull]
[NotNull]
[return: System.Diagnostics.CodeAnalysis.NotNull]
[AssertionMethod]
[ContractAnnotation("value: null => halt; value: notnull => notnull")]
public static T FailIfNull<T>(
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumeration]
this T value,
Violation message)
Violation message
)
{
if (value == null)
throw Fail.Because(message);
Expand Down Expand Up @@ -90,16 +93,23 @@ string name
/// <returns>Value (not null) of the passed argument / parameter</returns>
/// <exception cref="DesignByContractViolationException"></exception>
[ContractAnnotation("value: null => halt")]
public static T OrFail<T>(this T? value, [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name) where T : struct
public static T OrFail<T>(
this T? value,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null
#else
string name
#endif
) where T : struct
{
Fail.RequiresArgumentName(name);

if (value == null)
throw Fail.Because(Violation.WhenVariableIsNull(name));

return value.Value;
}

/// <summary>
/// Throws exception when provided value is <see langword="null"/>.
/// </summary>
Expand Down

0 comments on commit 0d1e66a

Please sign in to comment.