Skip to content

Instantly share code, notes, and snippets.

@SzymonMrozek
Last active November 15, 2022 12:30
Show Gist options
  • Save SzymonMrozek/b104a8bb1220e784c039984acbf1cbb6 to your computer and use it in GitHub Desktop.
Save SzymonMrozek/b104a8bb1220e784c039984acbf1cbb6 to your computer and use it in GitHub Desktop.
import Combine
{% if argument.imports %}{{ argument.imports }}{% endif %}
{% for type in types.all where type|annotated:"AutoCombineTestUtil" %}
extension {{ type.name }} {
struct TestUtil {
{% for property in type.storedVariables where property.readAccess != "private" %}
let {{ property.name }}: {% if property.type|annotated:"AutoCombineTestUtil" %}{% if property.isOptional %}{{ property.typeName | replace:"?","" }}.TestUtil?{% else %}{{ property.typeName }}.TestUtil {% endif %} {% elif property.typeName|contains:"AnySubscriber" %}{{ property.typeName|replace:"AnySubscriber","PassthroughSubject"}}{% elif property.typeName|contains:"AnyPublisher" %}CurrentValueSubject<{{ property.typeName.generic.typeParameters.first.typeName }}?, Never>{% else %}{{ property.typeName }}{% endif %}
{% endfor %}
init(_ unit: {{ type.name }}) {
{% for property in type.storedVariables where property.readAccess != "private" %}
{% if property.type|annotated:"AutoCombineTestUtil" %}self.{{ property.name }} = {% if property.isOptional %}unit.{{ property.name }}.flatMap { {{ property.typeName | replace:"?","" }}.TestUtil($0) }{% else %}{{ property.typeName}}.TestUtil(unit.{{ property.name }}){% endif %}
{% elif property.typeName|contains:"AnySubscriber" %}self.{{ property.name }} = PassthroughSubject()
{% elif property.typeName|contains:"AnyPublisher" %}self.{{ property.name }} = CurrentValueSubject(nil)
{% else %}self.{{ property.name }} = unit.{{ property.name }}{% endif %}
{% endfor %}
{% for property in type.storedVariables where property.readAccess != "private" %}
{% if property.typeName|contains:"AnySubscriber" %}{% if property.isOptional %}if let {{ property.name }} = unit.{{ property.name }} { self.{{ property.name }}!.subscribe({{ property.name }}) }{% else %}self.{{ property.name }}.subscribe(unit.{{ property.name }}){% endif %}
{% elif property.typeName|contains:"AnyPublisher" %}unit.{{ property.name }}{% if property.isOptional %}?{% endif %}.map(Optional.init(_:)).subscribe(AnySubscriber(self.{{ property.name }}))
{% else %}{% endif %}
{% endfor %}
}
}
}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment