Skip to content

Commit

Permalink
Do initial work on 2.0.0 final migration
Browse files Browse the repository at this point in the history
  • Loading branch information
reibitto committed Jun 27, 2022
1 parent e192f60 commit 1cb8f13
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 58 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ addCommandAlias(
";mockNative/compile"
)

val zioVersion = "2.0.0-RC6"
val zioVersion = "2.0.0"

lazy val root = (project in file("."))
.aggregate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ object AdvancedEffectMockSpec extends ZIOBaseSpec with MockSpecUtils[PureModule]
"capability",
_.capability,
equalTo(capability)
)
&&
hasField[UnexpectedCallException[PureModule, I, E, A], Any]("args", _.args, equalTo(args))
)
&&
hasField[UnexpectedCallException[PureModule, I, E, A], Any]("args", _.args, equalTo(args))
)

def hasUnsatisfiedExpectations(implicit trace: Trace): Assertion[Throwable] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ object AdvancedMethodMockSpec extends ZIOBaseSpec with MockSpecUtils[ImpureModul
)
}

def hasUnexpectedCall[I, E, A](capability: Capability[ImpureModule, I, E, A], args: I)(implicit trace: Trace): Assertion[Throwable] =
def hasUnexpectedCall[I, E, A](capability: Capability[ImpureModule, I, E, A], args: I)(implicit
trace: Trace
): Assertion[Throwable] =
isSubtype[UnexpectedCallException[ImpureModule, I, E, A]](
hasField[UnexpectedCallException[ImpureModule, I, E, A], Capability[ImpureModule, I, E, A]](
"capability",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,9 @@ object BasicMethodMockSpec extends ZIOBaseSpec with MockSpecUtils[ImpureModule]
testDied("invalid arguments")(
ImpureModuleMock.ParameterizedCommand(equalTo(1)),
ImpureModule.parameterizedCommand(2),
kindaEqualTo(InvalidCallException(List(InvalidArguments(ImpureModuleMock.ParameterizedCommand, 2, equalTo(1)))))
kindaEqualTo(
InvalidCallException(List(InvalidArguments(ImpureModuleMock.ParameterizedCommand, 2, equalTo(1))))
)
),
testDied("invalid method")(
ImpureModuleMock.ParameterizedCommand(equalTo(1)),
Expand Down
12 changes: 6 additions & 6 deletions mock-tests/shared/src/test/scala/zio/mock/MockReporterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object MockReporterSpec extends ZIOSpecDefault {
val behaviorSuite = suite("Behavior")(
test("should render `And` correctly.") {
verifyRendering(andSuite) { summary =>
val output = summary.summary
val output = summary.failureDetails
assertTrue(
summary.fail == 1,
summary.total == 1,
Expand All @@ -41,7 +41,7 @@ object MockReporterSpec extends ZIOSpecDefault {
},
test("should render `InvalidCall`s correctly.") {
verifyRendering(invalidCallSuite) { summary =>
val output = summary.summary
val output = summary.failureDetails
assertTrue(
summary.fail == 1,
summary.total == 1,
Expand All @@ -59,7 +59,7 @@ object MockReporterSpec extends ZIOSpecDefault {
},
test("should render `InvalidCall.InvalidCapability` correctly.") {
verifyRendering(invalidCapabiltySuite) { summary =>
val output = summary.summary
val output = summary.failureDetails
assertTrue(
summary.fail == 1,
summary.total == 1,
Expand All @@ -77,7 +77,7 @@ object MockReporterSpec extends ZIOSpecDefault {
},
test("should render polymorphic mock failures correctly.") {
verifyRendering(polySuite) { summary =>
val output = summary.summary
val output = summary.failureDetails
assertTrue(
summary.fail == 1,
summary.total == 1,
Expand All @@ -95,7 +95,7 @@ object MockReporterSpec extends ZIOSpecDefault {
},
test("should render `UnexpectedCallException` correctly.") {
verifyRendering(unexpectedCallSuite) { summary =>
val output = summary.summary
val output = summary.failureDetails
assertTrue(
summary.fail == 1,
summary.total == 1,
Expand All @@ -110,7 +110,7 @@ object MockReporterSpec extends ZIOSpecDefault {
},
test("should render `UnexpectedSatisfiedExpectaionException` correctly.") {
verifyRendering(unsatisfiedExpectationSuite) { summary =>
val output = summary.summary
val output = summary.failureDetails
assertTrue(
summary.fail == 1,
summary.total == 1,
Expand Down
4 changes: 3 additions & 1 deletion mock-tests/shared/src/test/scala/zio/mock/TestAspects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ object TestAspects {
mockedZEnv <- env
// Since we may be overriding native services that are not available explicitly in the environment,
// we need to explicitly override them in a scope.
_ <- ZEnv.services.locallyScopedWith(_.union(mockedZEnv))

// TODO: This was originally `ZEnv.services.locallyScopedWith(_.union(mockedZEnv))`. Not sure what the 2.0.0 equivalent is
_ <- DefaultServices.currentServices.locallyScopedWith(_.add(mockedZEnv))
} yield {
{ (success: TestSuccess) => ZIO.succeed(success) }
}).mapError(TestFailure.fail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zio.mock.module

import com.github.ghik.silencer.silent
import zio.mock.{Mock, Proxy}
import zio.{EnvironmentTag, URLayer, ZIO, ZLayer}
import zio.{EnvironmentTag, URLayer, Unsafe, ZIO, ZLayer}

/** Example module used for testing ZIO Mock framework.
*/
Expand Down Expand Up @@ -43,57 +43,99 @@ object ImpureModuleMock extends Mock[ImpureModule] {
withRuntime[Proxy, ImpureModule] { rts =>
ZIO.succeed {
new ImpureModule {
def zeroParams: String = rts.unsafeRunTask(proxy(ZeroParams))
def zeroParams: String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(ZeroParams)).getOrThrowFiberFailure()
}

def zeroParamsWithParens(): String = rts.unsafeRunTask(proxy(ZeroParamsWithParens))
def zeroParamsWithParens(): String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(ZeroParamsWithParens)).getOrThrowFiberFailure()
}

def singleParam(a: Int): String = rts.unsafeRunTask(proxy(SingleParam, a))
def singleParam(a: Int): String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(SingleParam, a)).getOrThrowFiberFailure()
}

def manyParams(a: Int, b: String, c: Long): String = rts.unsafeRunTask(proxy(ManyParams, (a, b, c)))
def manyParams(a: Int, b: String, c: Long): String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(ManyParams, (a, b, c))).getOrThrowFiberFailure()
}

def manyParamLists(a: Int)(b: String)(c: Long): String =
rts.unsafeRunTask(proxy(ManyParamLists, a, b, c))
Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(ManyParamLists, a, b, c)).getOrThrowFiberFailure()
}

@silent("side-effecting nullary methods")
def command: Unit = rts.unsafeRunTask(proxy(Command))
def command: Unit = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(Command))
}

def parameterizedCommand(a: Int): Unit = rts.unsafeRunTask(proxy(ParameterizedCommand, a))
def parameterizedCommand(a: Int): Unit = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(ParameterizedCommand, a))
}

def overloaded(n: Int): String = rts.unsafeRunTask(proxy(Overloaded._0, n))
def overloaded(n: Int): String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(Overloaded._0, n)).getOrThrowFiberFailure()
}

def overloaded(n: Long): String = rts.unsafeRunTask(proxy(Overloaded._1, n))
def overloaded(n: Long): String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(Overloaded._1, n)).getOrThrowFiberFailure()
}

def polyInput[I: EnvironmentTag](v: I): String = rts.unsafeRunTask(proxy(PolyInput.of[I], v))
def polyInput[I: EnvironmentTag](v: I): String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyInput.of[I], v)).getOrThrowFiberFailure()
}

def polyError[E <: Throwable: EnvironmentTag](v: String): String =
rts.unsafeRunTask(proxy(PolyError.of[E], v))
Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyError.of[E], v)).getOrThrowFiberFailure()
}

def polyOutput[A: EnvironmentTag](v: String): A = rts.unsafeRunTask(proxy(PolyOutput.of[A], v))
def polyOutput[A: EnvironmentTag](v: String): A = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyOutput.of[A], v)).getOrThrowFiberFailure()
}

def polyInputError[I: EnvironmentTag, E <: Throwable: EnvironmentTag](v: I): String =
rts.unsafeRunTask(proxy(PolyInputError.of[I, E], v))
Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyInputError.of[I, E], v)).getOrThrowFiberFailure()
}

def polyInputOutput[I: EnvironmentTag, A: EnvironmentTag](v: I): A =
rts.unsafeRunTask(proxy(PolyInputOutput.of[I, A], v))
Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyInputOutput.of[I, A], v)).getOrThrowFiberFailure()
}

def polyErrorOutput[E <: Throwable: EnvironmentTag, A: EnvironmentTag](v: String): A =
rts.unsafeRunTask(proxy(PolyErrorOutput.of[E, A], v))
Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyErrorOutput.of[E, A], v)).getOrThrowFiberFailure()
}

def polyInputErrorOutput[I: EnvironmentTag, E <: Throwable: EnvironmentTag, A: EnvironmentTag](
v: I
): A =
rts.unsafeRunTask(proxy(PolyInputErrorOutput.of[I, E, A], v))
Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyInputErrorOutput.of[I, E, A], v)).getOrThrowFiberFailure()
}

def polyMixed[A: EnvironmentTag]: (A, String) = rts.unsafeRunTask(proxy(PolyMixed.of[(A, String)]))
def polyMixed[A: EnvironmentTag]: (A, String) = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyMixed.of[(A, String)])).getOrThrowFiberFailure()
}

