From 11074f2cfee494577fe64162dc5da53598e62056 Mon Sep 17 00:00:00 2001 From: Georgii Kovalev Date: Mon, 30 Sep 2024 22:27:00 +0500 Subject: [PATCH] fix readme, add resetStubsF --- README.md | 85 +++++++++++++++++-- build.sbt | 5 +- .../core/src/main/scala/backstub/Stubs.scala | 3 + 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index cc19ce7..b2e5917 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,9 @@ Then you've come to the right place, meet **backstub** - stubbing library for Scala 3 inspired by [scalamock](https://github.com/paulbutcher/ScalaMock) and compatible with any testing framework out of the box -How it works: -1. Setup cleanup after each test-case -2. Setup expected results based on input arguments per suit or per test-case -3. After stubs were used - get the data passed through the method and verify - ## Setup +### Basic Add to your build.sbt ```scala @@ -27,9 +23,23 @@ Library hugely relies on experimental scala 3 features, so consider also adding Test \ scalacOptions += "-experimental" ``` +### ZIO + +For ZIO integration also add: +```scala +libraryDependencies += "io.github.goshacodes" %% "backstub-zio" % "" +``` + +### Cats Effect + +For Cats Effect integration also add: +```scala +libraryDependencies += "io.github.goshacodes" %% "backstub-cats-effect" % "" +``` + ## API -### Stubs +### Basic Stubs Should be mixed with your test-suite, to provide clean-up API @@ -37,10 +47,9 @@ Should be mixed with your test-suite, to provide clean-up API package backstub trait Stubs: - given stubs: CreatedStubs = CreatedStubs() - - def resetStubs(): Unit = stubs.clearAll() + final given stubs: CreatedStubs = CreatedStubs() + final def resetStubs(): Unit = stubs.clearAll() ``` Using it as simple as: @@ -54,6 +63,48 @@ class MySpec extends munit.FunSuite with Stubs: ``` +### ZIO Stubs + +Gives you instance of StubEffect, allowing to integrate ZIO + +```scala 3 +package backstub + +import zio.* + +trait ZIOStubs extends Stubs: + // this method is actually in Stubs + final def resetStubsIO[F[+_, +_]: StubEffect]: F[Nothing, Unit] = + summon[StubEffect[F]].unit(resetStubs()) + + given effect.StubEffect[IO] with + def unit[T](t: => T): UIO[T] = ZIO.succeed(t) + def flatMap[E, EE >: E, T, T2](fa: IO[E, T])(f: T => IO[EE, T2]): IO[EE, T2] = fa.flatMap(f) + + +``` + +### Cats Stubs + +Gives you instance of StubEffect.Mono, allowing to integrate Cats Effect + +```scala 3 +package backstub + +import cats.effect.* + +trait CatsEffectStubs extends Stubs: + // this method is actually in Stubs + final def resetStubsF[F[+_]: StubEffect.Mono]: F[Unit] = + summon[StubEffect.Mono[F]].unit(resetStubs()) + + given StubEffect.Mono[IO] = new StubEffect.Mono[IO]: + def unit[T](t: => T): IO[T] = IO(t) + def flatMap[E, EE >: E, T, T2](fa: IO[T])(f: T => IO[T2]): IO[T2] = fa.flatMap(f) + + +``` + ### stub Generates a stub. Without expectations setup just throws **NotImplementedError** @@ -132,6 +183,14 @@ inline given Expect[Foo] = Expect[Foo] val foo = stub[Foo] ``` +### ZIO Expect + +The only difference is - you should choose method using `methodIO`. + +### Cats Effect Expect + +The only difference is - you should choose method using `methodF0` (if no arguments) or `methodF`. + ### Verify @@ -161,6 +220,14 @@ foo.calls(_.twoArgs) // List((5, "foo")) ``` +### Verify ZIO + +You can use `callsIO` and `timesIO` methods + +### Verify Cats Effect + +You can use `callsF` and `timesF` methods + ### Overloaded methods To set expectations for overloaded methods - method type should be specified diff --git a/build.sbt b/build.sbt index f085c7d..b2c9e75 100644 --- a/build.sbt +++ b/build.sbt @@ -4,7 +4,7 @@ lazy val backstub = project .in(file(".")) .settings( commonSettings, - publishTo := None + publish / skip := true ) .dependsOn(core, cats, zio) .aggregate(core, cats, zio) @@ -30,7 +30,7 @@ lazy val core = project.in(modules / "core") ) lazy val zio = project.in(modules / "zio") - .dependsOn(core % "compile->compile;compile->test") + .dependsOn(core) .settings( name := "backstub-zio", commonSettings, @@ -74,3 +74,4 @@ inThisBuild( ) sonatypeRepository := "https://s01.oss.sonatype.org/service/local" + diff --git a/modules/core/src/main/scala/backstub/Stubs.scala b/modules/core/src/main/scala/backstub/Stubs.scala index a05d073..3a2e502 100644 --- a/modules/core/src/main/scala/backstub/Stubs.scala +++ b/modules/core/src/main/scala/backstub/Stubs.scala @@ -7,5 +7,8 @@ trait Stubs: final def resetStubs(): Unit = stubs.clearAll() + final def resetStubsF[F[+_]: StubEffect.Mono]: F[Unit] = + summon[StubEffect.Mono[F]].unit(resetStubs()) + final def resetStubsIO[F[+_, +_]: StubEffect]: F[Nothing, Unit] = summon[StubEffect[F]].unit(resetStubs())