From 86da8c22272d2f3efc4899fb64bba71d047d57b3 Mon Sep 17 00:00:00 2001 From: yoshiki-tsukada Date: Fri, 21 Jun 2024 16:29:27 +0900 Subject: [PATCH 1/3] Support existential type --- .../Factories/ReceivedArgumentsFactory.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/SpyableMacro/Factories/ReceivedArgumentsFactory.swift b/Sources/SpyableMacro/Factories/ReceivedArgumentsFactory.swift index 3a43f6b..9ee89b4 100644 --- a/Sources/SpyableMacro/Factories/ReceivedArgumentsFactory.swift +++ b/Sources/SpyableMacro/Factories/ReceivedArgumentsFactory.swift @@ -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, From fee164eb4a8f46df18d3c36956d54c7716693a43 Mon Sep 17 00:00:00 2001 From: yoshiki-tsukada Date: Fri, 21 Jun 2024 16:29:44 +0900 Subject: [PATCH 2/3] Add test case to ReceivedArgumentsFactory --- .../Factories/UT_ReceivedArgumentsFactory.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Tests/SpyableMacroTests/Factories/UT_ReceivedArgumentsFactory.swift b/Tests/SpyableMacroTests/Factories/UT_ReceivedArgumentsFactory.swift index 4189d55..949e530 100644 --- a/Tests/SpyableMacroTests/Factories/UT_ReceivedArgumentsFactory.swift +++ b/Tests/SpyableMacroTests/Factories/UT_ReceivedArgumentsFactory.swift @@ -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))", From c22f00be7f5bc5199115b3d54c572de6759395a4 Mon Sep 17 00:00:00 2001 From: yoshiki-tsukada Date: Fri, 21 Jun 2024 16:29:56 +0900 Subject: [PATCH 3/3] Add test case to SpyFactory --- .../Factories/UT_SpyFactory.swift | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Tests/SpyableMacroTests/Factories/UT_SpyFactory.swift b/Tests/SpyableMacroTests/Factories/UT_SpyFactory.swift index 4a6ffb4..6845ca9 100644 --- a/Tests/SpyableMacroTests/Factories/UT_SpyFactory.swift +++ b/Tests/SpyableMacroTests/Factories/UT_SpyFactory.swift @@ -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: """