Search code examples
scalascalacheck

scalacheck: Generate a non empty string


What is the best way to generate a non empty string, when in the context of something like this

  private def createIndexFn() = {
      for{
        someChar <-  Gen.alphaString
        aNumber <- Gen.choose(1,100)
        //...
       }       
       yield { 
           MyThing(AnotherType(someChar.toString), SomeOtherType(aNumber), aNumber)
   }
 }

where you need maybe someChar to be a non empty string. I know you can use whenever in the forAll section but I wonder how to do it in this part of the generator.

Thanks


Solution

  • 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))