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
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,51 @@ package zio.test

import zio.test.Assertion._
import zio.test.TestAspect._
import zio.test.environment.TestEnvironment
import zio.{Has, Ref, ZIO, ZLayer}
import zio.{Has, Ref, UIO, ZIO, ZLayer}

// because of bug in Tag with Scala 3
// otherwise the environment can be simplified to Has[Ref[Int]]
trait RefInt {
def get: UIO[Int]
def inc: UIO[Unit]
}

object RefInt {
val live: ZLayer[Any, Nothing, Has[RefInt]] = ZLayer.fromEffect(
Ref
.make(0)
.map(ref =>
new RefInt {
override def get: UIO[Int] = ref.get

override def inc: UIO[Unit] = ref.update(_ + 1)
}
)
)
}

object MutableRunnableSpecSpec
extends MutableRunnableSpec[MutableRunnableSpecSpecCompat.Environment](
TestEnvironment.any ++ ZLayer.fromEffect(Ref.make(0)),
sequential >>> samples(10) >>> before(ZIO.service[Ref[Int]].flatMap(_.update(_ + 1)))
RefInt.live,
sequential >>> samples(10) >>> before(ZIO.service[RefInt].flatMap(_.inc))
) {
testM("ref 1") {
assertM(ZIO.service[Ref[Int]].flatMap(_.get))(equalTo(1))
assertM(ZIO.service[RefInt].flatMap(_.get))(equalTo(1))
}

testM("ref 2") {
assertM(ZIO.service[Ref[Int]].flatMap(_.get))(equalTo(2))
assertM(ZIO.service[RefInt].flatMap(_.get))(equalTo(2))
}

testM("check samples") {
for {
ref <- ZIO.service[Ref[Int]]
_ <- checkM(Gen.anyInt.noShrink)(_ => assertM(ref.update(_ + 1))(anything))
ref <- ZIO.service[RefInt]
_ <- checkM(Gen.anyInt.noShrink)(_ => assertM(ref.inc)(anything))
value <- ref.get
} yield assert(value)(equalTo(13))
}
}

object MutableRunnableSpecSpecCompat {
type Environment = TestEnvironment with Has[Ref[Int]]
type Environment = Has[RefInt]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package zio.test

import zio.ZLayer
import zio.test.environment.TestEnvironment
import zio.{Has, ZLayer}

/**
* Syntax for writing test like
Expand All @@ -35,5 +34,4 @@ import zio.test.environment.TestEnvironment
* }
* }}}
*/
class DefaultMutableRunnableSpec
extends MutableRunnableSpec[TestEnvironment](ZLayer.identity[TestEnvironment], TestAspect.identity)
class DefaultMutableRunnableSpec extends MutableRunnableSpec[Has[Any]](ZLayer.succeed[Any](()), TestAspect.identity)
24 changes: 13 additions & 11 deletions test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package zio.test

import izumi.reflect.Tag
import zio.clock.Clock
import zio.duration._
import zio.test.environment.TestEnvironment
Expand All @@ -39,9 +40,9 @@ import scala.util.control.NoStackTrace
* }
* }}}
*/
class MutableRunnableSpec[R <: Has[_]](
class MutableRunnableSpec[R <: Has[_]: Tag](
layer: ZLayer[TestEnvironment, Throwable, R],
aspect: TestAspect[R, R, Any, Any]
aspect: TestAspect[R with TestEnvironment, R with TestEnvironment, Any, Any] = TestAspect.identity
) extends RunnableSpec[TestEnvironment, Any] {
self =>

Expand All @@ -50,14 +51,15 @@ class MutableRunnableSpec[R <: Has[_]](
with NoStackTrace

sealed trait SpecBuilder {
def toSpec: ZSpec[R, Any]
def toSpec: ZSpec[R with TestEnvironment, Any]
def label: String
}

sealed case class SuiteBuilder(label: String) extends SpecBuilder {

private[test] var nested: Chunk[SpecBuilder] = Chunk.empty
private var aspects: Chunk[TestAspect[R, R, Failure, Failure]] = Chunk.empty
private[test] var nested: Chunk[SpecBuilder] = Chunk.empty
private var aspects: Chunk[TestAspect[R with TestEnvironment, R with TestEnvironment, Failure, Failure]] =
Chunk.empty

/**
* Syntax for adding aspects.
Expand All @@ -66,21 +68,21 @@ class MutableRunnableSpec[R <: Has[_]](
* }}}
*/
final def @@(
aspect: TestAspect[R, R, Failure, Failure]
aspect: TestAspect[R with TestEnvironment, R with TestEnvironment, Failure, Failure]
): SuiteBuilder = {
aspects = aspects :+ aspect
this
}

def toSpec: ZSpec[R, Any] =
def toSpec: ZSpec[R with TestEnvironment, Any] =
aspects.foldLeft(
zio.test.suite(label)(
nested.map(_.toSpec): _*
)
)((spec, aspect) => spec @@ aspect)
}

sealed case class TestBuilder(label: String, var toSpec: ZSpec[R, Any]) extends SpecBuilder {
sealed case class TestBuilder(label: String, var toSpec: ZSpec[R with TestEnvironment, Any]) extends SpecBuilder {

/**
* Syntax for adding aspects.
Expand All @@ -89,7 +91,7 @@ class MutableRunnableSpec[R <: Has[_]](
* }}}
*/
final def @@(
aspect: TestAspect[R, R, Failure, Failure]
aspect: TestAspect[R with TestEnvironment, R with TestEnvironment, Failure, Failure]
): TestBuilder = {
toSpec = toSpec @@ aspect
this
Expand Down Expand Up @@ -134,7 +136,7 @@ class MutableRunnableSpec[R <: Has[_]](
*/
final def testM(
label: String
)(assertion: => ZIO[R, Failure, TestResult])(implicit loc: SourceLocation): TestBuilder = {
)(assertion: => ZIO[R with TestEnvironment, Failure, TestResult])(implicit loc: SourceLocation): TestBuilder = {
if (specBuilt)
throw new InAnotherTestException("Test", label)
val test = zio.test.testM(label)(assertion)
Expand All @@ -145,7 +147,7 @@ class MutableRunnableSpec[R <: Has[_]](

final override def spec: ZSpec[Environment, Failure] = {
specBuilt = true
(stack.head @@ aspect).toSpec.provideLayerShared(layer.mapError(TestFailure.fail))
(stack.head @@ aspect).toSpec.provideCustomLayerShared(layer.mapError(TestFailure.fail))
}

override def aspects: List[TestAspect[Nothing, TestEnvironment, Nothing, Any]] =
Expand Down