Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/TestAspectSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ object TestAspectSpec extends ZIOBaseSpec {
assertM(ZIO.fail("fail"))(anything)
} @@ nonTermination(1.minute) @@ failing
),
testM("repeats sets the number of times to repeat a test to the specified value") {
for {
ref <- Ref.make(0)
spec = testM("test")(assertM(ref.update(_ + 1))(anything)) @@ nonFlaky @@ repeats(42)
_ <- execute(spec)
value <- ref.get
} yield assert(value)(equalTo(43))
},
testM("repeats sets the number of times to repeat a test to the specified value") {
for {
ref <- Ref.make(0)
spec = testM("test")(assertM(ref.update(_ + 1))(nothing)) @@ flaky @@ retries(42)
_ <- execute(spec)
value <- ref.get
} yield assert(value)(equalTo(43))
},
testM("samples sets the number of sufficient samples to the specified value") {
for {
ref <- Ref.make(0)
_ <- checkM(Gen.anyInt.noShrink)(_ => assertM(ref.update(_ + 1))(anything))
value <- ref.get
} yield assert(value)(equalTo(42))
} @@ samples(42),
testM("scala2 applies test aspect only on Scala 2") {
for {
ref <- Ref.make(false)
Expand All @@ -224,6 +247,13 @@ object TestAspectSpec extends ZIOBaseSpec {
testM("setSeed sets the random seed to the specified value before each test") {
assertM(TestRandom.getSeed)(equalTo(seed & ((1L << 48) - 1)))
} @@ setSeed(seed),
testM("shrinks sets the maximum number of shrinkings to the specified value") {
for {
ref <- Ref.make(0)
_ <- checkM(Gen.anyInt)(_ => assertM(ref.update(_ + 1))(nothing))
value <- ref.get
} yield assert(value)(equalTo(1))
} @@ shrinks(0),
testM("timeout makes tests fail after given duration") {
assertM(ZIO.never *> ZIO.unit)(equalTo(()))
} @@ timeout(1.nanos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ object RandomSpec extends ZIOBaseSpec {

def checkClear[A, B <: Random](generate: SRandom => A)(feed: (ZRandom, List[A]) => UIO[Unit])(
clear: ZRandom => UIO[Unit]
)(extract: ZRandom => UIO[A]): URIO[Random, TestResult] =
)(extract: ZRandom => UIO[A]): URIO[Random with TestConfig, TestResult] =
checkM(Gen.anyLong) { seed =>
for {
sRandom <- ZIO.effectTotal(new SRandom(seed))
Expand All @@ -97,7 +97,7 @@ object RandomSpec extends ZIOBaseSpec {

def checkFeed[A, B >: Random](generate: SRandom => A)(
feed: (ZRandom, List[A]) => UIO[Unit]
)(extract: ZRandom => UIO[A]): URIO[Random, TestResult] =
)(extract: ZRandom => UIO[A]): URIO[Random with TestConfig, TestResult] =
checkM(Gen.anyLong) { seed =>
for {
sRandom <- ZIO.effectTotal(new SRandom(seed))
Expand All @@ -122,7 +122,7 @@ object RandomSpec extends ZIOBaseSpec {

def forAllEqual[A](
f: ZRandom => UIO[A]
)(g: SRandom => A): URIO[Random, TestResult] =
)(g: SRandom => A): URIO[Random with TestConfig, TestResult] =
checkM(Gen.anyLong) { seed =>
for {
sRandom <- ZIO.effectTotal(new SRandom(seed))
Expand All @@ -133,7 +133,7 @@ object RandomSpec extends ZIOBaseSpec {
} yield assert(actual)(equalTo(expected))
}

def forAllEqualBytes: URIO[Random, TestResult] =
def forAllEqualBytes: URIO[Random with TestConfig, TestResult] =
checkM(Gen.anyLong) { seed =>
for {
sRandom <- ZIO.effectTotal(new SRandom(seed))
Expand All @@ -147,7 +147,7 @@ object RandomSpec extends ZIOBaseSpec {
} yield assert(actual)(equalTo(expected))
}

def forAllEqualGaussian: URIO[Random, TestResult] =
def forAllEqualGaussian: URIO[Random with TestConfig, TestResult] =
checkM(Gen.anyLong) { seed =>
for {
sRandom <- ZIO.effectTotal(new SRandom(seed))
Expand All @@ -160,7 +160,7 @@ object RandomSpec extends ZIOBaseSpec {

def forAllEqualN[A](
f: (ZRandom, Int) => UIO[A]
)(g: (SRandom, Int) => A): URIO[Random, TestResult] =
)(g: (SRandom, Int) => A): URIO[Random with TestConfig, TestResult] =
checkM(Gen.anyLong, Gen.int(1, 100)) { (seed, size) =>
for {
sRandom <- ZIO.effectTotal(new SRandom(seed))
Expand All @@ -173,7 +173,7 @@ object RandomSpec extends ZIOBaseSpec {

def forAllEqualShuffle(
f: (ZRandom, List[Int]) => UIO[List[Int]]
)(g: (SRandom, List[Int]) => List[Int]): ZIO[Random with Sized, Nothing, TestResult] =
)(g: (SRandom, List[Int]) => List[Int]): ZIO[Random with Sized with TestConfig, Nothing, TestResult] =
checkM(Gen.anyLong, Gen.listOf(Gen.anyInt)) { (seed, testList) =>
for {
sRandom <- ZIO.effectTotal(new SRandom(seed))
Expand All @@ -186,7 +186,7 @@ object RandomSpec extends ZIOBaseSpec {

def forAllBounded[A: Numeric](gen: Gen[Random, A])(
next: (Random.Service, A) => UIO[A]
): URIO[Random, TestResult] = {
): URIO[Random with TestConfig, TestResult] = {
val num = implicitly[Numeric[A]]
import num._
checkM(gen.map(num.abs(_))) { upper =>
Expand All @@ -199,7 +199,7 @@ object RandomSpec extends ZIOBaseSpec {

def forAllBetween[A: Numeric](gen: Gen[Random, A])(
between: (Random.Service, A, A) => UIO[A]
): URIO[Random, TestResult] = {
): URIO[Random with TestConfig, TestResult] = {
val num = implicitly[Numeric[A]]
import num._
val genMinMax = for {
Expand Down
15 changes: 12 additions & 3 deletions test/js/src/main/scala/zio/test/PlatformSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@ import zio.test.environment._

private[test] abstract class PlatformSpecific {
type TestEnvironment =
ZEnv with Annotations with TestClock with TestConsole with Live with TestRandom with Sized with TestSystem
Annotations
with Live
with Sized
with TestClock
with TestConfig
with TestConsole
with TestRandom
with TestSystem
with ZEnv

object TestEnvironment {
val any: ZLayer[TestEnvironment, Nothing, TestEnvironment] =
ZLayer.requires[TestEnvironment]
val live: ZLayer[ZEnv, Nothing, TestEnvironment] =
Annotations.live ++
Live.default ++
Sized.live(100) ++
((Live.default ++ Annotations.live) >>> TestClock.default) ++
TestConfig.live(100, 100, 200, 1000) ++
(Live.default >>> TestConsole.debug) ++
Live.default ++
TestRandom.deterministic ++
Sized.live(100) ++
TestSystem.default
}
}
15 changes: 12 additions & 3 deletions test/jvm/src/main/scala/zio/test/PlatformSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@ import zio.{ ZEnv, ZLayer }

private[test] abstract class PlatformSpecific {
type TestEnvironment =
ZEnv with Annotations with TestClock with TestConsole with Live with TestRandom with Sized with TestSystem
Annotations
with Live
with Sized
with TestClock
with TestConfig
with TestConsole
with TestRandom
with TestSystem
with ZEnv

object TestEnvironment {
val any: ZLayer[TestEnvironment, Nothing, TestEnvironment] =
ZLayer.requires[TestEnvironment]
lazy val live: ZLayer[ZEnv, Nothing, TestEnvironment] =
Annotations.live ++
Blocking.live ++
Live.default ++
Sized.live(100) ++
((Live.default ++ Annotations.live) >>> TestClock.default) ++
TestConfig.live(100, 100, 200, 1000) ++
(Live.default >>> TestConsole.debug) ++
Live.default ++
TestRandom.deterministic ++
Sized.live(100) ++
TestSystem.default
}
}
94 changes: 90 additions & 4 deletions test/shared/src/main/scala/zio/test/TestAspect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,17 @@ object TestAspect extends TimeoutVariants {
* 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)
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
Expand Down Expand Up @@ -422,6 +431,23 @@ object TestAspect extends TimeoutVariants {
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 applies the specified aspect on ScalaNative.
*/
Expand All @@ -440,8 +466,17 @@ object TestAspect extends TimeoutVariants {
* 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)
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
Expand Down Expand Up @@ -515,6 +550,23 @@ object TestAspect extends TimeoutVariants {
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
Expand Down Expand Up @@ -571,6 +623,23 @@ object TestAspect extends TimeoutVariants {
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.
*/
Expand All @@ -588,6 +657,23 @@ object TestAspect extends TimeoutVariants {
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 =>
new TestConfig.Service {
val repeats = old.repeats
val retries = old.retries
val samples = n
val shrinks = old.shrinks
}
}
}

/**
* An aspect that executes the members of a suite sequentially.
*/
Expand Down
Loading