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
39 changes: 39 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/TestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zio.test

import zio._
import zio.Clock._
import zio.internal.macros.StringUtils.StringOps
import zio.stm.STM
import zio.test.Assertion._
import zio.test.TestAspect.{failing, timeout}
Expand Down Expand Up @@ -60,6 +61,44 @@ object TestSpec extends ZIOBaseSpec {
message <- STM.succeed("Hello from an STM transaction!")
} yield assert(message)(anything)
},
test("fail-fast assertion by automatic lifting to ZIO") {
for {
ref <- Ref.make(0)
spec = test("test") {
for {
_ <- ref.update(_ + 1)
_ <- assertTrue(1 == 2)
_ <- ref.update(_ + 1)
} yield assertTrue(3 == 4)
}
summary <- execute(spec)
count <- ref.get
} yield assert(count)(equalTo(1)) &&
assert(summary.fail)(equalTo(1)) &&
assert(summary.failureDetails.unstyled)(
containsString("1 == 2") &&
not(containsString("3 == 4"))
)
},
test("composed assertions are lifted to ZIO and fully evaluated") {
for {
ref <- Ref.make(0)
spec = test("test") {
for {
_ <- ref.update(_ + 1)
_ <- assertTrue(1 == 2) && assertTrue(3 == 4)
_ <- ref.update(_ + 1)
} yield assertCompletes
}
summary <- execute(spec)
count <- ref.get
} yield assert(count)(equalTo(1)) &&
assert(summary.fail)(equalTo(1)) &&
assert(summary.failureDetails.unstyled)(
containsString("1 == 2") &&
containsString("3 == 4")
)
},
suite("suites can be effectual") {
ZIO.succeed {
Chunk(
Expand Down
9 changes: 8 additions & 1 deletion test/shared/src/main/scala/zio/test/TestArrow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package zio.test
import zio.stacktracer.TracingImplicits.disableAutoTrace

import scala.language.implicitConversions
import zio.Trace
import zio.{Trace, ZIO}
import zio.internal.stacktracer.SourceLocation

import scala.util.control.NonFatal
Expand Down Expand Up @@ -47,6 +47,13 @@ object TestResult {

def any(asserts: TestResult*): TestResult = asserts.reduce(_ || _)

implicit def liftTestResultToZIO[R, E](result: TestResult)(implicit trace: Trace): ZIO[R, E, TestResult] =
if (result.isSuccess)
ZIO.succeedNow(result)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure if I correctly understood the purpose of suceedNow here.

Copy link
Member

@kitlangton kitlangton Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just like succeed but it is private to zio and doesn't store the argument in a thunk (i.e., it doesn't use a by-name parameter). So it's faster, but you have to be careful where you use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kitlangton thanks for clarifying. What about tracing in this case? I wonder if it's okay that the implicit trace argument is ignored in the happy path.

else
ZIO.die(Exit(result))

private[zio] final case class Exit(result: TestResult) extends Throwable
}

sealed trait TestArrow[-A, +B] { self =>
Expand Down
6 changes: 5 additions & 1 deletion test/shared/src/main/scala/zio/test/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ package object test extends CompileVariants {
}
}
.foldCauseZIO(
cause => ZIO.fail(TestFailure.Runtime(cause)),
cause =>
cause.dieOption match {
case Some(TestResult.Exit(assert)) => ZIO.fail(TestFailure.Assertion(assert))
case _ => ZIO.fail(TestFailure.Runtime(cause))
},
assert =>
if (assert.isFailure)
ZIO.fail(TestFailure.Assertion(assert))
Expand Down