Skip to content

Commit

Permalink
Swift Testing support (#178)
Browse files Browse the repository at this point in the history
* Swift Testing support

* wip

* wip

* wip

* wip
  • Loading branch information
stephencelis authored Jul 22, 2024
1 parent c395e60 commit 031704b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 39 deletions.
18 changes: 9 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"version" : "1.0.0"
}
},
{
"identity" : "swift-issue-reporting",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-issue-reporting",
"state" : {
"revision" : "926f43898706eaa127db79ac42138e1ad7e85a3f",
"version" : "1.2.0"
}
},
{
"identity" : "swift-macro-testing",
"kind" : "remoteSourceControl",
Expand All @@ -44,15 +53,6 @@
"revision" : "4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c",
"version" : "600.0.0-prerelease-2024-06-12"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "6f30bdba373bbd7fbfe241dddd732651f2fbd1e2",
"version" : "1.1.2"
}
}
],
"version" : 2
Expand Down
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"),
.package(url: "https://github.com/pointfreeco/swift-issue-reporting", from: "1.2.0"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.0.0"),
],
targets: [
.target(
name: "CasePaths",
dependencies: [
"CasePathsMacros",
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
.product(name: "IssueReporting", package: "swift-issue-reporting"),
.product(name: "XCTestDynamicOverlay", package: "swift-issue-reporting"),
]
),
.testTarget(
Expand Down
5 changes: 3 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"),
.package(url: "https://github.com/pointfreeco/swift-issue-reporting", from: "1.2.0"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.0.0"),
],
targets: [
.target(
name: "CasePaths",
dependencies: [
"CasePathsMacros",
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
.product(name: "IssueReporting", package: "swift-issue-reporting"),
.product(name: "XCTestDynamicOverlay", package: "swift-issue-reporting"),
]
),
.testTarget(
Expand Down
16 changes: 10 additions & 6 deletions Sources/CasePaths/CasePathable.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import XCTestDynamicOverlay
import IssueReporting

/// A type that provides a collection of all of its case paths.
///
Expand Down Expand Up @@ -479,18 +479,22 @@ extension CasePathable {
public mutating func modify<Value>(
_ keyPath: CaseKeyPath<Self, Value>,
yield: (inout Value) -> Void,
file: StaticString = #filePath,
line: UInt = #line
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) {
let `case` = Case(keyPath)
guard var value = `case`.extract(from: self) else {
XCTFail(
reportIssue(
"""
Can't modify '\(String(describing: self))' via 'CaseKeyPath<\(Self.self), \(Value.self)>' \
(aka '\(String(reflecting: keyPath))')
""",
file: file,
line: line
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/CasePaths/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public func XCTUnwrap<Enum, Case>(
let message = message()
XCTFail(
"""
XCTUnwrap failed: expected to extract value of type "\(typeName(Case.self))" from \
XCTUnwrap: Expected to extract value of type "\(typeName(Case.self))" from \
"\(typeName(Enum.self))"\
\(message.isEmpty ? "" : " - " + message)
Expand Down
12 changes: 7 additions & 5 deletions Sources/CasePaths/XCTestSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import Foundation
/// - optional: An optional value.
/// - message: An optional description of a failure.
/// - body: A closure that can modify the wrapped value of the given optional.
@available(*, deprecated, message: "Use 'CasePathable.modify' to mutate an expected case, instead.")
public func XCTModify<Wrapped>(
_ optional: inout Wrapped?,
_ message: @autoclosure () -> String = "",
_ body: (inout Wrapped) throws -> Void,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) {
XCTModify(&optional, case: \.some, message(), body, file: file, line: line)
Expand All @@ -24,12 +25,13 @@ public func XCTModify<Wrapped>(
/// - casePath: A case path that can extract and embed the associated value of a particular case.
/// - message: An optional description of a failure.
/// - body: A closure that can modify the associated value of the given case.
@available(*, deprecated, message: "Use 'CasePathable.modify' to mutate an expected case, instead.")
public func XCTModify<Enum, Case>(
_ enum: inout Enum,
case keyPath: CaseKeyPath<Enum, Case>,
_ message: @autoclosure () -> String = "",
_ body: (inout Case) throws -> Void,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) {
_XCTModify(&`enum`, case: AnyCasePath(keyPath), message(), body, file: file, line: line)
Expand All @@ -40,7 +42,7 @@ func _XCTModify<Enum, Case>(
case casePath: AnyCasePath<Enum, Case>,
_ message: @autoclosure () -> String = "",
_ body: (inout Case) throws -> Void,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) {
guard var value = casePath.extract(from: `enum`)
Expand All @@ -51,7 +53,7 @@ func _XCTModify<Enum, Case>(
let message = message()
XCTFail(
"""
XCTModify failed: expected to extract value of type "\(typeName(Case.self))" from \
XCTModify: Expected to extract value of type "\(typeName(Case.self))" from \
"\(typeName(Enum.self))"\
\(message.isEmpty ? "" : " - " + message)
Expand All @@ -77,7 +79,7 @@ func _XCTModify<Enum, Case>(
{
XCTFail(
"""
XCTModify failed: expected "\(typeName(Case.self))" value to be modified but it was unchanged.
XCTModify: Expected "\(typeName(Case.self))" value to be modified but it was unchanged.
"""
)
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/CasePathsTests/DeprecatedXCTModifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected to extract value of type "Int" from "Result<Int, Error>"
failed - XCTModify: Expected to extract value of type "Int" from "Result<Int, Error>"
Actual:
failure(CasePathsTests.DeprecatedXCTModifyTests.SomeError())
Expand All @@ -41,7 +41,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected to extract value of type \
failed - XCTModify: Expected to extract value of type \
"DeprecatedXCTModifyTests.Sheet.State" from \
"DeprecatedXCTModifyTests.Destination.State?"
Expand All @@ -61,7 +61,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected to extract value of type "Int" from \
failed - XCTModify: Expected to extract value of type "Int" from \
"Optional<Result<Int, Error>>"
Actual:
Expand All @@ -80,7 +80,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected to extract value of type "Int" from "Result<Int, Error>" - \
failed - XCTModify: Expected to extract value of type "Int" from "Result<Int, Error>" - \
Should be success …
Actual:
Expand Down Expand Up @@ -119,7 +119,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected "Int" value to be modified but it was unchanged.
failed - XCTModify: Expected "Int" value to be modified but it was unchanged.
"""
}

Expand All @@ -135,7 +135,7 @@

XCTExpectFailure {
$0.compactDescription == """
Threw error: SomeError()
failed - Threw error: SomeError()
"""
}

Expand Down
12 changes: 6 additions & 6 deletions Tests/CasePathsTests/XCTModifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected to extract value of type "Int" from "Result<Int, Error>"
failed - XCTModify: Expected to extract value of type "Int" from "Result<Int, Error>"
Actual:
failure(CasePathsTests.XCTModifyTests.SomeError())
Expand All @@ -41,7 +41,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected to extract value of type "XCTModifyTests.Sheet.State" from \
failed - XCTModify: Expected to extract value of type "XCTModifyTests.Sheet.State" from \
"XCTModifyTests.Destination.State?"
Actual:
Expand All @@ -60,7 +60,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected to extract value of type "Int" from \
failed - XCTModify: Expected to extract value of type "Int" from \
"Optional<Result<Int, Error>>"
Actual:
Expand All @@ -79,7 +79,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected to extract value of type "Int" from "Result<Int, Error>" - \
failed - XCTModify: Expected to extract value of type "Int" from "Result<Int, Error>" - \
Should be success …
Actual:
Expand Down Expand Up @@ -118,7 +118,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTModify failed: expected "Int" value to be modified but it was unchanged.
failed - XCTModify: Expected "Int" value to be modified but it was unchanged.
"""
}

Expand All @@ -134,7 +134,7 @@

XCTExpectFailure {
$0.compactDescription == """
Threw error: SomeError()
failed - Threw error: SomeError()
"""
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/CasePathsTests/XCTUnwrapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTUnwrap failed: expected to extract value of type "Error" from "Result<Int, Error>"
failed - XCTUnwrap: Expected to extract value of type "Error" from "Result<Int, Error>"
Actual:
success(2)
Expand All @@ -23,7 +23,7 @@

XCTExpectFailure {
$0.compactDescription == """
XCTUnwrap failed: expected to extract value of type "Error" from "Result<Int, Error>" - \
failed - XCTUnwrap: Expected to extract value of type "Error" from "Result<Int, Error>" - \
Should be 'failure' …
Actual:
Expand Down

0 comments on commit 031704b

Please sign in to comment.