Skip to content

Commit

Permalink
Upgrade to munit 1.0.0-M5
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Jun 19, 2022
1 parent df9cd7d commit ba30103
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 148 deletions.
51 changes: 0 additions & 51 deletions core/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala

This file was deleted.

28 changes: 0 additions & 28 deletions core/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package munit

import cats.effect.{IO, Resource}
import munit.catseffect.ResourceFixture.FixtureNotInstantiatedException

import scala.concurrent.duration._

class CatsEffectFixturesPlatformSpec extends CatsEffectSuite with CatsEffectAssertions {
Expand Down Expand Up @@ -66,7 +68,7 @@ class CatsEffectFixturesPlatformSpec extends CatsEffectSuite with CatsEffectAsse

test("throws exception") {
IO(uninitializedFixture())
.interceptMessage[ResourceSuiteLocalFixture.FixtureNotInstantiatedException](
.interceptMessage[FixtureNotInstantiatedException](
"The fixture `uninitialized-fixture` was not instantiated. Override `munitFixtures` and include a reference to this fixture."
)
}
Expand Down
44 changes: 17 additions & 27 deletions core/shared/src/main/scala/munit/CatsEffectFixtures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,27 @@

package munit

import cats.effect.{IO, Resource}
import cats.effect.IO
import cats.effect.Resource
import munit.catseffect.IOFixture
import munit.catseffect.ResourceFixture

trait CatsEffectFixtures extends CatsEffectFixturesPlatform { self: CatsEffectSuite =>
trait CatsEffectFixtures {

import CatsEffectSuite.Deferred

/** Similar to `ResourceSuiteLocalFixture`, but supported on both JVM and JS via several caveats.
* Instead of directly providing `T` provides a (memoized) `IO[T]` that is backed by a
* `Deferred[T]`. It is unsafe because on JS the resource is closed concurrently without
* backpressure,
* i.e. the suite will complete even while the resource has not closed yet. On JVM it is
* semantically equivalent to `ResourceSuiteLocalFixture`. Note also that constructing this
* fixture is impure because it unsafely allocates a `Deferred`.
*/
object UnsafeResourceSuiteLocalDeferredFixture {

def apply[T](name: String, resource: Resource[IO, T]): Fixture[IO[T]] =
new Fixture[IO[T]](name) {
val value: Deferred[IO, (T, IO[Unit])] = Deferred.unsafe

def apply(): IO[T] = value.get.map(_._1)
object ResourceTestLocalFixture {
def apply[A](name: String, resource: Resource[IO, A]): IOFixture[A] =
ResourceFixture.testLocal(name, resource)
}

override def beforeAll(): Unit = {
val resourceEffect = resource.allocated.flatMap(value.complete)
unsafeRunSyncOrForget(resourceEffect)
}
object ResourceSuiteLocalFixture {
def apply[A](name: String, resource: Resource[IO, A]): IOFixture[A] =
ResourceFixture.suiteLocal(name, resource)
}

override def afterAll(): Unit = {
unsafeRunSyncOrForget(value.get.flatMap(_._2))
}
}
@deprecated("Use ResourceSuiteLocalFixture", "2.0.0")
object UnsafeResourceSuiteLocalDeferredFixture {
def apply[A](name: String, resource: Resource[IO, A]): IOFixture[IO[A]] =
ResourceSuiteLocalFixture(name, resource.map(IO.pure))
}

}
66 changes: 32 additions & 34 deletions core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,49 @@

package munit

import cats.effect.{IO, SyncIO, Resource}
import cats.syntax.flatMap._

import scala.concurrent.Promise
import cats.effect.IO
import cats.effect.Resource
import cats.effect.SyncIO
import cats.effect.kernel.Deferred
import cats.syntax.all._

trait CatsEffectFunFixtures extends FunFixtures { self: CatsEffectSuite =>

@deprecated("Use ResourceFunFixture", "2.0.0")
object ResourceFixture {
def apply[A](
resource: Resource[IO, A]
): SyncIO[FunFixture[A]] = ResourceFunFixture(_ => resource)

def apply[T](
resource: Resource[IO, T]
): SyncIO[FunFixture[T]] =
apply(
resource,
(_, _) => IO.unit,
_ => IO.unit
)

@deprecated("ResourceFunFixture(TestOptions => Resource[IO, T])", "2.0.0")
def apply[T](
resource: Resource[IO, T],
setup: (TestOptions, T) => IO[Unit],
teardown: T => IO[Unit]
): SyncIO[FunFixture[T]] = SyncIO {
val promise = Promise[IO[Unit]]()
): SyncIO[FunFixture[T]] = ResourceFunFixture { options =>
resource.flatTap(t => Resource.make(setup(options, t))(_ => teardown(t)))
}
}

FunFixture.async(
setup = { testOptions =>
val resourceEffect = resource.allocated
val setupEffect =
resourceEffect
.map { case (t, release) =>
promise.success(release)
t
}
.flatTap(t => setup(testOptions, t))
object ResourceFunFixture {

setupEffect.unsafeToFuture()
},
teardown = { (argument: T) =>
teardown(argument)
.flatMap(_ => IO.fromFuture(IO(promise.future)).flatten)
.unsafeToFuture()
}
)
}
def apply[A](
resource: Resource[IO, A]
): SyncIO[FunFixture[A]] = apply(_ => resource)

def apply[A](resource: TestOptions => Resource[IO, A]): SyncIO[FunFixture[A]] =
Deferred.in[SyncIO, IO, IO[Unit]].map { deferred =>
FunFixture.async(
setup = { testOptions =>
resource(testOptions).allocated
.flatMap { case (a, release) =>
deferred.complete(release).as(a)
}
.unsafeToFuture()
},
teardown = { _ => deferred.get.flatten.unsafeToFuture() }
)
}

}

Expand Down
7 changes: 0 additions & 7 deletions core/shared/src/main/scala/munit/CatsEffectSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import munit.internal.NestingChecks.{checkNestingIO, checkNestingSyncIO}

abstract class CatsEffectSuite
extends FunSuite
with CatsEffectSuitePlatform
with CatsEffectAssertions
with CatsEffectFixtures
with CatsEffectFunFixtures {
Expand All @@ -48,9 +47,3 @@ abstract class CatsEffectSuite
{ case e: SyncIO[_] => Future(checkNestingSyncIO(e).unsafeRunSync())(munitExecutionContext) }
)
}

object CatsEffectSuite {
private[munit] type Deferred[F[_], A] = cats.effect.kernel.Deferred[F, A]
private[munit] val Deferred = cats.effect.kernel.Deferred

}
75 changes: 75 additions & 0 deletions core/shared/src/main/scala/munit/catseffect/ResourceFixture.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package munit
package catseffect

import cats.effect.IO
import cats.effect.Resource

object ResourceFixture {

final class FixtureNotInstantiatedException(name: String)
extends Exception(
s"The fixture `$name` was not instantiated. Override `munitFixtures` and include a reference to this fixture."
)

def testLocal[A](name: String, resource: Resource[IO, A]): IOFixture[A] =
new IOFixture[A](name) {
@volatile var value: Option[(A, IO[Unit])] = None

def apply() = value match {
case Some(v) => v._1
case None => throw new FixtureNotInstantiatedException(name)
}

override def beforeEach(context: BeforeEach) = resource.allocated.flatMap { value =>
IO(this.value = Some(value))
}

override def afterEach(context: AfterEach) = value.fold(IO.unit)(_._2)
}

def suiteLocal[A](name: String, resource: Resource[IO, A]): IOFixture[A] =
new IOFixture[A](name) {
@volatile var value: A = _
@volatile var release: IO[Unit] = IO.unit

def apply() = value

override def beforeAll() = resource.allocated.flatMap { case (a, release) =>
IO {
value = a
this.release = release
}
}

override def afterAll() = release
}

}

abstract class IOFixture[A](name: String) extends AnyFixture[A](name: String) {

override def beforeAll(): IO[Unit] = IO.unit

override def beforeEach(context: BeforeEach): IO[Unit] = IO.unit

override def afterEach(context: AfterEach): IO[Unit] = IO.unit

override def afterAll(): IO[Unit] = IO.unit

}

0 comments on commit ba30103

Please sign in to comment.