|
| 1 | +// |
| 2 | +// MapToResultTests.swift |
| 3 | +// CombineExt |
| 4 | +// |
| 5 | +// Created by Yurii Zadoianchuk on 05/03/2021. |
| 6 | +// Copyright © 2021 Combine Community. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +#if !os(watchOS) |
| 12 | +import XCTest |
| 13 | +import Combine |
| 14 | +import CombineExt |
| 15 | + |
| 16 | +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 17 | +final class MapToResultTests: XCTestCase { |
| 18 | + private var subscription: AnyCancellable! |
| 19 | + |
| 20 | + enum MapToResultError: Error { |
| 21 | + case someError |
| 22 | + } |
| 23 | + |
| 24 | + func testMapResultNoError() { |
| 25 | + let subject = PassthroughSubject<Int, Error>() |
| 26 | + let testInt = 5 |
| 27 | + var completed = false |
| 28 | + var results: [Result<Int, Error>] = [] |
| 29 | + |
| 30 | + subscription = subject |
| 31 | + .mapToResult() |
| 32 | + .sink(receiveCompletion: { _ in completed = true }, |
| 33 | + receiveValue: { results.append($0) }) |
| 34 | + |
| 35 | + subject.send(testInt) |
| 36 | + XCTAssertFalse(completed) |
| 37 | + subject.send(testInt) |
| 38 | + subject.send(completion: .finished) |
| 39 | + XCTAssertTrue(completed) |
| 40 | + XCTAssertEqual(results.count, 2) |
| 41 | + let intsCorrect = results |
| 42 | + .compactMap { try? $0.get() } |
| 43 | + .allSatisfy { $0 == testInt } |
| 44 | + XCTAssertTrue(intsCorrect) |
| 45 | + } |
| 46 | + |
| 47 | + func testMapCustomError() { |
| 48 | + let subject = PassthroughSubject<Int, Error>() |
| 49 | + var completed = false |
| 50 | + var gotFailure = false |
| 51 | + var gotSuccess = false |
| 52 | + var result: Result<Int, Error>? = nil |
| 53 | + |
| 54 | + subscription = subject |
| 55 | + .tryMap { _ -> Int in throw MapToResultError.someError } |
| 56 | + .mapToResult() |
| 57 | + .eraseToAnyPublisher() |
| 58 | + .sink(receiveCompletion: { _ in completed = true }, |
| 59 | + receiveValue: { result = $0 }) |
| 60 | + |
| 61 | + subject.send(0) |
| 62 | + XCTAssertNotNil(result) |
| 63 | + |
| 64 | + do { |
| 65 | + _ = try result!.get() |
| 66 | + gotSuccess = true |
| 67 | + } catch { |
| 68 | + gotFailure = true |
| 69 | + } |
| 70 | + |
| 71 | + XCTAssertTrue(gotFailure) |
| 72 | + XCTAssertFalse(gotSuccess) |
| 73 | + XCTAssertTrue(completed) |
| 74 | + } |
| 75 | + |
| 76 | + func testCatchDecodeError() { |
| 77 | + struct ToDecode: Decodable { |
| 78 | + let foo: Int |
| 79 | + } |
| 80 | + |
| 81 | + let incorrectJson = """ |
| 82 | + { |
| 83 | + "foo": "1" |
| 84 | + } |
| 85 | + """ |
| 86 | + |
| 87 | + let subject = PassthroughSubject<Data, Error>() |
| 88 | + var completed = false |
| 89 | + var gotFailure = false |
| 90 | + var gotSuccess = false |
| 91 | + var result: Result<ToDecode, Error>? = nil |
| 92 | + |
| 93 | + subscription = subject |
| 94 | + .decode(type: ToDecode.self, decoder: JSONDecoder()) |
| 95 | + .mapToResult() |
| 96 | + .eraseToAnyPublisher() |
| 97 | + .sink(receiveCompletion: { _ in completed = true }, |
| 98 | + receiveValue: { result = $0 }) |
| 99 | + |
| 100 | + subject.send(incorrectJson.data(using: .utf8)!) |
| 101 | + XCTAssertNotNil(result) |
| 102 | + |
| 103 | + do { |
| 104 | + _ = try result!.get() |
| 105 | + gotSuccess = true |
| 106 | + } catch let e { |
| 107 | + XCTAssert(e is DecodingError) |
| 108 | + gotFailure = true |
| 109 | + } |
| 110 | + |
| 111 | + XCTAssertTrue(gotFailure) |
| 112 | + XCTAssertFalse(gotSuccess) |
| 113 | + XCTAssertTrue(completed) |
| 114 | + } |
| 115 | + |
| 116 | + func testMapEncodeError() { |
| 117 | + struct ToEncode: Encodable { |
| 118 | + let foo: Int |
| 119 | + |
| 120 | + func encode(to encoder: Encoder) throws { |
| 121 | + throw EncodingError.invalidValue((), EncodingError.Context(codingPath: [], debugDescription: String())) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + let subject = PassthroughSubject<ToEncode, Error>() |
| 126 | + var completed = false |
| 127 | + var gotFailure = false |
| 128 | + var gotSuccess = false |
| 129 | + var result: Result<Data, Error>? = nil |
| 130 | + |
| 131 | + subscription = subject |
| 132 | + .encode(encoder: JSONEncoder()) |
| 133 | + .mapToResult() |
| 134 | + .eraseToAnyPublisher() |
| 135 | + .sink(receiveCompletion: { _ in completed = true }, |
| 136 | + receiveValue: { result = $0 }) |
| 137 | + |
| 138 | + subject.send(ToEncode(foo: 0)) |
| 139 | + XCTAssertNotNil(result) |
| 140 | + |
| 141 | + do { |
| 142 | + _ = try result!.get() |
| 143 | + gotSuccess = true |
| 144 | + } catch let e { |
| 145 | + XCTAssert(e is EncodingError) |
| 146 | + gotFailure = true |
| 147 | + } |
| 148 | + |
| 149 | + XCTAssertTrue(gotFailure) |
| 150 | + XCTAssertFalse(gotSuccess) |
| 151 | + XCTAssertTrue(completed) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +#endif |
0 commit comments