Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etorreborre/28dd7746c18b2cf752ce to your computer and use it in GitHub Desktop.
Save etorreborre/28dd7746c18b2cf752ce to your computer and use it in GitHub Desktop.
Use fragments in a ScalaCheck Properties
package examples
import org.specs2._
import org.scalacheck._, Gen._, Arbitrary._, Prop._
import org.specs2.execute.Details
import org.specs2.io.StringOutput
import org.specs2.main.Arguments
import org.specs2.specification.core._
import reporter._
/**
* This specification shows how to create examples using the "acceptance" style
*/
class HelloWorldSpec(env: Env) extends Specification with ScalaCheck { def is = s2"""
This is a specification to check the 'Hello world' string
The 'Hello world' string should
use generators to create examples $e1
"""
def e1 = forAll(arbitrary[Int], arbitrary[String]) { (i: Int, s: String) =>
check(i, s)
}
def check(i: Int, s: String): Fragments =
br ^
"an int is equal to itself unless very large" ! {
ko
} ^
"a string is equal to itself unless very large" ! {
ok
} ^ br
implicit def CheckedFragments(fs: Fragments): Prop = {
val logger = scalacheckLineLogger
val notifier = scalacheckNotifier
val env1 = env.setLineLogger(logger).setArguments(Arguments.split("console notifier showOnly x!+-o"))
val printers = List(TextPrinter, NotifierPrinter.printer(notifier))
Reporter.report(env1, printers)(SpecStructure.create(SpecHeader(getClass, title = Some("")), fs)).run(env.systemLogger).unsafePerformIO
if (notifier.hasIssues) execute.Failure(logger.messages.mkString("\n"))
else execute.Success()
}
def scalacheckLineLogger = new BufferedLineLogger with StringOutput {
def infoLine(msg: String) = msg.split("\n").foreach(append)
def errorLine(msg: String) = msg.split("\n").foreach(append)
def failureLine(msg: String) = msg.split("\n").foreach(append)
override def toString = "scalacheckLineLogger"
}
def scalacheckNotifier = new SilentNotifier {
var hasIssues = false
override def exampleFailure(name: String, message: String, location: String, f: Throwable, details: Details, duration: Long) = hasIssues = true
override def exampleError (name: String, message: String, location: String, f: Throwable, duration: Long) = hasIssues = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment