Guillaume Chérel
Guillaume Chérel

Reputation: 1518

How to write a test with 2 parameters when the generation of the second depends on the 1st?

How can I write a generator for the second argument someBoundedInt which will generate an Int randomly between the values generated for minmaxBound?

val boundedIntProperty = forAll {
  (minmaxBound: (Int,Int), someBoundedInt: Int) => 
    minmaxBound._1 <= someBoundedInt && someBoundedInt <= minmaxBound._2

}

Upvotes: 2

Views: 370

Answers (1)

Rickard Nilsson
Rickard Nilsson

Reputation: 1443

You can nest calls to forAll like this:

val boundedIntProperty = forAll { (minBound: Int, maxBound: Int) =>
  forAll( Gen.choose(minBound, maxBound) ) { someBoundedInt =>
    ...
  }
}

Note that above, minBound can be larger than maxBound sometimes, which will make Gen.choose fail (not produce a value). So you probably want to generate your bounds in a smarter way too.

Upvotes: 6

Related Questions