Skip to content

Instantly share code, notes, and snippets.

@kelseyq
Last active January 24, 2016 18:51
Show Gist options
  • Save kelseyq/d4a2c73a41609c563ad1 to your computer and use it in GitHub Desktop.
Save kelseyq/d4a2c73a41609c563ad1 to your computer and use it in GitHub Desktop.
witchy ScalaCheck examples
import org.scalacheck.Prop.forAll
import org.scalacheck.Properties
object ShakespeareSpec extends Properties("Bubble") {
def spell(words: List[String], mysticNumber: Int) =
mysticNumber.toString +
words.mkString + "toil" + "trouble"
property("bubble") = forAll { (a: List[String], b: Int) =>
spell(a, b).length ==
Math.ceil(Math.log10(b + 1)) + a.map(_.length).sum + 11
}
}
[info] ! Bubble.bubble: Falsified after 2 passed tests.
[info] > ARG_0: List("")
[info] > ARG_0_ORIGINAL: List("漢字")
[info] > ARG_1: 0
[info] > ARG_1_ORIGINAL: -677524853
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
//everyone knows magic numbers are always positive!
forAll(listOf(alphaStr), posNum[Int]) { (a, b) =>
spell(a, b).length ==
Math.ceil(Math.log10(b + 1)) + a.map(_.length).sum + 11
}
forAll(Gen.choose(1, 10)) { magicNumber =>
magicNumber.invoke == motherHecate
}
forAll(Gen.oneOf(eyeOfNewt, toeOfFrog, wingOfBat)) { ingredient =>
ingredient.addToCauldron == hellBroth
}
forAll(Gen.someOf(tongueOfDog, addresFork, blindwormsSting,
lizardsLeg, howletsWing)) { ingredients =>
boil(ingredients.map(_.addToCauldron)) == charmOfPowerfulTrouble
}
val slimy = Gen.oneOf(eyeOfNewt, toeOfFrog, lizardsLeg)
val crawly = Gen.oneOf(addersFork, blindwormsSting, lizardsLeg)
val creepy = Gen.oneOf(wingOfBat, tongueOfDog, howletsWing)
forAll(Gen.someOf(slimy, crawly, creepy)) { ingredients =>
boil(ingredients.map(_.addToCauldron)) == charmOfPowerfulTrouble
}
forAll(Gen.listOf(slimy)) { ingredients =>
touch(ingredients) == skinCrawl
}
val spellGen = arbitrary[String].map(_ + ", so mote it be")
val titleGen = arbitrary[String].flatMap{ name =>
Gen.oneOf("East", "West", "North", "South").map { dir =>
name + ", Wicked Witch of the " + dir
}
}
case class Spell(ingredients: List[(Int, Ingredient)], boilingTime: Int)
val spellRecipeGen = for {
ingredientList <- Gen.someOf(slimy, crawly, creepy)
amounts <- Gen.listOfN(ingredientList.length, Gen.choose(1, 10))
bubblesFor <- Gen.posNum[Int].suchThat(_ < 720)
} yield {
Spell(amounts.zip(ingredientList), bubblesFor)
}
forAll { p: Prince =>
princessKiss(turnIntoFrog(p)) == p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment