Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross-platform suite-local fixtures #104

Merged
merged 6 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ce2/js/src/main/scala/munit/CatsEffectSuitePlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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

import cats.effect.IO

private[munit] trait CatsEffectSuitePlatform { self: CatsEffectSuite =>

private[munit] def unsafeRunSyncOrForget[A](ioa: IO[A]): Unit = ioa.unsafeRunAsyncAndForget()

}
28 changes: 28 additions & 0 deletions ce2/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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

import cats.effect.IO

private[munit] trait CatsEffectSuitePlatform { self: CatsEffectSuite =>

private[munit] def unsafeRunSyncOrForget[A](ioa: IO[A]): Unit = {
ioa.unsafeRunSync()
()
}

}
6 changes: 6 additions & 0 deletions ce2/shared/src/main/scala/munit/CatsEffectSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import scala.concurrent.{Future, ExecutionContext}

abstract class CatsEffectSuite
extends FunSuite
with CatsEffectSuitePlatform
with CatsEffectAssertions
with CatsEffectFixtures
with CatsEffectFunFixtures {
Expand All @@ -48,3 +49,8 @@ abstract class CatsEffectSuite
)

}

object CatsEffectSuite {
private[munit] type Deferred[F[_], A] = cats.effect.concurrent.Deferred[F, A]
private[munit] val Deferred = cats.effect.concurrent.Deferred
}
25 changes: 25 additions & 0 deletions ce3/js/src/main/scala/munit/CatsEffectSuitePlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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

import cats.effect.IO

private[munit] trait CatsEffectSuitePlatform { self: CatsEffectSuite =>

private[munit] def unsafeRunSyncOrForget[A](ioa: IO[A]): Unit = ioa.unsafeRunAndForget()

}
28 changes: 28 additions & 0 deletions ce3/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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

import cats.effect.IO

private[munit] trait CatsEffectSuitePlatform { self: CatsEffectSuite =>

private[munit] def unsafeRunSyncOrForget[A](ioa: IO[A]): Unit = {
ioa.unsafeRunSync()
()
}

}
6 changes: 6 additions & 0 deletions ce3/shared/src/main/scala/munit/CatsEffectSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import scala.concurrent.{ExecutionContext, Future}

abstract class CatsEffectSuite
extends FunSuite
with CatsEffectSuitePlatform
with CatsEffectAssertions
with CatsEffectFixtures
with CatsEffectFunFixtures {
Expand All @@ -47,3 +48,8 @@ abstract class CatsEffectSuite
)

}

object CatsEffectSuite {
private[munit] type Deferred[F[_], A] = cats.effect.kernel.Deferred[F, A]
private[munit] val Deferred = cats.effect.kernel.Deferred
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

package munit

trait CatsEffectFixtures { self: CatsEffectSuite => }
trait CatsEffectFixturesPlatform { self: CatsEffectSuite => }
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package munit

import cats.effect.{IO, Resource}

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

object ResourceSuiteLocalFixture {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package munit

import cats.effect.{IO, Resource}

class CatsEffectFixturesSpec extends CatsEffectSuite with CatsEffectAssertions {
class CatsEffectFixturesPlatformSpec extends CatsEffectSuite with CatsEffectAssertions {

var acquired: Int = 0
var released: Int = 0
Expand Down
52 changes: 52 additions & 0 deletions common/shared/src/main/scala/munit/CatsEffectFixtures.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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

import cats.effect.{IO, Resource}

trait CatsEffectFixtures extends CatsEffectFixturesPlatform { self: CatsEffectSuite =>

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)

override def beforeAll(): Unit = {
val resourceEffect = resource.allocated.flatMap(value.complete)
unsafeRunSyncOrForget(resourceEffect)
}

override def afterAll(): Unit = {
unsafeRunSyncOrForget(value.get.flatMap(_._2))
}
}
}

}
61 changes: 61 additions & 0 deletions common/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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

import cats.effect.{IO, Resource}

class CatsEffectFixturesSpec extends CatsEffectSuite with CatsEffectAssertions {

@volatile var acquired: Int = 0
@volatile var released: Int = 0

val fixture = UnsafeResourceSuiteLocalDeferredFixture(
"fixture",
Resource.make(
IO {
acquired += 1
()
}
)(_ =>
IO {
released += 1
()
}
)
)

override def munitFixtures = List(fixture)

override def beforeAll(): Unit = {
assertEquals(acquired, 0)
assertEquals(released, 0)
}

override def afterAll(): Unit = {
assertEquals(acquired, 1)
// assertEquals(released, 1) // Release is async, no way to check
}

test("first test") {
fixture().assertEquals(())
}

test("second test") {
fixture().assertEquals(())
}

}