mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
Refactor assertions about iterable contents
- Add iterable-based assertions - Have a single actual implementation; let everything else forward to it - Delete obsolete assertion variants - Stop requiring UnstableContainersPreview for Iterable assertions
This commit is contained in:
1 file changed
+123
-134
@@ -121,7 +121,7 @@ public func expectIterableContents<
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Check if `left` contains lifetime tracked instances whose payloads equal
|
||||
/// the elements in `right`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@@ -138,38 +138,42 @@ public func expectIterablePayloads<
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(E) where E1.Element: ~Copyable {
|
||||
var it1 = left.makeBorrowingIterator()
|
||||
var it2 = right.makeIterator()
|
||||
var i = 0
|
||||
while true {
|
||||
let next1 = try it1.nextSpan(maxCount: 1)
|
||||
let next2 = it2.next()
|
||||
switch (next1.isEmpty, next2) {
|
||||
case (true, nil):
|
||||
return
|
||||
case (true, _?):
|
||||
_expectFailure(
|
||||
"Borrowing sequence is shorter than expected",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
case (false, nil):
|
||||
_expectFailure(
|
||||
"Borrowing sequence is longer than expected",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
case (false, let b?):
|
||||
let a = next1[0].payload
|
||||
guard a == b else {
|
||||
_expectFailure(
|
||||
"Element at offset \(i) '\(a)' is not equal to '\(b)'",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
}
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
try expectBorrowingIteratorPayloads(
|
||||
left.makeBorrowingIterator(),
|
||||
equalTo: right,
|
||||
message(),
|
||||
trapping: trapping,
|
||||
file: file, line: line)
|
||||
}
|
||||
|
||||
/// Check if `left` contains lifetime tracked instances whose payloads equal
|
||||
/// the elements in `right`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
public func expectBorrowingIteratorPayloads<
|
||||
Payload: Equatable,
|
||||
E1: BorrowingIteratorProtocol<LifetimeTrackedStruct<Payload>, E> & ~Copyable & ~Escapable,
|
||||
C2: Collection<Payload>,
|
||||
E,
|
||||
>(
|
||||
_ left: consuming E1,
|
||||
equalTo right: C2,
|
||||
_ message: @autoclosure () -> String = "",
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(E) where E1.Element: ~Copyable {
|
||||
try expectBorrowingIteratorContents(
|
||||
left,
|
||||
equivalentTo: right,
|
||||
by: { $0.payload == $1 },
|
||||
printer: { "\($0.payload)" },
|
||||
message(),
|
||||
trapping: trapping,
|
||||
file: file, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4)
|
||||
/// Check if `left` contains lifetime tracked instances whose payloads equal
|
||||
/// the elements in `right`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@@ -186,96 +190,53 @@ public func expectIterablePayloads<
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(E) {
|
||||
var it1 = left.makeBorrowingIterator()
|
||||
var it2 = right.makeIterator()
|
||||
var i = 0
|
||||
while true {
|
||||
let next1 = try it1.nextSpan(maxCount: 1)
|
||||
let next2 = it2.next()
|
||||
switch (next1.isEmpty, next2) {
|
||||
case (true, nil):
|
||||
return
|
||||
case (true, _?):
|
||||
_expectFailure(
|
||||
"Borrowing sequence is shorter than expected",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
case (false, nil):
|
||||
_expectFailure(
|
||||
"Borrowing sequence is longer than expected",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
case (false, let b?):
|
||||
let a = next1[0].payload
|
||||
guard a == b else {
|
||||
_expectFailure(
|
||||
"Element at offset \(i) '\(a)' is not equal to '\(b)'",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
}
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
try expectIterableContents(
|
||||
left,
|
||||
equivalentTo: right,
|
||||
by: { $0.payload == $1 },
|
||||
printer: { "\($0.payload)" },
|
||||
message(),
|
||||
trapping: trapping,
|
||||
file: file, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Check if `left` contains lifetime tracked instances whose payloads equal
|
||||
/// the elements in `right`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
public func expectIterablesWithEquivalentElements<
|
||||
S1: Iterable & ~Copyable & ~Escapable,
|
||||
S2: Iterable & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ left: borrowing S1,
|
||||
_ right: borrowing S2,
|
||||
by areEquivalent: (borrowing S1.Element, borrowing S2.Element) -> Bool,
|
||||
_ message: @autoclosure () -> String = "",
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(S1.Failure)
|
||||
where
|
||||
S1.Element: ~Copyable,
|
||||
S2.Element: ~Copyable,
|
||||
S1.Failure == S2.Failure
|
||||
{
|
||||
if try left._elementsEqual(right, by: areEquivalent) { return }
|
||||
_expectFailure(
|
||||
"Containers do not have equivalent elements",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
}
|
||||
|
||||
/// Check if `left` and `right` contain equal elements in the same order.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
public func expectIterablesWithEqualElements<
|
||||
Element: Equatable,
|
||||
S1: Iterable<Element, E> & ~Copyable & ~Escapable,
|
||||
S2: Iterable<Element, E> & ~Copyable & ~Escapable,
|
||||
public func expectBorrowingIteratorPayloads<
|
||||
Payload: Equatable,
|
||||
E1: BorrowingIteratorProtocol<LifetimeTracked<Payload>, E> & ~Copyable & ~Escapable,
|
||||
C2: Collection<Payload>,
|
||||
E,
|
||||
>(
|
||||
_ left: borrowing S1,
|
||||
_ right: borrowing S2,
|
||||
_ left: consuming E1,
|
||||
equalTo right: C2,
|
||||
_ message: @autoclosure () -> String = "",
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(E)
|
||||
where
|
||||
S1.Element: ~Copyable,
|
||||
S2.Element: ~Copyable
|
||||
{
|
||||
if try left._elementsEqual(right) { return }
|
||||
_expectFailure(
|
||||
"Containers do not have equal elements",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
) throws(E) {
|
||||
try expectBorrowingIteratorContents(
|
||||
left,
|
||||
equivalentTo: right,
|
||||
by: { $0.payload == $1 },
|
||||
printer: { "\($0.payload)" },
|
||||
message(),
|
||||
trapping: trapping,
|
||||
file: file, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4)
|
||||
/// Check if `left` and `right` contain equal elements in the same order.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
public func expectIterableContents<
|
||||
Element: Equatable,
|
||||
S1: Iterable<Element, E> & ~Copyable & ~Escapable,
|
||||
Failure,
|
||||
S1: Iterable<Element, Failure> & ~Copyable & ~Escapable,
|
||||
C2: Collection<Element>,
|
||||
E,
|
||||
>(
|
||||
_ left: borrowing S1,
|
||||
equalTo right: C2,
|
||||
@@ -283,33 +244,13 @@ public func expectIterableContents<
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(E) {
|
||||
var it1 = left.makeBorrowingIterator()
|
||||
var it2 = right.makeIterator()
|
||||
while true {
|
||||
let span = try it1.nextSpan()
|
||||
if span.isEmpty { break }
|
||||
for i in 0 ..< span.count {
|
||||
guard let b = it2.next() else {
|
||||
_expectFailure(
|
||||
"Container is longer than expected",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
}
|
||||
guard span[i] == b else {
|
||||
_expectFailure(
|
||||
"'\(span[i])' is not equal to '\(b)'",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
guard it2.next() == nil else {
|
||||
_expectFailure(
|
||||
"Container is shorter than expected",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
}
|
||||
) throws(Failure) {
|
||||
try expectBorrowingIteratorContents(
|
||||
left.makeBorrowingIterator(),
|
||||
equalTo: right,
|
||||
message(),
|
||||
trapping: trapping,
|
||||
file: file, line: line)
|
||||
}
|
||||
|
||||
/// Check if `left` and `right` contain equal elements in the same order.
|
||||
@@ -327,7 +268,55 @@ public func expectIterableContents<
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(S1.Failure) where S1.Element: ~Copyable {
|
||||
var it1 = left.makeBorrowingIterator()
|
||||
try expectBorrowingIteratorContents(
|
||||
left.makeBorrowingIterator(),
|
||||
equivalentTo: right,
|
||||
by: areEquivalent,
|
||||
printer: printer,
|
||||
message(),
|
||||
trapping: trapping,
|
||||
file: file, line: line)
|
||||
}
|
||||
|
||||
/// Check if `left` and `right` contain equal elements in the same order.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
public func expectBorrowingIteratorContents<
|
||||
S1: BorrowingIteratorProtocol & ~Copyable & ~Escapable,
|
||||
C2: Collection,
|
||||
>(
|
||||
_ left: consuming S1,
|
||||
equalTo right: C2,
|
||||
_ message: @autoclosure () -> String = "",
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(S1.Failure) where S1.Element == C2.Element, S1.Element: Equatable {
|
||||
try expectBorrowingIteratorContents(
|
||||
left,
|
||||
equivalentTo: right,
|
||||
by: ==,
|
||||
printer: { "\($0)" },
|
||||
message(),
|
||||
trapping: trapping,
|
||||
file: file, line: line)
|
||||
}
|
||||
|
||||
/// Check if `left` and `right` contain equal elements in the same order.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
public func expectBorrowingIteratorContents<
|
||||
S1: BorrowingIteratorProtocol & ~Copyable & ~Escapable,
|
||||
C2: Collection,
|
||||
>(
|
||||
_ left: consuming S1,
|
||||
equivalentTo right: C2,
|
||||
by areEquivalent: (borrowing S1.Element, C2.Element) -> Bool,
|
||||
printer: (borrowing S1.Element) -> String,
|
||||
_ message: @autoclosure () -> String = "",
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) throws(S1.Failure) where S1.Element: ~Copyable {
|
||||
var it1 = left
|
||||
var it2 = right.makeIterator()
|
||||
var offset = 0
|
||||
while true {
|
||||
@@ -336,7 +325,7 @@ public func expectIterableContents<
|
||||
for i in 0 ..< span.count {
|
||||
guard let b = it2.next() else {
|
||||
_expectFailure(
|
||||
"Container is longer than expected",
|
||||
"Iterable is longer than expected",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
}
|
||||
@@ -351,7 +340,7 @@ public func expectIterableContents<
|
||||
}
|
||||
guard it2.next() == nil else {
|
||||
_expectFailure(
|
||||
"Container is shorter than expected",
|
||||
"Iterable is shorter than expected",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user