Skip to content

Commit

Permalink
Merge pull request #93 from yoshiki-tsukada/feature/support-existenti…
Browse files Browse the repository at this point in the history
…al-type

Fix to allow existential type to function arguments
  • Loading branch information
Matejkob authored Jun 24, 2024
2 parents 76717f8 + c22f00b commit 8c8e265
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/SpyableMacro/Factories/ReceivedArgumentsFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ struct ReceivedArgumentsFactory {
),
questionMark: .postfixQuestionMarkToken()
)
} else if onlyParameterType.is(SomeOrAnyTypeSyntax.self) {
variableType = OptionalTypeSyntax(
wrappedType: TupleTypeSyntax(
elements: TupleTypeElementListSyntax {
TupleTypeElementSyntax(type: onlyParameterType)
}
),
questionMark: .postfixQuestionMarkToken()
)
} else {
variableType = OptionalTypeSyntax(
wrappedType: onlyParameterType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ final class UT_ReceivedArgumentsFactory: XCTestCase {
)
}

func testVariableDeclarationSingleExistentialTypeArgument() throws {
try assertProtocolFunction(
withFunctionDeclaration: "func foo(bar: any BarProtocol)",
prefixForVariable: "_prefix_",
expectingVariableDeclaration: "var _prefix_ReceivedBar: (any BarProtocol)?"
)
}

func testVariableDeclarationSingleArgumentDoubleParameterName() throws {
try assertProtocolFunction(
withFunctionDeclaration: "func foo(firstName secondName: (String, Int))",
Expand Down
27 changes: 27 additions & 0 deletions Tests/SpyableMacroTests/Factories/UT_SpyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ final class UT_SpyFactory: XCTestCase {
)
}

func testDeclarationExistentialTypeArguments() throws {
try assertProtocol(
withDeclaration: """
protocol ViewModelProtocol {
func foo(model: any ModelProtocol)
}
""",
expectingClassDeclaration: """
class ViewModelProtocolSpy: ViewModelProtocol {
var fooModelCallsCount = 0
var fooModelCalled: Bool {
return fooModelCallsCount > 0
}
var fooModelReceivedModel: (any ModelProtocol)?
var fooModelReceivedInvocations: [any ModelProtocol] = []
var fooModelClosure: ((any ModelProtocol) -> Void)?
func foo(model: any ModelProtocol) {
fooModelCallsCount += 1
fooModelReceivedModel = (model)
fooModelReceivedInvocations.append((model))
fooModelClosure?(model)
}
}
"""
)
}

func testDeclarationEscapingAutoClosureArgument() throws {
try assertProtocol(
withDeclaration: """
Expand Down

0 comments on commit 8c8e265

Please sign in to comment.