scalacheck: Generate a non empty string
Another way is to create a fix length random string
Gen.listOfN(10, Gen.asciiPrintableChar).map(_.mkString)
or to create a random length with a constrained length
for {
n <- Gen.chooseNum(5, 20)
chars <- Gen.listOfN(n, Gen.asciiPrintableChar)
} yield chars.mkString
The accepted answer caused a high ratio of discarded tests for me, I ended up using:
import org.scalacheck._
Arbitrary(Gen.nonEmptyListOf[Char](Arbitrary.arbChar.arbitrary)).map(_.mkString))
What I was looking for was:
import org.scalacheck.Arbitrary.arbitrary
arbitrary[String].suchThat(!_.isEmpty)
and can be used like
for {
name <- arbitrary[String].suchThat(!_.isEmpty)
age <- Gen.choose(0, 100)
} yield Person(name, age)
Hopefully this helps someone