Skip to content

Commit

Permalink
Add support for some keyword to function parameter type in AutoMockab…
Browse files Browse the repository at this point in the history
…le.stencil
  • Loading branch information
paulbancarelextadevinta committed Sep 10, 2023
1 parent ba63cd6 commit 453c45c
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Templates/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
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 @@ -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)?)
}
10 changes: 10 additions & 0 deletions Templates/Tests/Context_Linux/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
}
96 changes: 96 additions & 0 deletions Templates/Tests/Expected/AutoMockable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
96 changes: 96 additions & 0 deletions Templates/Tests/Generated/AutoMockable.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 453c45c

Please sign in to comment.