Skip to content

Commit

Permalink
Fix Closure Parameter CVarArg with Existential (#1305)
Browse files Browse the repository at this point in the history
* Fixed compilation issue for closures with existential arguments

* Added index to MethodParameter

* Automatically assign argument name in case it is empty

* Add an additional closing parenthesis in case type is a closure with multiple arguments

* Disabled implicitly unwrapped optional for return type of a closure

* Disabled CVarArg for tuple type

* Added new sample for AutoMockable contexts

* Updated expected and generated

* Support for var arg and "any" keyword

* Updated expected and generated code

* Updated contexts for AutoMockable

* Squashed commit of the following:

commit 55da8ed
Author: art-divin <art-divin@users.noreply.github.com>
Date:   Tue Mar 19 15:05:06 2024 +0000

    Update Docs

commit a8a314d
Author: Ruslan Alikhamov <r.alikhamov@gmail.com>
Date:   Tue Mar 19 18:55:42 2024 +0400

    Make AutoMockable Generate Compilable Swift Code (#1304)

    * Fixed compilation issue for closures with existential arguments

    * Added index to MethodParameter

    * Automatically assign argument name in case it is empty

    * Add an additional closing parenthesis in case type is a closure with multiple arguments

    * Disabled implicitly unwrapped optional for return type of a closure

    * Disabled CVarArg for tuple type

    * Added new sample for AutoMockable contexts

    * Updated expected and generated

* Support for var arg and "any" keyword

* Updated expected and generated code

* Updated contexts for AutoMockable
  • Loading branch information
art-divin authored Mar 19, 2024
1 parent 55da8ed commit 44d04b2
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 14 deletions.
5 changes: 5 additions & 0 deletions Templates/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ import {{ import }}
{{ typeName }}
{%- endif -%}
{%- endmacro %}
{# Swift does not support closures with variadic parameters of existential types as arguments. That is why existentialClosureVariableTypeName.isVariadic should be false when typeName is a closure #}
{% macro existentialClosureVariableTypeName typeName isVariadic keepInout -%}
{% set name %}
{%- if keepInout -%}
Expand All @@ -263,6 +264,8 @@ import {{ import }}
{{ name | replace:"some","(any" | replace:"?",")?" }}
{%- elif typeName|contains:"some " and typeName|contains:"?" -%}
{{ name | replace:"some","(any" | replace:"?",")?" }}
{%- elif isVariadic and typeName|contains:"any " -%}
[({{ name }})]
{%- elif isVariadic -%}
{{ name }}...
{%- else -%}
Expand Down Expand Up @@ -321,6 +324,8 @@ import {{ import }}
{{ typeName | replace:"some","(some" | replace:"?",")?" }}
{%- elif isVariadic -%}
{{ typeName }}...
{%- elif typeName.isClosure and typeName.closure.parameters.count > 0 and typeName.closure.parameters.last.isVariadic -%}
{{ typeName }})
{%- else -%}
{{ typeName }}
{%- endif -%}
Expand Down
27 changes: 20 additions & 7 deletions Templates/Tests/Context/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import Foundation

protocol AutoMockable {}
public protocol AutoMockable {}
protocol StubProtocol {}
protocol StubWithAnyNameProtocol {}
protocol StubWithSomeNameProtocol {}
protocol PersonProtocol {}

protocol BasicProtocol: AutoMockable {
func loadConfiguration() -> String?
Expand Down Expand Up @@ -154,9 +158,6 @@ protocol StaticMethodProtocol: AutoMockable {
static func staticFunction(_: String) -> String
}

protocol StubProtocol {}
protocol StubWithAnyNameProtocol {}

protocol AnyProtocol: AutoMockable {
var a: any StubProtocol { get }
var b: (any StubProtocol)? { get }
Expand Down Expand Up @@ -187,8 +188,6 @@ protocol AnyProtocol: AutoMockable {
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
Expand All @@ -197,7 +196,6 @@ protocol SomeProtocol: AutoMockable {
func d(_ x: (some StubWithSomeNameProtocol)?)
}

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

protocol HouseProtocol: AutoMockable {
Expand Down Expand Up @@ -227,6 +225,21 @@ protocol ExampleVararg {
func string(key: String, args: CVarArg...) -> String
}

// sourcery: AutoMockable
protocol ExampleVarargTwo {
func toto(args: any StubWithSomeNameProtocol...)
}

// sourcery: AutoMockable
protocol ExampleVarargThree {
func toto(arg: ((String, any Collection...) -> any Collection))
}

// sourcery: AutoMockable
protocol ExampleVarargFour {
func toto(arg: ((String, any Collection...) -> Void))
}

// sourcery: AutoMockable
public protocol ProtocolWithOverrides {
func doSomething(_ data: Int) -> [String]
Expand Down
27 changes: 20 additions & 7 deletions Templates/Tests/Context_Linux/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import Foundation

protocol AutoMockable {}
public protocol AutoMockable {}
protocol StubProtocol {}
protocol StubWithAnyNameProtocol {}
protocol StubWithSomeNameProtocol {}
protocol PersonProtocol {}

protocol BasicProtocol: AutoMockable {
func loadConfiguration() -> String?
Expand Down Expand Up @@ -154,9 +158,6 @@ protocol StaticMethodProtocol: AutoMockable {
static func staticFunction(_: String) -> String
}

protocol StubProtocol {}
protocol StubWithAnyNameProtocol {}

protocol AnyProtocol: AutoMockable {
var a: any StubProtocol { get }
var b: (any StubProtocol)? { get }
Expand Down Expand Up @@ -187,8 +188,6 @@ protocol AnyProtocol: AutoMockable {
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
Expand All @@ -197,7 +196,6 @@ protocol SomeProtocol: AutoMockable {
func d(_ x: (some StubWithSomeNameProtocol)?)
}

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

protocol HouseProtocol: AutoMockable {
Expand Down Expand Up @@ -227,6 +225,21 @@ protocol ExampleVararg {
func string(key: String, args: CVarArg...) -> String
}

// sourcery: AutoMockable
protocol ExampleVarargTwo {
func toto(args: any StubWithSomeNameProtocol...)
}

// sourcery: AutoMockable
protocol ExampleVarargThree {
func toto(arg: ((String, any Collection...) -> any Collection))
}

// sourcery: AutoMockable
protocol ExampleVarargFour {
func toto(arg: ((String, any Collection...) -> Void))
}

// sourcery: AutoMockable
public protocol ProtocolWithOverrides {
func doSomething(_ data: Int) -> [String]
Expand Down
64 changes: 64 additions & 0 deletions Templates/Tests/Expected/AutoMockable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,70 @@ class ExampleVarargMock: ExampleVararg {
}


}
class ExampleVarargFourMock: ExampleVarargFour {




//MARK: - toto

var totoArgStringAnyCollectionVoidVoidCallsCount = 0
var totoArgStringAnyCollectionVoidVoidCalled: Bool {
return totoArgStringAnyCollectionVoidVoidCallsCount > 0
}
var totoArgStringAnyCollectionVoidVoidClosure: (((String, any Collection...) -> Void) -> Void)?

func toto(arg: ((String, any Collection...) -> Void)) {
totoArgStringAnyCollectionVoidVoidCallsCount += 1
totoArgStringAnyCollectionVoidVoidClosure?(arg)
}


}
class ExampleVarargThreeMock: ExampleVarargThree {




//MARK: - toto

var totoArgStringAnyCollectionAnyCollectionVoidCallsCount = 0
var totoArgStringAnyCollectionAnyCollectionVoidCalled: Bool {
return totoArgStringAnyCollectionAnyCollectionVoidCallsCount > 0
}
var totoArgStringAnyCollectionAnyCollectionVoidClosure: (((String, any Collection...) -> any Collection) -> Void)?

func toto(arg: ((String, any Collection...) -> any Collection)) {
totoArgStringAnyCollectionAnyCollectionVoidCallsCount += 1
totoArgStringAnyCollectionAnyCollectionVoidClosure?(arg)
}


}
class ExampleVarargTwoMock: ExampleVarargTwo {




//MARK: - toto

var totoArgsAnyStubWithSomeNameProtocolVoidCallsCount = 0
var totoArgsAnyStubWithSomeNameProtocolVoidCalled: Bool {
return totoArgsAnyStubWithSomeNameProtocolVoidCallsCount > 0
}
var totoArgsAnyStubWithSomeNameProtocolVoidReceivedArgs: ([(any StubWithSomeNameProtocol)])?
var totoArgsAnyStubWithSomeNameProtocolVoidReceivedInvocations: [([(any StubWithSomeNameProtocol)])] = []
var totoArgsAnyStubWithSomeNameProtocolVoidClosure: (([(any StubWithSomeNameProtocol)]) -> Void)?

func toto(args: any StubWithSomeNameProtocol...) {
totoArgsAnyStubWithSomeNameProtocolVoidCallsCount += 1
totoArgsAnyStubWithSomeNameProtocolVoidReceivedArgs = args
totoArgsAnyStubWithSomeNameProtocolVoidReceivedInvocations.append(args)
totoArgsAnyStubWithSomeNameProtocolVoidClosure?(args)
}


}
class ExtendableProtocolMock: ExtendableProtocol {

Expand Down
64 changes: 64 additions & 0 deletions Templates/Tests/Generated/AutoMockable.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,70 @@ class ExampleVarargMock: ExampleVararg {
}


}
class ExampleVarargFourMock: ExampleVarargFour {




//MARK: - toto

var totoArgStringAnyCollectionVoidVoidCallsCount = 0
var totoArgStringAnyCollectionVoidVoidCalled: Bool {
return totoArgStringAnyCollectionVoidVoidCallsCount > 0
}
var totoArgStringAnyCollectionVoidVoidClosure: (((String, any Collection...) -> Void) -> Void)?

func toto(arg: ((String, any Collection...) -> Void)) {
totoArgStringAnyCollectionVoidVoidCallsCount += 1
totoArgStringAnyCollectionVoidVoidClosure?(arg)
}


}
class ExampleVarargThreeMock: ExampleVarargThree {




//MARK: - toto

var totoArgStringAnyCollectionAnyCollectionVoidCallsCount = 0
var totoArgStringAnyCollectionAnyCollectionVoidCalled: Bool {
return totoArgStringAnyCollectionAnyCollectionVoidCallsCount > 0
}
var totoArgStringAnyCollectionAnyCollectionVoidClosure: (((String, any Collection...) -> any Collection) -> Void)?

func toto(arg: ((String, any Collection...) -> any Collection)) {
totoArgStringAnyCollectionAnyCollectionVoidCallsCount += 1
totoArgStringAnyCollectionAnyCollectionVoidClosure?(arg)
}


}
class ExampleVarargTwoMock: ExampleVarargTwo {




//MARK: - toto

var totoArgsAnyStubWithSomeNameProtocolVoidCallsCount = 0
var totoArgsAnyStubWithSomeNameProtocolVoidCalled: Bool {
return totoArgsAnyStubWithSomeNameProtocolVoidCallsCount > 0
}
var totoArgsAnyStubWithSomeNameProtocolVoidReceivedArgs: ([(any StubWithSomeNameProtocol)])?
var totoArgsAnyStubWithSomeNameProtocolVoidReceivedInvocations: [([(any StubWithSomeNameProtocol)])] = []
var totoArgsAnyStubWithSomeNameProtocolVoidClosure: (([(any StubWithSomeNameProtocol)]) -> Void)?

func toto(args: any StubWithSomeNameProtocol...) {
totoArgsAnyStubWithSomeNameProtocolVoidCallsCount += 1
totoArgsAnyStubWithSomeNameProtocolVoidReceivedArgs = args
totoArgsAnyStubWithSomeNameProtocolVoidReceivedInvocations.append(args)
totoArgsAnyStubWithSomeNameProtocolVoidClosure?(args)
}


}
class ExtendableProtocolMock: ExtendableProtocol {

Expand Down

0 comments on commit 44d04b2

Please sign in to comment.