Upgrade CombineExpectations

This commit is contained in:
Gwendal Roué committed 2021-10-24 12:37:28 +02:00
1 parent 8ccaf59d4d
commit a3a804fa82
5 files changed
+121 -5

No files matched your search

+8
View File
@@ -580,6 +580,9 @@
56915783231BF28B00E1D237 /* PoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56915781231BF28B00E1D237 /* PoolTests.swift */; };
56915784231BF28B00E1D237 /* PoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56915781231BF28B00E1D237 /* PoolTests.swift */; };
569178491CED9B6000E179EA /* DatabaseQueueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569178451CED9B6000E179EA /* DatabaseQueueTests.swift */; };
5691D96A27256C3B0021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D96927256C3A0021D540 /* AvailableElements.swift */; };
5691D96B27256C3B0021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D96927256C3A0021D540 /* AvailableElements.swift */; };
5691D96C27256C3B0021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D96927256C3A0021D540 /* AvailableElements.swift */; };
569531091C9067DC00CF1A2B /* DatabaseQueueCrashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569530FB1C9067CC00CF1A2B /* DatabaseQueueCrashTests.swift */; };
5695310A1C9067DC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569530FC1C9067CC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift */; };
5695310B1C9067DC00CF1A2B /* GRDBCrashTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569530FD1C9067CC00CF1A2B /* GRDBCrashTestCase.swift */; };
@@ -1487,6 +1490,7 @@
5690C33F1D23E82A00E59934 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
56915781231BF28B00E1D237 /* PoolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PoolTests.swift; sourceTree = "<group>"; };
569178451CED9B6000E179EA /* DatabaseQueueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueTests.swift; sourceTree = "<group>"; };
5691D96927256C3A0021D540 /* AvailableElements.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvailableElements.swift; sourceTree = "<group>"; };
569530FB1C9067CC00CF1A2B /* DatabaseQueueCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueCrashTests.swift; sourceTree = "<group>"; };
569530FC1C9067CC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConvertibleCrashTests.swift; sourceTree = "<group>"; };
569530FD1C9067CC00CF1A2B /* GRDBCrashTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBCrashTestCase.swift; sourceTree = "<group>"; };
@@ -1975,6 +1979,7 @@
56419A6024A51601004967E1 /* PublisherExpectations */ = {
isa = PBXGroup;
children = (
5691D96927256C3A0021D540 /* AvailableElements.swift */,
56419A6124A51601004967E1 /* Finished.swift */,
56419A6224A51601004967E1 /* NextOne.swift */,
56419A6324A51601004967E1 /* Next.swift */,
@@ -3224,6 +3229,7 @@
562756471E963AAC0035B653 /* DatabaseWriterTests.swift in Sources */,
569531381C919DF700CF1A2B /* DatabasePoolFunctionTests.swift in Sources */,
563B06F821861D8400B38F35 /* ValueObservationCountTests.swift in Sources */,
5691D96B27256C3B0021D540 /* AvailableElements.swift in Sources */,
56DF001C228DDBA300D611F3 /* AssociationPrefetchingRowTests.swift in Sources */,
56A238481B9C74A90082EB20 /* RowCopiedFromStatementTests.swift in Sources */,
56EA63C6209C7CE3009715B8 /* DerivableRequestTests.swift in Sources */,
@@ -3457,6 +3463,7 @@
56D496981D81317B008276D7 /* DatabaseValueTests.swift in Sources */,
56D4966B1D81309E008276D7 /* TableRecordDeleteTests.swift in Sources */,
563B06F721861D8400B38F35 /* ValueObservationCountTests.swift in Sources */,
5691D96A27256C3B0021D540 /* AvailableElements.swift in Sources */,
56DF001B228DDBA300D611F3 /* AssociationPrefetchingRowTests.swift in Sources */,
56D4965B1D81304E008276D7 /* FoundationNSDecimalNumberTests.swift in Sources */,
562756431E963AAC0035B653 /* DatabaseWriterTests.swift in Sources */,
@@ -3824,6 +3831,7 @@
AAA4DD26230F262000C74B15 /* DatabaseWriterTests.swift in Sources */,
AAA4DD27230F262000C74B15 /* DatabasePoolFunctionTests.swift in Sources */,
AAA4DD28230F262000C74B15 /* ValueObservationCountTests.swift in Sources */,
5691D96C27256C3B0021D540 /* AvailableElements.swift in Sources */,
AAA4DD29230F262000C74B15 /* AssociationPrefetchingRowTests.swift in Sources */,
AAA4DD2A230F262000C74B15 /* RowCopiedFromStatementTests.swift in Sources */,
AAA4DD2B230F262000C74B15 /* DerivableRequestTests.swift in Sources */,
+6
View File
@@ -384,6 +384,8 @@
5690C3451D23E82A00E59934 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
5691578D231BF2BE00E1D237 /* PoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691578B231BF2BE00E1D237 /* PoolTests.swift */; };
5691578E231BF2BE00E1D237 /* PoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691578B231BF2BE00E1D237 /* PoolTests.swift */; };
5691D96F27256C490021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D96D27256C490021D540 /* AvailableElements.swift */; };
5691D97027256C490021D540 /* AvailableElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691D96D27256C490021D540 /* AvailableElements.swift */; };
56959620222C458A002CB7C9 /* AssociationHasManyThroughSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5695961E222C4589002CB7C9 /* AssociationHasManyThroughSQLTests.swift */; };
56959621222C458A002CB7C9 /* AssociationHasManyThroughSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5695961E222C4589002CB7C9 /* AssociationHasManyThroughSQLTests.swift */; };
56959636222D058B002CB7C9 /* AssociationHasManySQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56959635222D058B002CB7C9 /* AssociationHasManySQLTests.swift */; };
@@ -998,6 +1000,7 @@
5690C33F1D23E82A00E59934 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
5691578B231BF2BE00E1D237 /* PoolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PoolTests.swift; sourceTree = "<group>"; };
569178451CED9B6000E179EA /* DatabaseQueueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueTests.swift; sourceTree = "<group>"; };
5691D96D27256C490021D540 /* AvailableElements.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvailableElements.swift; sourceTree = "<group>"; };
5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSchemaCache.swift; sourceTree = "<group>"; };
569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueSchemaCacheTests.swift; sourceTree = "<group>"; };
569531281C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePoolSchemaCacheTests.swift; sourceTree = "<group>"; };
@@ -1455,6 +1458,7 @@
56419A7924A51614004967E1 /* PublisherExpectations */ = {
isa = PBXGroup;
children = (
5691D96D27256C490021D540 /* AvailableElements.swift */,
56419A7A24A51614004967E1 /* Finished.swift */,
56419A7B24A51614004967E1 /* NextOne.swift */,
56419A7C24A51614004967E1 /* Next.swift */,
@@ -2524,6 +2528,7 @@
5674A70E1F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
5657AB551D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */,
5665F869203EF4650084C6C0 /* ColumnInfoTests.swift in Sources */,
5691D97027256C490021D540 /* AvailableElements.swift in Sources */,
F3BA81291CFB3063003DC1BA /* RecordInitializersTests.swift in Sources */,
56419C8424A51D6F004967E1 /* DatabaseReaderReadPublisherTests.swift in Sources */,
56DAA2D91DE99DAB006E10C8 /* DatabaseCursorTests.swift in Sources */,
@@ -2890,6 +2895,7 @@
5698AC8C1DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */,
5665F868203EF4640084C6C0 /* ColumnInfoTests.swift in Sources */,
F3BA81391CFB3064003DC1BA /* RecordWithColumnNameManglingTests.swift in Sources */,
5691D96F27256C490021D540 /* AvailableElements.swift in Sources */,
56419C7F24A51D6E004967E1 /* DatabaseReaderReadPublisherTests.swift in Sources */,
56FF45431D2C23BA00F21EF9 /* TableRecordDeleteTests.swift in Sources */,
F3BA80F01CFB3017003DC1BA /* FetchableRecordTests.swift in Sources */,
@@ -6,10 +6,21 @@ public enum PublisherExpectations { }
/// The base protocol for PublisherExpectation. It is an implementation detail
/// that you are not supposed to use, as shown by the underscore prefix.
///
/// :nodoc:
public protocol _PublisherExpectationBase {
/// Sets up an XCTestExpectation. This method is an implementation detail
/// that you are not supposed to use, as shown by the underscore prefix.
func _setup(_ expectation: XCTestExpectation)
/// Returns an object that waits for the expectation. If nil, expectation
/// is waited by the XCTestCase.
func _makeWaiter() -> XCTWaiter?
}
extension _PublisherExpectationBase {
/// :nodoc:
public func _makeWaiter() -> XCTWaiter? { nil }
}
/// The protocol for publisher expectations.
@@ -94,7 +105,11 @@ extension XCTestCase {
{
let expectation = self.expectation(description: description)
publisherExpectation._setup(expectation)
wait(for: [expectation], timeout: timeout)
if let waiter = publisherExpectation._makeWaiter() {
waiter.wait(for: [expectation], timeout: timeout)
} else {
wait(for: [expectation], timeout: timeout)
}
return try publisherExpectation.get()
}
}
@@ -0,0 +1,62 @@
#if canImport(Combine)
import XCTest
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension PublisherExpectations {
/// A publisher expectation which waits for the timeout to expire, or
/// the recorded publisher to complete.
///
/// When waiting for this expectation, the publisher error is thrown if
/// the publisher fails before the expectation has expired.
///
/// Otherwise, an array of all elements published before the expectation
/// has expired is returned.
///
/// Unlike other expectations, `AvailableElements` does not make a test fail
/// on timeout expiration. It just returns the elements published so far.
///
/// For example:
///
/// // SUCCESS: no timeout, no error
/// func testTimerPublishesIncreasingDates() throws {
/// let publisher = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
/// let recorder = publisher.record()
/// let dates = try wait(for: recorder.availableElements, timeout: ...)
/// XCTAssertEqual(dates.sorted(), dates)
/// }
public struct AvailableElements<Input, Failure: Error>: PublisherExpectation {
let recorder: Recorder<Input, Failure>
public func _makeWaiter() -> XCTWaiter? { Waiter() }
public func _setup(_ expectation: XCTestExpectation) {
recorder.fulfillOnCompletion(expectation)
}
/// Returns all elements published so far, or throws an error if the
/// publisher has failed.
public func get() throws -> [Input] {
try recorder.value { (elements, completion, remainingElements, consume) in
if case let .failure(error) = completion {
throw error
}
consume(remainingElements.count)
return elements
}
}
/// A waiter that waits but never fails
private class Waiter: XCTWaiter, XCTWaiterDelegate {
init() {
super.init(delegate: nil)
delegate = self
}
func waiter(_ waiter: XCTWaiter, didTimeoutWithUnfulfilledExpectations unfulfilledExpectations: [XCTestExpectation]) { }
func waiter(_ waiter: XCTWaiter, fulfillmentDidViolateOrderingConstraintsFor expectation: XCTestExpectation, requiredExpectation: XCTestExpectation) { }
func waiter(_ waiter: XCTWaiter, didFulfillInvertedExpectation expectation: XCTestExpectation) { }
func nestedWaiter(_ waiter: XCTWaiter, wasInterruptedByTimedOutWaiter outerWaiter: XCTWaiter) { }
}
}
}
#endif
+29 -4
View File
@@ -277,21 +277,46 @@ public class Recorder<Input, Failure: Error>: Subscriber {
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension PublisherExpectations {
/// The type of the publisher expectation returned by Recorder.completion
/// The type of the publisher expectation returned by `Recorder.completion`.
public typealias Completion<Input, Failure: Error> = Map<Recording<Input, Failure>, Subscribers.Completion<Failure>>
/// The type of the publisher expectation returned by Recorder.elements
/// The type of the publisher expectation returned by `Recorder.elements`.
public typealias Elements<Input, Failure: Error> = Map<Recording<Input, Failure>, [Input]>
/// The type of the publisher expectation returned by Recorder.last
/// The type of the publisher expectation returned by `Recorder.last`.
public typealias Last<Input, Failure: Error> = Map<Elements<Input, Failure>, Input?>
/// The type of the publisher expectation returned by Recorder.single
/// The type of the publisher expectation returned by `Recorder.single`.
public typealias Single<Input, Failure: Error> = Map<Elements<Input, Failure>, Input>
}
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension Recorder {
/// Returns a publisher expectation which waits for the timeout to expire,
/// or the recorded publisher to complete.
///
/// When waiting for this expectation, the publisher error is thrown if
/// the publisher fails before the expectation has expired.
///
/// Otherwise, an array of all elements published before the expectation
/// has expired is returned.
///
/// Unlike other expectations, `availableElements` does not make a test fail
/// on timeout expiration. It just returns the elements published so far.
///
/// For example:
///
/// // SUCCESS: no timeout, no error
/// func testTimerPublishesIncreasingDates() throws {
/// let publisher = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
/// let recorder = publisher.record()
/// let dates = try wait(for: recorder.availableElements, timeout: ...)
/// XCTAssertEqual(dates.sorted(), dates)
/// }
public var availableElements: PublisherExpectations.AvailableElements<Input, Failure> {
PublisherExpectations.AvailableElements(recorder: self)
}
/// Returns a publisher expectation which waits for the recorded publisher
/// to complete.
///