Skip to content

Instantly share code, notes, and snippets.

@Chandler
Created November 18, 2015 19:21
Show Gist options
  • Save Chandler/6fc83730f82c627ba3f7 to your computer and use it in GitHub Desktop.
Save Chandler/6fc83730f82c627ba3f7 to your computer and use it in GitHub Desktop.
intro to scalacheck gens
import org.scalacheck.Gen
//Gen a long
val genLong = Gen.posNum[Long]
// Gen a string
val genString = Gen.alphaChar
//Gen random length list of longs
val genList = Gen.listOf(Gen.posNum[Long])
// Gen from a list of hard coded strings
val genFriendName =
Gen.oneOf("chrissy", "chandler", "mike", "sasha", "alex")
case class Friend(age: Long, name: String)
val genFriend =
for {
age <- Gen.posNum[Long]
name <- genFriendName
} yield { Friend(age, name) }
scala> genLong.sample
res6: Option[Long] = Some(48)
scala> genString.sample
res7: Option[Char] = Some(r)
scala> genList.sample
res8: Option[List[Long]] = Some(List(1, 71, 99, 6, 53, 65, 18, 68, 13, 70, 5, 95, 51, 61, 51, 47, 57, 30, 81, 26, 41, 18, 15, 68, 96))
scala> genFriendName.sample
res9: Option[String] = Some(chrissy)
scala> genFriend.sample
res10: Option[Friend] = Some(Friend(75,alex))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment