Skip to content

Commit c2a2534

Browse files
committed
Make a 'count the elements of a parameter pack' helper function
1 parent 2bda2da commit c2a2534

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Diff for: Sources/Testing/ExitTests/ExitTest.CapturedValue.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,7 @@ extension Collection where Element == ExitTest.CapturedValue {
144144
// exit test handler or entry point is likely broken.
145145
guard let wrappedValue = capturedValues.first?.wrappedValue else {
146146
let actualCount = self.count
147-
let expectedCount: Int = {
148-
var result = 0
149-
for _ in repeat (each T).self {
150-
result += 1
151-
}
152-
return result
153-
}()
147+
let expectedCount = parameterPackCount(repeat (each T).self)
154148
fatalError("Found fewer captured values (\(actualCount)) than expected (\(expectedCount)) when passing them to the current exit test.")
155149
}
156150

Diff for: Sources/Testing/Support/Additions/ArrayAdditions.swift

+18
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,21 @@ extension Array {
2121
self = optionalValue.map { [$0] } ?? []
2222
}
2323
}
24+
25+
/// Get the number of elements in a parameter pack.
26+
///
27+
/// - Parameters:
28+
/// - pack: The parameter pack.
29+
///
30+
/// - Returns: The number of elements in `pack`.
31+
///
32+
/// - Complexity: O(_n_) where _n_ is the number of elements in `pack`. The
33+
/// compiler may be able to optimize this operation when the types of `pack`
34+
/// are statically known.
35+
func parameterPackCount<each T>(_ pack: repeat each T) -> Int {
36+
var result = 0
37+
for _ in repeat each pack {
38+
result += 1
39+
}
40+
return result
41+
}

0 commit comments

Comments
 (0)