Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added Fail.IfCollectionDoesNotContain(collect…
Browse files Browse the repository at this point in the history
…ion, predicate) method
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 5c6efcc commit 1e66e4d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Technical Debt for Synergy.Contracts

Total: 8
Total: 7

## [README.Generate.cs](../../Docs/README.Generate.cs)
- TODO: Marcin Celej [from: Marcin Celej on: 03-02-2024]: Prepare full description of Contract checks
Expand All @@ -17,5 +17,4 @@ Total: 8
- TODO:mace (from:mace @ 22-10-2016): variable.FailIfTrue(message)

## [FailCollection.cs](../../../Synergy.Contracts/Failures/FailCollection.cs)
- TODO:mace (from:mace @ 22-10-2016) public static void IfCollectionDoesNotContain<T>([CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] IEnumerable<T> collection,)
- TODO: Marcin Celej [from: Marcin Celej on: 29-05-2023]: Fail.IfCollectionContainsDuplicates
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionDoesNotContain<T>(
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
func: Func<T, bool> [NotNull, NotNull],
message: Violation
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionEmpty(
collection: IEnumerable [CanBeNull, AssertionCondition],
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionDoesNotContain<T>(
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
func: Func<T, bool> [NotNull, NotNull],
message: Violation
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionEmpty(
collection: IEnumerable [CanBeNull, AssertionCondition],
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionDoesNotContain<T>(
collection: IEnumerable<T> [CanBeNull, AssertionCondition],
func: Func<T, bool> [NotNull, NotNull],
message: Violation
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfCollectionEmpty(
collection: IEnumerable [CanBeNull, AssertionCondition],
collectionName: string? [Nullable, CallerArgumentExpression, Optional]
Expand Down
26 changes: 26 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,32 @@ public void IfCollectionContainsSuccess([NotNull] Tuple<IEnumerable<object>, obj

#endregion

#region Fail.IfCollectionDoesNotContain()

[Test]
[TestCaseSource(nameof(FailCollectionTest.GetCollectionsWithoutSuchElement))]
public void IfCollectionDoesNotContain(Tuple<IEnumerable<object>, object> pair)
{
var collection = pair.Item1;
var element = pair.Item2;

Assert.Throws<DesignByContractViolationException>(
() => Fail.IfCollectionDoesNotContain(collection, e => object.Equals(e, element), Violation.Of("this collection contains '{0}'", element))
);
}

[Test]
[TestCaseSource(nameof(FailCollectionTest.GetCollectionsWithSuchElement))]
public void IfCollectionDoesNotContainSuccess(Tuple<IEnumerable<object>, object> pair)
{
var collection = pair.Item1;
var element = pair.Item2;

Fail.IfCollectionDoesNotContain(collection, e => object.Equals(e,element), Violation.Of("this collection contains '{0}'", element));
}

#endregion

#region Fail.IfCollectionsAreNotEquivalent()

[Test]
Expand Down
35 changes: 30 additions & 5 deletions Contracts/Synergy.Contracts/Failures/FailCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Synergy.Contracts
{
static partial class Fail
{
// TODO:mace (from:mace @ 22-10-2016) public static void IfCollectionDoesNotContain<T>([CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] IEnumerable<T> collection,)
// TODO: Marcin Celej [from: Marcin Celej on: 29-05-2023]: Fail.IfCollectionContainsDuplicates

#region Fail.IfCollectionEmpty()
Expand Down Expand Up @@ -113,7 +112,7 @@ Violation message


[MustUseReturnValue]
private static bool IsEmpty([NotNull] [System.Diagnostics.CodeAnalysis.NotNull] this IEnumerable source)
private static bool IsEmpty(this IEnumerable source)
{
if (source is ICollection collection)
return collection.Count == 0;
Expand Down Expand Up @@ -161,7 +160,7 @@ string collectionName

/// <summary>
/// Throws the exception when collection contains element meeting specified criteria.
/// <para>REMARKS: The provided collection CANNOT by <see langword="null"/> as it will throw the exception.</para>
/// <para>REMARKS: The provided collection CANNOT be <see langword="null"/> as it will throw the exception.</para>
/// </summary>
/// <typeparam name="T">Type of the collection element.</typeparam>
/// <param name="collection">Collection to investigate whether contains specific element.</param>
Expand All @@ -177,8 +176,34 @@ Violation message
)
{
Fail.IfArgumentNull(collection, nameof(collection));
T element = collection.FirstOrDefault(func);
Fail.IfNotNull(element, message);
var found = collection.Any(func);
Fail.IfTrue(found, message);
}

#endregion

#region Fail.IfCollectionDoesNotContain()

/// <summary>
/// Throws the exception when collection does not contain element meeting specified criteria.
/// <para>REMARKS: The provided collection CANNOT be <see langword="null"/> as it will throw the exception.</para>
/// </summary>
/// <typeparam name="T">Type of the collection element.</typeparam>
/// <param name="collection">Collection to investigate whether contains specific element.</param>
/// <param name="func">Function with criteria that at least one element must meet.</param>
/// <param name="message">Message that will be passed to <see cref="DesignByContractViolationException"/> when the check fails.</param>
[AssertionMethod]
[ContractAnnotation("collection: null => halt")]
public static void IfCollectionDoesNotContain<T>(
[CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)]
IEnumerable<T> collection,
[NotNull] [System.Diagnostics.CodeAnalysis.NotNull] Func<T, bool> func,
Violation message
)
{
Fail.IfArgumentNull(collection, nameof(collection));
var found = collection.Any(func);
Fail.IfFalse(found, message);
}

#endregion
Expand Down

0 comments on commit 1e66e4d

Please sign in to comment.