Skip to content

Commit

Permalink
Add support to any keyword for function return type to AutoMockable.s…
Browse files Browse the repository at this point in the history
…tencil (#1186)

* Add support to any keyword for function return type to AutoMockable.stencil

* Update changelog

* Fix tests
  • Loading branch information
paul1893 authored Aug 10, 2023
1 parent 0ff1fbd commit c16a209
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 2.1.0
## Changes
- Added support for Swift Package Manager config ([#1184](https://github.com/krzysztofzablocki/Sourcery/pull/1184))
- Add support to any keyword for function parameter type to AutoMockable.stencil ([#1169](https://github.com/krzysztofzablocki/Sourcery/pull/1169))
- Add support to any keyword for function return type to AutoMockable.stencil([#1186](https://github.com/krzysztofzablocki/Sourcery/pull/1186))

## 2.0.3
## Internal Changes
Expand Down
8 changes: 4 additions & 4 deletions Templates/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {{ import }}
{% macro closureReturnTypeName method %}{% if method.isOptionalReturnType %}{{ method.unwrappedReturnTypeName }}?{% else %}{{ method.returnTypeName }}{% endif %}{% endmacro %}

{% macro methodClosureDeclaration method %}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call methodClosureName method %}: (({% for param in method.parameters %}{% call existentialClosureVariableTypeName param.typeName %}{% if not forloop.last %}, {% endif %}{% endfor %}) {% if method.isAsync %}async {% endif %}{% if method.throws %}throws {% endif %}-> {% if method.isInitializer %}Void{% else %}{% call closureReturnTypeName method %}{% endif %})?
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call methodClosureName method %}: (({% for param in method.parameters %}{% call existentialClosureVariableTypeName param.typeName %}{% if not forloop.last %}, {% endif %}{% endfor %}) {% if method.isAsync %}async {% endif %}{% if method.throws %}throws {% endif %}-> {% if method.isInitializer %}Void{% else %}{% call existentialVariableTypeName method.returnTypeName %}{% endif %})?
{% endmacro %}

{% macro methodClosureCallParameters method %}{% for param in method.parameters %}{{ param.name }}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %}
Expand Down Expand Up @@ -84,7 +84,7 @@ import {{ import }}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {% if param.typeAttributes.escaping %}{% call existentialClosureVariableTypeName param.typeName.unwrappedTypeName %}{% else %}{% call existentialClosureVariableTypeName param.typeName %}{% endif %}{{ ', ' if not forloop.last }}{% endfor %})] = []
{% endif %}
{% if not method.returnTypeName.isVoid and not method.isInitializer %}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType or method.returnTypeName|contains:"any "}}{% call existentialVariableTypeName method.returnTypeName %}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType or method.returnTypeName|contains:"any " }}{{ '!' if not method.isOptionalReturnType }}
{% endif %}
{% call methodClosureDeclaration method %}

Expand All @@ -99,7 +99,7 @@ import {{ import }}
{{ value }}
{% endfor %}
{% endfor %}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}{% call methodName method %}{{ ' async' if method.isAsync }}{{ ' throws' if method.throws }}{% if not method.returnTypeName.isVoid %} -> {{ method.returnTypeName }}{% endif %} {
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}{% call methodName method %}{{ ' async' if method.isAsync }}{{ ' throws' if method.throws }}{% if not method.returnTypeName.isVoid %} -> {% call existentialVariableTypeName method.returnTypeName %}{% endif %} {
{% if method.throws %}
{% call methodThrowableErrorUsage method %}
{% endif %}
Expand Down Expand Up @@ -202,7 +202,7 @@ 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 %}({{ 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:"?",")?" }}){%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 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 %}
Expand Down
10 changes: 10 additions & 0 deletions Templates/Tests/Context/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,14 @@ protocol AnyProtocol: AutoMockable {
func n(x: @escaping ((any StubProtocol)?) -> Void)
var o: any StubWithAnyNameProtocol { get }
func p(_ x: (any StubWithAnyNameProtocol)?)
func q() -> any StubProtocol
func r() -> (any StubProtocol)?
func s() -> () -> any StubProtocol
func t() -> () -> (any StubProtocol)?
func u() -> (Int, () -> (any StubProtocol)?)
func v() -> (Int, (() -> any StubProtocol)?)
func w() -> [(any StubProtocol)?]
func x() -> [String: (any StubProtocol)?]
func y() -> (any StubProtocol, (any StubProtocol)?)
func z() -> any StubProtocol & CustomStringConvertible
}
182 changes: 181 additions & 1 deletion Templates/Tests/Expected/AutoMockable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,186 @@ class AnyProtocolMock: AnyProtocol {
pClosure?(x)
}

