defining a simple implicit Arbitary
Posted
by
FredOverflow
on Stack Overflow
See other posts from Stack Overflow
or by FredOverflow
Published on 2012-04-03T10:55:00Z
Indexed on
2012/04/03
11:29 UTC
Read the original article
Hit count: 249
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.
© Stack Overflow or respective owner