-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArbitrary.stencil
124 lines (116 loc) · 6.83 KB
/
Arbitrary.stencil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{% for type in types.all where type.name == "SourceryImports_Arbitrary" %}
{% for variable in type.variables %}
{% if variable|annotated:"testable" %}@testable {% else %}{% endif %}{% if variable|annotated:"preconcurrency" %}@preconcurrency {% else %}{% endif %}import {{ variable.name }}
{% endfor %}
{% endfor %}
{% macro checkError typeNameString %}{% if typeNameString == "Error" %}String{% else %}{{ typeNameString }}{% endif %}{% endmacro %}
{% macro iterateVariables variables %}
{% for var in variables %}
{% if var.actualTypeName.name|hasPrefix:"UI" %}
{{ var.name }}: {{ var.actualTypeName.name }}(){% if not forloop.last %},{% endif %}
{% elif var.isOptional %}
{{ var.name }}: $0.generate(using: Optional<{{ var.actualTypeName.unwrappedTypeName }}>.arbitrary.map { $0 }){% if not forloop.last %}, {% endif %}
{% elif var.isArray %}
{{ var.name }}: $0.generate(using: Array<{{ var.typeName.array.elementTypeName.name }}>.arbitrary.map { $0 }){% if not forloop.last %}, {% endif %}
{% elif var.isDictionary %}
{{ var.name }}: $0.generate(using: Dictionary<{{ var.typeName.dictionary.keyTypeName.name }}, {{ var.typeName.dictionary.valueTypeName.name }}>.arbitrary.map { $0 }){% if not forloop.last %}, {% endif %}
{% elif var.actualTypeName.name|contains:">" %}
{{ var.name }}: $0.generate(using: Arbitrary{{ var.actualTypeName.name }}.arbitrary.map { $0.get }){% if not forloop.last %}, {% endif %}
{% elif var.actualTypeName.name == "Error" %}
{{ var.name }}: $0.generate(using: String.arbitrary){% if not forloop.last %}, {% endif %}
{% else %}
{{ var.name }}: $0.generate(){% if not forloop.last %}, {% endif %}
{% endif %}
{% endfor %}
{% endmacro %}
{% macro caseOptional type case associatedValue %}Optional<{{ associatedValue.actualTypeName.unwrappedTypeName }}>.arbitrary.map { $0 }.map({{ type.name}}.{{ case.name }}){% if not forloop.last %},{% endif %}{% endmacro %}
{% macro caseArray type case associatedValue %}Array<{{ associatedValue.typeName.array.elementTypeName.name }}>.arbitrary.map { $0 }.map({{ type.name}}.{{ case.name }}){% if not forloop.last %},{% endif %}{% endmacro %}
{% macro caseDictionary type case associatedValue %}Dictionary<{{ associatedValue.typeName.dictionary.keyTypeName.name }}, {{ associatedValue.typeName.dictionary.valueTypeName.name }}>.arbitrary.map { $0 }.map({{ type.name}}.{{ case.name }}){% if not forloop.last %},{% endif %}{% endmacro %}
{% macro iterateCases cases pureTypeName optionalGenericParameter %}
{% for case in cases %}
{% if not case.hasAssociatedValue %}
Gen.pure({{ pureTypeName }}{{ optionalGenericParameter }}.{{ case.name }}){% if not forloop.last %},{% endif %}
{% elif case.associatedValues.count == 1 %}
{% if case.associatedValues.first.actualTypeName.name|hasPrefix:"UI" %}
Gen<{{ case.associatedValues.first.actualTypeName.name }}>.pure({{ case.associatedValues.first.actualTypeName.name }}()).map({{ type.name }}.{{ case.name }}){% if not forloop.last %},{% endif %}
{% elif case.associatedValues.first.isOptional %}
{% call caseOptional type case case.associatedValues.first %}
{% elif case.associatedValues.first.isArray %}
{% call caseArray type case case.associatedValues.first %}
{% elif case.associatedValues.first.isDictionary %}
{% call caseDictionary type case case.associatedValues.first %}
{% else %}
{% call checkError case.associatedValues.first.actualTypeName.name %}.arbitrary.map({{ type.name }}.{{ case.name }}){% if not forloop.last %},{% endif %}
{% endif %}
{% else %}
GenZip.with(
{% for assoVal in case.associatedValues %}
{% if assoVal.actualTypeName.name|hasPrefix:"UI" %}
Gen<{{ assoVal.actualTypeName.name }}>.pure({{ assoVal.actualTypeName.name }}()){% if not forloop.last %},{% endif %}
{% elif case.associatedValues.first.isOptional %}
{% call caseOptional type case assoVal %}
{% elif case.associatedValues.first.isArray %}
{% call caseArray type case assoVal %}
{% elif case.associatedValues.first.isDictionary %}
{% call caseDictionary type case assoVal %}
{% else %}
{% call checkError assoVal.actualTypeName.name %}.arbitrary{% if not forloop.last %},{% endif %}{% endif %}{% endfor %})
.map({{ type.name}}.{{ case.name }}){% if not forloop.last %},{% endif %}
{% endif %}
{% endfor %}
{% endmacro %}
// MARK: - Arbitrary for structs and classes
{% for type in types.structs|annotated:"arbitrary" %}
{% if type.isGeneric %}
struct Arbitrary{{ type.name }}<T>: Arbitrary where T: Arbitrary{% if type.annotations.genericParameterProtocols %} & {{ type.annotations.genericParameterProtocols }}{% endif %} {
let get: {{ type.name }}<T>
init(_ get: {{ type.name }}<T>) {
self.get = get
}
public static var arbitrary: Gen<Arbitrary{{ type.name }}<T>> {
return Gen<{{ type.name }}<T>>
.compose {
{{ type.name }}<T>.init(
{% call iterateVariables type.storedVariables %}
)
}
.map(Arbitrary{{ type.name }}<T>.init)
}
}
{% else %}
extension {{ type.name }}: {% if type|annotated:"retroactive" %}@retroactive {% else %}{% endif %}Arbitrary {
public static var arbitrary: Gen<{{ type.name }}> {
return Gen<{{ type.name }}>
.compose {
{{ type.name }}.init(
{% call iterateVariables type.storedVariables %}
)
}
}
}
{% endif %}
{% endfor %}
// MARK: - Arbitrary for enums
{% for type in types.enums|annotated:"arbitrary" %}
{% if type.isGeneric %}
struct Arbitrary{{ type.name }}<T>: Arbitrary where T: Arbitrary{% if type.annotations.genericParameterProtocols %} & {{ type.annotations.genericParameterProtocols }}{% endif %} {
let get: {{ type.name }}<T>
init(_ get: {{ type.name }}<T>) {
self.get = get
}
public static var arbitrary: Gen<Arbitrary{{ type.name }}<T>> {
return Gen<{{ type.name }}<T>>.one(of: [
{% call iterateCases type.cases type.name "<T>" %}
]).map(Arbitrary{{ type.name }}<T>.init)
}
}
{% else %}
extension {{ type.name }}: {% if type|annotated:"retroactive" %}@retroactive {% else %}{% endif %}Arbitrary {
public static var arbitrary: Gen<{{ type.name}}> {
return Gen<{{ type.name}}>.one(of: [
{% call iterateCases type.cases type.name "" %}
])
}
}
{% endif %}
{% endfor %}