forked from bUnit-dev/bUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionAssertExtensions.cs
56 lines (52 loc) · 2.37 KB
/
CollectionAssertExtensions.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Xunit;
using Xunit.Sdk;
namespace Egil.RazorComponents.Testing.Asserting
{
/// <summary>
/// Collection test assertions
/// </summary>
public static class CollectionAssertExtensions
{
/// <summary>
/// Verifies that a collection contains exactly a given number of elements, which
/// meet the criteria provided by the element inspectors.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection">The collection to be inspected</param>
/// <param name="elementInspectors">The element inspectors, which inspect each element in turn. The total number of element inspectors must exactly match the number of elements in the collection.</param>
public static void ShouldAllBe<T>(this IEnumerable<T> collection, params Action<T>[] elementInspectors)
{
Assert.Collection(collection, elementInspectors);
}
/// <summary>
/// Verifies that a collection contains exactly a given number of elements, which
/// meet the criteria provided by the element inspectors.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection">The collection to be inspected</param>
/// <param name="elementInspectors">The element inspectors, which inspect each element and its index in the collection in turn. The total number of element inspectors must exactly match the number of elements in the collection.</param>
public static void ShouldAllBe<T>(this IEnumerable<T> collection, params Action<T, int>[] elementInspectors)
{
T[] elements = collection.ToArray();
int expectedCount = elementInspectors.Length;
int actualCount = elements.Length;
if (expectedCount != actualCount)
throw new CollectionException(collection, expectedCount, actualCount);
for (int idx = 0; idx < actualCount; idx++)
{
try
{
elementInspectors[idx](elements[idx], idx);
}
catch (Exception ex)
{
throw new CollectionException(collection, expectedCount, actualCount, idx, ex);
}
}
}
}
}