Skip to content

Commit

Permalink
Merge pull request #23 from reibitto/zio2.0.0-final
Browse files Browse the repository at this point in the history
Migrate to ZIO 2.0.0 final
  • Loading branch information
vigoo committed Jun 28, 2022
2 parents e192f60 + 9ffa6c5 commit 664a1e4
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 68 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 @@ -3,7 +3,7 @@ package zio.mock.examples
import zio.mock.Expectation.{unit, value, valueF}
import zio.mock.{MockClock, MockConsole, MockRandom}
import zio.test.Assertion._
import zio.test.{Spec, TestFailure, TestSuccess, ZIOSpecDefault, assertZIO}
import zio.test.{Spec, ZIOSpecDefault, assertZIO}
import zio.{Clock, Console, Random, ZIO}

import java.io.IOException
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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))
_ <- DefaultServices.currentServices.locallyScopedWith(_.union(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)).getOrThrow()
}

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

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)).getOrThrow()
}

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))).getOrThrow()
}

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)).getOrThrow()
}

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

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)).getOrThrow()
}

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)).getOrThrow()
}

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)).getOrThrow()
}

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)).getOrThrow()
}

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)).getOrThrow()
}

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)).getOrThrow()
}

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)).getOrThrow()
}

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)).getOrThrow()
}

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)).getOrThrow()
}

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)).getOrThrow()
}

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)])).getOrThrow()
}

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])).getOrThrow()
}

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))).getOrThrow()
}

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))).getOrThrow()
}

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)).getOrThrow()
}

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)))
.getOrThrow()
}
}
}
}
}
)

}
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ package object testing {

val testOutput = if (showSpecOutput) TestOutput.live else ZLayer.succeed(SilentTestOutput)
val layer0 = testEnvironment ++ Scope.default ++ ZIOAppArgs.empty
val layer1 = (Console.live >>> TestLogger.fromConsole(
val layer1 = TestLogger.fromConsole(
Console.ConsoleLive
) >>> ExecutionEventPrinter.live >>> testOutput >>> ExecutionEventSink.live)
) >>> ExecutionEventPrinter.live(
ReporterEventRenderer.ConsoleEventRenderer
) >>> testOutput >>> ExecutionEventSink.live

// perTestLayer = (ZLayer.succeedEnvironment(environment1) ++ ZEnv.live) >>> (TestEnvironment.live ++ ZLayer
// .environment[Scope] ++ ZLayer.environment[ZIOAppArgs])
Expand Down
30 changes: 23 additions & 7 deletions mock/shared/src/main/scala-2/zio/mock/MockableMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,39 @@ private[mock] object MockableMacro {
if (info.symbol.isAbstract) Modifiers(Flag.FINAL)
else Modifiers(Flag.FINAL | Flag.OVERRIDE)

val returnType = info.capability match {
val returnType = info.capability match {
case Capability.Method(t) => tq"$t"
case Capability.Stream(r, e, a) => tq"_root_.zio.stream.ZStream[$r, $e, $a]"
case _ => tq"_root_.zio.ZIO[$r, $e, $a]"
}

def wrapInUnsafe(tree: Tree): Tree =
q"""_root_.zio.Unsafe.unsafeCompat { __unsafeVal =>
implicit val __unsafeImplicit = __unsafeVal
$tree
}
"""

val returnValue =
(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) =>
wrapInUnsafe(q"rts.unsafe.run(proxy($tag)).getOrThrow()")
case (_: Capability.Method, paramNames) =>
wrapInUnsafe(q"rts.unsafe.run(proxy($tag, ..$paramNames)).getOrThrow()")
case (_: Capability.Sink, Nil) =>
q"rts.unsafeRun(proxy($tag).catchAll(error => _root_.zio.UIO(_root_.zio.stream.ZSink.fail(error))))"
wrapInUnsafe(
q"rts.unsafe.run(proxy($tag).catchAll(error => _root_.zio.UIO(_root_.zio.stream.ZSink.fail(error)))).getOrThrowFiberFailure()"
)
case (_: Capability.Sink, paramNames) =>
q"rts.unsafeRun(proxy($tag, ..$paramNames).catchAll(error => _root_.zio.UIO(_root_.zio.stream.ZSink.fail(error))))"
case (_: Capability.Stream, Nil) => q"rts.unsafeRun(proxy($tag))"
case (_: Capability.Stream, paramNames) => q"rts.unsafeRun(proxy($tag, ..$paramNames))"
wrapInUnsafe(
q"rts.unsafe.run(proxy($tag, ..$paramNames).catchAll(error => _root_.zio.UIO(_root_.zio.stream.ZSink.fail(error)))).getOrThrowFiberFailure()"
)
case (_: Capability.Stream, Nil) =>
wrapInUnsafe(q"rts.unsafe.run(proxy($tag)).getOrThrowFiberFailure()")
case (_: Capability.Stream, paramNames) =>
wrapInUnsafe(q"rts.unsafe.run(proxy($tag, ..$paramNames)).getOrThrowFiberFailure()")
}

val noParams = info.symbol.paramLists.isEmpty // Scala 2.11 workaround. For some reason isVal == false in 2.11
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
Loading

0 comments on commit 664a1e4

Please sign in to comment.