|
| 1 | +// |
| 2 | +// MapToValueTests.swift |
| 3 | +// CombineExt |
| 4 | +// |
| 5 | +// Created by Dan Halliday on 08/05/2022. |
| 6 | +// Copyright © 2022 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 MapToValueTests: XCTestCase { |
| 18 | + private var subscription: AnyCancellable! |
| 19 | + |
| 20 | + func testMapToConstantValue() { |
| 21 | + let subject = PassthroughSubject<Int, Never>() |
| 22 | + var result: Int? = nil |
| 23 | + |
| 24 | + subscription = subject |
| 25 | + .mapToValue(2) |
| 26 | + .sink(receiveValue: { result = $0 }) |
| 27 | + |
| 28 | + subject.send(1) |
| 29 | + XCTAssertEqual(result, 2) |
| 30 | + } |
| 31 | + |
| 32 | + func testMapToWithMultipleElements() { |
| 33 | + let expectation = XCTestExpectation() |
| 34 | + expectation.expectedFulfillmentCount = 3 |
| 35 | + |
| 36 | + let subject = PassthroughSubject<Int, Never>() |
| 37 | + |
| 38 | + subscription = subject |
| 39 | + .mapToValue("hello") |
| 40 | + .sink { element in |
| 41 | + XCTAssertEqual(element, "hello") |
| 42 | + expectation.fulfill() |
| 43 | + } |
| 44 | + |
| 45 | + subject.send(1) |
| 46 | + subject.send(2) |
| 47 | + subject.send(1) |
| 48 | + |
| 49 | + wait(for: [expectation], timeout: 3) |
| 50 | + } |
| 51 | + |
| 52 | + func testMapToVoidType() { |
| 53 | + let expectation = XCTestExpectation() |
| 54 | + let subject = PassthroughSubject<Int, Never>() |
| 55 | + |
| 56 | + subscription = subject |
| 57 | + .mapToValue(Void()) |
| 58 | + .sink { element in |
| 59 | + XCTAssertTrue(type(of: element) == Void.self) |
| 60 | + |
| 61 | + expectation.fulfill() |
| 62 | + } |
| 63 | + |
| 64 | + subject.send(1) |
| 65 | + |
| 66 | + wait(for: [expectation], timeout: 3) |
| 67 | + } |
| 68 | + |
| 69 | + func testMapToOptionalType() { |
| 70 | + let subject = PassthroughSubject<Int, Never>() |
| 71 | + let value: String? = nil |
| 72 | + |
| 73 | + var result: String? = nil |
| 74 | + |
| 75 | + subscription = subject |
| 76 | + .mapToValue(value) |
| 77 | + .sink(receiveValue: { result = $0 }) |
| 78 | + |
| 79 | + subject.send(1) |
| 80 | + XCTAssertEqual(result, nil) |
| 81 | + } |
| 82 | + |
| 83 | + /// Checks if regular map functions complies and works as expected. |
| 84 | + func testMapNameCollision() { |
| 85 | + let fooSubject = PassthroughSubject<Int, Never>() |
| 86 | + let barSubject = PassthroughSubject<Int, Never>() |
| 87 | + |
| 88 | + var result: String? = nil |
| 89 | + |
| 90 | + let combinedPublisher = Publishers.CombineLatest(fooSubject, barSubject) |
| 91 | + .map { fooItem, barItem in |
| 92 | + fooItem * barItem |
| 93 | + } |
| 94 | + |
| 95 | + subscription = combinedPublisher |
| 96 | + .map { |
| 97 | + "\($0)" |
| 98 | + } |
| 99 | + .sink(receiveValue: { result = $0 }) |
| 100 | + |
| 101 | + fooSubject.send(5) |
| 102 | + barSubject.send(6) |
| 103 | + XCTAssertEqual(result, "30") |
| 104 | + } |
| 105 | +} |
| 106 | +#endif |
0 commit comments