Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ScalaCheck Arbitrary[T] and Scalacheck Gen[T]

In my tests I am making quite an extensive usage of Specs2 + ScalaCheck and there are some patterns to factor out. I still haven't found out if my functions should use an Arbitrary[T] or a Gen[T], since they are very similar:

sealed abstract class Arbitrary[T] {
  val arbitrary: Gen[T]
}

Would a function signature looks like that:

maxSizedIntervalArbitrary[A,B](implicit ordering:Ordering[A], genStart:Arbitrary[A], genEnd:Arbitrary[B]):Arbitrary[TreeMap[A,B]]

or should I work at the Gen abstraction level?

like image 633
Edmondo1984 Avatar asked Feb 24 '14 08:02

Edmondo1984


1 Answers

I'd say do both:

def maxSizedIntervalArbitrary[A,B](genStart:Gen[A], genEnd:Gen[B])(implicit ordering:Ordering[A]):Gen[TreeMap[A,B]]

implicit def maxSizedIntervalArbitrary[A,B](implicit ordering:Ordering[A], genStart:Arbitrary[A], genEnd:Arbitrary[B]):Arbitrary[TreeMap[A,B]] = 
  Arbitrary(maxSizedIntervalArbitrary(arbitrary[A], arbitrary[B]))

Arbitrary is used to supply implicit Gens, basically, so this allows to use both forAll variants with explicit Gen and with implicit Arbitrary. I don't think non-implicit Arbitrary is ever useful.

like image 128
Alexey Romanov Avatar answered Oct 11 '22 09:10

Alexey Romanov