-
Notifications
You must be signed in to change notification settings - Fork 508
/
Generators.scala
395 lines (316 loc) · 12.5 KB
/
Generators.scala
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright 2020-2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cats.effect.kernel
package testkit
import cats.{Applicative, ApplicativeError, Eq, Monad, MonadError}
import cats.syntax.all._
import org.scalacheck.{Arbitrary, Cogen, Gen}
import org.scalacheck.Arbitrary.arbitrary
import scala.collection.immutable.SortedMap
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.FiniteDuration
trait GenK[F[_]] extends Serializable {
def apply[A: Arbitrary: Cogen]: Gen[F[A]]
}
// Generators for * -> * kinded types
trait Generators1[F[_]] extends Serializable {
protected val maxDepth: Int = 10
// todo: uniqueness based on... names, I guess. Have to solve the diamond problem somehow
// Generators of base cases, with no recursion
protected def baseGen[A: Arbitrary: Cogen]: List[(String, Gen[F[A]])] = {
// prevent unused implicit param warnings, the params need to stay because
// this method is overriden in subtraits
val _ = (implicitly[Arbitrary[A]], implicitly[Cogen[A]])
Nil
}
// Only recursive generators - the argument is a generator of the next level of depth
protected def recursiveGen[A: Arbitrary: Cogen](
deeper: GenK[F]): List[(String, Gen[F[A]])] = {
// prevent unused params warnings, the params need to stay because
// this method is overriden in subtraits
val _ = (deeper, implicitly[Arbitrary[A]], implicitly[Cogen[A]])
Nil
}
// All generators possible at depth [[depth]]
private def gen[A: Arbitrary: Cogen](depth: Int): Gen[F[A]] = {
val genK: GenK[F] = new GenK[F] {
def apply[B: Arbitrary: Cogen]: Gen[F[B]] = Gen.delay(gen(depth + 1))
}
val gens =
if (depth > maxDepth) baseGen[A]
else baseGen[A] ++ recursiveGen[A](genK)
Gen.oneOf(SortedMap(gens: _*).map(_._2)).flatMap(identity)
}
// All generators possible at depth 0 - the only public method
def generators[A: Arbitrary: Cogen]: Gen[F[A]] = gen[A](0)
}
//Applicative is the first place that lets us introduce values in the context, if we discount InvariantMonoidal
trait ApplicativeGenerators[F[_]] extends Generators1[F] {
implicit val F: Applicative[F]
override protected def baseGen[A: Arbitrary: Cogen]: List[(String, Gen[F[A]])] =
List("pure" -> genPure[A]) ++ super.baseGen[A]
override protected def recursiveGen[A: Arbitrary: Cogen](
deeper: GenK[F]): List[(String, Gen[F[A]])] =
List(
"map" -> genMap[A](deeper),
"ap" -> genAp[A](deeper)
) ++ super.recursiveGen(deeper)
private def genPure[A: Arbitrary]: Gen[F[A]] =
arbitrary[A].map(_.pure[F])
private def genMap[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
for {
fa <- deeper[A]
f <- Arbitrary.arbitrary[A => A]
} yield F.map(fa)(f)
private def genAp[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
for {
fa <- deeper[A]
ff <- deeper[A => A]
} yield F.ap(ff)(fa)
}
trait MonadGenerators[F[_]] extends ApplicativeGenerators[F] {
implicit val F: Monad[F]
override protected def recursiveGen[A: Arbitrary: Cogen](
deeper: GenK[F]): List[(String, Gen[F[A]])] =
List(
"flatMap" -> genFlatMap(deeper)
) ++ super.recursiveGen(deeper)
private def genFlatMap[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
for {
fa <- deeper[A]
f <- Gen.function1[A, F[A]](deeper[A])
} yield fa.flatMap(f)
}
trait ApplicativeErrorGenerators[F[_], E] extends ApplicativeGenerators[F] {
implicit val arbitraryE: Arbitrary[E]
implicit val cogenE: Cogen[E]
implicit val F: ApplicativeError[F, E]
override protected def baseGen[A: Arbitrary: Cogen]: List[(String, Gen[F[A]])] =
List(
"raiseError" -> genRaiseError[A]
) ++ super.baseGen[A]
override protected def recursiveGen[A](
deeper: GenK[F])(implicit AA: Arbitrary[A], AC: Cogen[A]): List[(String, Gen[F[A]])] =
List(
"handleErrorWith" -> genHandleErrorWith[A](deeper)(AA, AC)
) ++ super.recursiveGen(deeper)(AA, AC)
private def genRaiseError[A]: Gen[F[A]] =
arbitrary[E].map(F.raiseError[A](_))
private def genHandleErrorWith[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
for {
fa <- deeper[A]
f <- Gen.function1[E, F[A]](deeper[A])
} yield F.handleErrorWith(fa)(f)
}
trait MonadErrorGenerators[F[_], E]
extends MonadGenerators[F]
with ApplicativeErrorGenerators[F, E] {
implicit val F: MonadError[F, E]
}
trait ClockGenerators[F[_]] extends Generators1[F] {
implicit val F: Clock[F]
implicit protected val arbitraryFD: Arbitrary[FiniteDuration]
override protected def baseGen[A: Arbitrary: Cogen] =
List("monotonic" -> genMonotonic[A], "realTime" -> genRealTime[A]) ++ super.baseGen[A]
private def genMonotonic[A: Arbitrary] =
arbitrary[A].map(F.applicative.as(F.monotonic, _))
private def genRealTime[A: Arbitrary] =
arbitrary[A].map(F.applicative.as(F.realTime, _))
}
trait SyncGenerators[F[_]] extends MonadErrorGenerators[F, Throwable] with ClockGenerators[F] {
implicit val F: Sync[F]
override protected def baseGen[A: Arbitrary: Cogen] =
("delay" -> arbitrary[A].map(F.delay(_))) :: super.baseGen[A]
}
trait MonadCancelGenerators[F[_], E] extends MonadErrorGenerators[F, E] {
implicit val F: MonadCancel[F, E]
override protected def baseGen[A: Arbitrary: Cogen]: List[(String, Gen[F[A]])] =
("canceled" -> genCanceled[A]) :: super.baseGen[A]
override protected def recursiveGen[A](
deeper: GenK[F])(implicit AA: Arbitrary[A], AC: Cogen[A]): List[(String, Gen[F[A]])] =
List(
"forceR" -> genForceR[A](deeper),
"uncancelable" -> genUncancelable[A](deeper),
"onCancel" -> genOnCancel[A](deeper)
) ++ super.recursiveGen(deeper)(AA, AC)
private def genCanceled[A: Arbitrary]: Gen[F[A]] =
arbitrary[A].map(F.canceled.as(_))
private def genForceR[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
for {
left <- deeper[Unit]
right <- deeper[A]
} yield F.forceR(left)(right)
// TODO we can't really use poll :-( since we can't Cogen FunctionK
private def genUncancelable[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
deeper[A].map(pc =>
F.uncancelable(_ => pc)
.flatMap(F.pure(_))
.handleErrorWith(
F.raiseError(_)
)) // this is a bit of a hack to get around functor law breakage
private def genOnCancel[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
for {
fa <- deeper[A]
fin <- deeper[Unit]
} yield F.onCancel(fa, fin)
}
trait GenSpawnGenerators[F[_], E] extends MonadCancelGenerators[F, E] {
implicit val F: GenSpawn[F, E]
override protected def baseGen[A: Arbitrary: Cogen]: List[(String, Gen[F[A]])] =
List(
"cede" -> genCede[A],
"never" -> genNever[A]
) ++ super.baseGen[A]
override protected def recursiveGen[A](
deeper: GenK[F])(implicit AA: Arbitrary[A], AC: Cogen[A]): List[(String, Gen[F[A]])] =
List(
"racePair" -> genRacePair[A](deeper),
"start" -> genStart[A](deeper),
"join" -> genJoin[A](deeper)) ++ super.recursiveGen(deeper)(AA, AC)
private def genCede[A: Arbitrary]: Gen[F[A]] =
arbitrary[A].map(F.cede.as(_))
private def genNever[A]: Gen[F[A]] =
F.never[A]
private def genStart[A: Arbitrary](deeper: GenK[F]): Gen[F[A]] =
deeper[Unit].flatMap(pc => arbitrary[A].map(a => F.start(pc).as(a)))
private def genJoin[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
for {
fiber <- deeper[A]
cont <- deeper[Unit]
a <- arbitrary[A]
} yield F.start(fiber).flatMap(f => cont >> f.join).as(a)
private def genRacePair[A: Arbitrary: Cogen](deeper: GenK[F]): Gen[F[A]] =
for {
fa <- deeper[A]
fb <- deeper[A]
cancel <- arbitrary[Boolean]
back = F.racePair(fa, fb).flatMap {
case Left((oc, f)) =>
if (cancel)
f.cancel *> oc.embedNever
else
f.join *> oc.embedNever
case Right((f, oc)) =>
if (cancel)
f.cancel *> oc.embedNever
else
f.join *> oc.embedNever
}
} yield back
}
trait GenTemporalGenerators[F[_], E] extends GenSpawnGenerators[F, E] with ClockGenerators[F] {
implicit val F: GenTemporal[F, E]
override protected def baseGen[A: Arbitrary: Cogen] =
("sleep" -> genSleep[A]) :: super.baseGen[A]
private def genSleep[A: Arbitrary] =
for {
t <- arbitraryFD.arbitrary
a <- arbitrary[A]
} yield F.sleep(t).as(a)
}
trait AsyncGenerators[F[_]] extends GenTemporalGenerators[F, Throwable] with SyncGenerators[F] {
implicit val F: Async[F]
implicit protected val arbitraryEC: Arbitrary[ExecutionContext]
implicit protected val cogenFU: Cogen[F[Unit]]
override protected def recursiveGen[A: Arbitrary: Cogen](deeper: GenK[F]) =
List("async" -> genAsync[A](deeper), "evalOn" -> genEvalOn[A](deeper)) ++ super
.recursiveGen[A](deeper)
private def genAsync[A: Arbitrary](deeper: GenK[F]) =
for {
result <- arbitrary[Either[Throwable, A]]
fo <- deeper[Option[F[Unit]]](
Arbitrary(Gen.option[F[Unit]](deeper[Unit])),
Cogen.cogenOption(cogenFU))
} yield F
.async[A](k => F.delay(k(result)) >> fo)
.flatMap(F.pure(_))
.handleErrorWith(F.raiseError(_))
private def genEvalOn[A: Arbitrary: Cogen](deeper: GenK[F]) =
for {
fa <- deeper[A]
ec <- arbitraryEC.arbitrary
} yield F.evalOn(fa, ec)
}
trait AsyncGeneratorsWithoutEvalShift[F[_]]
extends GenTemporalGenerators[F, Throwable]
with SyncGenerators[F] {
implicit val F: Async[F]
implicit protected val cogenFU: Cogen[F[Unit]] = Cogen[Unit].contramap(_ => ())
override protected def recursiveGen[A: Arbitrary: Cogen](deeper: GenK[F]) =
("async" -> genAsync[A](deeper)) :: super.recursiveGen[A](deeper)
private def genAsync[A: Arbitrary](deeper: GenK[F]) =
for {
result <- arbitrary[Either[Throwable, A]]
fo <- deeper[Option[F[Unit]]](
Arbitrary(Gen.option[F[Unit]](deeper[Unit])),
Cogen.cogenOption(cogenFU))
} yield F
.async[A](k => F.delay(k(result)) >> fo)
.flatMap(F.pure(_))
.handleErrorWith(F.raiseError(_))
}
trait ParallelFGenerators {
implicit def arbitraryParallelF[F[_], A](
implicit ArbF: Arbitrary[F[A]]): Arbitrary[ParallelF[F, A]] =
Arbitrary {
ArbF.arbitrary.map(f => ParallelF(f))
}
implicit def eqParallelF[F[_], A](implicit EqF: Eq[F[A]]): Eq[ParallelF[F, A]] =
EqF.imap(ParallelF.apply)(ParallelF.value)
}
object ParallelFGenerators extends ParallelFGenerators
trait OutcomeGenerators {
def outcomeGenerators[F[_]: Applicative, E: Arbitrary: Cogen] =
new ApplicativeErrorGenerators[Outcome[F, E, *], E] {
val arbitraryE: Arbitrary[E] = implicitly
val cogenE: Cogen[E] = implicitly
implicit val F: ApplicativeError[Outcome[F, E, *], E] = Outcome.applicativeError[F, E]
override protected def baseGen[A: Arbitrary: Cogen]
: List[(String, Gen[Outcome[F, E, A]])] =
List(
"const(Canceled)" -> Gen.const(Outcome.Canceled[F, E, A]())
) ++ super.baseGen[A]
}
implicit def arbitraryOutcome[F[_]: Applicative, E: Arbitrary: Cogen, A: Arbitrary: Cogen]
: Arbitrary[Outcome[F, E, A]] =
Arbitrary {
outcomeGenerators[F, E].generators[A]
}
implicit def cogenOutcome[F[_], E: Cogen, A](
implicit A: Cogen[F[A]]): Cogen[Outcome[F, E, A]] =
Cogen[Option[Either[E, F[A]]]].contramap {
case Outcome.Canceled() => None
case Outcome.Succeeded(fa) => Some(Right(fa))
case Outcome.Errored(e) => Some(Left(e))
}
}
object OutcomeGenerators extends OutcomeGenerators
trait SyncTypeGenerators {
implicit val arbitrarySyncType: Arbitrary[Sync.Type] = {
import Sync.Type._
Arbitrary(Gen.oneOf(Delay, Blocking, InterruptibleOnce, InterruptibleMany))
}
implicit val cogenSyncType: Cogen[Sync.Type] = {
import Sync.Type._
Cogen[Int] contramap {
case Delay => 0
case Blocking => 1
case InterruptibleOnce => 2
case InterruptibleMany => 3
}
}
}
object SyncTypeGenerators extends SyncTypeGenerators