Last active
August 7, 2024 21:36
-
-
Save dabrowski-adam/15a19ab30afb64e86b224d393bbc06ac to your computer and use it in GitHub Desktop.
Scala: Weaver Test and Hedgehog Integration PoC
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 com.adamdabrowski | |
import weaver.* | |
import hedgehog.* | |
object ExampleSuite extends SimpleIOSuite with Hedgehog: | |
// Passes | |
propertyTest("length of the result of concatenation of two lists is equal to the sum of their lengths"): | |
for | |
l1 <- Gen.int(Range.linear(-100, 100)).list(Range.linear(0, 100)).forAll | |
l2 <- Gen.int(Range.linear(-100, 100)).list(Range.linear(0, 100)).forAll | |
yield l1.size + l2.size ==== (l1 ::: l2).size | |
// weaver.junit.WeaverRunner$$anon$2: - square root of n squared equals n 25ms | |
// - ExampleSuite.square root of n squared equals n: Falsified after 1 passed tests | |
// > -1 | |
// ... | |
propertyTest("square root of n squared equals n"): | |
for | |
n <- Gen.int(Range.linearFrom(0, -100, 100)).forAll | |
yield scala.math.sqrt(n * n) ==== n |
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 com.adamdabrowski | |
import cats.effect.* | |
import weaver.* | |
import weaver.Expectations.Helpers.expect | |
import hedgehog.Property | |
import hedgehog.core.{PropertyConfig, Report, Seed, Status} | |
import hedgehog.runner.{Prop, SeedSource, Test} | |
trait Hedgehog: | |
this: BaseCatsSuite & MutableIOSuite => | |
def propertyTest(name: String)(property: Property): Unit = | |
val prop: Prop = hedgehog.runner.property(name, property) | |
val seed: Seed = Seed.fromLong(SeedSource.fromEnvOrTime().seed) | |
def render(prop: Prop, report: Report) = | |
Test.renderReport(this.getSuite.name, prop, report, ansiCodesSupported = true) | |
def run(prop: Prop, seed: Seed): IO[Expectations] = | |
for | |
report <- IO.delay: | |
Property.check(prop.withConfig(PropertyConfig.default), prop.result, seed) | |
outcome = report.status match | |
case OK => success | |
case _ => failure(render(prop, report)) | |
yield outcome | |
test(name)(run(prop, seed)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment