From da959b05d637d824f66935f86e8f9d884e148bdb Mon Sep 17 00:00:00 2001 From: fokot Date: Fri, 23 Apr 2021 22:38:04 +0200 Subject: [PATCH 1/5] Test layer is not shared between tests anymore --- .../zio/test/DefaultMutableRunnableSpec.scala | 5 ++-- .../scala/zio/test/MutableRunnableSpec.scala | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala index 605fc8a9b523..5920753ec2cc 100644 --- a/test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala @@ -16,7 +16,7 @@ package zio.test -import zio.ZLayer +import zio.{Has, ZLayer} import zio.test.environment.TestEnvironment /** @@ -35,5 +35,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) diff --git a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala index 341e96c1b12a..59494455dc0b 100644 --- a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala @@ -16,6 +16,7 @@ package zio.test +import izumi.reflect.Tag import zio.clock.Clock import zio.duration._ import zio.test.environment.TestEnvironment @@ -39,25 +40,27 @@ 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 => + type RT = R with TestEnvironment + private class InAnotherTestException(`type`: String, label: String) extends Exception(s"${`type`} `${label}` is in another test") with NoStackTrace sealed trait SpecBuilder { - def toSpec: ZSpec[R, Any] + def toSpec: ZSpec[RT, 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[RT, RT, Failure, Failure]] = Chunk.empty /** * Syntax for adding aspects. @@ -66,13 +69,13 @@ class MutableRunnableSpec[R <: Has[_]]( * }}} */ final def @@( - aspect: TestAspect[R, R, Failure, Failure] + aspect: TestAspect[RT, RT, Failure, Failure] ): SuiteBuilder = { aspects = aspects :+ aspect this } - def toSpec: ZSpec[R, Any] = + def toSpec: ZSpec[RT, Any] = aspects.foldLeft( zio.test.suite(label)( nested.map(_.toSpec): _* @@ -80,7 +83,7 @@ class MutableRunnableSpec[R <: Has[_]]( )((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[RT, Any]) extends SpecBuilder { /** * Syntax for adding aspects. @@ -89,7 +92,7 @@ class MutableRunnableSpec[R <: Has[_]]( * }}} */ final def @@( - aspect: TestAspect[R, R, Failure, Failure] + aspect: TestAspect[RT, RT, Failure, Failure] ): TestBuilder = { toSpec = toSpec @@ aspect this @@ -134,7 +137,7 @@ class MutableRunnableSpec[R <: Has[_]]( */ final def testM( label: String - )(assertion: => ZIO[R, Failure, TestResult])(implicit loc: SourceLocation): TestBuilder = { + )(assertion: => ZIO[RT, Failure, TestResult])(implicit loc: SourceLocation): TestBuilder = { if (specBuilt) throw new InAnotherTestException("Test", label) val test = zio.test.testM(label)(assertion) @@ -145,7 +148,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]] = From 456c9a28292907a6f1fc2421d3dc7b67691098ea Mon Sep 17 00:00:00 2001 From: fokot Date: Fri, 23 Apr 2021 22:47:07 +0200 Subject: [PATCH 2/5] Test layer is not shared between tests anymore --- .../src/main/scala/zio/test/DefaultMutableRunnableSpec.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala index 5920753ec2cc..65937d68b749 100644 --- a/test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala @@ -17,7 +17,6 @@ package zio.test import zio.{Has, ZLayer} -import zio.test.environment.TestEnvironment /** * Syntax for writing test like From 4b1c4b44644cc231b03c96904312b5e07a1f3ab1 Mon Sep 17 00:00:00 2001 From: fokot Date: Sun, 2 May 2021 11:06:57 +0200 Subject: [PATCH 3/5] Scala 3 fix --- .../zio/test/MutableRunnableSpecSpec.scala | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/MutableRunnableSpecSpec.scala b/test-tests/shared/src/test/scala/zio/test/MutableRunnableSpecSpec.scala index 54bde3e5553d..080fbcce7031 100644 --- a/test-tests/shared/src/test/scala/zio/test/MutableRunnableSpecSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/MutableRunnableSpecSpec.scala @@ -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] } From 01b09f3dc93b2a3065e6f89e350129d7bbd51e07 Mon Sep 17 00:00:00 2001 From: fokot Date: Fri, 7 May 2021 22:48:43 +0200 Subject: [PATCH 4/5] removed RT type alias --- .../scala/zio/test/MutableRunnableSpec.scala | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala index 59494455dc0b..f40dd9ea49ef 100644 --- a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala @@ -46,21 +46,19 @@ class MutableRunnableSpec[R <: Has[_]: Tag]( ) extends RunnableSpec[TestEnvironment, Any] { self => - type RT = R with TestEnvironment - private class InAnotherTestException(`type`: String, label: String) extends Exception(s"${`type`} `${label}` is in another test") with NoStackTrace sealed trait SpecBuilder { - def toSpec: ZSpec[RT, 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[RT, RT, Failure, Failure]] = Chunk.empty + private var aspects: Chunk[TestAspect[R with TestEnvironment, R with TestEnvironment, Failure, Failure]] = Chunk.empty /** * Syntax for adding aspects. @@ -69,13 +67,13 @@ class MutableRunnableSpec[R <: Has[_]: Tag]( * }}} */ final def @@( - aspect: TestAspect[RT, RT, Failure, Failure] + aspect: TestAspect[R with TestEnvironment, R with TestEnvironment, Failure, Failure] ): SuiteBuilder = { aspects = aspects :+ aspect this } - def toSpec: ZSpec[RT, Any] = + def toSpec: ZSpec[R with TestEnvironment, Any] = aspects.foldLeft( zio.test.suite(label)( nested.map(_.toSpec): _* @@ -83,7 +81,7 @@ class MutableRunnableSpec[R <: Has[_]: Tag]( )((spec, aspect) => spec @@ aspect) } - sealed case class TestBuilder(label: String, var toSpec: ZSpec[RT, Any]) extends SpecBuilder { + sealed case class TestBuilder(label: String, var toSpec: ZSpec[R with TestEnvironment, Any]) extends SpecBuilder { /** * Syntax for adding aspects. @@ -92,7 +90,7 @@ class MutableRunnableSpec[R <: Has[_]: Tag]( * }}} */ final def @@( - aspect: TestAspect[RT, RT, Failure, Failure] + aspect: TestAspect[R with TestEnvironment, R with TestEnvironment, Failure, Failure] ): TestBuilder = { toSpec = toSpec @@ aspect this @@ -137,7 +135,7 @@ class MutableRunnableSpec[R <: Has[_]: Tag]( */ final def testM( label: String - )(assertion: => ZIO[RT, 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) From 7beab7c0bb509c80d342cc966c88e1bb53b9c2a9 Mon Sep 17 00:00:00 2001 From: fokot Date: Sat, 8 May 2021 07:24:57 +0200 Subject: [PATCH 5/5] scalafmt --- .../shared/src/main/scala/zio/test/MutableRunnableSpec.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala index f40dd9ea49ef..eba42d7695fa 100644 --- a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala @@ -57,8 +57,9 @@ class MutableRunnableSpec[R <: Has[_]: Tag]( sealed case class SuiteBuilder(label: String) extends SpecBuilder { - private[test] var nested: Chunk[SpecBuilder] = Chunk.empty - private var aspects: Chunk[TestAspect[R with TestEnvironment, R with TestEnvironment, 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.