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

Merged
Merged
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
2 changes: 2 additions & 0 deletions Sources/MockingClient/MethodOverloads.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// Copyright © 2025 Fetch.
//

// swiftformat:disable opaqueGenericParameters

import Foundation
public import Mocking

Expand Down
33 changes: 25 additions & 8 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,16 +151,30 @@ 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)
.compactMap { CompositionTypeElementSyntax(type: $0) }

let composedInheritedTypes = inheritanceClause
.inheritedTypes(ofType: CompositionTypeSyntax.self)
.flatMap(\.elements)

let inheritedTypes = commaSeparatedInheritedTypes + composedInheritedTypes
let lastIndex = inheritedTypes.count - 1
let inheritedTypeElements = CompositionTypeElementListSyntax {
for (index, inheritedType) in inheritedTypes.enumerated() {
inheritedType
.trimmed
.with(\.ampersand, index < lastIndex ? .binaryOperator("&") : nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

.binaryOperator("&") is the ampersand token used in Swift Syntax's CompositionTypeElementListSyntax by default.

}
}

GenericParameterSyntax(
name: genericParameterName,
colon: .colonToken(),
inheritedType: TypeSyntax(stringLiteral: genericInheritedType)
inheritedType: CompositionTypeSyntax(elements: inheritedTypeElements)
)
} else {
GenericParameterSyntax(name: genericParameterName)
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