Last active
September 14, 2020 23:14
-
-
Save rjchatfield/6472bbc5a977aacf407d1a59e524ab6f to your computer and use it in GitHub Desktop.
Sourcery generation of EnvironmentCall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//sourcery: mockEnvironment | |
struct Environment { | |
var call1: () -> Void | |
var call2: () -> Foo | |
var call3: (Foo) -> Void | |
var call4: (Foo, Bar) -> Bazel | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for type in types.types where type|annotated:"mockEnvironment" %} | |
@testable import EnvironmentCall | |
/* | |
{% for var in type.storedVariables %} | |
name : {{ var.name }} | |
typeName: {{ var.typeName }} | |
{{ var.typeName.closure }} | |
{% endfor %} | |
*/ | |
// MARK: - {{ type.name }}.Call | |
extension {{ type.name }} { | |
enum Call { | |
{% for var in type.variables|instance %} | |
case {{ var.name }}{% for p in var.typeName.closure.parameters %}{% if forloop.first %}({% endif %}{% if p.name %}{{p.name}}: {% endif %}{{p.typeName}}{% if forloop.last %}){% else %}, {% endif %}{% endfor %} | |
{% endfor %} | |
} | |
} | |
extension EnvironmentCallCapturer where Environment == {{ type.name }}, Call == {{ type.name }}.Call { | |
static func capturer( | |
{% for var in type.variables|instance where var.typeName.closure.returnTypeName.unwrappedTypeName != "Void" %} | |
{{ var.name }}: {{ var.typeName.closure.returnTypeName }}{% if forloop.last %}{% else %},{% endif %} | |
{% endfor %} | |
{% for var in type.variables|instance where var.typeName.closure.returnTypeName.unwrappedTypeName != "Void" %} | |
// {{ var.name }}: ({% for p in var.typeName.closure.parameters %}{% if p.name %}{{p.name}}: {% endif %}{{p.typeName}}{% if not forloop.last %}, {% endif %}{% endfor %}) -> {{ var.typeName.closure.returnTypeName }}{% if forloop.last %}{% else %},{% endif %} | |
{% endfor %} | |
) -> EnvironmentCallCapturer<{{ type.name }}, {{ type.name }}.Call> { | |
EnvironmentCallCapturer { (send: @escaping ({{ type.name }}.Call) -> Void) in | |
{{ type.name }}( | |
{% for var in type.variables|instance %} | |
{{ var.name }}: { {% for p in var.typeName.closure.parameters %}{% if forloop.first %}({% endif %}{% if p.name %}{{p.name}}{% else %}arg{{forloop.counter0}}{% endif %}{% if forloop.last %}) in{% else %}, {% endif %}{% endfor %} | |
send(.{{ var.name }}{% for p in var.typeName.closure.parameters %}{% if forloop.first %}({% endif %}{% if p.name %}{{p.name}}: {{p.name}}{% else %}arg{{forloop.counter0}}{% endif %}{% if forloop.last %}){% else %}, {% endif %}{% endfor %}) | |
{% if var.typeName.closure.returnTypeName.unwrappedTypeName != "Void" %} | |
return {{ var.name }} | |
{% else %} | |
// return void | |
{% endif %} | |
{% if forloop.last %}}{% else %}},{% endif %} | |
{% endfor %} | |
{{!-- | |
load: { | |
send(.load($0, $1, $2)) | |
return load | |
}, | |
showIssueList: { | |
send(.showIssueList($0)) | |
// return void | |
}, | |
currentDate: { | |
send(.currentDate) | |
return currentDate | |
} | |
--}} | |
) | |
} | |
} | |
} | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated using Sourcery 1.0.0 — https://github.com/krzysztofzablocki/Sourcery | |
// DO NOT EDIT | |
@testable import EnvironmentCall | |
/* | |
name : call1 | |
typeName: () -> Void | |
ClosureType: name = () -> Void, parameters = [], returnTypeName = Void, actualReturnTypeName = Void, `throws` = false | |
name : call2 | |
typeName: () -> Foo | |
ClosureType: name = () -> Foo, parameters = [], returnTypeName = Foo, actualReturnTypeName = Foo, `throws` = false | |
name : call3 | |
typeName: (Foo) -> Void | |
ClosureType: name = (Foo) -> Void, parameters = [MethodParameter: argumentLabel = nil, name = , typeName = Foo, `inout` = false, typeAttributes = [:], defaultValue = nil, annotations = [:]], returnTypeName = Void, actualReturnTypeName = Void, `throws` = false | |
name : call4 | |
typeName: (foo: Foo, Bar) -> Bazel | |
ClosureType: name = (foo: Foo, Bar) -> Bazel, parameters = [MethodParameter: argumentLabel = nil, name = foo, typeName = Foo, `inout` = false, typeAttributes = [:], defaultValue = nil, annotations = [:], MethodParameter: argumentLabel = nil, name = , typeName = Bar, `inout` = false, typeAttributes = [:], defaultValue = nil, annotations = [:]], returnTypeName = Bazel, actualReturnTypeName = Bazel, `throws` = false | |
*/ | |
// MARK: - Environment.Call | |
extension Environment { | |
enum Call { | |
case call1 | |
case call2 | |
case call3(Foo) | |
case call4(foo: Foo, Bar) | |
} | |
} | |
extension EnvironmentCallCapturer where Environment == Environment, Call == Environment.Call { | |
static func capturer( | |
call2: Foo, | |
call4: Bazel | |
// call2: () -> Foo, | |
// call4: (foo: Foo, Bar) -> Bazel | |
) -> EnvironmentCallCapturer<Environment, Environment.Call> { | |
EnvironmentCallCapturer { (send: @escaping (Environment.Call) -> Void) in | |
Environment( | |
call1: { | |
send(.call1) | |
// return void | |
}, | |
call2: { | |
send(.call2) | |
return call2 | |
}, | |
call3: { (arg0) in | |
send(.call3(arg0)) | |
// return void | |
}, | |
call4: { (foo, arg1) in | |
send(.call4(foo: foo, arg1)) | |
return call4 | |
} | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Asked how to pull apart the closure's type: krzysztofzablocki/Sourcery#855
It doesn't seem possible at the moment.