-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Optional.publisher as a convenience for Optional.Publisher.init. (…
…#32)
- Loading branch information
Showing
5 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Optional.swift | ||
// CombineExt | ||
// | ||
// Created by Jasdev Singh on 11/05/2020. | ||
// Copyright © 2020 Combine Community. All rights reserved. | ||
// | ||
|
||
#if canImport(Combine) | ||
import Combine | ||
|
||
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public extension Optional { | ||
/// A publisher that publishes an optional value to each subscriber exactly once, if the optional has a value. | ||
var publisher: Optional.Publisher { | ||
Optional.Publisher(self) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// OptionalTests.swift | ||
// CombineExt | ||
// | ||
// Created by Jasdev Singh on 11/05/2020. | ||
// Copyright © 2020 Combine Community. All rights reserved. | ||
// | ||
|
||
#if !os(watchOS) | ||
import Combine | ||
import CombineExt | ||
import XCTest | ||
|
||
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
final class OptionalTests: XCTestCase { | ||
private var subscription: AnyCancellable! | ||
|
||
func testSomeInitialization() { | ||
var results = [Int]() | ||
var completion: Subscribers.Completion<Never>? | ||
|
||
subscription = Optional(1) | ||
.publisher | ||
.sink(receiveCompletion: { completion = $0 }, | ||
receiveValue: { results.append($0) }) | ||
|
||
XCTAssertEqual([1], results) | ||
XCTAssertEqual(.finished, completion) | ||
} | ||
|
||
func testNoneInitialization() { | ||
var results = [Int]() | ||
var completion: Subscribers.Completion<Never>? | ||
|
||
subscription = Optional<Int>.none | ||
.publisher | ||
.sink(receiveCompletion: { completion = $0 }, | ||
receiveValue: { results.append($0) }) | ||
|
||
XCTAssertTrue(results.isEmpty) | ||
XCTAssertEqual(.finished, completion) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters