Skip to content

Handle composed and mixed associated type constraints #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions Sources/MockingMacros/Macros/MockedMacro/MockedMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,16 @@ extension MockedMacro {
/// Returns the generic parameter clause to apply to the mock declaration,
/// generated from the associated types defined by the provided protocol.
///
/// The clause supports associated types with comma-separated constraints,
/// composition (`&`), or a combination of both.
///
/// ```swift
/// @Mocked
/// protocol Dependency {
/// associatedtype Item: Equatable, Identifiable
/// associatedtype Item: Equatable & Identifiable, Sendable
/// }
///
/// final class DependencyMock<Item: Equatable & Identifiable>: Dependency {}
/// final class DependencyMock<Item: Sendable & Equatable & Identifiable>: Dependency {}
/// ```
///
/// - Parameter protocolDeclaration: The protocol to which the mock must
Expand All @@ -148,12 +151,21 @@ extension MockedMacro {
return GenericParameterClauseSyntax {
for associatedTypeDeclaration in associatedTypeDeclarations {
let genericParameterName = associatedTypeDeclaration.name.trimmed
let genericInheritedType = associatedTypeDeclaration.inheritanceClause?
.inheritedTypes(ofType: IdentifierTypeSyntax.self)
.map(\.name.text)
.joined(separator: " & ")

if let genericInheritedType {
if let inheritanceClause = associatedTypeDeclaration.inheritanceClause {
let commaSeparatedInheritedTypes = inheritanceClause
.inheritedTypes(ofType: IdentifierTypeSyntax.self)

let composedInheritedTypes = inheritanceClause
.inheritedTypes(ofType: CompositionTypeSyntax.self)
.flatMap(\.elements)
.compactMap { $0.type.as(IdentifierTypeSyntax.self) }

let genericInheritedType =
(commaSeparatedInheritedTypes + composedInheritedTypes)
Comment on lines +164 to +165
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clean up the line wrapping a little bit, assuming this doesn't have the same issue with going over the line limit:

Suggested change
let genericInheritedType =
(commaSeparatedInheritedTypes + composedInheritedTypes)
let inheritedTypes = commaSeparatedInheritedTypes + composedInheritedTypes
let genericInheritedType = inheritedTypes

.map(\.name.text)
.joined(separator: " & ")
Comment on lines +164 to +167
Copy link
Collaborator

@graycampbell graycampbell May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nolinmcfarland It would probably be more correct for us to update this to be an instance of CompositionTypeSyntax instead of a string literal. Do you want to take a stab at that, or would you like me to push a commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call out, I'll look into this!


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging both comma-separated and composed inherited types allows us to handle mixed constraint cases correctly.

GenericParameterSyntax(
name: genericParameterName,
colon: .colonToken(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct Mocked_AssociatedTypeTests {
}

@Test(arguments: mockedTestConfigurations)
func protocolAssociatedTypeInheritanceWithMultipleInheritedTypes(
func protocolAssociatedTypeInheritanceWithMultipleCommaSeparatedInheritedTypes(
interface: InterfaceConfiguration,
mock: MockConfiguration
) {
Expand All @@ -61,6 +61,56 @@ struct Mocked_AssociatedTypeTests {
)
}

@Test(arguments: mockedTestConfigurations)
func protocolAssociatedTypeInheritanceWithMultipleComposedInheritedTypes(
interface: InterfaceConfiguration,
mock: MockConfiguration
) {
assertMocked(
"""
\(interface.accessLevel) protocol Dependency {
associatedtype A: Hashable & Identifiable
associatedtype B: Comparable & Equatable & RawRepresentable
}
""",
generates: """
#if SWIFT_MOCKING_ENABLED
@MockedMembers
\(mock.modifiers)\
class DependencyMock<\
A: Hashable & Identifiable, \
B: Comparable & Equatable & RawRepresentable\
>: Dependency {
}
#endif
"""
)
}

@Test(arguments: mockedTestConfigurations)
func protocolAssociatedTypeInheritanceWithMultipleMixedInheritedTypes(
interface: InterfaceConfiguration,
mock: MockConfiguration
) {
assertMocked(
"""
\(interface.accessLevel) protocol Dependency {
associatedtype A: Sendable, Hashable & Identifiable
}
""",
generates: """
#if SWIFT_MOCKING_ENABLED
@MockedMembers
\(mock.modifiers)\
class DependencyMock<\
A: Sendable & Hashable & Identifiable\
>: Dependency {
}
#endif
"""
)
}

// MARK: Associated Type Generic Where Clauses Tests

@Test(arguments: mockedTestConfigurations)
Expand Down
Loading