Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added Fail.IfCollectionContainsNull(collectio…
Browse files Browse the repository at this point in the history
…n) method
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent fb11baf commit 817e4ab
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionContainsNull<T>(
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
collectionName: string [NotNull, NotNull]
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionEmpty(
collection: IEnumerable [CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionContainsNull<T>(
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
collectionName: string [NotNull, NotNull]
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionEmpty(
collection: IEnumerable [CanBeNull, AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionContainsNull<T>(
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
collectionName: string [NotNull, NotNull]
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionEmpty(
collection: IEnumerable [CanBeNull, AssertionCondition],
Expand Down
10 changes: 10 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,22 @@ public void IfCollectionContainsNull([CanBeNull] IEnumerable<object> collection)
() => Fail.IfCollectionContainsNull(collection, nameof(collection))
);
}

[Test]
[TestCaseSource(nameof(FailCollectionTest.GetCollectionsWithNull))]
public void IfCollectionContainsNullCallerArgumentExpression([CanBeNull] IEnumerable<object> collection)
{
Assert.Throws<DesignByContractViolationException>(
() => Fail.IfCollectionContainsNull(collection)
);
}

[Test]
[TestCaseSource(nameof(FailCollectionTest.GetCollectionsWithoutNull))]
public void IfCollectionContainsNullSuccess([CanBeNull] IEnumerable<object> collection)
{
Fail.IfCollectionContainsNull(collection, nameof(collection));
Fail.IfCollectionContainsNull(collection);
}

#endregion
Expand Down
8 changes: 6 additions & 2 deletions Contracts/Synergy.Contracts/Failures/FailCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ private static bool IsEmpty([NotNull] [System.Diagnostics.CodeAnalysis.NotNull]
public static void IfCollectionContainsNull<T>(
[CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)]
IEnumerable<T> collection,
[NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string collectionName
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("collection")] string? collectionName = null
#else
string collectionName
#endif
)
where T : class
{
Expand Down Expand Up @@ -221,7 +225,7 @@ Violation message
/// Checks if collection name was provided.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
private static void RequiresCollectionName([NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string collectionName)
private static void RequiresCollectionName(string collectionName)
{
if (string.IsNullOrWhiteSpace(collectionName))
throw new ArgumentNullException(nameof(collectionName));
Expand Down

0 comments on commit 817e4ab

Please sign in to comment.