Repository navigation
Expand file tree
/
Copy pathTestAspect.scala
More file actions
580 lines (521 loc) · 21.2 KB
/
Copy pathTestAspect.scala
File metadata and controls
580 lines (521 loc) · 21.2 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* Copyright 2019-2020 John A. De Goes and the ZIO Contributors
*
* 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 zio.test
import zio.clock.Clock
import zio.duration._
import zio.system
import zio.system.System
import zio.test.environment.{ Live, Restorable, TestClock, TestConsole, TestRandom, TestSystem }
import zio.{ Cause, Schedule, ZIO, ZManaged }
/**
* A `TestAspect` is an aspect that can be weaved into specs. You can think of
* an aspect as a polymorphic function, capable of transforming one test into
* another, possibly enlarging the environment, error, or success type.
*/
trait TestAspect[+LowerR, -UpperR, +LowerE, -UpperE, +LowerS, -UpperS] { self =>
/**
* Applies the aspect to some tests in the spec, chosen by the provided
* predicate.
*/
def some[R >: LowerR <: UpperR, E >: LowerE <: UpperE, S >: LowerS <: UpperS, L](
predicate: L => Boolean,
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S]
/**
* An alias for [[all]].
*/
final def apply[R >: LowerR <: UpperR, E >: LowerE <: UpperE, S >: LowerS <: UpperS, L](
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S] =
all(spec)
/**
* Applies the aspect to every test in the spec.
*/
final def all[R >: LowerR <: UpperR, E >: LowerE <: UpperE, S >: LowerS <: UpperS, L](
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S] =
some[R, E, S, L](_ => true, spec)
/**
* Returns a new aspect that represents the sequential composition of this
* aspect with the specified one.
*/
final def >>>[
LowerR1 >: LowerR,
UpperR1 <: UpperR,
LowerE1 >: LowerE,
UpperE1 <: UpperE,
LowerS1 >: LowerS,
UpperS1 <: UpperS
](
that: TestAspect[LowerR1, UpperR1, LowerE1, UpperE1, LowerS1, UpperS1]
): TestAspect[LowerR1, UpperR1, LowerE1, UpperE1, LowerS1, UpperS1] =
new TestAspect[LowerR1, UpperR1, LowerE1, UpperE1, LowerS1, UpperS1] {
def some[R >: LowerR1 <: UpperR1, E >: LowerE1 <: UpperE1, S >: LowerS1 <: UpperS1, L](
predicate: L => Boolean,
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S] =
that.some(predicate, self.some(predicate, spec))
}
final def andThen[
LowerR1 >: LowerR,
UpperR1 <: UpperR,
LowerE1 >: LowerE,
UpperE1 <: UpperE,
LowerS1 >: LowerS,
UpperS1 <: UpperS
](
that: TestAspect[LowerR1, UpperR1, LowerE1, UpperE1, LowerS1, UpperS1]
): TestAspect[LowerR1, UpperR1, LowerE1, UpperE1, LowerS1, UpperS1] =
self >>> that
}
object TestAspect extends TimeoutVariants {
/**
* An aspect that returns the tests unchanged
*/
val identity: TestAspectPoly =
new TestAspectPoly {
def some[R >: Nothing <: Any, E >: Nothing <: Any, S >: Nothing <: Any, L](
predicate: L => Boolean,
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S] = spec
}
/**
* An aspect that marks tests as ignored.
*/
val ignore: TestAspectAtLeastR[Annotations] =
new TestAspectAtLeastR[Annotations] {
def some[R <: Annotations, E, S, L](predicate: L => Boolean, spec: ZSpec[R, E, L, S]): ZSpec[R, E, L, S] =
spec.when(false)
}
/**
* Constructs an aspect that runs the specified effect after every test.
*/
def after[R0, E0](effect: ZIO[R0, E0, Any]): TestAspect[Nothing, R0, E0, Any, Nothing, Any] =
new TestAspect.PerTest[Nothing, R0, E0, Any, Nothing, Any] {
def perTest[R >: Nothing <: R0, E >: E0 <: Any, S](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
test.run
.zipWith(effect.catchAllCause(cause => ZIO.fail(TestFailure.Runtime(cause))).run)(_ <* _)
.flatMap(ZIO.done)
}
/**
* Constructs an aspect that evaluates every test between two effects, `before` and `after`,
* where the result of `before` can be used in `after`.
*/
def around[R0, E0, A0](
before: ZIO[R0, E0, A0]
)(after: A0 => ZIO[R0, Nothing, Any]): TestAspect[Nothing, R0, E0, Any, Nothing, Any] =
new TestAspect.PerTest[Nothing, R0, E0, Any, Nothing, Any] {
def perTest[R >: Nothing <: R0, E >: E0 <: Any, S >: Nothing <: Any](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
before.catchAllCause(c => ZIO.fail(TestFailure.Runtime(c))).bracket(after)(_ => test)
}
/**
* A less powerful variant of `around` where the result of `before` is not required by after.
*/
def around_[R0, E0](
before: ZIO[R0, E0, Any],
after: ZIO[R0, Nothing, Any]
): TestAspect[Nothing, R0, E0, Any, Nothing, Any] = around(before)(_ => after)
/**
* Constructs an aspect that evaluates every test inside the context of the managed function.
*/
def aroundTest[R0, E0, S0](
managed: ZManaged[R0, TestFailure[E0], TestSuccess[S0] => ZIO[R0, TestFailure[E0], TestSuccess[S0]]]
) =
new TestAspect.PerTest[Nothing, R0, E0, Any, S0, S0] {
def perTest[R >: Nothing <: R0, E >: E0 <: Any, S >: S0 <: S0](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
managed.use(f => test.flatMap(f))
}
/**
* Constructs a simple monomorphic aspect that only works with the specified
* environment and error type.
*/
def aspect[R0, E0, S0](
f: ZIO[R0, TestFailure[E0], TestSuccess[S0]] => ZIO[R0, TestFailure[E0], TestSuccess[S0]]
): TestAspect[R0, R0, E0, E0, S0, S0] =
new TestAspect.PerTest[R0, R0, E0, E0, S0, S0] {
def perTest[R >: R0 <: R0, E >: E0 <: E0, S >: S0 <: S0](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
f(test)
}
/**
* Constructs an aspect that runs the specified effect before every test.
*/
def before[R0, E0, S0](effect: ZIO[R0, Nothing, Any]): TestAspect[Nothing, R0, E0, Any, S0, Any] =
new TestAspect.PerTest[Nothing, R0, E0, Any, S0, Any] {
def perTest[R >: Nothing <: R0, E >: E0 <: Any, S >: S0 <: Any](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
effect *> test
}
/**
* An aspect that applies the specified aspect on Dotty.
*/
def dotty[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS](
that: TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS]
): TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS] =
if (TestVersion.isDotty) that else identity
/**
* An aspect that only runs tests on Dotty.
*/
val dottyOnly: TestAspectAtLeastR[Annotations] =
if (TestVersion.isDotty) identity else ignore
/**
* An aspect that retries a test until success, without limit.
*/
val eventually: TestAspectAtLeastR[ZTestEnv] = {
val eventually = new PerTest.Poly {
def perTest[R >: Nothing <: Any, E >: Nothing <: Any, S >: Nothing <: Any](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
test.eventually
}
restoreTestEnvironment >>> eventually
}
/**
* An aspect that runs tests on all versions except Dotty.
*/
val exceptDotty: TestAspectAtLeastR[Annotations] =
if (TestVersion.isDotty) ignore else identity
/**
* An aspect that runs tests on all platforms except ScalaJS.
*/
val exceptJS: TestAspectAtLeastR[Annotations] =
if (TestPlatform.isJS) ignore else identity
/**
* An aspect that runs tests on all platforms except the JVM.
*/
val exceptJVM: TestAspectAtLeastR[Annotations] =
if (TestPlatform.isJVM) ignore else identity
/**
* An aspect that runs tests on all versions except Scala2.
*/
val exceptScala2: TestAspectAtLeastR[Annotations] =
if (TestVersion.isScala2) ignore else identity
/**
* An aspect that sets suites to the specified execution strategy, but only
* if their current strategy is inherited (undefined).
*/
def executionStrategy(exec: ExecutionStrategy): TestAspectPoly =
new TestAspectPoly {
def some[R >: Nothing <: Any, E >: Nothing <: Any, S >: Nothing <: Any, L](
predicate: L => Boolean,
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S] = spec.transform[R, TestFailure[E], L, TestSuccess[S]] {
case Spec.SuiteCase(label, specs, None) if (predicate(label)) => Spec.SuiteCase(label, specs, Some(exec))
case c => c
}
}
/**
* An aspect that makes a test that failed for any reason pass. Note that if the test
* passes this aspect will make it fail.
*/
val failure: PerTest[Nothing, Any, Nothing, Any, Unit, Unit] =
failure(Assertion.anything)
/**
* An aspect that makes a test that failed for the specified failure pass. Note that the
* test will fail for other failures and also if it passes correctly.
*/
def failure[E0](p: Assertion[TestFailure[E0]]): PerTest[Nothing, Any, Nothing, E0, Unit, Unit] =
new TestAspect.PerTest[Nothing, Any, Nothing, E0, Unit, Unit] {
def perTest[R >: Nothing <: Any, E >: Nothing <: E0, S >: Unit <: Unit](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] = {
lazy val succeed = ZIO.succeed(TestSuccess.Succeeded(BoolAlgebra.unit))
test.foldM(
{
case testFailure =>
p.run(testFailure).run.flatMap { p1 =>
if (p1.isSuccess) succeed
else ZIO.fail(TestFailure.Assertion(assert(testFailure)(p)))
}
},
_ => ZIO.fail(TestFailure.Runtime(zio.Cause.die(new RuntimeException("did not fail as expected"))))
)
}
}
/**
* An aspect that retries a test until success, with a default limit, for use
* with flaky tests.
*/
val flaky: TestAspectAtLeastR[ZTestEnv with Annotations] =
flaky(100)
/**
* An aspect that retries a test until success, with the specified limit, for
* use with flaky tests.
*/
def flaky(
n: Int
): TestAspectAtLeastR[ZTestEnv with Annotations] =
retry(Schedule.recurs(n))
/**
* An aspect that only runs a test if the specified environment variable
* satisfies the specified assertion.
*/
def ifEnv(env: String, assertion: Assertion[String]): TestAspectAtLeastR[Live[System] with Annotations] =
new TestAspectAtLeastR[Live[System] with Annotations] {
def some[R <: Live[System] with Annotations, E, S, L](
predicate: L => Boolean,
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S] =
spec.whenM(Live.live(system.env(env)).orDie.flatMap(_.fold(ZIO.succeed(false))(assertion.test)))
}
/**
* As aspect that only runs a test if the specified environment variable is
* set.
*/
def ifEnvSet(env: String): TestAspectAtLeastR[Live[System] with Annotations] =
ifEnv(env, Assertion.anything)
/**
* An aspect that only runs a test if the specified Java property satisfies
* the specified assertion.
*/
def ifProp(
prop: String,
assertion: Assertion[String]
): TestAspectAtLeastR[Live[System] with Annotations] =
new TestAspectAtLeastR[Live[System] with Annotations] {
def some[R <: Live[System] with Annotations, E, S, L](
predicate: L => Boolean,
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S] =
spec.whenM(Live.live(system.property(prop)).orDie.flatMap(_.fold(ZIO.succeed(false))(assertion.test)))
}
/**
* As aspect that only runs a test if the specified Java property is set.
*/
def ifPropSet(prop: String): TestAspectAtLeastR[Live[System] with Annotations] =
ifProp(prop, Assertion.anything)
/**
* An aspect that applies the specified aspect on ScalaJS.
*/
def js[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS](
that: TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS]
): TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS] =
if (TestPlatform.isJS) that else identity
/**
* An aspect that only runs tests on ScalaJS.
*/
val jsOnly: TestAspectAtLeastR[Annotations] =
if (TestPlatform.isJS) identity else ignore
/**
* An aspect that applies the specified aspect on the JVM.
*/
def jvm[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS](
that: TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS]
): TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS] =
if (TestPlatform.isJVM) that else identity
/**
* An aspect that only runs tests on the JVM.
*/
val jvmOnly: TestAspectAtLeastR[Annotations] =
if (TestPlatform.isJVM) identity else ignore
/**
* An aspect that repeats the test a default number of times, ensuring it is
* stable ("non-flaky"). Stops at the first failure.
*/
val nonFlaky: TestAspectAtLeastR[ZTestEnv with Annotations] =
nonFlaky(100)
/**
* An aspect that repeats the test a specified number of times, ensuring it
* is stable ("non-flaky"). Stops at the first failure.
*/
def nonFlaky(
n: Int
): TestAspectAtLeastR[ZTestEnv with Annotations] = {
val nonFlaky = new PerTest.AtLeastR[Annotations] {
def perTest[R >: Nothing <: Annotations, E >: Nothing <: Any, S >: Nothing <: Any](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] = {
def repeat(n: Int)(test: ZIO[R, TestFailure[E], TestSuccess[S]]): ZIO[R, TestFailure[E], TestSuccess[S]] =
if (n <= 1) test
else test.flatMap(_ => repeat(n - 1)(test))
repeat(n)(test <* Annotations.annotate(TestAnnotation.repeated, 1))
}
}
restoreTestEnvironment >>> nonFlaky
}
/**
* An aspect that executes the members of a suite in parallel.
*/
val parallel: TestAspectPoly = executionStrategy(ExecutionStrategy.Parallel)
/**
* An aspect that executes the members of a suite in parallel, up to the
* specified number of concurrent fibers.
*/
def parallelN(n: Int): TestAspectPoly = executionStrategy(ExecutionStrategy.ParallelN(n))
/**
* An aspect that restores a given [[zio.test.environment.Restorable Restorable]]'s state to its starting state
* after the test is run.
* Note that this is only useful when repeating tests.
*/
def restore[R0](service: R0 => Restorable): TestAspectAtLeastR[R0] =
around(ZIO.accessM[R0](r => service(r).save))(restore => restore)
/**
* An aspect that restores the [[zio.test.environment.TestClock TestClock]]'s state to its starting state after
* the test is run.
* Note that this is only useful when repeating tests.
*/
def restoreTestClock: TestAspectAtLeastR[TestClock] = restore[TestClock](_.clock)
/**
* An aspect that restores the [[zio.test.environment.TestConsole TestConsole]]'s state to its starting state after
* the test is run.
* Note that this is only useful when repeating tests.
*/
def restoreTestConsole: TestAspectAtLeastR[TestConsole] = restore[TestConsole](_.console)
/**
* An aspect that restores the [[zio.test.environment.TestRandom TestRandom]]'s state to its starting state after
* the test is run.
* Note that this is only useful when repeating tests.
*/
def restoreTestRandom: TestAspectAtLeastR[TestRandom] = restore[TestRandom](_.random)
/**
* An aspect that restores the [[zio.test.environment.TestSystem TestSystem]]'s state to its starting state after
* the test is run.
* Note that this is only useful when repeating tests.
*/
def restoreTestSystem: TestAspectAtLeastR[ZTestEnv] = restore[TestSystem](_.system)
/**
* An aspect that restores all state in the standard provided test environments
* ([[zio.test.environment.TestClock TestClock]], [[zio.test.environment.TestConsole TestConsole]],
* [[zio.test.environment.TestRandom TestRandom]] and [[zio.test.environment.TestSystem TestSystem]]) to their
* starting state after the test is run.
* Note that this is only useful when repeating tests.
*/
def restoreTestEnvironment: TestAspectAtLeastR[ZTestEnv] =
restoreTestClock >>> restoreTestConsole >>> restoreTestRandom >>> restoreTestSystem
/**
* An aspect that retries failed tests according to a schedule.
*/
def retry[R0 <: ZTestEnv with Annotations, E0, S0](
schedule: Schedule[R0, TestFailure[E0], S0]
): TestAspect[Nothing, R0, Nothing, E0, Nothing, Any] = {
val retry = new TestAspect.PerTest[Nothing, R0, Nothing, E0, Nothing, Any] {
def perTest[R >: Nothing <: R0, E >: Nothing <: E0, S >: Nothing <: Any](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
test.retry(schedule.tapOutput(_ => Annotations.annotate(TestAnnotation.retried, 1)))
}
restoreTestEnvironment >>> retry
}
/**
* An aspect that executes the members of a suite sequentially.
*/
val sequential: TestAspectPoly = executionStrategy(ExecutionStrategy.Sequential)
/**
* An aspect that applies the specified aspect on Scala 2.
*/
def scala2[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS](
that: TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS]
): TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS] =
if (TestVersion.isScala2) that else identity
/**
* An aspect that only runs tests on Scala 2.
*/
val scala2Only: TestAspectAtLeastR[Annotations] =
if (TestVersion.isScala2) identity else ignore
/**
* An aspect that converts ignored tests into test failures.
*/
val success: TestAspectPoly =
new PerTest.Poly {
def perTest[R >: Nothing <: Any, E >: Nothing <: Any, S >: Nothing <: Any](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
test.flatMap {
case TestSuccess.Ignored =>
ZIO.fail(TestFailure.Runtime(Cause.die(new RuntimeException("Test was ignored."))))
case x => ZIO.succeed(x)
}
}
/**
* Annotates tests with their execution times.
*/
val timed: TestAspect[Nothing, Live[Clock] with Annotations, Nothing, Any, Nothing, Any] =
new TestAspect.PerTest[Nothing, Live[Clock] with Annotations, Nothing, Any, Nothing, Any] {
def perTest[R >: Nothing <: Live[Clock] with Annotations, E >: Nothing <: Any, S >: Nothing <: Any](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] =
Live.withLive(test)(_.either.timed).flatMap {
case (duration, result) =>
ZIO.fromEither(result) <* Annotations.annotate(TestAnnotation.timing, duration)
}
}
/**
* An aspect that times out tests using the specified duration.
* @param duration maximum test duration
* @param interruptDuration after test timeout will wait given duration for successful interruption
*/
def timeout(
duration: Duration,
interruptDuration: Duration = 1.second
): TestAspectAtLeastR[Live[Clock]] =
new PerTest.AtLeastR[Live[Clock]] {
def perTest[R >: Nothing <: Live[Clock], E >: Nothing <: Any, S >: Nothing <: Any](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]] = {
def timeoutFailure =
TestTimeoutException(s"Timeout of ${duration.render} exceeded.")
def interruptionTimeoutFailure = {
val msg =
s"Timeout of ${duration.render} exceeded. Couldn't interrupt test within ${interruptDuration.render}, possible resource leak!"
TestTimeoutException(msg)
}
Live
.withLive(test)(_.either.timeoutFork(duration).flatMap {
case Left(fiber) =>
fiber.join.raceWith(ZIO.sleep(interruptDuration))(
(_, fiber) => fiber.interrupt *> ZIO.fail(TestFailure.Runtime(Cause.die(timeoutFailure))),
(_, _) => ZIO.fail(TestFailure.Runtime(Cause.die(interruptionTimeoutFailure)))
)
case Right(result) => result.fold(ZIO.fail, ZIO.succeed)
})
}
}
trait PerTest[+LowerR, -UpperR, +LowerE, -UpperE, +LowerS, -UpperS]
extends TestAspect[LowerR, UpperR, LowerE, UpperE, LowerS, UpperS] {
def perTest[R >: LowerR <: UpperR, E >: LowerE <: UpperE, S >: LowerS <: UpperS](
test: ZIO[R, TestFailure[E], TestSuccess[S]]
): ZIO[R, TestFailure[E], TestSuccess[S]]
final def some[R >: LowerR <: UpperR, E >: LowerE <: UpperE, S >: LowerS <: UpperS, L](
predicate: L => Boolean,
spec: ZSpec[R, E, L, S]
): ZSpec[R, E, L, S] =
spec.transform[R, TestFailure[E], L, TestSuccess[S]] {
case c @ Spec.SuiteCase(_, _, _) => c
case Spec.TestCase(label, test) =>
Spec.TestCase(label, if (predicate(label)) perTest(test) else test)
}
}
object PerTest {
/**
* A `PerTest.AtLeast[R]` is a `TestAspect.PerTest` that that requires at least an R in its environment
*/
type AtLeastR[R] =
TestAspect.PerTest[Nothing, R, Nothing, Any, Nothing, Any]
/**
* A `PerTest.Poly` is a `TestAspect.PerTest` that is completely polymorphic,
* having no requirements ZRTestEnv on error or environment.
*/
type Poly = TestAspect.PerTest[Nothing, Any, Nothing, Any, Nothing, Any]
}
}