Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added sth.FailIfNull() extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 9524ef5 commit 3acff68
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfNull<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
name: string [NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.IfArgumentEmpty(
argumentValue: string [CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfNull<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
name: string [NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.IfArgumentEmpty(
argumentValue: string [CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfNull<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
name: string [NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.IfArgumentEmpty(
argumentValue: string [CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public string GetName()
[NotNull]
public string GetCity()
{
return this.Address.FailIfNull(nameof(this.Address))
.City.FailIfNull(nameof(this.Address.City));
return this.Address.FailIfNull()
.City.FailIfNull();
}

public void SetPersonName([NotNull] string firstName, [NotNull] string lastName)
Expand Down
17 changes: 16 additions & 1 deletion Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ public void FailIfNull(object someNullObject)
Assert.That(exception.Message, Is.EqualTo("'someNullObject' is null; and it shouldn't be;"));
}

[Test]
[TestCaseSource(nameof(FailNullabilityTest.GetNulls))]
public void FailIfNullCallerArgumentExpression(object someNullObject)
{
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
// ReSharper disable once ExpressionIsAlwaysNull
() => someNullObject.FailIfNull()
);

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

[Test]
[TestCaseSource(nameof(FailNullabilityTest.GetNulls))]
public void FailIfNullWithViolationMessage(object someNullObject)
Expand All @@ -46,6 +60,7 @@ public void FailIfNullSuccess(object thisIsNotNull)
{
// ACT
thisIsNotNull.FailIfNull(nameof(thisIsNotNull));
thisIsNotNull.FailIfNull();
}

[Test]
Expand Down Expand Up @@ -92,7 +107,7 @@ public void OrFail(object thisMustBeNull)

[Test]
[TestCaseSource(nameof(FailNullabilityTest.GetNulls))]
public void OrFailNetCore(object thisMustBeNull)
public void OrFailCallerArgumentExpression(object thisMustBeNull)
{
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
Expand Down
7 changes: 6 additions & 1 deletion Contracts/Synergy.Contracts/Failures/FailNullability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ static partial class Fail
public static T FailIfNull<T>(
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumeration]
this T value,
[NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name)
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null
#else
string name
#endif
)
{
Fail.RequiresArgumentName(name);
return value.FailIfNull(Violation.WhenVariableIsNull(name));
Expand Down

0 comments on commit 3acff68

Please sign in to comment.