From a3a804fa8231f567eab3205bc3a70f8f5dabac5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 24 Oct 2021 12:35:31 +0200 Subject: [PATCH] Upgrade CombineExpectations --- GRDB.xcodeproj/project.pbxproj | 8 +++ GRDBCustom.xcodeproj/project.pbxproj | 6 ++ .../PublisherExpectation.swift | 17 ++++- .../AvailableElements.swift | 62 +++++++++++++++++++ Tests/CombineExpectations/Recorder.swift | 33 ++++++++-- 5 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 Tests/CombineExpectations/PublisherExpectations/AvailableElements.swift diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 6d1db28ad..94d433eb1 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -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 = ""; }; 56915781231BF28B00E1D237 /* PoolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PoolTests.swift; sourceTree = ""; }; 569178451CED9B6000E179EA /* DatabaseQueueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueTests.swift; sourceTree = ""; }; + 5691D96927256C3A0021D540 /* AvailableElements.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvailableElements.swift; sourceTree = ""; }; 569530FB1C9067CC00CF1A2B /* DatabaseQueueCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueCrashTests.swift; sourceTree = ""; }; 569530FC1C9067CC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConvertibleCrashTests.swift; sourceTree = ""; }; 569530FD1C9067CC00CF1A2B /* GRDBCrashTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBCrashTestCase.swift; sourceTree = ""; }; @@ -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 */, diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index 4fbaf6473..2927caaa9 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -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 = ""; }; 5691578B231BF2BE00E1D237 /* PoolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PoolTests.swift; sourceTree = ""; }; 569178451CED9B6000E179EA /* DatabaseQueueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueTests.swift; sourceTree = ""; }; + 5691D96D27256C490021D540 /* AvailableElements.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvailableElements.swift; sourceTree = ""; }; 5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSchemaCache.swift; sourceTree = ""; }; 569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueSchemaCacheTests.swift; sourceTree = ""; }; 569531281C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePoolSchemaCacheTests.swift; sourceTree = ""; }; @@ -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 */, diff --git a/Tests/CombineExpectations/PublisherExpectation.swift b/Tests/CombineExpectations/PublisherExpectation.swift index 774511c7e..b8fca0b5a 100644 --- a/Tests/CombineExpectations/PublisherExpectation.swift +++ b/Tests/CombineExpectations/PublisherExpectation.swift @@ -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() } } diff --git a/Tests/CombineExpectations/PublisherExpectations/AvailableElements.swift b/Tests/CombineExpectations/PublisherExpectations/AvailableElements.swift new file mode 100644 index 000000000..82947bcca --- /dev/null +++ b/Tests/CombineExpectations/PublisherExpectations/AvailableElements.swift @@ -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: PublisherExpectation { + let recorder: Recorder + + 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 diff --git a/Tests/CombineExpectations/Recorder.swift b/Tests/CombineExpectations/Recorder.swift index 6120d2415..6b91f405c 100644 --- a/Tests/CombineExpectations/Recorder.swift +++ b/Tests/CombineExpectations/Recorder.swift @@ -277,21 +277,46 @@ public class Recorder: 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 = Map, Subscribers.Completion> - /// The type of the publisher expectation returned by Recorder.elements + /// The type of the publisher expectation returned by `Recorder.elements`. public typealias Elements = Map, [Input]> - /// The type of the publisher expectation returned by Recorder.last + /// The type of the publisher expectation returned by `Recorder.last`. public typealias Last = Map, Input?> - /// The type of the publisher expectation returned by Recorder.single + /// The type of the publisher expectation returned by `Recorder.single`. public typealias Single = Map, 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 { + PublisherExpectations.AvailableElements(recorder: self) + } + /// Returns a publisher expectation which waits for the recorded publisher /// to complete. ///