def polyBounded[A <: AnyVal: EnvironmentTag]: A = rts.unsafeRunTask(proxy(PolyBounded.of[A]))
def polyBounded[A <: AnyVal: EnvironmentTag]: A = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(PolyBounded.of[A])).getOrThrowFiberFailure()
}

def varargs(a: Int, b: String*): String = rts.unsafeRunTask(proxy(Varargs, (a, b)))
def varargs(a: Int, b: String*): String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(Varargs, (a, b))).getOrThrowFiberFailure()
}

def curriedVarargs(a: Int, b: String*)(c: Long, d: Char*): String =
rts.unsafeRunTask(proxy(CurriedVarargs, (a, b, c, d)))
Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(CurriedVarargs, (a, b, c, d))).getOrThrowFiberFailure()
}

def byName(a: => Int): String = rts.unsafeRunTask(proxy(ByName, a))
def byName(a: => Int): String = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(ByName, a)).getOrThrowFiberFailure()
}

def maxParams(
a: Int,
Expand All @@ -118,12 +160,15 @@ object ImpureModuleMock extends Mock[ImpureModule] {
t: Int,
u: Int,
v: Int
): String =
rts
.unsafeRunTask(proxy(MaxParams, (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)))
): String = Unsafe.unsafeCompat { implicit unsafe =>
rts.unsafe
.run(proxy(MaxParams, (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)))
.getOrThrowFiberFailure()
}
}
}
}
}
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zio.mock.module

import zio.mock.{Mock, Proxy}
import zio.stream.ZSink
import zio.{URLayer, ZIO, ZLayer}
import zio.{URLayer, Unsafe, ZIO, ZLayer}

