Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Docs/pages/docs/expectations/07-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ await Expect.That(values).Contains([1, 1, 2, 2]).IgnoringDuplicates();
await Expect.That(values).Contains([3, 3, 1, 1]).InAnyOrder().IgnoringDuplicates();
```

*Note: You can also negate this expectation with `DoesNotContain`.*

*Note: The same expectation works also for `IAsyncEnumerable<T>`.*

To check for a proper subset, append `.Properly()` (which would fail for equal collections).
Expand All @@ -270,6 +272,8 @@ await Expect.That(values).IsContainedIn([1, 1, 2, 2, 3, 3, 4, 4]).IgnoringDuplic
await Expect.That(values).IsContainedIn([4, 4, 3, 3, 2, 2, 1, 1]).InAnyOrder().IgnoringDuplicates();
```

*Note: You can also negate this expectation with `IsNotContainedIn`.*

*Note: The same expectation works also for `IAsyncEnumerable<T>`.*

To check for a proper superset, append `.Properly()` (which would fail for equal collections).
Expand Down
46 changes: 46 additions & 0 deletions Source/aweXpect/That/Collections/ThatAsyncEnumerable.Contains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,52 @@ public static CountResult<IAsyncEnumerable<TItem>, IThat<IAsyncEnumerable<TItem>
quantifier);
}

/// <summary>
/// Verifies that the collection does not contain the provided <paramref name="expected" /> collection.
/// </summary>
public static ObjectCollectionContainResult<IAsyncEnumerable<TItem>, IThat<IAsyncEnumerable<TItem>?>, TItem>
DoesNotContain<TItem>(
this IThat<IAsyncEnumerable<TItem>?> source,
IEnumerable<TItem> expected,
[CallerArgumentExpression("expected")] string doNotPopulateThisValue = "")
{
ObjectEqualityOptions<TItem> options = new();
CollectionMatchOptions matchOptions = new(CollectionMatchOptions.EquivalenceRelations.Contains);
ExpectationBuilder expectationBuilder = source.Get().ExpectationBuilder;
return new ObjectCollectionContainResult<IAsyncEnumerable<TItem>, IThat<IAsyncEnumerable<TItem>?>, TItem>(
expectationBuilder.AddConstraint((it, grammars) =>
new IsEqualToConstraint<TItem, TItem>(
expectationBuilder, it, grammars,
doNotPopulateThisValue.TrimCommonWhiteSpace(), expected,
options, matchOptions).Invert()),
source,
options,
matchOptions);
}

/// <summary>
/// Verifies that the collection does not contain the provided <paramref name="expected" /> collection.
/// </summary>
public static StringCollectionContainResult<IAsyncEnumerable<string?>, IThat<IAsyncEnumerable<string?>?>>
DoesNotContain(
this IThat<IAsyncEnumerable<string?>?> source,
IEnumerable<string?> expected,
[CallerArgumentExpression("expected")] string doNotPopulateThisValue = "")
{
StringEqualityOptions options = new();
CollectionMatchOptions matchOptions = new(CollectionMatchOptions.EquivalenceRelations.Contains);
ExpectationBuilder expectationBuilder = source.Get().ExpectationBuilder;
return new StringCollectionContainResult<IAsyncEnumerable<string?>, IThat<IAsyncEnumerable<string?>?>>(
expectationBuilder.AddConstraint((it, grammars) =>
new IsEqualToConstraint<string?, string?>(
expectationBuilder, it, grammars,
doNotPopulateThisValue.TrimCommonWhiteSpace(),
expected, options, matchOptions).Invert()),
source,
options,
matchOptions);
}

private sealed class ContainConstraint<TItem>(
ExpectationBuilder expectationBuilder,
string it,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using aweXpect.Core;
using aweXpect.Core.Constraints;
using aweXpect.Helpers;
using aweXpect.Options;
using aweXpect.Results;
Expand Down Expand Up @@ -60,5 +61,56 @@ public static partial class ThatAsyncEnumerable
options,
matchOptions);
}

/// <summary>
/// Verifies that the collection is not contained in the provided <paramref name="expected" /> collection.
/// </summary>
public static ObjectCollectionBeContainedInResult<IAsyncEnumerable<TItem>, IThat<IAsyncEnumerable<TItem>?>, TItem>
IsNotContainedIn<TItem>(
this IThat<IAsyncEnumerable<TItem>?> source,
IEnumerable<TItem> expected,
[CallerArgumentExpression("expected")] string doNotPopulateThisValue = "")
{
ObjectEqualityOptions<TItem> options = new();
CollectionMatchOptions matchOptions = new(CollectionMatchOptions.EquivalenceRelations.IsContainedIn);
ExpectationBuilder expectationBuilder = source.Get().ExpectationBuilder;
return new
ObjectCollectionBeContainedInResult<IAsyncEnumerable<TItem>, IThat<IAsyncEnumerable<TItem>?>, TItem>(
expectationBuilder.AddConstraint((it, grammars) =>
new IsEqualToConstraint<TItem, TItem>(expectationBuilder, it, grammars,
doNotPopulateThisValue.TrimCommonWhiteSpace(),
expected,
options,
matchOptions).Invert()),
source,
options,
matchOptions);
}

