-
Notifications
You must be signed in to change notification settings - Fork 204
/
FilterPublisherTests.swift
80 lines (70 loc) · 2.75 KB
/
FilterPublisherTests.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
//
// FilterPublisherTests.swift
// UsingCombineTests
//
// Created by Joseph Heck on 7/11/19.
// Copyright © 2019 SwiftUI-Notes. All rights reserved.
//
import XCTest
import Combine
class FilterPublisherTests: XCTestCase {
func testFilter() {
let simplePublisher = PassthroughSubject<String, Error>()
let cancellable = simplePublisher
.filter { stringValue in
return stringValue == "onefish"
}
.print(self.debugDescription)
.sink(receiveCompletion: { completion in
print(".sink() received the completion:", String(describing: completion))
switch completion {
case .failure(let anError):
print(".sink() received completion error: ", anError)
XCTFail("no error should be received")
break
case .finished:
break
}
}, receiveValue: { stringValue in
print(".sink() received \(stringValue)")
XCTAssertEqual(stringValue, "onefish")
})
simplePublisher.send("onefish") // onefish will pass the filter
simplePublisher.send("twofish") // twofish will not
simplePublisher.send(completion: Subscribers.Completion.finished)
XCTAssertNotNil(cancellable)
}
func testTryFilter() {
enum TestFailure: Error {
case boom
}
let simplePublisher = PassthroughSubject<String, Error>()
let cancellable = simplePublisher
.tryFilter { stringValue in
if stringValue == "explode" {
throw TestFailure.boom
}
return stringValue == "onefish"
}
.print(self.debugDescription)
.sink(receiveCompletion: { completion in
print(".sink() received the completion:", String(describing: completion))
switch completion {
case .failure(let anError):
print(".sink() received completion error: ", anError)
break
case .finished:
XCTFail("test sequence should fail before receiving finished")
break
}
}, receiveValue: { stringValue in
print(".sink() received \(stringValue)")
XCTAssertEqual(stringValue, "onefish")
})
simplePublisher.send("onefish") // onefish will pass the filter
simplePublisher.send("twofish") // twofish will not
simplePublisher.send("explode") // explode will trigger a failure
simplePublisher.send(completion: Subscribers.Completion.finished)
XCTAssertNotNil(cancellable)
}
}