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

Support scalatest assertions in Checkers #50

Open
brandon-leapyear opened this issue Sep 15, 2021 · 1 comment
Open

Support scalatest assertions in Checkers #50

brandon-leapyear opened this issue Sep 15, 2021 · 1 comment

Comments

@brandon-leapyear
Copy link

brandon-leapyear commented Sep 15, 2021

As a follow up to scalatest/scalatest#251, ScalaCheckDrivenPropertyChecks isn't good for nested forAlls and is also missing features like forAllNoShrink.

It seems like Checkers is what I want anyway, to explicitly build up a Prop, then check it. But it currently doesn't work with normal scalatest assertions like

check {
  forAll { a: Int =>
    foo(a) shouldBe 42
  }
}

This currently throws a compile-time error:

No implicit view available from org.scalatest.Assertion => org.scalacheck.Prop.

It would be nice to augment Checkers or provide a separate class to include support for normal scalatest assertions.

@brandon-leapyear brandon-leapyear changed the title Add forAllNoShrink to ScalaCheckDrivenPropertyChecks Support scalatest assertions in Checkers Sep 15, 2021
@brandon-leapyear
Copy link
Author

brandon-leapyear commented Sep 15, 2021

This is an old work account. Please reference @brandonchinn178 for all future communication


Currently working around by writing my own Checkers trait with an implicit function to convert an Assertion into a Prop

import org.scalacheck.Prop
import org.scalacheck.Prop._
import org.scalatest.Assertion
import org.scalatest.exceptions.DiscardedEvaluationException
import org.scalatestplus.scalacheck

// https://github.com/scalatest/scalatestplus-scalacheck/issues/50
trait Checkers extends scalacheck.Checkers {
  implicit def assertionToProp(test: Assertion)
    (implicit
      asserting: scalacheck.CheckerAsserting[Assertion]
    ): Prop = {
      val (unmetCondition, succeeded, exception) =
        try {
          val (succeeded, cause) = asserting.succeed(test)
          (false, succeeded, cause)
        }
        catch {
          case e: DiscardedEvaluationException => (true, false, None)
          case e: Throwable => (false, false, Some(e))
        }
      !unmetCondition ==> (
        if (exception.isEmpty) {
          if (succeeded)
            Prop.passed
          else
            Prop.falsified
        }
        else
          Prop.exception(exception.get)
      )
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant