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
98 changes: 64 additions & 34 deletions Source/aweXpect/That/Collections/CollectionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ internal static ExpectationBuilder AddCollectionContext<TItem>(this ExpectationB
}

return expectationBuilder.UpdateContexts(contexts
=> contexts
.Add(new ResultContext("Collection",
() => Formatter.Format(value, typeof(TItem).GetFormattingOption(value switch
{
ICollection<TItem> coll => coll.Count,
ICountable countable => countable.Count,
_ => null,
})).AppendIsIncomplete(isIncomplete),
-1)));
=>
{
if (contexts.All(c => c.Title != "Collection"))
{
contexts
.Add(new ResultContext("Collection",
() => Formatter.Format(value, typeof(TItem).GetFormattingOption(value switch
{
ICollection<TItem> coll => coll.Count,
ICountable countable => countable.Count,
_ => null,
})).AppendIsIncomplete(isIncomplete),
-1));
}
});
}

internal static ExpectationBuilder AddCollectionContext(this ExpectationBuilder expectationBuilder,
Expand All @@ -77,15 +83,21 @@ internal static ExpectationBuilder AddCollectionContext(this ExpectationBuilder
}

return expectationBuilder.UpdateContexts(contexts
=> contexts
.Add(new ResultContext("Collection",
() => Formatter.Format(value, type.GetFormattingOption(value switch
{
ICollection coll => coll.Count,
ICountable countable => countable.Count,
_ => null,
})).AppendIsIncomplete(isIncomplete),
-1)));
=>
{
if (contexts.All(c => c.Title != "Collection"))
{
contexts
.Add(new ResultContext("Collection",
() => Formatter.Format(value, type.GetFormattingOption(value switch
{
ICollection coll => coll.Count,
ICountable countable => countable.Count,
_ => null,
})).AppendIsIncomplete(isIncomplete),
-1));
}
});
}

#if NET8_0_OR_GREATER
Expand All @@ -98,12 +110,18 @@ internal static ExpectationBuilder AddCollectionContext<TItem>(this ExpectationB
}

return expectationBuilder.UpdateContexts(contexts
=> contexts
.Add(new ResultContext("Collection",
() => Formatter.Format(value.MaterializedItems,
typeof(TItem).GetFormattingOption(value.Count))
.AppendIsIncomplete(isIncomplete),
-1)));
=>
{
if (contexts.All(c => c.Title != "Collection"))
{
contexts
.Add(new ResultContext("Collection",
() => Formatter.Format(value.MaterializedItems,
typeof(TItem).GetFormattingOption(value.Count))
.AppendIsIncomplete(isIncomplete),
-1));
}
});
}
#endif

Expand All @@ -116,11 +134,17 @@ internal static ExpectationBuilder AddCollectionContext<TKey, TValue>(this Expec
}

return expectationBuilder.UpdateContexts(contexts
=> contexts
.Add(new ResultContext("Dictionary",
() => Formatter.Format(value, typeof(TValue).GetFormattingOption(value.Count))
.AppendIsIncomplete(isIncomplete),
-2)));
=>
{
if (contexts.All(c => c.Title != "Dictionary"))
{
contexts
.Add(new ResultContext("Dictionary",
() => Formatter.Format(value, typeof(TValue).GetFormattingOption(value.Count))
.AppendIsIncomplete(isIncomplete),
-2));
}
});
}

internal static ExpectationBuilder AddCollectionContext<TKey, TValue>(this ExpectationBuilder expectationBuilder,
Expand All @@ -132,11 +156,17 @@ internal static ExpectationBuilder AddCollectionContext<TKey, TValue>(this Expec
}

return expectationBuilder.UpdateContexts(contexts
=> contexts
.Add(new ResultContext("Dictionary",
() => Formatter.Format(value, typeof(TValue).GetFormattingOption(value.Count))
.AppendIsIncomplete(isIncomplete),
-2)));
=>
{
if (contexts.All(c => c.Title != "Dictionary"))
{
contexts
.Add(new ResultContext("Dictionary",
() => Formatter.Format(value, typeof(TValue).GetFormattingOption(value.Count))
.AppendIsIncomplete(isIncomplete),
-2));
}
});
}

internal static string AppendIsIncomplete(this string formattedItems, bool isIncomplete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,29 @@ Expected that subject
but it was <null>
""");
}

[Fact]
public async Task WithMultipleFailures_ShouldIncludeCollectionOnlyOnce()
{
IAsyncEnumerable<string> subject = ToAsyncEnumerable(["a", "b", "c",]);

async Task Act()
=> await That(subject).Contains("d").And.Contains("e");

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
contains "d" at least once and contains "e" at least once,
but it did not contain it

Collection:
[
"a",
"b",
"c"
]
""");
}
}

public sealed class StringItemTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ Expected that subject
but it was <null>
""");
}

[Fact]
public async Task WithMultipleFailures_ShouldIncludeCollectionOnlyOnce()
{
IDictionary<int, string> subject = ToDictionary([1, 2, 3,], ["foo", "bar", "baz",]);

async Task Act()
=> await That(subject).ContainsKey(4).And.ContainsKey(5);

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
contains key 4 and contains key 5,
but it contained only [
1,
2,
3
]

Dictionary:
{
[1] = "foo",
[2] = "bar",
[3] = "baz"
}
""");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,25 @@ Expected that subject
but it was <null>
""");
}

[Fact]
public async Task WithMultipleFailures_ShouldIncludeCollectionOnlyOnce()
{
IEnumerable subject = ToEnumerable([1, 2, 3,]);

async Task Act()
=> await That(subject).Contains(4).And.Contains(5);

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
contains 4 at least once and contains 5 at least once,
but it did not contain it

Collection:
[1, 2, 3]
""");
}
}

public sealed class EnumerablePredicateTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,29 @@ but it did not contain it
{Formatter.Format(subject)}
""");
}

[Fact]
public async Task WithMultipleFailures_ShouldIncludeCollectionOnlyOnce()
{
ImmutableArray<string?> subject = ["a", "b", "c",];

async Task Act()
=> await That(subject).Contains("d").And.Contains("e");

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
contains "d" at least once and contains "e" at least once,
but it did not contain it

Collection:
[
"a",
"b",
"c"
]
""");
}
}

public sealed class ImmutableStringItemTests
Expand Down
23 changes: 23 additions & 0 deletions Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,29 @@ Expected that subject
but it was <null>
""");
}

[Fact]
public async Task WithMultipleFailures_ShouldIncludeCollectionOnlyOnce()
{
IEnumerable<string> subject = ToEnumerable(["a", "b", "c",]);

async Task Act()
=> await That(subject).Contains("d").And.Contains("e");

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
contains "d" at least once and contains "e" at least once,
but it did not contain it

Collection:
[
"a",
"b",
"c"
]
""");
}
}

public sealed class StringItemTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,33 @@ Expected that subject
but it was <null>
""");
}

[Fact]
public async Task WithMultipleFailures_ShouldIncludeCollectionOnlyOnce()
{
IReadOnlyDictionary<int, string> subject = ToDictionary([1, 2, 3,], ["foo", "bar", "baz",]);

async Task Act()
=> await That(subject).ContainsKey(4).And.ContainsKey(5);

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
contains key 4 and contains key 5,
but it contained only [
1,
2,
3
]

Dictionary:
{
[1] = "foo",
[2] = "bar",
[3] = "baz"
}
""");
}
}
}
}
Loading