-
Notifications
You must be signed in to change notification settings - Fork 437
/
UnitSpec.kt
247 lines (224 loc) · 6.22 KB
/
UnitSpec.kt
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package arrow.core.test
import arrow.core.NonEmptyList
import arrow.core.Tuple4
import arrow.core.Tuple5
import arrow.core.test.generators.unit
import arrow.core.test.laws.Law
import io.kotest.core.names.TestName
import io.kotest.core.spec.style.StringSpec
import io.kotest.property.Arb
import io.kotest.property.Gen
import io.kotest.property.PropertyContext
import io.kotest.property.arbitrary.bind
import io.kotest.property.arbitrary.filter
import io.kotest.property.arbitrary.map
import io.kotest.property.arbitrary.list as KList
import io.kotest.property.arbitrary.map as KMap
import io.kotest.property.checkAll
import kotlin.jvm.JvmOverloads
import kotlin.math.max
/**
* Base class for unit tests
*/
public abstract class UnitSpec(
public val iterations: Int = 250,
public val maxDepth: Int = 15,
spec: UnitSpec.() -> Unit = {}
) : StringSpec() {
public constructor(spec: UnitSpec.() -> Unit) : this(250, 15, spec)
public fun <A> Arb.Companion.list(gen: Gen<A>, range: IntRange = 0..maxDepth): Arb<List<A>> =
Arb.KList(gen, range)
public fun <A> Arb.Companion.nonEmptyList(arb: Arb<A>, depth: Int = maxDepth): Arb<NonEmptyList<A>> =
Arb.list(arb, 1..max(1, depth)).filter(List<A>::isNotEmpty).map(NonEmptyList.Companion::fromListUnsafe)
public fun <A> Arb.Companion.sequence(arbA: Arb<A>, range: IntRange = 0..maxDepth): Arb<Sequence<A>> =
Arb.list(arbA, range).map { it.asSequence() }
@JvmOverloads
public inline fun <reified A> Arb.Companion.array(gen: Arb<A>, range: IntRange = 0..maxDepth): Arb<Array<A>> =
Arb.list(gen, range).map { it.toTypedArray() }
public fun <K, V> Arb.Companion.map(
keyArb: Arb<K>,
valueArb: Arb<V>,
minSize: Int = 1,
maxSize: Int = 15
): Arb<Map<K, V>> =
Arb.KMap(keyArb, valueArb, minSize = minSize, maxSize = maxSize)
init {
spec()
}
public fun testLaws(vararg laws: List<Law>): Unit = laws
.flatMap { list: List<Law> -> list.asIterable() }
.distinctBy { law: Law -> law.name }
.forEach { law: Law ->
registration().addTest(TestName(law.name), xdisabled = false, law.test)
}
public fun testLaws(prefix: String, vararg laws: List<Law>): Unit = laws
.flatMap { list: List<Law> -> list.asIterable() }
.distinctBy { law: Law -> law.name }
.forEach { law: Law ->
registration().addTest(TestName(prefix, law.name, true), xdisabled = false, law.test)
}
public suspend fun checkAll(property: suspend PropertyContext.() -> Unit): PropertyContext =
checkAll(iterations, Arb.unit()) { property() }
public suspend fun <A> checkAll(
genA: Arb<A>,
property: suspend PropertyContext.(A) -> Unit
): PropertyContext =
checkAll(
iterations,
genA,
property
)
public suspend fun <A, B> checkAll(
genA: Arb<A>,
genB: Arb<B>,
property: suspend PropertyContext.(A, B) -> Unit
): PropertyContext =
checkAll(
iterations,
genA,
genB,
property
)
public suspend fun <A, B, C> checkAll(
genA: Arb<A>,
genB: Arb<B>,
genC: Arb<C>,
property: suspend PropertyContext.(A, B, C) -> Unit
): PropertyContext =
checkAll(
iterations,
genA,
genB,
genC,
property
)
public suspend fun <A, B, C, D> checkAll(
genA: Arb<A>,
genB: Arb<B>,
genC: Arb<C>,
genD: Arb<D>,
property: suspend PropertyContext.(A, B, C, D) -> Unit
): PropertyContext =
checkAll(
iterations,
genA,
genB,
genC,
genD,
property
)
public suspend fun <A, B, C, D, E> checkAll(
genA: Arb<A>,
genB: Arb<B>,
genC: Arb<C>,
genD: Arb<D>,
genE: Arb<E>,
property: suspend PropertyContext.(A, B, C, D, E) -> Unit
): PropertyContext =
checkAll(
iterations,
genA,
genB,
genC,
genD,
genE,
property
)
public suspend fun <A, B, C, D, E, F> checkAll(
genA: Arb<A>,
genB: Arb<B>,
genC: Arb<C>,
genD: Arb<D>,
genE: Arb<E>,
genF: Arb<F>,
property: suspend PropertyContext.(A, B, C, D, E, F) -> Unit
): PropertyContext =
checkAll(
iterations,
genA,
genB,
genC,
genD,
genE,
genF,
property
)
public suspend fun <A, B, C, D, E, F, G> checkAll(
gena: Arb<A>,
genb: Arb<B>,
genc: Arb<C>,
gend: Arb<D>,
gene: Arb<E>,
genf: Arb<F>,
geng: Arb<G>,
fn: suspend PropertyContext.(a: A, b: B, c: C, d: D, e: E, f: F, g: G) -> Unit
) {
checkAll(gena, genb, genc, gend, gene, Arb.bind(genf, geng, ::Pair)) { a, b, c, d, e, (f, g) ->
fn(a, b, c, d, e, f, g)
}
}
public suspend fun <A, B, C, D, E, F, G, H> checkAll(
gena: Arb<A>,
genb: Arb<B>,
genc: Arb<C>,
gend: Arb<D>,
gene: Arb<E>,
genf: Arb<F>,
geng: Arb<G>,
genh: Arb<H>,
fn: suspend PropertyContext.(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) -> Unit
) {
checkAll(gena, genb, genc, gend, gene, Arb.bind(genf, geng, genh, ::Triple)) { a, b, c, d, e, (f, g, h) ->
fn(a, b, c, d, e, f, g, h)
}
}
public suspend fun <A, B, C, D, E, F, G, H, I> checkAll(
gena: Arb<A>,
genb: Arb<B>,
genc: Arb<C>,
gend: Arb<D>,
gene: Arb<E>,
genf: Arb<F>,
geng: Arb<G>,
genh: Arb<H>,
geni: Arb<I>,
fn: suspend PropertyContext.(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) -> Unit
) {
checkAll(gena, genb, genc, gend, gene, Arb.bind(genf, geng, genh, geni, ::Tuple4)) { a, b, c, d, e, (f, g, h, i) ->
fn(a, b, c, d, e, f, g, h, i)
}
}
public suspend fun <A, B, C, D, E, F, G, H, I, J> checkAll(
gena: Arb<A>,
genb: Arb<B>,
genc: Arb<C>,
gend: Arb<D>,
gene: Arb<E>,
genf: Arb<F>,
geng: Arb<G>,
genh: Arb<H>,
geni: Arb<I>,
genj: Arb<J>,
fn: suspend PropertyContext.(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) -> Unit
) {
checkAll(
gena,
genb,
genc,
gend,
gene,
Arb.bind(genf, geng, genh, geni, genj, ::Tuple5)
) { a, b, c, d, e, (f, g, h, i, j) ->
fn(a, b, c, d, e, f, g, h, i, j)
}
}
public suspend fun forFew(
iterations: Int,
property: suspend PropertyContext.(Unit) -> Unit
): PropertyContext =
checkAll(
iterations,
Arb.unit(),
property
)
}