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
16 changes: 15 additions & 1 deletion test/shared/src/main/scala/zio/test/TestAspect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,24 @@ object TestAspect extends TimeoutVariants {
test <* effect.catchAllCause(cause => ZIO.fail(TestFailure.Runtime(cause)))
}

/**
* Constructs an aspect that evaluates every test inside the context of a `Managed`.
*/
def around[R0, E0](before: ZIO[R0, E0, Any], after: ZIO[R0, 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]] =
ZManaged
.make(before)(_ => after)
.foldCauseM(c => ZManaged.fail(TestFailure.Runtime(c)), ZManaged.succeed)
.use(_ => test)
}

/**
* Constructs an aspect that evaluates every test inside the context of the managed function.
*/
def around[R0, E0, S0](
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] {
Expand Down
14 changes: 14 additions & 0 deletions test/shared/src/test/scala/zio/test/TestAspectSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@ import zio.test.TestUtils.{ execute, ignored, label, succeeded }
object TestAspectSpec extends DefaultRuntime {

val run: List[Async[(Boolean, String)]] = List(
label(aroundEvaluatesTestsInsideContextOfManaged, "around evaluates tests inside context of Managed"),
label(jsAppliesTestAspectOnlyOnJS, "js applies test aspect only on ScalaJS"),
label(jsOnlyRunsTestsOnlyOnScalaJS, "jsOnly runs tests only on ScalaJS"),
label(jvmAppliesTestAspectOnlyOnJVM, "jvm applies test aspect only on ScalaJS"),
label(jvmOnlyRunsTestsOnlyOnTheJVM, "jvmOnly runs tests only on the JVM")
)

def aroundEvaluatesTestsInsideContextOfManaged: Future[Boolean] =
unsafeRunToFuture {
for {
ref <- Ref.make(0)
spec = testM("test") {
assertM(ref.get, equalTo(1))
} @@ around(ref.set(1), ref.set(-1))
_ <- execute(spec)
result <- succeeded(spec)
after <- ref.get
} yield result && (after == -1)
}

def jsAppliesTestAspectOnlyOnJS: Future[Boolean] =
unsafeRunToFuture {
for {
Expand Down