Skip to content

Commit

Permalink
Merge pull request #187 from manuelcueto/186-custom-test-params
Browse files Browse the repository at this point in the history
refactor scalacheck effect suite to support custom params
  • Loading branch information
mpilquist authored Apr 29, 2022
2 parents 66ad553 + 3568f59 commit f146a34
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
43 changes: 27 additions & 16 deletions munit/shared/src/main/scala/munit/ScalaCheckEffectSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,17 @@ trait ScalaCheckEffectSuite extends ScalaCheckSuite {
.withLegacyShrinking(scalaCheckTestParameters.useLegacyShrinking)
.withInitialSeed(initialSeed)

override def munitValueTransforms: List[ValueTransform] =
super.munitValueTransforms :+ scalaCheckPropFValueTransform
override def munitValueTransforms: List[ValueTransform] = {
val testResultTransform =
new ValueTransform(
"ScalaCheck TestResult",
{ case p: Test.Result =>
super.munitValueTransform(parseTestResult(p))
}
)

super.munitValueTransforms :+ scalaCheckPropFValueTransform :+ testResultTransform
}

private val scalaCheckPropFValueTransform: ValueTransform =
new ValueTransform(
Expand All @@ -58,21 +67,21 @@ trait ScalaCheckEffectSuite extends ScalaCheckSuite {
}
)

private def checkPropF[F[_]](
prop: PropF[F]
)(implicit loc: Location): F[Unit] = {
private def checkPropF[F[_]](prop: PropF[F])(implicit loc: Location): F[Unit] = {
import prop.F
prop.check(scalaCheckTestParameters, genParameters).map(fixResultException).flatMap { result =>
if (result.passed) F.unit
else {
val seed = genParameters.initialSeed.get
val seedMessage = s"""|Failing seed: ${seed.toBase64}
|You can reproduce this failure by adding the following override to your suite:
|
| override def scalaCheckInitialSeed = "${seed.toBase64}"
|""".stripMargin
fail(seedMessage + "\n" + Pretty.pretty(result, scalaCheckPrettyParameters))
}
prop.check(scalaCheckTestParameters, genParameters).map(fixResultException).map(parseTestResult)
}

private def parseTestResult(result: Test.Result)(implicit loc: Location): Unit = {
if (!result.passed) {
val seed = genParameters.initialSeed.get
val seedMessage =
s"""|Failing seed: ${seed.toBase64}
|You can reproduce this failure by adding the following override to your suite:
|
| override def scalaCheckInitialSeed = "${seed.toBase64}"
|""".stripMargin
fail(seedMessage + "\n" + Pretty.pretty(result, scalaCheckPrettyParameters))
}
}

Expand All @@ -94,3 +103,5 @@ trait ScalaCheckEffectSuite extends ScalaCheckSuite {
}

}

object ScalaCheckEffectSuite {}
9 changes: 9 additions & 0 deletions munit/shared/src/test/scala/munit/Example.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package munit

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import org.scalacheck.Test.Parameters
import org.scalacheck.effect.PropF

import java.util.concurrent.atomic.AtomicBoolean
Expand All @@ -40,6 +41,14 @@ class Example extends ScalaCheckEffectSuite {
}
}

test("three") {
PropF
.forAllF { (a: Int) =>
IO { assert(a == a) }
}
.check(Parameters.default.withMinSuccessfulTests(1))
}

val ready = new AtomicBoolean(false)

val fixture = FunFixture[Unit](
Expand Down

0 comments on commit f146a34

Please sign in to comment.