ScalaCheck integration for specs2 testing framework - provides property-based testing capabilities with seamless integration into specs2 specifications
npx @tessl/cli install tessl/maven-org-specs2--specs2-scalacheck-2-12@3.10.0specs2-scalacheck provides ScalaCheck integration for the specs2 testing framework, enabling property-based testing within specs2 specifications. It bridges ScalaCheck's powerful property generators and testing capabilities with specs2's expressive DSL and comprehensive reporting system.
build.sbt:
libraryDependencies += "org.specs2" %% "specs2-scalacheck" % "3.10.0"import org.specs2.ScalaCheck
import org.specs2.scalacheck._For specific functionality:
import org.specs2.scalacheck.{Parameters, ScalaCheckProperty}import org.specs2.{Specification, ScalaCheck}
import org.scalacheck.{Arbitrary, Gen}
class MySpec extends Specification with ScalaCheck { def is = s2"""
Basic property testing
addition is commutative $commutativeAddition
string reversal twice returns original $doubleReverse
"""
def commutativeAddition = prop { (a: Int, b: Int) =>
a + b must_== b + a
}
def doubleReverse = prop { (s: String) =>
s.reverse.reverse must_== s
}
}specs2-scalacheck is built around several key components:
Core functionality for creating ScalaCheck properties from test functions. Supports functions with 1-8 parameters with full typeclass customization.
trait ScalaCheckPropertyCreation {
def prop[T, R](result: T => R)(implicit
arbitrary: Arbitrary[T],
shrink: Shrink[T],
pretty: T => Pretty,
prettyFreqMap: FreqMap[Set[Any]] => Pretty,
asResult: AsResult[R],
parameters: Parameters
): ScalaCheckFunction1[T, R]
}Comprehensive parameter configuration system with fluent API for setting test parameters, verbosity, and ScalaCheck settings.
case class Parameters(
minTestsOk: Int = Test.Parameters.default.minSuccessfulTests,
minSize: Int = Test.Parameters.default.minSize,
maxDiscardRatio: Float = Test.Parameters.default.maxDiscardRatio,
maxSize: Int = Test.Parameters.default.maxSize,
workers: Int = Test.Parameters.default.workers,
testCallback: Test.TestCallback = Test.Parameters.default.testCallback,
loader: Option[ClassLoader] = Test.Parameters.default.customClassLoader,
prettyParams: Pretty.Params = Pretty.defaultParams
)Core property checking functionality that executes ScalaCheck properties and converts results to specs2 format.
trait ScalaCheckPropertyCheck {
def check(
prop: Prop,
parameters: Parameters,
prettyFreqMap: FreqMap[Set[Any]] => Pretty
): Result
def checkProperties(
properties: Properties,
parameters: Parameters,
prettyFreqMap: FreqMap[Set[Any]] => Pretty
): Result
}Bidirectional conversion system between ScalaCheck Prop/Properties and specs2 AsResult types for seamless integration.
trait AsResultProp {
implicit def asResultToProp[R : AsResult](r: R): Prop
implicit def propAsResult(implicit p: Parameters, pfq: FreqMap[Set[Any]] => Pretty): AsResult[Prop]
implicit def propertiesAsResult(implicit p: Parameters, pfq: FreqMap[Set[Any]] => Pretty): AsResult[Properties]
}Integration with specs2's specification DSL for displaying Properties as examples and converting Prop objects.
trait ScalaCheckPropertyDsl {
implicit def propToScalaCheckProperty(prop: Prop)(implicit
parameters: Parameters,
prettyFreqMap: FreqMap[Set[Any]] => Pretty
): ScalaCheckProp
def properties(ps: Properties): Fragments
}Utility traits and objects for expectation control, pretty printing enhancements, and Scalaz integration.
trait OneExpectationPerProp extends AsResultProp
object PrettyProduct
object PrettyDetails
trait GenInstances// Main entry point trait combining all functionality
trait ScalaCheck extends
ScalaCheckPropertyCreation with
ScalaCheckPropertyCheck with
ScalaCheckParameters with
AsResultProp with
ScalaCheckPropertyDsl with
GenInstances
// Base property trait
trait ScalaCheckProperty {
type SelfType <: ScalaCheckProperty
def prop: Prop
def parameters: Parameters
def prettyFreqMap: FreqMap[Set[Any]] => Pretty
def setParameters(ps: Parameters): SelfType
def setVerbosity(v: Int): SelfType
def verbose: SelfType
}
// Property with context support
trait ScalaCheckFunction extends ScalaCheckProperty {
def noShrink: SelfType
def context: Option[Context]
def setContext(context: Context): SelfType
def before(action: =>Any): SelfType
def after(action: =>Any): SelfType
def around(action: Result => Result): SelfType
}
// Simple property wrapper
case class ScalaCheckProp(
prop: Prop,
parameters: Parameters,
prettyFreqMap: FreqMap[Set[Any]] => Pretty
) extends ScalaCheckProperty
// Parameter bundle for typeclass instances
case class ScalaCheckArgInstances[T](
arbitrary: Arbitrary[T],
shrink: Option[Shrink[T]],
collectors: List[T => Any],
pretty: T => Pretty
)
// Utility trait for controlling expectation counts in specs2
trait OneExpectationPerProp extends AsResultProp {
implicit override def propAsResult(implicit p: Parameters, pfq: FreqMap[Set[Any]] => Pretty): AsResult[Prop]
implicit override def propertiesAsResult(implicit p: Parameters, pfq: FreqMap[Set[Any]] => Pretty): AsResult[Properties]
}
// Utility object for creating Pretty instances for case classes
object PrettyProduct {
def toString[P <: Product](p: P): String
def apply[P <: Product]: P => Pretty
}
// Utility object for handling failure details in frequency maps
object PrettyDetails {
def collectDetails[T](fq: FreqMap[Set[T]]): execute.Details
def removeDetails(fq: FreqMap[Set[Any]]): FreqMap[Set[Any]]
}
// Utility trait for controlling expectation counts in specs2
trait OneExpectationPerProp extends AsResultProp {
implicit override def propAsResult(implicit p: Parameters, pfq: FreqMap[Set[Any]] => Pretty): AsResult[Prop]
implicit override def propertiesAsResult(implicit p: Parameters, pfq: FreqMap[Set[Any]] => Pretty): AsResult[Properties]
}
// Scalaz typeclass instances for ScalaCheck Gen
trait GenInstances {
implicit def genMonad: Monad[Gen]
}