forked from iZettle/Flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisposable+CombineTests.swift
114 lines (81 loc) · 2.77 KB
/
Disposable+CombineTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// Disposable+CombineTests.swift
// Flow
//
// Created by Carl Ekman on 2023-02-09.
// Copyright © 2023 PayPal Inc. All rights reserved.
//
import XCTest
#if DEBUG
@testable import Flow
#else
import Flow
#endif
import Foundation
#if canImport(Combine)
import Combine
@available(iOS 13.0, macOS 10.15, *)
final class Disposable_CombineTests: XCTestCase {
var bag = CancelBag()
override func tearDownWithError() throws {
bag.cancel()
try super.tearDownWithError()
}
func testCancellingDisposable() {
let disposed = expectation(description: "Disposed")
let disposer = Disposer { disposed.fulfill() }
disposer.asAnyCancellable.cancel()
wait(for: [disposed], timeout: 1)
}
func testCancelBag() {
var bag = CancelBag()
let cancelled1 = expectation(description: "Cancelled 1")
let cancelled2 = expectation(description: "Cancelled 2")
let cancelled3 = expectation(description: "Cancelled 3")
bag += { cancelled1.fulfill() }
bag += { cancelled2.fulfill() }
bag += { cancelled3.fulfill() }
bag.cancel()
XCTAssertFalse(bag.isEmpty)
wait(for: [cancelled1, cancelled2, cancelled3], timeout: 1)
XCTAssertFalse(bag.isEmpty)
bag.empty()
XCTAssertTrue(bag.isEmpty)
}
func testCancellingDisposeBag() {
let bag = DisposeBag()
let cancelled1 = expectation(description: "Cancelled 1")
let cancelled2 = expectation(description: "Cancelled 2")
let cancelled3 = expectation(description: "Cancelled 3")
bag += { cancelled1.fulfill() }
bag += { cancelled2.fulfill() }
bag += { cancelled3.fulfill() }
bag.asAnyCancellable.cancel()
wait(for: [cancelled1, cancelled2, cancelled3], timeout: 1)
}
func testDisposeBagToCancelBag() {
let disposeBag = DisposeBag()
let disposed = expectation(description: "Disposed")
disposeBag += { disposed.fulfill() }
var cancelBag = CancelBag(disposable: disposeBag)
cancelBag.empty()
wait(for: [disposed], timeout: 1)
}
func testCancelPublisherSink() {
let callbacker = Callbacker<Event<Int>>()
let signal = FiniteSignal(callbacker: callbacker)
let publisher = signal.asAnyPublisher
let cancelled = expectation(description: "Cancelled")
bag += { cancelled.fulfill() }
publisher.sink { _ in
XCTFail("Did not expect completion")
} receiveValue: { _ in
XCTFail("Did not expect value")
}.store(in: &bag)
bag.cancel()
callbacker.callAll(with: .value(1))
callbacker.callAll(with: .end(TestError.fatal))
wait(for: [cancelled], timeout: 1)
}
}
#endif