Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Arbitrary StreamingT more arbitrary #649

Merged
merged 1 commit into from
Nov 15, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions laws/src/main/scala/cats/laws/discipline/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,35 @@ object arbitrary {
implicit def streamingArbitrary[A](implicit A: Arbitrary[A]): Arbitrary[Streaming[A]] =
Arbitrary(Gen.listOf(A.arbitrary).map(Streaming.fromList(_)))

// TODO: it would be better to do this recursively, i.e. more like:
//
// Gen.oneOf(
// for { a <- arbitrary[A]; s <- arbitrary[F[StreamingT[F, A]]] } yield cons(a, s),
// for { s <- arbitrary[F[StreamingT[F, A]]] } yield wait(s),
// const(StreamingT.empty[F, A]))
//
// However, getting this right with Scalacheck (and avoiding SOEs) is
// somewhat fiddly, so this will have to do for now.
//
def emptyStreamingTGen[F[_], A]: Gen[StreamingT[F, A]] =
Gen.const(StreamingT.empty[F, A])

def streamingTGen[F[_], A](maxDepth: Int)(implicit F: Monad[F], A: Arbitrary[A]): Gen[StreamingT[F, A]] = {
if (maxDepth <= 1)
emptyStreamingTGen[F, A]
else {
Gen.oneOf(
// Empty
emptyStreamingTGen[F, A],
// Wait
streamingTGen[F, A](maxDepth - 1).map(s =>
StreamingT.wait(F.pure(s))),
// Cons
for {
a <- A.arbitrary
s <- streamingTGen[F, A](maxDepth - 1)
} yield StreamingT.cons(a, F.pure(s))
)
}
}

// The max possible size of a StreamingT instance (n) will result in
// instances of up to n^3 in length when testing flatMap
// composition. The current value (8) could result in streams of up
// to 512 elements in length. Thus, since F may not be stack-safe,
// we want to keep n relatively small.
implicit def streamingTArbitrary[F[_], A](implicit F: Monad[F], A: Arbitrary[A]): Arbitrary[StreamingT[F, A]] =
Arbitrary(for {
as <- Gen.listOf(A.arbitrary).map(_.take(8))
} yield StreamingT.fromList(as))
Arbitrary(streamingTGen[F, A](8))

implicit def writerTArbitrary[F[_], L, V](implicit F: Arbitrary[F[(L, V)]]): Arbitrary[WriterT[F, L, V]] =
Arbitrary(F.arbitrary.map(WriterT(_)))
Expand Down