Question

The ScalaCheck api defines 8 forAll methods for creating properties from function with up to 8 parameters. Is it possible to test a function that has more than 8 parameters?

Était-ce utile?

La solution

This can be done in many ways. Of course you can try to write your own forAll9 implicit, but I advise you to create your own Generator

For example, we have a fun, that receives longtitude, latitude and height, let it be something like

case class Coordinate(longtitude: Int, latitude: Int, height: Int)

Let this case class be the parameter to some fun

teleportToHawaii(coordinate: Coordinate): Coordinate

Now lets write generator for it. Lets assume that long/lat can be -180/+180 and height from 0 to 8000. I'm not too familiar with ScalaCheck, but I guess it should be like

import org.scalacheck.Gen

val coordsGen = for {
  long <- Gen.choose(-180, 180)
  lat <- Gen.choose(-180, 180)
  height <- Gen.choose(0, 8000)
} yield Coordinate(long, lat, height)

So now, we can test our teleport fun, with the usage of just one parameter, instead of three:

forAll (coordsGen) { (coord: Coordinate) =>
  teleportToHawaii(coord) should equal HawaiiCoordinate
}

Writing your own Generator you can incorporate any number of arguments incapsulated in something

So why only 8? I guess guys from scalaCheck were just lazier than those who wrote Tuple22

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top