Skip to content

Commit

Permalink
Update Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Jun 10, 2022
1 parent ab77d83 commit 4da5029
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 81 deletions.
61 changes: 61 additions & 0 deletions Tests/NetworkReachabilityTests/NetworkMonitorCombineTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// NetworkReachabiliy
// NetworkMonitorCombineTests.swift
//
// MIT License
//
// Copyright (c) 2021 Varun Santhanam
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
//
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#if canImport(Network)
import Network
#endif

#if canImport(Combine)

import Combine
import Foundation
@testable import NetworkReachability
import XCTest

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
final class NetworkMonitorCombineTests: XCTestCase {

var cancellable: AnyCancellable?

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func test_observe_combine() {
let expectation = expectation(description: "pass")
cancellable = NetworkMonitor
.networkPathPublisher
.map { $0.status == .satisfied }
.removeDuplicates()
.sink { isReachable in
XCTAssertTrue(isReachable)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}

deinit {
cancellable?.cancel()
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// NetworkReachabiliy
// NetworkMonitorConcurrencyTests.swift
//
// MIT License
//
// Copyright (c) 2021 Varun Santhanam
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
//
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#if canImport(Network)
import Network
#endif

import Foundation
@testable import NetworkReachability
import XCTest

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
final class NetworkMonitorConcurrencyTests: XCTestCase {

func test_get_concurrency() async {
let path = await NetworkMonitor.networkPath
XCTAssertEqual(path.status, .satisfied)
}

func test_observe_concurrency() {
let expectation = expectation(description: "pass")

Task {
for await status in NetworkMonitor.networkPathUpdates.map(\.status) {
if status == .satisfied {
expectation.fulfill()
}
}
}
waitForExpectations(timeout: 5, handler: nil)
}

}
47 changes: 1 addition & 46 deletions Tests/NetworkReachabilityTests/NetworkMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#if canImport(Combine)
import Combine
#if canImport(Network)
import Network
#endif

Expand All @@ -34,30 +33,6 @@ import XCTest
@available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *)
final class NetworkMonitorTests: XCTestCase {

#if canImport(Combine)
var cancellable: AnyCancellable?
#endif

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func test_get_concurrency() async {
let path = await NetworkMonitor.networkPath
XCTAssertEqual(path.status, .satisfied)
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func test_observe_concurrency() {
let expectation = expectation(description: "pass")

Task {
for await status in NetworkMonitor.networkPathUpdates.map(\.status) {
if status == .satisfied {
expectation.fulfill()
}
}
}
waitForExpectations(timeout: 5, handler: nil)
}

func test_observe_closure() {
let expectation = expectation(description: "pass")
withExtendedLifetime(NetworkMonitor(updateHandler: { _, path in
Expand All @@ -69,20 +44,6 @@ final class NetworkMonitorTests: XCTestCase {
}
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func test_observe_combine() {
let expectation = expectation(description: "pass")
cancellable = NetworkMonitor
.networkPathPublisher
.map { $0.status == .satisfied }
.removeDuplicates()
.sink { isReachable in
XCTAssertTrue(isReachable)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}

func test_observe_delegate() {
let expectation = expectation(description: "pass")

Expand Down Expand Up @@ -146,10 +107,4 @@ final class NetworkMonitorTests: XCTestCase {
}
}
}

deinit {
if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
cancellable?.cancel()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// NetworkReachabiliy
// ReachabilityMonitorCombineTests.swift
//
// MIT License
//
// Copyright (c) 2021 Varun Santhanam
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
//
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#if canImport(Combine)

import Combine
import Foundation
@testable import NetworkReachability
import XCTest

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
final class ReachabilityMonitorConcurrencyTests: XCTestCase {

var cancellable: AnyCancellable?

func test_observe_combine() {
let expectation = expectation(description: "pass")
cancellable = ReachabilityMonitor
.reachabilityPublisher
.map(\.status.isReachable)
.replaceError(with: false)
.removeDuplicates()
.sink { isReachable in
XCTAssertTrue(isReachable)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}

deinit {
cancellable?.cancel()
}

}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// NetworkReachabiliy
// ReachabilityMonitorConcurrencyTests.swift
//
// MIT License
//
// Copyright (c) 2021 Varun Santhanam
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
//
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation
@testable import NetworkReachability
import XCTest

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
final class ReachabiltyMonitorConcurrencyTests: XCTestCase {

func test_observe_concurrency() {
let expectation = expectation(description: "pass")

Task {
do {
for try await isReachable in ReachabilityMonitor.reachabilityUpdates.map(\.status.isReachable) {
if isReachable {
expectation.fulfill()
} else {
XCTFail()
}
}
} catch {
XCTFail()
}
}
waitForExpectations(timeout: 5, handler: nil)
}

}
35 changes: 0 additions & 35 deletions Tests/NetworkReachabilityTests/ReachabilityMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,6 @@ final class ReachabilityMonitorTests: XCTestCase {
}
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func test_observe_concurrency() {
let expectation = expectation(description: "pass")

Task {
do {
for try await isReachable in ReachabilityMonitor.reachabilityUpdates.map(\.status.isReachable) {
if isReachable {
expectation.fulfill()
} else {
XCTFail()
}
}
} catch {
XCTFail()
}
}
waitForExpectations(timeout: 5, handler: nil)
}

func test_observe_closure() {
let expectation = expectation(description: "pass")
do {
Expand All @@ -87,21 +67,6 @@ final class ReachabilityMonitorTests: XCTestCase {
}
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func test_observe_combine() {
let expectation = expectation(description: "pass")
cancellable = ReachabilityMonitor
.reachabilityPublisher
.map(\.status.isReachable)
.replaceError(with: false)
.removeDuplicates()
.sink { isReachable in
XCTAssertTrue(isReachable)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}

func test_observe_delegate() {
let expectation = expectation(description: "pass")

Expand Down

0 comments on commit 4da5029

Please sign in to comment.