/// <summary>
/// Verifies that the collection is not contained in the provided <paramref name="expected" /> collection.
/// </summary>
public static StringCollectionBeContainedInResult<IAsyncEnumerable<string?>,
IThat<IAsyncEnumerable<string?>?>>
IsNotContainedIn(
this IThat<IAsyncEnumerable<string?>?> source,
IEnumerable<string?> expected,
[CallerArgumentExpression("expected")] string doNotPopulateThisValue = "")
{
StringEqualityOptions options = new();
CollectionMatchOptions matchOptions = new(CollectionMatchOptions.EquivalenceRelations.IsContainedIn);
ExpectationBuilder expectationBuilder = source.Get().ExpectationBuilder;
return new StringCollectionBeContainedInResult<IAsyncEnumerable<string?>,
IThat<IAsyncEnumerable<string?>?>>(
expectationBuilder.AddConstraint((it, grammars) =>
new IsEqualToConstraint<string?, string?>(expectationBuilder, it, grammars,
doNotPopulateThisValue.TrimCommonWhiteSpace(),
expected,
options,
matchOptions).Invert()),
source,
options,
matchOptions);
}
}
#endif
13 changes: 10 additions & 3 deletions Source/aweXpect/That/Collections/ThatAsyncEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ public async Task<ConstraintResult> IsMetBy(IAsyncEnumerable<TItem>? actual, IEv
_items = [];
}

expectationBuilder.UpdateContexts(contexts => contexts
.Add(new ResultContext("Expected",
() => Formatter.Format(expected, typeof(TItem).GetFormattingOption(expected switch
{
ICollection<TItem> coll => coll.Count,
ICountable countable => countable.Count,
_ => null,
})),
-2)));
await foreach (TItem item in materializedEnumerable.WithCancellation(cancellationToken))
{
if (_items?.Count < maximumNumber + 1)
Expand Down Expand Up @@ -381,9 +390,7 @@ private string TooManyDeviationsError()
sb.Append(It);
sb.Append(" had more than ");
sb.Append(2 * maximumNumberOfCollectionItems);
sb.Append(" deviations compared to ");
ValueFormatters.Format(Formatter, sb, expected?.Take(maximumNumberOfCollectionItems + 1),
FormattingOptions.MultipleLines);
sb.Append(" deviations");
return sb.ToString();
}

Expand Down
43 changes: 43 additions & 0 deletions Source/aweXpect/That/Collections/ThatEnumerable.Contains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,49 @@ public static CountResult<IEnumerable<TItem>, IThat<IEnumerable<TItem>?>>
quantifier);
}

/// <summary>
/// Verifies that the collection does not contain the provided <paramref name="expected" /> collection.
/// </summary>
public static ObjectCollectionContainResult<IEnumerable<TItem>, IThat<IEnumerable<TItem>?>, TItem>
DoesNotContain<TItem>(
this IThat<IEnumerable<TItem>?> source,
IEnumerable<TItem> expected,
[CallerArgumentExpression("expected")] string doNotPopulateThisValue = "")
{
ObjectEqualityOptions<TItem> options = new();
CollectionMatchOptions matchOptions = new(CollectionMatchOptions.EquivalenceRelations.Contains);
ExpectationBuilder expectationBuilder = source.Get().ExpectationBuilder;
return new ObjectCollectionContainResult<IEnumerable<TItem>, IThat<IEnumerable<TItem>?>, TItem>(
expectationBuilder.AddConstraint((it, grammars) =>
new IsEqualToConstraint<TItem, TItem>(expectationBuilder, it, grammars,
doNotPopulateThisValue.TrimCommonWhiteSpace(),
expected,
options, matchOptions).Invert()),
source,
options,
matchOptions);
}

/// <summary>
/// Verifies that the collection does not contain the provided <paramref name="expected" /> collection.
/// </summary>
public static StringCollectionContainResult<IEnumerable<string?>, IThat<IEnumerable<string?>?>>
DoesNotContain(this IThat<IEnumerable<string?>?> source,
IEnumerable<string?> expected,
[CallerArgumentExpression("expected")] string doNotPopulateThisValue = "")
{
StringEqualityOptions options = new();
CollectionMatchOptions matchOptions = new(CollectionMatchOptions.EquivalenceRelations.Contains);
ExpectationBuilder expectationBuilder = source.Get().ExpectationBuilder;
return new StringCollectionContainResult<IEnumerable<string?>, IThat<IEnumerable<string?>?>>(
expectationBuilder.AddConstraint((it, grammars) =>
new IsEqualToConstraint<string?, string?>(expectationBuilder, it, grammars, doNotPopulateThisValue.TrimCommonWhiteSpace(),
expected, options, matchOptions).Invert()),
source,
options,
matchOptions);
}

private sealed class ContainConstraint<TItem>(
ExpectationBuilder expectationBuilder,
string it,
Expand Down
47 changes: 47 additions & 0 deletions Source/aweXpect/That/Collections/ThatEnumerable.IsContainedIn.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using aweXpect.Core;
using aweXpect.Core.Constraints;
using aweXpect.Helpers;
using aweXpect.Options;
using aweXpect.Results;
Expand Down Expand Up @@ -55,4 +56,50 @@ public static partial class ThatEnumerable
options,
matchOptions);
}
/// <summary>
/// Verifies that the collection is not contained in the provided <paramref name="expected" /> collection.
/// </summary>
public static ObjectCollectionBeContainedInResult<IEnumerable<TItem>, IThat<IEnumerable<TItem>?>, TItem>
IsNotContainedIn<TItem>(
this IThat<IEnumerable<TItem>?> source,
IEnumerable<TItem> expected,
[CallerArgumentExpression("expected")] string doNotPopulateThisValue = "")
{
ObjectEqualityOptions<TItem> options = new();
CollectionMatchOptions matchOptions = new(CollectionMatchOptions.EquivalenceRelations.IsContainedIn);
ExpectationBuilder expectationBuilder = source.Get().ExpectationBuilder;
return new ObjectCollectionBeContainedInResult<IEnumerable<TItem>, IThat<IEnumerable<TItem>?>, TItem>(
expectationBuilder.AddConstraint((it, grammars) =>
new IsEqualToConstraint<TItem, TItem>(expectationBuilder, it, grammars,
doNotPopulateThisValue.TrimCommonWhiteSpace(),
expected,
options,
matchOptions).Invert()),
source,
options,
matchOptions);
}

/// <summary>
/// Verifies that the collection is not contained in the provided <paramref name="expected" /> collection.
/// </summary>
public static StringCollectionBeContainedInResult<IEnumerable<string?>, IThat<IEnumerable<string?>?>>
IsNotContainedIn(this IThat<IEnumerable<string?>?> source,
IEnumerable<string?> expected,
[CallerArgumentExpression("expected")] string doNotPopulateThisValue = "")
{
StringEqualityOptions options = new();
CollectionMatchOptions matchOptions = new(CollectionMatchOptions.EquivalenceRelations.IsContainedIn);
ExpectationBuilder expectationBuilder = source.Get().ExpectationBuilder;
return new StringCollectionBeContainedInResult<IEnumerable<string?>, IThat<IEnumerable<string?>?>>(
expectationBuilder.AddConstraint((it, grammars) =>
new IsEqualToConstraint<string?, string?>(expectationBuilder, it, grammars,
doNotPopulateThisValue.TrimCommonWhiteSpace(),
expected,
options,
matchOptions).Invert()),
source,
options,
matchOptions);
}
}
15 changes: 12 additions & 3 deletions Source/aweXpect/That/Collections/ThatEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public ConstraintResult IsMetBy(IEnumerable<TItem>? actual, IEvaluationContext c
return this;
}

expectationBuilder.UpdateContexts(contexts => contexts
.Add(new ResultContext("Expected",
() => Formatter.Format(expected, typeof(TItem).GetFormattingOption(expected switch
{
ICollection<TItem> coll => coll.Count,
ICountable countable => countable.Count,
_ => null,
})),
-2)));
Comment thread
vbreuss marked this conversation as resolved.
IEnumerable<TItem> materializedEnumerable =
context.UseMaterializedEnumerable<TItem, IEnumerable<TItem>>(actual);
ICollectionMatcher<TItem, TMatch> matcher = matchOptions.GetCollectionMatcher<TItem, TMatch>(expected);
Expand All @@ -65,12 +74,13 @@ public ConstraintResult IsMetBy(IEnumerable<TItem>? actual, IEvaluationContext c
return this;
}

expectationBuilder.AddCollectionContext(materializedEnumerable);
Outcome = Outcome.Success;
return this;
}

private string TooManyDeviationsError()
=> $"{It} had more than {2 * Customize.aweXpect.Formatting().MaximumNumberOfCollectionItems.Get()} deviations compared to {Formatter.Format(expected, FormattingOptions.MultipleLines)}";
=> $"{It} had more than {2 * Customize.aweXpect.Formatting().MaximumNumberOfCollectionItems.Get()} deviations";

protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null)
{
Expand Down Expand Up @@ -101,8 +111,7 @@ protected override void AppendNegatedResult(StringBuilder stringBuilder, string?
}
else
{
stringBuilder.Append(It).Append(" did in ");
Formatter.Format(stringBuilder, Actual, FormattingOptions.MultipleLines);
stringBuilder.Append(It).Append(" did");
}
}
}
Expand Down
Loading
Loading