diff --git a/CHANGELOG.md b/CHANGELOG.md index ad6abfdc2..27f550a39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Add support to any keyword for function return type to AutoMockable.stencil([#1186](https://github.com/krzysztofzablocki/Sourcery/pull/1186)) - Add support for protocol compositions in EJS templates. ([#1192](https://github.com/krzysztofzablocki/Sourcery/pull/1192)) - Linux Support (experimental) ([#1188](https://github.com/krzysztofzablocki/Sourcery/pull/1188)) +- Add support for opaque type (some keyword) to function parameter type in AutoMockable.stencil ([#1197](https://github.com/krzysztofzablocki/Sourcery/pull/1197)) ## 2.0.3 ## Internal Changes diff --git a/Templates/Templates/AutoMockable.stencil b/Templates/Templates/AutoMockable.stencil index b795c4b15..8640a96e2 100755 --- a/Templates/Templates/AutoMockable.stencil +++ b/Templates/Templates/AutoMockable.stencil @@ -202,9 +202,9 @@ import {{ import }} {% macro underlyingMockedVariableName variable %}underlying{{ variable.name|upperFirstLetter }}{% endmacro %} {% macro mockedVariableName variable %}{{ variable.name }}{% endmacro %} -{% macro existentialVariableTypeName typeName %}{% if typeName|contains:"any" and typeName|contains:"!" %}{{ typeName | replace:"any","(any" | replace:"!",")!" }}{% elif typeName|contains:"any" and typeName.isOptional %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{% elif typeName|contains:"any" and typeName.isClosure and typeName|contains:"?"%}({{ typeName | replace:"any","(any" | replace:"?",")?" }}){%else%}{{ typeName }}{%endif%}{% endmacro %} -{% macro existentialClosureVariableTypeName typeName %}{% if typeName|contains:"any" and typeName|contains:"!" %}{{ typeName | replace:"any","(any" | replace:"!",")?" }}{% elif typeName|contains:"any" and typeName.isClosure and typeName|contains:"?" %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{% elif typeName|contains:"any" and typeName|contains:"?" %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{%else%}{{ typeName }}{%endif%}{% endmacro %} -{% macro existentialParameterTypeName typeName %}{% if typeName|contains:"any" and typeName|contains:"!" %}{{ typeName | replace:"any","(any" | replace:"!",")!" }}{% elif typeName|contains:"any" and typeName.isClosure and typeName|contains:"?" %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{% elif typeName|contains:"any" and typeName.isOptional %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{%else%}{{ typeName }}{%endif%}{% endmacro %} +{% macro existentialVariableTypeName typeName %}{% if typeName|contains:"any " and typeName|contains:"!" %}{{ typeName | replace:"any","(any" | replace:"!",")!" }}{% elif typeName|contains:"any " and typeName.isOptional %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{% elif typeName|contains:"any " and typeName.isClosure and typeName|contains:"?"%}({{ typeName | replace:"any","(any" | replace:"?",")?" }}){% elif typeName|contains:"some " and typeName|contains:"!" %}{{ typeName | replace:"some","(some" | replace:"!",")!" }}{% elif typeName|contains:"some " and typeName.isOptional %}{{ typeName | replace:"some","(some" | replace:"?",")?" }}{% elif typeName|contains:"some " and typeName.isClosure and typeName|contains:"?"%}({{ typeName | replace:"some","(some" | replace:"?",")?" }}){%else%}{{ typeName }}{%endif%}{% endmacro %} +{% macro existentialClosureVariableTypeName typeName %}{% if typeName|contains:"any " and typeName|contains:"!" %}{{ typeName | replace:"any","(any" | replace:"!",")?" }}{% elif typeName|contains:"any " and typeName.isClosure and typeName|contains:"?" %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{% elif typeName|contains:"any " and typeName|contains:"?" %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{% elif typeName|contains:"some " and typeName|contains:"!" %}{{ typeName | replace:"some","(any" | replace:"!",")?" }}{% elif typeName|contains:"some " and typeName.isClosure and typeName|contains:"?" %}{{ typeName | replace:"some","(any" | replace:"?",")?" }}{% elif typeName|contains:"some " and typeName|contains:"?" %}{{ typeName | replace:"some","(any" | replace:"?",")?" }}{%else%}{{ typeName|replace:"some ","any " }}{%endif%}{% endmacro %} +{% macro existentialParameterTypeName typeName %}{% if typeName|contains:"any " and typeName|contains:"!" %}{{ typeName | replace:"any","(any" | replace:"!",")!" }}{% elif typeName|contains:"any " and typeName.isClosure and typeName|contains:"?" %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{% elif typeName|contains:"any " and typeName.isOptional %}{{ typeName | replace:"any","(any" | replace:"?",")?" }}{% elif typeName|contains:"some " and typeName|contains:"!" %}{{ typeName | replace:"some","(some" | replace:"!",")!" }}{% elif typeName|contains:"some " and typeName.isClosure and typeName|contains:"?" %}{{ typeName | replace:"some","(some" | replace:"?",")?" }}{% elif typeName|contains:"some " and typeName.isOptional %}{{ typeName | replace:"some","(some" | replace:"?",")?" }}{%else%}{{ typeName }}{%endif%}{% endmacro %} {% macro methodName method %}func {{ method.shortName}}({%- for param in method.parameters %}{% if param.argumentLabel == nil %}_ {{ param.name }}{%elif param.argumentLabel == param.name%}{{ param.name }}{%else%}{{ param.argumentLabel }} {{ param.name }}{% endif %}: {% call existentialParameterTypeName param.typeName %}{% if not forloop.last %}, {% endif %}{% endfor -%}){% endmacro %} {% for type in types.protocols where type.based.AutoMockable or type|annotated:"AutoMockable" %}{% if type.name != "AutoMockable" %} diff --git a/Templates/Tests/Context/AutoMockable.swift b/Templates/Tests/Context/AutoMockable.swift index 6e97c121d..0bb6c76c9 100644 --- a/Templates/Tests/Context/AutoMockable.swift +++ b/Templates/Tests/Context/AutoMockable.swift @@ -182,3 +182,13 @@ protocol AnyProtocol: AutoMockable { func y() -> (any StubProtocol, (any StubProtocol)?) func z() -> any StubProtocol & CustomStringConvertible } + +protocol StubWithSomeNameProtocol {} + +protocol SomeProtocol: AutoMockable { + func a(_ x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) + func b(x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) async -> String + func someConfusingFuncName(x: some StubProtocol) + func c(someConfusingArgumentName: some StubProtocol) + func d(_ x: (some StubWithSomeNameProtocol)?) +} diff --git a/Templates/Tests/Context_Linux/AutoMockable.swift b/Templates/Tests/Context_Linux/AutoMockable.swift index 6e97c121d..0bb6c76c9 100644 --- a/Templates/Tests/Context_Linux/AutoMockable.swift +++ b/Templates/Tests/Context_Linux/AutoMockable.swift @@ -182,3 +182,13 @@ protocol AnyProtocol: AutoMockable { func y() -> (any StubProtocol, (any StubProtocol)?) func z() -> any StubProtocol & CustomStringConvertible } + +protocol StubWithSomeNameProtocol {} + +protocol SomeProtocol: AutoMockable { + func a(_ x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) + func b(x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) async -> String + func someConfusingFuncName(x: some StubProtocol) + func c(someConfusingArgumentName: some StubProtocol) + func d(_ x: (some StubWithSomeNameProtocol)?) +} diff --git a/Templates/Tests/Expected/AutoMockable.expected b/Templates/Tests/Expected/AutoMockable.expected index e771308c2..67fa3c22c 100644 --- a/Templates/Tests/Expected/AutoMockable.expected +++ b/Templates/Tests/Expected/AutoMockable.expected @@ -1041,6 +1041,102 @@ class SingleOptionalParameterFunctionMock: SingleOptionalParameterFunction { sendMessageClosure?(message) } +} +class SomeProtocolMock: SomeProtocol { + + + + + //MARK: - a + + var ayzCallsCount = 0 + var ayzCalled: Bool { + return ayzCallsCount > 0 + } + var ayzReceivedArguments: (x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)? + var ayzReceivedInvocations: [(x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)] = [] + var ayzClosure: (((any StubProtocol)?, (any StubProtocol)?, any StubProtocol) -> Void)? + + func a(_ x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) { + ayzCallsCount += 1 + ayzReceivedArguments = (x: x, y: y, z: z) + ayzReceivedInvocations.append((x: x, y: y, z: z)) + ayzClosure?(x, y, z) + } + + //MARK: - b + + var bxyzCallsCount = 0 + var bxyzCalled: Bool { + return bxyzCallsCount > 0 + } + var bxyzReceivedArguments: (x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)? + var bxyzReceivedInvocations: [(x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)] = [] + var bxyzReturnValue: String! + var bxyzClosure: (((any StubProtocol)?, (any StubProtocol)?, any StubProtocol) async -> String)? + + func b(x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) async -> String { + bxyzCallsCount += 1 + bxyzReceivedArguments = (x: x, y: y, z: z) + bxyzReceivedInvocations.append((x: x, y: y, z: z)) + if let bxyzClosure = bxyzClosure { + return await bxyzClosure(x, y, z) + } else { + return bxyzReturnValue + } + } + + //MARK: - someConfusingFuncName + + var someConfusingFuncNameXCallsCount = 0 + var someConfusingFuncNameXCalled: Bool { + return someConfusingFuncNameXCallsCount > 0 + } + var someConfusingFuncNameXReceivedX: (any StubProtocol)? + var someConfusingFuncNameXReceivedInvocations: [(any StubProtocol)] = [] + var someConfusingFuncNameXClosure: ((any StubProtocol) -> Void)? + + func someConfusingFuncName(x: some StubProtocol) { + someConfusingFuncNameXCallsCount += 1 + someConfusingFuncNameXReceivedX = x + someConfusingFuncNameXReceivedInvocations.append(x) + someConfusingFuncNameXClosure?(x) + } + + //MARK: - c + + var cSomeConfusingArgumentNameCallsCount = 0 + var cSomeConfusingArgumentNameCalled: Bool { + return cSomeConfusingArgumentNameCallsCount > 0 + } + var cSomeConfusingArgumentNameReceivedSomeConfusingArgumentName: (any StubProtocol)? + var cSomeConfusingArgumentNameReceivedInvocations: [(any StubProtocol)] = [] + var cSomeConfusingArgumentNameClosure: ((any StubProtocol) -> Void)? + + func c(someConfusingArgumentName: some StubProtocol) { + cSomeConfusingArgumentNameCallsCount += 1 + cSomeConfusingArgumentNameReceivedSomeConfusingArgumentName = someConfusingArgumentName + cSomeConfusingArgumentNameReceivedInvocations.append(someConfusingArgumentName) + cSomeConfusingArgumentNameClosure?(someConfusingArgumentName) + } + + //MARK: - d + + var dCallsCount = 0 + var dCalled: Bool { + return dCallsCount > 0 + } + var dReceivedX: (any StubWithSomeNameProtocol)? + var dReceivedInvocations: [(any StubWithSomeNameProtocol)?] = [] + var dClosure: (((any StubWithSomeNameProtocol)?) -> Void)? + + func d(_ x: (some StubWithSomeNameProtocol)?) { + dCallsCount += 1 + dReceivedX = x + dReceivedInvocations.append(x) + dClosure?(x) + } + } class StaticMethodProtocolMock: StaticMethodProtocol { diff --git a/Templates/Tests/Generated/AutoMockable.generated.swift b/Templates/Tests/Generated/AutoMockable.generated.swift index 1b272044c..6525ccb28 100644 --- a/Templates/Tests/Generated/AutoMockable.generated.swift +++ b/Templates/Tests/Generated/AutoMockable.generated.swift @@ -1064,6 +1064,102 @@ class SingleOptionalParameterFunctionMock: SingleOptionalParameterFunction { sendMessageClosure?(message) } +} +class SomeProtocolMock: SomeProtocol { + + + + + //MARK: - a + + var ayzCallsCount = 0 + var ayzCalled: Bool { + return ayzCallsCount > 0 + } + var ayzReceivedArguments: (x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)? + var ayzReceivedInvocations: [(x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)] = [] + var ayzClosure: (((any StubProtocol)?, (any StubProtocol)?, any StubProtocol) -> Void)? + + func a(_ x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) { + ayzCallsCount += 1 + ayzReceivedArguments = (x: x, y: y, z: z) + ayzReceivedInvocations.append((x: x, y: y, z: z)) + ayzClosure?(x, y, z) + } + + //MARK: - b + + var bxyzCallsCount = 0 + var bxyzCalled: Bool { + return bxyzCallsCount > 0 + } + var bxyzReceivedArguments: (x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)? + var bxyzReceivedInvocations: [(x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)] = [] + var bxyzReturnValue: String! + var bxyzClosure: (((any StubProtocol)?, (any StubProtocol)?, any StubProtocol) async -> String)? + + func b(x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) async -> String { + bxyzCallsCount += 1 + bxyzReceivedArguments = (x: x, y: y, z: z) + bxyzReceivedInvocations.append((x: x, y: y, z: z)) + if let bxyzClosure = bxyzClosure { + return await bxyzClosure(x, y, z) + } else { + return bxyzReturnValue + } + } + + //MARK: - someConfusingFuncName + + var someConfusingFuncNameXCallsCount = 0 + var someConfusingFuncNameXCalled: Bool { + return someConfusingFuncNameXCallsCount > 0 + } + var someConfusingFuncNameXReceivedX: (any StubProtocol)? + var someConfusingFuncNameXReceivedInvocations: [(any StubProtocol)] = [] + var someConfusingFuncNameXClosure: ((any StubProtocol) -> Void)? + + func someConfusingFuncName(x: some StubProtocol) { + someConfusingFuncNameXCallsCount += 1 + someConfusingFuncNameXReceivedX = x + someConfusingFuncNameXReceivedInvocations.append(x) + someConfusingFuncNameXClosure?(x) + } + + //MARK: - c + + var cSomeConfusingArgumentNameCallsCount = 0 + var cSomeConfusingArgumentNameCalled: Bool { + return cSomeConfusingArgumentNameCallsCount > 0 + } + var cSomeConfusingArgumentNameReceivedSomeConfusingArgumentName: (any StubProtocol)? + var cSomeConfusingArgumentNameReceivedInvocations: [(any StubProtocol)] = [] + var cSomeConfusingArgumentNameClosure: ((any StubProtocol) -> Void)? + + func c(someConfusingArgumentName: some StubProtocol) { + cSomeConfusingArgumentNameCallsCount += 1 + cSomeConfusingArgumentNameReceivedSomeConfusingArgumentName = someConfusingArgumentName + cSomeConfusingArgumentNameReceivedInvocations.append(someConfusingArgumentName) + cSomeConfusingArgumentNameClosure?(someConfusingArgumentName) + } + + //MARK: - d + + var dCallsCount = 0 + var dCalled: Bool { + return dCallsCount > 0 + } + var dReceivedX: (any StubWithSomeNameProtocol)? + var dReceivedInvocations: [(any StubWithSomeNameProtocol)?] = [] + var dClosure: (((any StubWithSomeNameProtocol)?) -> Void)? + + func d(_ x: (some StubWithSomeNameProtocol)?) { + dCallsCount += 1 + dReceivedX = x + dReceivedInvocations.append(x) + dClosure?(x) + } + } class StaticMethodProtocolMock: StaticMethodProtocol {