-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathCollectionCountValueAssertion.cs
More file actions
105 lines (93 loc) · 3.34 KB
/
Copy pathCollectionCountValueAssertion.cs
File metadata and controls
105 lines (93 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Collections;
using TUnit.Assertions.Core;
using TUnit.Assertions.Sources;
namespace TUnit.Assertions.Conditions;
/// <summary>
/// Assertion that evaluates the count of a collection and provides numeric assertions on that count.
/// Implements IAssertionSource<int> to enable all numeric assertion methods.
/// </summary>
public class CollectionCountValueAssertion<TCollection, TItem> : ValueAssertion<int>
where TCollection : IEnumerable<TItem>
{
public CollectionCountValueAssertion(
AssertionContext<TCollection> collectionContext,
Func<TItem, bool>? predicate)
: base(CreateIntContext(collectionContext, predicate))
{
}
private static AssertionContext<int> CreateIntContext(
AssertionContext<TCollection> collectionContext,
Func<TItem, bool>? predicate)
{
return collectionContext.Map<int>(collection =>
{
if (collection == null)
{
return 0;
}
// Calculate count efficiently
if (predicate == null)
{
return collection switch
{
ICollection c => c.Count,
_ => System.Linq.Enumerable.Count(collection)
};
}
return System.Linq.Enumerable.Count(collection, predicate);
});
}
}
/// <summary>
/// Assertion that evaluates the count of items satisfying an inner assertion and provides numeric assertions on that count.
/// Implements IAssertionSource<int> to enable all numeric assertion methods.
/// This allows using the full assertion builder (e.g., item => item.IsGreaterThan(10)) instead of a simple predicate.
/// </summary>
public class CollectionCountWithAssertionValueAssertion<TCollection, TItem> : ValueAssertion<int>
where TCollection : IEnumerable<TItem>
{
public CollectionCountWithAssertionValueAssertion(
AssertionContext<TCollection> collectionContext,
Func<IAssertionSource<TItem>, Assertion<TItem>?> assertion)
: base(CreateIntContext(collectionContext, assertion))
{
}
private static AssertionContext<int> CreateIntContext(
AssertionContext<TCollection> collectionContext,
Func<IAssertionSource<TItem>, Assertion<TItem>?> assertion)
{
return collectionContext.Map<int>(async collection =>
{
if (collection == null)
{
return 0;
}
int count = 0;
int index = 0;
foreach (var item in collection)
{
var itemAssertion = new ValueAssertion<TItem>(item, $"item[{index}]");
var resultingAssertion = assertion(itemAssertion);
if (resultingAssertion != null)
{
try
{
await resultingAssertion.AssertAsync();
count++;
}
catch
{
// Item did not satisfy the assertion, don't count it
}
}
else
{
// Null assertion means no constraint, count all items
count++;
}
index++;
}
return count;
});
}
}