Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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)
.map(\.name.text)
.joined(separator: " & ")

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