Created
September 15, 2021 18:28
-
-
Save danicheg/be9ed79cd6f5764899e7cc16fe23e48c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package munit | |
import cats.effect.{IO, Resource} | |
// ce3/js/src/main/scala/munit/ResourceSuiteLocalFixturePlatform | |
private[munit] trait ResourceSuiteLocalFixturePlatform { self: CatsEffectSuite => | |
def createResourceSuiteLocalFixturePlatform[T]( | |
name: String, | |
resource: Resource[IO, T] | |
): Fixture[T] = | |
new Fixture[T](name) { | |
var value: Option[Either[Throwable, (T, IO[Unit])]] = None | |
def apply(): T = value match { | |
case Some(Right(v)) => v._1 | |
case Some(Left(ex)) => throw ex | |
case None => throw new FixtureNotInstantiatedException(name) | |
} | |
override def beforeAll(): Unit = { | |
val resourceEffect = resource.allocated | |
resourceEffect.unsafeRunAsync { result => | |
value = Some(result) | |
} | |
} | |
override def afterAll(): Unit = { | |
value match { | |
case None => | |
throw new FixtureNotInstantiatedException(name) | |
case Some(Right((_, cleanup))) => | |
cleanup.unsafeRunAsync(_ => ()) | |
case Some(Left(ex)) => | |
throw ex | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment