Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added Fail.IfNotNull(sth) method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 36da6ff commit e4de427
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Contracts/Synergy.Contracts.Test/Architecture/Public/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public async Task Generate()
// ASSERT
await Verifier.Verify(publicApi, "md")
.UseMethodName("of." + assembly.GetName().Name)
.UniqueForTargetFrameworkAndVersion();
.UniqueForTargetFrameworkAndVersion()
.AutoVerify();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@
name: string [NotNull, NotNull]
) : void [AssertionMethod]
- Fail.IfNotNull<T>(
value: T? [CanBeNull, NoEnumeration],
message: Violation
) : void [AssertionMethod, ContractAnnotation]
value: T [Nullable, CanBeNull, NoEnumeration],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod, ContractAnnotation]
- Fail.IfNotNull<T>(
value: T? [CanBeNull, NoEnumeration],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfNull<T>(
value: T [Nullable, CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@
name: string [NotNull, NotNull]
) : void [AssertionMethod]
- Fail.IfNotNull<T>(
value: T? [CanBeNull, NoEnumeration],
message: Violation
) : void [AssertionMethod, ContractAnnotation]
value: T [Nullable, CanBeNull, NoEnumeration],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod, ContractAnnotation]
- Fail.IfNotNull<T>(
value: T? [CanBeNull, NoEnumeration],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfNull<T>(
value: T [Nullable, CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@
name: string [NotNull, NotNull]
) : void [AssertionMethod]
- Fail.IfNotNull<T>(
value: T? [CanBeNull, NoEnumeration],
message: Violation
) : void [AssertionMethod, ContractAnnotation]
value: T [Nullable, CanBeNull, NoEnumeration],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod, ContractAnnotation]
- Fail.IfNotNull<T>(
value: T? [CanBeNull, NoEnumeration],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfNull<T>(
value: T [Nullable, CanBeNull, AssertionCondition],
Expand Down
14 changes: 14 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,26 @@ public void IfNotNull(object argumentValue)
Assert.That(exception.Message, Is.EqualTo("'argumentValue' is NOT null; and it should be;"));
}

[Test]
[TestCaseSource(nameof(FailNullabilityTest.GetNotNulls))]
public void IfNotNullCallerArgumentExpression(object argumentValue)
{
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
() => Fail.IfNotNull(argumentValue)
);

// ASSERT
Assert.That(exception.Message, Is.EqualTo("'argumentValue' is NOT null; and it should be;"));
}

[Test]
[TestCaseSource(nameof(FailNullabilityTest.GetNulls))]
public void IfNotNullSuccess([CanBeNull] object argumentValue)
{
// ACT
Fail.IfNotNull(argumentValue, nameof(argumentValue));
Fail.IfNotNull(argumentValue);
}

#endregion
Expand Down
9 changes: 8 additions & 1 deletion Contracts/Synergy.Contracts/Failures/FailNullability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,14 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T value,
/// <param name="name">Name of the checked argument / parameter.</param>
[AssertionMethod]
[ContractAnnotation("value: notnull => halt")]
public static void IfNotNull<T>([CanBeNull] [NoEnumeration] T value, [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name)
public static void IfNotNull<T>(
[CanBeNull] [NoEnumeration] T value,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null
#else
string name
#endif
)
{
Fail.RequiresArgumentName(name);
Fail.IfNotNull(value, Violation.WhenVariableIsNotNull(name));
Expand Down

0 comments on commit e4de427

Please sign in to comment.