//MARK: - q

var qCallsCount = 0
var qCalled: Bool {
return qCallsCount > 0
}
var qReturnValue: (any StubProtocol)!
var qClosure: (() -> any StubProtocol)?

func q() -> any StubProtocol {
qCallsCount += 1
if let qClosure = qClosure {
return qClosure()
} else {
return qReturnValue
}
}

//MARK: - r

var rCallsCount = 0
var rCalled: Bool {
return rCallsCount > 0
}
var rReturnValue: ((any StubProtocol)?)
var rClosure: (() -> (any StubProtocol)?)?

func r() -> (any StubProtocol)? {
rCallsCount += 1
if let rClosure = rClosure {
return rClosure()
} else {
return rReturnValue
}
}

//MARK: - s

var sCallsCount = 0
var sCalled: Bool {
return sCallsCount > 0
}
var sReturnValue: (() -> any StubProtocol)!
var sClosure: (() -> () -> any StubProtocol)?

func s() -> () -> any StubProtocol {
sCallsCount += 1
if let sClosure = sClosure {
return sClosure()
} else {
return sReturnValue
}
}

//MARK: - t

var tCallsCount = 0
var tCalled: Bool {
return tCallsCount > 0
}
var tReturnValue: ((() -> (any StubProtocol)?))!
var tClosure: (() -> (() -> (any StubProtocol)?))?

func t() -> (() -> (any StubProtocol)?) {
tCallsCount += 1
if let tClosure = tClosure {
return tClosure()
} else {
return tReturnValue
}
}

//MARK: - u

var uCallsCount = 0
var uCalled: Bool {
return uCallsCount > 0
}
var uReturnValue: ((Int, () -> (any StubProtocol)?))!
var uClosure: (() -> (Int, () -> (any StubProtocol)?))?

func u() -> (Int, () -> (any StubProtocol)?) {
uCallsCount += 1
if let uClosure = uClosure {
return uClosure()
} else {
return uReturnValue
}
}

//MARK: - v

var vCallsCount = 0
var vCalled: Bool {
return vCallsCount > 0
}
var vReturnValue: ((Int, (() -> any StubProtocol)?))!
var vClosure: (() -> (Int, (() -> any StubProtocol)?))?

func v() -> (Int, (() -> any StubProtocol)?) {
vCallsCount += 1
if let vClosure = vClosure {
return vClosure()
} else {
return vReturnValue
}
}

//MARK: - w

var wCallsCount = 0
var wCalled: Bool {
return wCallsCount > 0
}
var wReturnValue: ([(any StubProtocol)?])!
var wClosure: (() -> [(any StubProtocol)?])?

func w() -> [(any StubProtocol)?] {
wCallsCount += 1
if let wClosure = wClosure {
return wClosure()
} else {
return wReturnValue
}
}

//MARK: - x

var xCallsCount = 0
var xCalled: Bool {
return xCallsCount > 0
}
var xReturnValue: ([String: (any StubProtocol)?])!
var xClosure: (() -> [String: (any StubProtocol)?])?

func x() -> [String: (any StubProtocol)?] {
xCallsCount += 1
if let xClosure = xClosure {
return xClosure()
} else {
return xReturnValue
}
}

//MARK: - y

var yCallsCount = 0
var yCalled: Bool {
return yCallsCount > 0
}
var yReturnValue: ((any StubProtocol, (any StubProtocol)?))!
var yClosure: (() -> (any StubProtocol, (any StubProtocol)?))?

func y() -> (any StubProtocol, (any StubProtocol)?) {
yCallsCount += 1
if let yClosure = yClosure {
return yClosure()
} else {
return yReturnValue
}
}

//MARK: - z

var zCallsCount = 0
var zCalled: Bool {
return zCallsCount > 0
}
var zReturnValue: (any StubProtocol & CustomStringConvertible)!
var zClosure: (() -> any StubProtocol & CustomStringConvertible)?

func z() -> any StubProtocol & CustomStringConvertible {
zCallsCount += 1
if let zClosure = zClosure {
return zClosure()
} else {
return zReturnValue
}
}

}
class AsyncProtocolMock: AsyncProtocol {

Expand Down Expand Up @@ -655,7 +835,7 @@ class ImplicitlyUnwrappedOptionalReturnValueProtocolMock: ImplicitlyUnwrappedOpt
return implicitReturnCallsCount > 0
}
var implicitReturnReturnValue: String!
var implicitReturnClosure: (() -> String?)?
var implicitReturnClosure: (() -> String!)?

func implicitReturn() -> String! {
implicitReturnCallsCount += 1
Expand Down
Loading

0 comments on commit c16a209

Please sign in to comment.