Skip to content

Commit

Permalink
[Bugfix] - Automockable Protocol with a property of type AnyPublisher (
Browse files Browse the repository at this point in the history
  • Loading branch information
paul1893 authored Nov 10, 2023
1 parent a69e548 commit da88f8f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## Master
## Changes
- Add support for `typealias`es in EJS templates. ([#1208](https://github.com/krzysztofzablocki/Sourcery/pull/1208))
- Add support for existential to Automockable Protocol with generic types. ([#1220](https://github.com/krzysztofzablocki/Sourcery/pull/1220))

## 2.1.2
## Changes
Expand Down
58 changes: 55 additions & 3 deletions Templates/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,61 @@ 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:"?",")?" }}){% 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 existentialVariableTypeName typeName -%}
{%- if typeName|contains:"<" and typeName|contains:">" -%}
{{ typeName }}
{%- elif 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:"?," and typeName|contains:">?" -%}
{{ typeName | replace:"any","(any" | replace:"?,",")?," }}
{%- elif 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" %}
Expand Down
18 changes: 18 additions & 0 deletions Templates/Tests/Context/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,21 @@ protocol SomeProtocol: AutoMockable {
func c(someConfusingArgumentName: some StubProtocol)
func d(_ x: (some StubWithSomeNameProtocol)?)
}

protocol PersonProtocol {}
class GenericType<A, B, C>{}

protocol HouseProtocol: AutoMockable {
var aPublisher: AnyPublisher<any PersonProtocol, Never>? { get }
var bPublisher: AnyPublisher<(any PersonProtocol)?, Never>? { get }
var cPublisher: CurrentValueSubject<(any PersonProtocol)?, Never>? { get }
var dPublisher: PassthroughSubject<(any PersonProtocol)?, Never>? { get }
var e1Publisher: GenericType<(any PersonProtocol)?, Never, Never>? { get }
var e2Publisher: GenericType<Never, (any PersonProtocol)?, Never>? { get }
var e3Publisher: GenericType<Never, Never, (any PersonProtocol)?>? { get }
var e4Publisher: GenericType<(any PersonProtocol)?, (any PersonProtocol)?, (any PersonProtocol)?>? { get }
var f1Publisher: GenericType<any PersonProtocol, Never, Never>? { get }
var f2Publisher: GenericType<Never, any PersonProtocol, Never>? { get }
var f3Publisher: GenericType<Never, Never, any PersonProtocol>? { get }
var f4Publisher: GenericType<any PersonProtocol, any PersonProtocol, any PersonProtocol>? { get }
}
18 changes: 18 additions & 0 deletions Templates/Tests/Context_Linux/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,21 @@ protocol SomeProtocol: AutoMockable {
func c(someConfusingArgumentName: some StubProtocol)
func d(_ x: (some StubWithSomeNameProtocol)?)
}

protocol PersonProtocol {}
class GenericType<A, B, C>{}

protocol HouseProtocol: AutoMockable {
var aPublisher: AnyPublisher<any PersonProtocol, Never>? { get }
var bPublisher: AnyPublisher<(any PersonProtocol)?, Never>? { get }
var cPublisher: CurrentValueSubject<(any PersonProtocol)?, Never>? { get }
var dPublisher: PassthroughSubject<(any PersonProtocol)?, Never>? { get }
var e1Publisher: GenericType<(any PersonProtocol)?, Never, Never>? { get }
var e2Publisher: GenericType<Never, (any PersonProtocol)?, Never>? { get }
var e3Publisher: GenericType<Never, Never, (any PersonProtocol)?>? { get }
var e4Publisher: GenericType<(any PersonProtocol)?, (any PersonProtocol)?, (any PersonProtocol)?>? { get }
var f1Publisher: GenericType<any PersonProtocol, Never, Never>? { get }
var f2Publisher: GenericType<Never, any PersonProtocol, Never>? { get }
var f3Publisher: GenericType<Never, Never, any PersonProtocol>? { get }
var f4Publisher: GenericType<any PersonProtocol, any PersonProtocol, any PersonProtocol>? { get }
}
18 changes: 18 additions & 0 deletions Templates/Tests/Expected/AutoMockable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,24 @@ class FunctionWithMultilineDeclarationMock: FunctionWithMultilineDeclaration {
startCarOfClosure?(car, model)
}

}
class HouseProtocolMock: HouseProtocol {


var aPublisher: AnyPublisher<any PersonProtocol, Never>?
var bPublisher: AnyPublisher<(any PersonProtocol)?, Never>?
var cPublisher: CurrentValueSubject<(any PersonProtocol)?, Never>?
var dPublisher: PassthroughSubject<(any PersonProtocol)?, Never>?
var e1Publisher: GenericType<(any PersonProtocol)?, Never, Never>?
var e2Publisher: GenericType<Never, (any PersonProtocol)?, Never>?
var e3Publisher: GenericType<Never, Never, (any PersonProtocol)?>?
var e4Publisher: GenericType<(any PersonProtocol)?, (any PersonProtocol)?, (any PersonProtocol)?>?
var f1Publisher: GenericType<any PersonProtocol, Never, Never>?
var f2Publisher: GenericType<Never, any PersonProtocol, Never>?
var f3Publisher: GenericType<Never, Never, any PersonProtocol>?
var f4Publisher: GenericType<any PersonProtocol, any PersonProtocol, any PersonProtocol>?


}
class ImplicitlyUnwrappedOptionalReturnValueProtocolMock: ImplicitlyUnwrappedOptionalReturnValueProtocol {

Expand Down
18 changes: 18 additions & 0 deletions Templates/Tests/Generated/AutoMockable.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,24 @@ class FunctionWithMultilineDeclarationMock: FunctionWithMultilineDeclaration {
startCarOfClosure?(car, model)
}

}
class HouseProtocolMock: HouseProtocol {


var aPublisher: AnyPublisher<any PersonProtocol, Never>?
var bPublisher: AnyPublisher<(any PersonProtocol)?, Never>?
var cPublisher: CurrentValueSubject<(any PersonProtocol)?, Never>?
var dPublisher: PassthroughSubject<(any PersonProtocol)?, Never>?
var e1Publisher: GenericType<(any PersonProtocol)?, Never, Never>?
var e2Publisher: GenericType<Never, (any PersonProtocol)?, Never>?
var e3Publisher: GenericType<Never, Never, (any PersonProtocol)?>?
var e4Publisher: GenericType<(any PersonProtocol)?, (any PersonProtocol)?, (any PersonProtocol)?>?
var f1Publisher: GenericType<any PersonProtocol, Never, Never>?
var f2Publisher: GenericType<Never, any PersonProtocol, Never>?
var f3Publisher: GenericType<Never, Never, any PersonProtocol>?
var f4Publisher: GenericType<any PersonProtocol, any PersonProtocol, any PersonProtocol>?


}
class ImplicitlyUnwrappedOptionalReturnValueProtocolMock: ImplicitlyUnwrappedOptionalReturnValueProtocol {

Expand Down

0 comments on commit da88f8f

Please sign in to comment.