defining a simple implicit Arbitary
- by FredOverflow
I have a type Foo with a constructor that takes an Int. How do I define an implicit Arbitrary for Foo to be used with scalacheck?
implicit def arbFoo: Arbitrary[Foo] = ???
I came up with the following solution, but it's a bit too "manual" and low-level for my taste:
val fooGen = for (i <- Gen.choose(Int.MinValue, Int.MaxValue)) yield new Foo(i)
implicit def arbFoo: Arbitrary[Foo] = Arbitrary(fooGen)
Ideally, I would want a higher-order function where I just have to plug in an Int => Foo function.
I managed to cut it down to:
implicit def arbFoo = Arbitrary(Gen.resultOf((i: Int) => new Foo(i)))
But I still feel like there has got to be a slightly simpler way.