-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add XCTUnwrap(_:case:) test helper. (#95)
* Add XCTUnwrap(_:case:) test helper. * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Update Package.swift * Update XCTUnwrap.swift * Update XCTUnwrapTests.swift * skip * Update .github/workflows/ci.yml * wip * Update XCTUnwrap.swift * Update Sources/CasePaths/XCTUnwrap.swift Co-authored-by: Stephen Celis <stephen@stephencelis.com>
- Loading branch information
1 parent
bb43642
commit c3a42e8
Showing
6 changed files
with
103 additions
and
30 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
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,38 @@ | ||
import Foundation | ||
@_spi(CurrentTestCase) import XCTestDynamicOverlay | ||
|
||
/// Asserts that an enum value matches a particular case and returns the associated value. | ||
/// | ||
/// - Parameters: | ||
/// - expression: An enum value. | ||
/// - extract: A closure that attempts to extract a particular case from the enum. You can supply | ||
/// a case path literal here, like '/Enum.case'. | ||
/// - message: An optional description of a failure. | ||
/// - Returns: The unwrapped associated value from the matched case of the enum. | ||
public func XCTUnwrap<Root, Case>( | ||
_ expression: @autoclosure () throws -> Root, | ||
case extract: (Root) -> Case?, | ||
_ message: @autoclosure () -> String = "", | ||
file: StaticString = #file, | ||
line: UInt = #line | ||
) throws -> Case { | ||
guard let value = try extract(expression()) | ||
else { | ||
#if canImport(ObjectiveC) | ||
_ = XCTCurrentTestCase?.perform(Selector(("setContinueAfterFailure:")), with: false) | ||
#endif | ||
let message = message() | ||
XCTFail( | ||
""" | ||
XCTUnwrap failed: expected non-nil value of type "\(Case.self)"\ | ||
\(message.isEmpty ? "" : " - " + message) | ||
""", | ||
file: file, | ||
line: line | ||
) | ||
throw UnwrappingCase() | ||
} | ||
return value | ||
} | ||
|
||
private struct UnwrappingCase: Error {} |
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,37 @@ | ||
#if DEBUG && (os(iOS) || os(macOS) || os(tvOS) || os(watchOS)) | ||
import CasePaths | ||
import XCTest | ||
|
||
final class XCTUnwrapTests: XCTestCase { | ||
func testXCTUnwrapFailure() throws { | ||
try XCTSkipIf(ProcessInfo.processInfo.environment["CI"] != nil) | ||
|
||
XCTExpectFailure { | ||
$0.compactDescription == """ | ||
XCTUnwrap failed: expected non-nil value of type "Error" | ||
""" | ||
} | ||
_ = try XCTUnwrap(Result<Int, Error>.success(2), case: /Result.failure) | ||
} | ||
|
||
func testXCTUnwrapFailure_WithMessage() throws { | ||
try XCTSkipIf(ProcessInfo.processInfo.environment["CI"] != nil) | ||
|
||
XCTExpectFailure { | ||
$0.compactDescription == """ | ||
XCTUnwrap failed: expected non-nil value of type "Error" - Should be success | ||
""" | ||
} | ||
_ = try XCTUnwrap(Result<Int, Error>.success(2), case: /Result.failure, "Should be success") | ||
} | ||
|
||
func testXCTUnwrapPass() throws { | ||
try XCTSkipIf(ProcessInfo.processInfo.environment["CI"] != nil) | ||
|
||
XCTAssertEqual( | ||
try XCTUnwrap(Result<Int, Error>.success(2), case: /Result.success), | ||
2 | ||
) | ||
} | ||
} | ||
#endif |