-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
TestAspect.scala
894 lines (801 loc) · 32.1 KB
/
TestAspect.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
* Copyright 2019-2021 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._
import zio.duration._
import zio.test.Assertion.{equalTo, hasMessage, isCase, isSubtype}
import zio.test.environment.{Live, Restorable, TestClock, TestConsole, TestRandom, TestSystem}
/**
* 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 or error type.
*/
abstract class TestAspect[+LowerR, -UpperR, +LowerE, -UpperE] { self =>
/**
* Applies the aspect to some tests in the spec, chosen by the provided
* predicate.
*/
def some[R >: LowerR <: UpperR, E >: LowerE <: UpperE](spec: ZSpec[R, E]): ZSpec[R, E]
/**
* An alias for [[all]].
*/
final def apply[R >: LowerR <: UpperR, E >: LowerE <: UpperE](spec: ZSpec[R, E]): ZSpec[R, E] =
all(spec)
/**
* Applies the aspect to every test in the spec.
*/
final def all[R >: LowerR <: UpperR, E >: LowerE <: UpperE](spec: ZSpec[R, E]): ZSpec[R, E] =
some[R, E](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](
that: TestAspect[LowerR1, UpperR1, LowerE1, UpperE1]
): TestAspect[LowerR1, UpperR1, LowerE1, UpperE1] =
new TestAspect[LowerR1, UpperR1, LowerE1, UpperE1] {
def some[R >: LowerR1 <: UpperR1, E >: LowerE1 <: UpperE1](
spec: ZSpec[R, E]
): ZSpec[R, E] =
that.some(self.some(spec))
}
final def andThen[LowerR1 >: LowerR, UpperR1 <: UpperR, LowerE1 >: LowerE, UpperE1 <: UpperE](
that: TestAspect[LowerR1, UpperR1, LowerE1, UpperE1]
): TestAspect[LowerR1, UpperR1, LowerE1, UpperE1] =
self >>> that
}
object TestAspect extends TimeoutVariants {
/**
* An aspect that returns the tests unchanged
*/
val identity: TestAspectPoly =
new TestAspectPoly {
def some[R, E](spec: ZSpec[R, E]): ZSpec[R, E] =
spec
}
/**
* An aspect that marks tests as ignored.
*/
val ignore: TestAspectAtLeastR[Annotations] =
new TestAspectAtLeastR[Annotations] {
def some[R <: Annotations, E](spec: ZSpec[R, E]): ZSpec[R, E] =
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] =
new TestAspect.PerTest[Nothing, R0, E0, Any] {
def perTest[R <: R0, E >: E0](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
test.run
.zipWith(effect.catchAllCause(cause => ZIO.fail(TestFailure.Runtime(cause))).run)(_ <* _)
.flatMap(ZIO.done(_))
}
/**
* Constructs an aspect that runs the specified effect after all tests.
*/
def afterAll[R0](effect: ZIO[R0, Nothing, Any]): TestAspect[Nothing, R0, Nothing, Any] =
aroundAll_(ZIO.unit, effect)
/**
* Annotates tests with the specified test annotation.
*/
def annotate[V](key: TestAnnotation[V], value: V): TestAspectPoly =
new TestAspectPoly {
def some[R, E](spec: ZSpec[R, E]): ZSpec[R, E] =
spec.annotate(key, value)
}
/**
* 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] =
new TestAspect.PerTest[Nothing, R0, E0, Any] {
def perTest[R <: R0, E >: E0](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
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] =
around(before)(_ => after)
/**
* Constructs an aspect that evaluates all tests between two effects, `before`
* and `after`, where the result of `before` can be used in `after`.
*/
def aroundAll[R0, E0, A0](
before: ZIO[R0, E0, A0]
)(after: A0 => ZIO[R0, Nothing, Any]): TestAspect[Nothing, R0, E0, Any] =
new TestAspect[Nothing, R0, E0, Any] {
def some[R <: R0, E >: E0](spec: ZSpec[R, E]): ZSpec[R, E] =
Spec.managed(ZManaged.make(before)(after).mapError(TestFailure.fail).as(spec))
}
/**
* A less powerful variant of `aroundAll` where the result of `before` is not
* required by `after`.
*/
def aroundAll_[R0, E0](before: ZIO[R0, E0, Any], after: ZIO[R0, Nothing, Any]): TestAspect[Nothing, R0, E0, Any] =
aroundAll(before)(_ => after)
/**
* Constructs an aspect that evaluates every test inside the context of the
* managed function.
*/
def aroundTest[R0, E0](
managed: ZManaged[R0, TestFailure[E0], TestSuccess => ZIO[R0, TestFailure[E0], TestSuccess]]
): TestAspect[Nothing, R0, E0, Any] =
new TestAspect.PerTest[Nothing, R0, E0, Any] {
def perTest[R <: R0, E >: E0](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
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](
f: ZIO[R0, TestFailure[E0], TestSuccess] => ZIO[R0, TestFailure[E0], TestSuccess]
): TestAspect[R0, R0, E0, E0] =
new TestAspect.PerTest[R0, R0, E0, E0] {
def perTest[R >: R0 <: R0, E >: E0 <: E0](
test: ZIO[R, TestFailure[E], TestSuccess]
): ZIO[R, TestFailure[E], TestSuccess] =
f(test)
}
/**
* Constructs an aspect that runs the specified effect before every test.
*/
def before[R0, E0](effect: ZIO[R0, E0, Any]): TestAspect[Nothing, R0, E0, Any] =
new TestAspect.PerTest[Nothing, R0, E0, Any] {
def perTest[R <: R0, E >: E0](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
effect.catchAllCause(cause => ZIO.fail(TestFailure.halt(cause))) *> test
}
/**
* Constructs an aspect that runs the specified effect a single time before
* all tests.
*/
def beforeAll[R0, E0](effect: ZIO[R0, E0, Any]): TestAspect[Nothing, R0, E0, Any] =
aroundAll_(effect, ZIO.unit)
/**
* An aspect that runs each test on a separate fiber and prints a fiber dump
* if the test fails or has not terminated within the specified duration.
*/
def diagnose(duration: Duration): TestAspectAtLeastR[Live with Annotations] =
new TestAspectAtLeastR[Live with Annotations] {
def some[R <: Live with Annotations, E](spec: ZSpec[R, E]): ZSpec[R, E] = {
def diagnose[R <: Live with Annotations, E](
label: String,
test: ZIO[R, TestFailure[E], TestSuccess]
): ZIO[R, TestFailure[E], TestSuccess] =
test.fork.flatMap { fiber =>
fiber.join.raceWith[R, TestFailure[E], TestFailure[E], Unit, TestSuccess](Live.live(ZIO.sleep(duration)))(
(exit, sleepFiber) => dump(label).when(!exit.succeeded) *> sleepFiber.interrupt *> ZIO.done(exit),
(_, _) => dump(label) *> fiber.join
)
}
def dump[E, A](label: String): URIO[Live with Annotations, Unit] =
Annotations.supervisedFibers.flatMap(fibers => Live.live(Fiber.putDumpStr(label, fibers.toSeq: _*).orDie))
spec.transform[R, TestFailure[E], TestSuccess] {
case Spec.TestCase(test, annotations) => Spec.TestCase(diagnose("", test), annotations)
case c => c
}
}
}
/**
* An aspect that runs each test with the `TestConsole` instance in the
* environment set to debug mode so that console output is rendered to
* standard output in addition to being written to the output buffer.
*/
val debug: TestAspectAtLeastR[TestConsole] =
new PerTest.AtLeastR[TestConsole] {
def perTest[R <: TestConsole, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
TestConsole.debug(test)
}
/**
* An aspect that applies the specified aspect on Dotty.
*/
def dotty[LowerR, UpperR, LowerE, UpperE](
that: TestAspect[LowerR, UpperR, LowerE, UpperE]
): TestAspect[LowerR, UpperR, LowerE, UpperE] =
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, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
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 platforms except ScalaNative.
*/
val exceptNative: TestAspectAtLeastR[Annotations] =
if (TestPlatform.isNative) ignore else identity
/**
* An aspect that runs tests on all versions except Scala 2.
*/
val exceptScala2: TestAspectAtLeastR[Annotations] =
if (TestVersion.isScala2) ignore else identity
/**
* An aspect that runs tests on all versions except Scala 2.11.
*/
val exceptScala211: TestAspectAtLeastR[Annotations] =
if (TestVersion.isScala211) ignore else identity
/**
* An aspect that runs tests on all versions except Scala 2.12.
*/
val exceptScala212: TestAspectAtLeastR[Annotations] =
if (TestVersion.isScala212) ignore else identity
/**
* An aspect that runs tests on all versions except Scala 2.13.
*/
val exceptScala213: TestAspectAtLeastR[Annotations] =
if (TestVersion.isScala213) 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, E](spec: ZSpec[R, E]): ZSpec[R, E] =
Spec.exec(exec, spec)
}
/**
* 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 failing: TestAspectPoly =
failing(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 failing[E0](assertion: Assertion[TestFailure[E0]]): TestAspect[Nothing, Any, Nothing, E0] =
new TestAspect.PerTest[Nothing, Any, Nothing, E0] {
def perTest[R, E <: E0](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
test.foldM(
failure =>
if (assertion.test(failure)) ZIO.succeedNow(TestSuccess.Succeeded(BoolAlgebra.unit))
else ZIO.fail(TestFailure.assertion(assertImpl(failure)(assertion))),
_ => ZIO.fail(TestFailure.die(new RuntimeException("did not fail as expected")))
)
}
import scala.collection.immutable.SortedSet
/**
* An aspect that records the state of fibers spawned by the current test in
* [[TestAnnotation.fibers]]. Applied by default in [[DefaultRunnableSpec]]
* and [[MutableRunnableSpec]] but not in [[RunnableSpec]]. This aspect is
* required for the proper functioning of `TestClock.adjust`.
*/
lazy val fibers: TestAspect[Nothing, Annotations, Nothing, Any] =
new TestAspect.PerTest[Nothing, Annotations, Nothing, Any] {
def perTest[R <: Annotations, E](
test: ZIO[R, TestFailure[E], TestSuccess]
): ZIO[R, TestFailure[E], TestSuccess] = {
val acquire = Ref.make(SortedSet.empty[Fiber.Runtime[Any, Any]]).tap { ref =>
Annotations.annotate(TestAnnotation.fibers, Right(Chunk(ref)))
}
val release = Annotations.get(TestAnnotation.fibers).flatMap {
case Right(refs) =>
ZIO.foreach(refs)(_.get).map(_.foldLeft(SortedSet.empty[Fiber.Runtime[Any, Any]])(_ ++ _).size).tap { n =>
Annotations.annotate(TestAnnotation.fibers, Left(n))
}
case Left(_) => ZIO.unit
}
acquire.bracket(_ => release) { ref =>
Supervisor.fibersIn(ref).flatMap(supervisor => test.supervised(supervisor))
}
}
}
/**
* An aspect that retries a test until success, with a default limit, for use
* with flaky tests.
*/
val flaky: TestAspectAtLeastR[Annotations with TestConfig with ZTestEnv] = {
val flaky = new PerTest.AtLeastR[Annotations with TestConfig with ZTestEnv] {
def perTest[R <: Annotations with TestConfig with ZTestEnv, E](
test: ZIO[R, TestFailure[E], TestSuccess]
): ZIO[R, TestFailure[E], TestSuccess] =
TestConfig.retries.flatMap { n =>
test.catchAll(_ => test.tapError(_ => Annotations.annotate(TestAnnotation.retried, 1)).retryN(n - 1))
}
}
restoreTestEnvironment >>> flaky
}
/**
* 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] = {
val flaky = new PerTest.AtLeastR[ZTestEnv with Annotations] {
def perTest[R <: ZTestEnv with Annotations, E](
test: ZIO[R, TestFailure[E], TestSuccess]
): ZIO[R, TestFailure[E], TestSuccess] =
test.catchAll(_ => test.tapError(_ => Annotations.annotate(TestAnnotation.retried, 1)).retryN(n - 1))
}
restoreTestEnvironment >>> flaky
}
/**
* An aspect that runs each test on its own separate fiber.
*/
val forked: TestAspectPoly =
new PerTest.Poly {
def perTest[R, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
test.fork.flatMap(_.join)
}
/**
* 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 with Annotations] =
new TestAspectAtLeastR[Live with Annotations] {
def some[R <: Live with Annotations, E](spec: ZSpec[R, E]): ZSpec[R, E] =
spec.whenM(Live.live(system.env(env)).orDie.map(_.fold(false)(assertion.test)))
}
/**
* As aspect that only runs a test if the specified environment variable is
* set.
*/
def ifEnvSet(env: String): TestAspectAtLeastR[Live 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 with Annotations] =
new TestAspectAtLeastR[Live with Annotations] {
def some[R <: Live with Annotations, E](spec: ZSpec[R, E]): ZSpec[R, E] =
spec.whenM(Live.live(system.property(prop)).orDie.map(_.fold(false)(assertion.test)))
}
/**
* As aspect that only runs a test if the specified Java property is set.
*/
def ifPropSet(prop: String): TestAspectAtLeastR[Live with Annotations] =
ifProp(prop, Assertion.anything)
/**
* An aspect that applies the specified aspect on ScalaJS.
*/
def js[LowerR, UpperR, LowerE, UpperE](
that: TestAspect[LowerR, UpperR, LowerE, UpperE]
): TestAspect[LowerR, UpperR, LowerE, UpperE] =
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](
that: TestAspect[LowerR, UpperR, LowerE, UpperE]
): TestAspect[LowerR, UpperR, LowerE, UpperE] =
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 runs each test with the maximum number of shrinkings to
* minimize large failures set to the specified value.
*/
def shrinks(n: Int): TestAspectAtLeastR[TestConfig] =
new PerTest.AtLeastR[TestConfig] {
def perTest[R <: TestConfig, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
test.updateService[TestConfig.Service] { old =>
new TestConfig.Service {
val repeats = old.repeats
val retries = old.retries
val samples = old.samples
val shrinks = n
}
}
}
/**
* An aspect that runs each test with the size set to the specified value.
*/
def sized(n: Int): TestAspectAtLeastR[Sized] =
new PerTest.AtLeastR[Sized] {
def perTest[R <: Sized, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
Sized.withSize(n)(test)
}
/**
* An aspect that applies the specified aspect on ScalaNative.
*/
def native[LowerR, UpperR, LowerE, UpperE](
that: TestAspect[LowerR, UpperR, LowerE, UpperE]
): TestAspect[LowerR, UpperR, LowerE, UpperE] =
if (TestPlatform.isNative) that else identity
/**
* An aspect that only runs tests on ScalaNative.
*/
val nativeOnly: TestAspectAtLeastR[Annotations] =
if (TestPlatform.isNative) 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 with TestConfig] = {
val nonFlaky = new PerTest.AtLeastR[ZTestEnv with Annotations with TestConfig] {
def perTest[R <: ZTestEnv with Annotations with TestConfig, E](
test: ZIO[R, TestFailure[E], TestSuccess]
): ZIO[R, TestFailure[E], TestSuccess] =
TestConfig.repeats.flatMap { n =>
test *> test.tap(_ => Annotations.annotate(TestAnnotation.repeated, 1)).repeatN(n - 1)
}
}
restoreTestEnvironment >>> nonFlaky
}
/**
* 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[ZTestEnv with Annotations] {
def perTest[R <: ZTestEnv with Annotations, E](
test: ZIO[R, TestFailure[E], TestSuccess]
): ZIO[R, TestFailure[E], TestSuccess] =
test *> test.tap(_ => Annotations.annotate(TestAnnotation.repeated, 1)).repeatN(n - 1)
}
restoreTestEnvironment >>> nonFlaky
}
/**
* Constructs an aspect that requires a test to not terminate within the
* specified time.
*/
def nonTermination(duration: Duration): TestAspectAtLeastR[Live] =
timeout(duration) >>>
failing(
isCase[TestFailure[Any], Throwable](
"Runtime",
{
case TestFailure.Runtime(cause) => cause.dieOption
case _ => None
},
isSubtype[TestTimeoutException](hasMessage(equalTo(s"Timeout of ${duration.render} exceeded.")))
)
)
/**
* Sets the seed of the `TestRandom` instance in the environment to a random
* value before each test.
*/
val nondeterministic: TestAspectAtLeastR[Live with TestRandom] =
before(Live.live(clock.nanoTime).flatMap(TestRandom.setSeed(_)))
/**
* 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 repeats successful tests according to a schedule.
*/
def repeat[R0 <: ZTestEnv with Annotations with Live](
schedule: Schedule[R0, TestSuccess, Any]
): TestAspectAtLeastR[R0] = {
val repeat = new PerTest.AtLeastR[R0] {
def perTest[R <: R0, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
ZIO.accessM(r =>
Live.live(
test
.provide(r)
.repeat(
(schedule *> Schedule
.identity[TestSuccess]).tapOutput(_ => Annotations.annotate(TestAnnotation.repeated, 1)).provide(r)
)
)
)
}
restoreTestEnvironment >>> repeat
}
/**
* An aspect that runs each test with the number of times to repeat tests to
* ensure they are stable set to the specified value.
*/
def repeats(n: Int): TestAspectAtLeastR[TestConfig] =
new PerTest.AtLeastR[TestConfig] {
def perTest[R <: TestConfig, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
test.updateService[TestConfig.Service] { old =>
new TestConfig.Service {
val repeats = n
val retries = old.retries
val samples = old.samples
val shrinks = old.shrinks
}
}
}
/**
* 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](_.get)
/**
* 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](_.get)
/**
* 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](_.get)
/**
* 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](_.get)
/**
* 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 runs each test with the number of times to retry flaky tests
* set to the specified value.
*/
def retries(n: Int): TestAspectAtLeastR[TestConfig] =
new PerTest.AtLeastR[TestConfig] {
def perTest[R <: TestConfig, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
test.updateService[TestConfig.Service] { old =>
new TestConfig.Service {
val repeats = old.repeats
val retries = n
val samples = old.samples
val shrinks = old.shrinks
}
}
}
/**
* An aspect that retries failed tests according to a schedule.
*/
def retry[R0 <: ZTestEnv with Annotations with Live, E0](
schedule: Schedule[R0, TestFailure[E0], Any]
): TestAspect[Nothing, R0, Nothing, E0] = {
val retry = new TestAspect.PerTest[Nothing, R0, Nothing, E0] {
def perTest[R <: R0, E <: E0](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
ZIO.accessM[R](r =>
Live.live(
test.provide(r).retry(schedule.tapOutput(_ => Annotations.annotate(TestAnnotation.retried, 1)).provide(r))
)
)
}
restoreTestEnvironment >>> retry
}
/**
* An aspect that runs each test with the number of sufficient samples to
* check for a random variable set to the specified value.
*/
def samples(n: Int): TestAspectAtLeastR[TestConfig] =
new PerTest.AtLeastR[TestConfig] {
def perTest[R <: TestConfig, E](test: ZIO[R, TestFailure[E], TestSuccess]): ZIO[R, TestFailure[E], TestSuccess] =
test.updateService[TestConfig.Service] { old =>