Skip to content

Commit aca9b98

Browse files
authored
⚠️ Fix map(to:) name collision (#128)
1 parent dcfb8a8 commit aca9b98

File tree

3 files changed

+109
-35
lines changed

3 files changed

+109
-35
lines changed

Sources/Operators/MapTo.swift renamed to Sources/Operators/MapToValue.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// MapTo.swift
2+
// MapToValue.swift
33
// CombineExt
44
//
55
// Created by Dan Halliday on 08/05/2022.
@@ -14,8 +14,8 @@ public extension Publisher {
1414
/// Replace each upstream value with a constant.
1515
///
1616
/// - Parameter value: The constant with which to replace each upstream value.
17-
/// - Returns: A new publisher wrapping the upstream, but with output type `Result`.
18-
func map<Result>(to value: Result) -> Publishers.Map<Self, Result> {
17+
/// - Returns: A new publisher wrapping the upstream, but with output type `Value`.
18+
func mapToValue<Value>(_ value: Value) -> Publishers.Map<Self, Value> {
1919
map { _ in value }
2020
}
2121
}

Tests/MapToTests.swift

-32
This file was deleted.

Tests/MapToValueTests.swift

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)