/** Example module used for testing ZIO Mock framework.
*/
Expand All @@ -20,9 +20,15 @@ object StreamModuleMock extends Mock[StreamModule] {
ZIO.succeed {
new StreamModule {
def sink(a: Int) =
rts.unsafeRun(proxy(Sink, a).catchAll(error => ZIO.succeed(ZSink.fail[String](error).dropLeftover)))
Unsafe.unsafeCompat { implicit u =>
rts.unsafe
.run(proxy(Sink, a).catchAll(error => ZIO.succeed(ZSink.fail[String](error))))
.getOrThrowFiberFailure()
}

def stream(a: Int) = rts.unsafeRun(proxy(Stream, a))
def stream(a: Int) = Unsafe.unsafeCompat { implicit u =>
rts.unsafe.run(proxy(Stream, a)).getOrThrowFiberFailure()
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions mock/shared/src/main/scala-2/zio/mock/MockableMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ private[mock] object MockableMacro {
(info.capability, info.params.map(_.name)) match {
case (_: Capability.Effect, Nil) => q"proxy($tag)"
case (_: Capability.Effect, paramNames) => q"proxy($tag, ..$paramNames)"
case (_: Capability.Method, Nil) => q"rts.unsafeRunTask(proxy($tag))"
case (_: Capability.Method, paramNames) => q"rts.unsafeRunTask(proxy($tag, ..$paramNames))"
case (_: Capability.Method, Nil) =>
q"_root_.zio.Unsafe.unsafeCompat { implicit u => rts.unsafe.run(proxy($tag)).getOrThrowFiberFailure() }"
case (_: Capability.Method, paramNames) =>
q"_root_.zio.Unsafe.unsafeCompat { implicit u => rts.unsafe.run(proxy($tag, ..$paramNames)).getOrThrowFiberFailure() }"
case (_: Capability.Sink, Nil) =>
q"rts.unsafeRun(proxy($tag).catchAll(error => _root_.zio.UIO(_root_.zio.stream.ZSink.fail(error))))"
case (_: Capability.Sink, paramNames) =>
Expand Down
19 changes: 8 additions & 11 deletions mock/shared/src/main/scala/zio/mock/Mock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package zio.mock

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.stream.{ZSink, ZStream}
import zio.test.TestPlatform
import zio.{EnvironmentTag, Executor, Runtime, Trace, UIO, ULayer, URIO, URLayer, ZIO}
import zio.{EnvironmentTag, Runtime, RuntimeFlag, RuntimeFlags, Trace, UIO, ULayer, URLayer, ZIO}

/** A `Mock[R]` represents a mockable environment `R`.
*/
Expand All @@ -40,15 +39,13 @@ abstract class Mock[R: EnvironmentTag] { self =>
ZIO
.runtime[R]
.flatMap { runtime0 =>
ZIO
.runtime[R]
.flatMap { runtime =>
f(runtime)
}
.provideSomeLayer[R](Runtime.setExecutor {
val ec = runtime0.executor.asExecutionContext
Executor.fromExecutionContext(Int.MaxValue)(ec)
})
f(
Runtime(
runtime0.environment,
runtime0.fiberRefs,
RuntimeFlags.disable(runtime0.runtimeFlags)(RuntimeFlag.CooperativeYielding)
)
)
}
}

Expand Down
8 changes: 6 additions & 2 deletions mock/shared/src/main/scala/zio/mock/MockClock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import zio.{UIO, _}

import java.time
import java.time.OffsetDateTime
import java.time.temporal.ChronoUnit
import java.util.concurrent.TimeUnit

object MockClock extends Mock[Clock] {

object CurrentTime extends Effect[TimeUnit, Nothing, Long]
object CurrentTime extends Effect[ChronoUnit, Nothing, Long]
object CurrentDateTime extends Effect[Unit, Nothing, OffsetDateTime]
object Instant extends Effect[Unit, Nothing, java.time.Instant]
object LocalDateTime extends Effect[Unit, Nothing, java.time.LocalDateTime]
Expand All @@ -43,14 +44,17 @@ object MockClock extends Mock[Clock] {
.map { proxy =>
new Clock {

def currentTime(unit: => TimeUnit)(implicit trace: Trace): UIO[Long] = proxy(CurrentTime, unit)
def currentTime(unit: => TimeUnit)(implicit trace: Trace): UIO[Long] = ??? // proxy(CurrentTime, unit)
def currentTime(unit: => ChronoUnit)(implicit trace: Trace, d: DummyImplicit): UIO[Long] =
??? // proxy(ChronoUnit, unit)
def currentDateTime(implicit trace: Trace): UIO[OffsetDateTime] = proxy(CurrentDateTime)
def nanoTime(implicit trace: Trace): UIO[Long] = proxy(NanoTime)
def scheduler(implicit trace: Trace): UIO[Scheduler] = proxy(Scheduler)
def sleep(duration: => Duration)(implicit trace: Trace): UIO[Unit] = proxy(Sleep, duration)
def instant(implicit trace: Trace): zio.UIO[java.time.Instant] = proxy(Instant)
def localDateTime(implicit trace: Trace): zio.UIO[java.time.LocalDateTime] = proxy(LocalDateTime)
def javaClock(implicit trace: zio.Trace): UIO[time.Clock] = proxy(JavaClock)

}
}
)
Expand Down
4 changes: 2 additions & 2 deletions mock/shared/src/main/scala/zio/mock/MockSpecDefault.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package zio.mock

import zio.Chunk
import zio.test.{TestAspectAtLeastR, TestEnvironment, ZIOSpecDefault}
import zio.{Chunk, ZIOAppArgs}

abstract class MockSpecDefault extends ZIOSpecDefault {

override def aspects: Chunk[TestAspectAtLeastR[Environment with TestEnvironment with ZIOAppArgs]] =
override def aspects: Chunk[TestAspectAtLeastR[TestEnvironment]] =
super.aspects :+ MockReporter()

}

0 comments on commit 1cb8f13

Please sign in to comment.