From 42bc4845db7f90863e6d1f2d296a7db99a826195 Mon Sep 17 00:00:00 2001 From: Dima Koskin Date: Sun, 13 Nov 2022 18:48:33 +0300 Subject: [PATCH] Remove fatalError check --- .../FatalErrorUtility.swift | 61 ------------------- .../PlatformSpecificValueTests.swift | 21 ------- 2 files changed, 82 deletions(-) delete mode 100644 Sources/PlatformSpecificValue/FatalErrorUtility.swift diff --git a/Sources/PlatformSpecificValue/FatalErrorUtility.swift b/Sources/PlatformSpecificValue/FatalErrorUtility.swift deleted file mode 100644 index b0ff6ac..0000000 --- a/Sources/PlatformSpecificValue/FatalErrorUtility.swift +++ /dev/null @@ -1,61 +0,0 @@ -// -// FatalErrorUtility.swift -// -// -// Created by Dima Koskin on 13.11.2022. -// - -import Foundation -import XCTest - - -// MARK: Mock fatalError() -// Source: https://stackoverflow.com/questions/32873212/unit-test-fatalerror-in-swift - - -internal extension XCTestCase { - func XCTestFatalError(_ message: String, testCase: @escaping () -> Void) { - let expectation = self.expectation(description: "expectingFatalError") - var assertionMessage: String? = nil - - FatalErrorUtility.replaceFatalError { message, _, _ in - DispatchQueue.main.async { - assertionMessage = message - expectation.fulfill() - } - Thread.exit() - fatalError("It will never be executed") - } - - Thread(block: testCase).start() - - waitForExpectations(timeout: 0.1) { _ in - XCTAssertEqual(assertionMessage, message) - FatalErrorUtility.restoreFatalError() - } - } -} - - -internal func fatalError(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never { - FatalErrorUtility.fatalErrorClosure(message(), file, line) -} - - -internal enum FatalErrorUtility { - typealias FatalErrorClosureType = (String, StaticString, UInt) -> Never - - static var fatalErrorClosure: FatalErrorClosureType = defaultFatalErrorClosure - - private static let defaultFatalErrorClosure: FatalErrorClosureType = { - Swift.fatalError($0, file: $1, line: $2) - } - - static func replaceFatalError(closure: @escaping FatalErrorClosureType) { - fatalErrorClosure = closure - } - - static func restoreFatalError() { - fatalErrorClosure = defaultFatalErrorClosure - } -} diff --git a/Tests/PlatformSpecificValueTests/PlatformSpecificValueTests.swift b/Tests/PlatformSpecificValueTests/PlatformSpecificValueTests.swift index 8beb6bd..80637c6 100644 --- a/Tests/PlatformSpecificValueTests/PlatformSpecificValueTests.swift +++ b/Tests/PlatformSpecificValueTests/PlatformSpecificValueTests.swift @@ -2,7 +2,6 @@ import XCTest @testable import PlatformSpecificValue - final class PlatformSpecificValueTests: XCTestCase { func testThatPlatformSpecificFunctionReturnsTheRightValue() throws { let value: String = platformSpecific([.macOS("macOS"), .iOS("iOS"), .watchOS("watchOS"), .tvOS("tvOS")]) @@ -19,24 +18,4 @@ final class PlatformSpecificValueTests: XCTestCase { #error("Testing on an unsupported platform") #endif } - - func testThatPlatformSpecificFunctionThrowsFatalErrorIfThereIsNoAppropriateValue() throws { - let values: Set> = [.macOS("macOS"), .iOS("iOS"), .watchOS("watchOS"), .tvOS("tvOS")] - - XCTestFatalError("No value was provided.") { - #if os(macOS) - let filteredValues = values.filter { $0 != .macOS("macOS") } - #elseif os(iOS) - let filteredValues = values.filter { $0 != .iOS("iOS") } - #elseif os(watchOS) - let filteredValues = values.filter { $0 != .watchOS("watchOS") } - #elseif os(tvOS) - let filteredValues = values.filter { $0 != .tvOS("tvOS") } - #else - #error("Testing on an unsupported platform") - #endif - _ = platformSpecific(filteredValues) - } - } } -