Skip to content

Commit

Permalink
Incorporate type name into failure message. (#208)
Browse files Browse the repository at this point in the history
* Incorporate type name into failure message.

* fix tests
  • Loading branch information
mbrandonw committed Apr 17, 2024
1 parent 7b6763f commit 9620f73
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 86 deletions.
1 change: 1 addition & 0 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ let package = Package(
.testTarget(
name: "DependenciesMacrosPluginTests",
dependencies: [
"DependenciesMacros",
"DependenciesMacrosPlugin",
.product(name: "MacroTesting", package: "swift-macro-testing"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public enum DependencyEndpointMacro: AccessorMacro, PeerMacro {
}
}
unimplementedDefault.statements.insert(
"""
XCTestDynamicOverlay.XCTFail("Unimplemented: '\(raw: unescapedIdentifier)'")
""",
#"""
XCTestDynamicOverlay.XCTFail("Unimplemented: '\(Self.self).\#(raw: unescapedIdentifier)'")
"""#,
at: unimplementedDefault.statements.startIndex
)
for index in unimplementedDefault.statements.indices {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ final class DependencyClientMacroTests: BaseTestCase {
}
"""
} expansion: {
"""
#"""
struct Client {@available(iOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(macOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(tvOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(watchOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.")
var fetch: (_ id: Int) throws -> String {
@storageRestrictions(initializes: _fetch)
Expand All @@ -729,7 +729,7 @@ final class DependencyClientMacroTests: BaseTestCase {
}
@available(iOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(macOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(tvOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(watchOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") private var _fetch: (_ id: Int) throws -> String = { _ in
XCTestDynamicOverlay.XCTFail("Unimplemented: 'fetch'")
XCTestDynamicOverlay.XCTFail("Unimplemented: '\(Self.self).fetch'")
throw DependenciesMacros.Unimplemented("fetch")
}
Expand All @@ -742,7 +742,7 @@ final class DependencyClientMacroTests: BaseTestCase {
init() {
}
}
"""
"""#
}
}

Expand All @@ -756,7 +756,7 @@ final class DependencyClientMacroTests: BaseTestCase {
}
"""
} expansion: {
"""
#"""
struct Client {
@available(iOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(macOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(tvOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(watchOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.")
var fetch: (_ id: Int) throws -> String {
Expand All @@ -777,7 +777,7 @@ final class DependencyClientMacroTests: BaseTestCase {
}
@available(iOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(macOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(tvOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(watchOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") private var _fetch: (_ id: Int) throws -> String = { _ in
XCTestDynamicOverlay.XCTFail("Unimplemented: 'fetch'")
XCTestDynamicOverlay.XCTFail("Unimplemented: '\(Self.self).fetch'")
throw DependenciesMacros.Unimplemented("fetch")
}
Expand All @@ -790,7 +790,7 @@ final class DependencyClientMacroTests: BaseTestCase {
init() {
}
}
"""
"""#
}
}

Expand All @@ -803,7 +803,7 @@ final class DependencyClientMacroTests: BaseTestCase {
}
"""
} expansion: {
"""
#"""
struct Client {
var fetch: (Int) throws -> String {
@storageRestrictions(initializes: _fetch)
Expand All @@ -819,7 +819,7 @@ final class DependencyClientMacroTests: BaseTestCase {
}
private var _fetch: (Int) throws -> String = { _ in
XCTestDynamicOverlay.XCTFail("Unimplemented: 'fetch'")
XCTestDynamicOverlay.XCTFail("Unimplemented: '\(Self.self).fetch'")
throw DependenciesMacros.Unimplemented("fetch")
}
Expand All @@ -832,7 +832,7 @@ final class DependencyClientMacroTests: BaseTestCase {
init() {
}
}
"""
"""#
}
}

Expand Down
53 changes: 53 additions & 0 deletions Tests/DependenciesMacrosPluginTests/DependencyClientTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#if canImport(ObjectiveC)
import DependenciesMacros
import XCTest

final class DependencyClientTests: BaseTestCase {
func testUnimplementedEndpoint() throws {
let client = Client()

XCTExpectFailure {
$0.compactDescription == """
Unimplemented: 'Client.fetch'
"""
}

do {
let _ = try client.fetch()
XCTFail("Client.fetch should throw an error.")
} catch {
}
}

func testSwiftBug() {
let client = ClientWithNonThrowingEndpoint()

// NB: This should cause a test failure but currently does not due to a Swift compiler bug:
// https://github.com/apple/swift/issues/71070
XCTAssertEqual(client.fetch(), 42)

XCTExpectFailure {
XCTAssertEqual(client.fetchWithUnimplemented(), 42)
} issueMatcher: {
$0.compactDescription == """
Unimplemented …
Defined at:
DependenciesMacrosPluginTests/DependencyClientTests.swift:\(ClientWithNonThrowingEndpoint.line + 1)
"""
}
}
}

@DependencyClient
struct Client {
var fetch: () throws -> Int
}

@DependencyClient
struct ClientWithNonThrowingEndpoint {
var fetch: () -> Int = { 42 }
static let line = #line
var fetchWithUnimplemented: () -> Int = { unimplemented(placeholder: 42) }
}
#endif
Loading

0 comments on commit 9620f73

Please sign in to comment.