-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7df8a4
commit 80e32c2
Showing
13 changed files
with
680 additions
and
725 deletions.
There are no files selected for viewing
22 changes: 17 additions & 5 deletions
22
Sources/OpenCombine/Publishers/GENERATED-Publishers.MapKeyPath.swift
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// TestLifecycle.swift | ||
// | ||
// | ||
// Created by Sergej Jaskiewicz on 08.10.2019. | ||
// | ||
|
||
import XCTest | ||
|
||
#if OPENCOMBINE_COMPATIBILITY_TEST | ||
import Combine | ||
#else | ||
import OpenCombine | ||
#endif | ||
|
||
@available(macOS 10.15, iOS 13.0, *) | ||
func testLifecycle<UpstreamOutput, Operator: Publisher>( | ||
file: StaticString = #file, | ||
line: UInt = #line, | ||
sendValue valueToBeSent: UpstreamOutput, | ||
cancellingSubscriptionReleasesSubscriber: Bool, | ||
_ makeOperator: (PassthroughSubject<UpstreamOutput, TestingError>) -> Operator | ||
) throws { | ||
var deinitCounter = 0 | ||
|
||
let onDeinit = { deinitCounter += 1 } | ||
|
||
// Lifecycle test #1 | ||
do { | ||
let passthrough = PassthroughSubject<UpstreamOutput, TestingError>() | ||
let operatorPublisher = makeOperator(passthrough) | ||
let emptySubscriber = | ||
TrackingSubscriberBase<Operator.Output, Operator.Failure>(onDeinit: onDeinit) | ||
XCTAssertTrue(emptySubscriber.history.isEmpty, | ||
"Lifecycle test #1: thesubscriber's history should be empty", | ||
file: file, | ||
line: line) | ||
operatorPublisher.subscribe(emptySubscriber) | ||
passthrough.send(valueToBeSent) | ||
passthrough.send(completion: .failure("failure")) | ||
} | ||
|
||
if cancellingSubscriptionReleasesSubscriber { | ||
XCTAssertEqual(deinitCounter, | ||
1, | ||
""" | ||
Lifecycle test #1: deinit should be called, because \ | ||
the subscription has completed | ||
""", | ||
file: file, | ||
line: line) | ||
} else { | ||
XCTAssertEqual(deinitCounter, | ||
0, | ||
""" | ||
Lifecycle test #1: deinit should not be called | ||
""", | ||
file: file, | ||
line: line) | ||
} | ||
|
||
// Lifecycle test #2 | ||
do { | ||
let passthrough = PassthroughSubject<UpstreamOutput, TestingError>() | ||
let operatorPublisher = makeOperator(passthrough) | ||
let emptySubscriber = | ||
TrackingSubscriberBase<Operator.Output, Operator.Failure>(onDeinit: onDeinit) | ||
operatorPublisher.subscribe(emptySubscriber) | ||
} | ||
|
||
XCTAssertEqual(deinitCounter, | ||
cancellingSubscriptionReleasesSubscriber ? 1 : 0, | ||
""" | ||
Lifecycle test #2: deinit should not be called, \ | ||
because the subscription is never cancelled | ||
""", | ||
file: file, | ||
line: line) | ||
|
||
// Lifecycle test #3 | ||
|
||
var subscription: Subscription? | ||
|
||
do { | ||
let passthrough = PassthroughSubject<UpstreamOutput, TestingError>() | ||
let operatorPublisher = makeOperator(passthrough) | ||
let emptySubscriber = TrackingSubscriberBase<Operator.Output, Operator.Failure>( | ||
receiveSubscription: { subscription = $0; $0.request(.unlimited) }, | ||
onDeinit: onDeinit | ||
) | ||
operatorPublisher.subscribe(emptySubscriber) | ||
passthrough.send(valueToBeSent) | ||
} | ||
|
||
XCTAssertEqual(deinitCounter, | ||
cancellingSubscriptionReleasesSubscriber ? 1 : 0, | ||
""" | ||
Lifecycle test #3: deinit should not be called, \ | ||
because the subscription is not cancelled yet | ||
""", | ||
file: file, | ||
line: line) | ||
|
||
try XCTUnwrap(subscription, file: file, line: line).cancel() | ||
|
||
if cancellingSubscriptionReleasesSubscriber { | ||
XCTAssertEqual(deinitCounter, | ||
2, | ||
""" | ||
Lifecycle test #3: deinit should be called, because | ||
the subscription has been cancelled | ||
""", | ||
file: file, | ||
line: line) | ||
} else { | ||
XCTAssertEqual(deinitCounter, | ||
0, | ||
"Lifecycle test #3: deinit should not be called", | ||
file: file, | ||
line: line) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.