Skip to content

Commit

Permalink
Better arbitrary for Int => Boolean
Browse files Browse the repository at this point in the history
Default Int => Boolean in scalacheck just returns a function which
always returns the same value (true or false). This means that
operations like dropWhile are not tested accurately.
  • Loading branch information
milanshen committed Feb 3, 2016
1 parent d374a93 commit 6cec86b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/data/Streaming.scala
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,15 @@ sealed abstract class Streaming[A] extends Product with Serializable { lhs =>
*
* For example:
*
* Streaming(1, 2, 3, 4, 5, 6, 7).takeWhile(n => n != 4)
* Streaming(1, 2, 3, 4, 5, 6, 7).dropWhile(n => n != 4)
*
* Will result in: Streaming(4, 5, 6, 7)
*/
def dropWhile(f: A => Boolean): Streaming[A] =
this match {
case Empty() => Empty()
case Wait(lt) => Wait(lt.map(_.dropWhile(f)))
case Cons(a, lt) => if (f(a)) Empty() else Cons(a, lt.map(_.dropWhile(f)))
case Cons(a, lt) => if (f(a)) Wait(lt.map(_.dropWhile(f))) else Cons(a, lt)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/StreamingT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ sealed abstract class StreamingT[F[_], A] extends Product with Serializable { lh
*/
def dropWhile(f: A => Boolean)(implicit ev: Functor[F]): StreamingT[F, A] =
this match {
case Cons(a, ft) => if (f(a)) Empty() else Cons(a, ft.map(_.dropWhile(f)))
case Cons(a, ft) => if (f(a)) Wait(ft.map(_.dropWhile(f))) else Cons(a, ft)
case Wait(ft) => Wait(ft.map(_.dropWhile(f)))
case Empty() => Empty()
}
Expand Down
6 changes: 6 additions & 0 deletions laws/src/main/scala/cats/laws/discipline/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import org.scalacheck.Arbitrary.{arbitrary => getArbitrary}
*/
object arbitrary extends ArbitraryInstances0 {

// A special function1Arbitrary for testing operations like dropWhile specifically
// in the context of Int => Boolean. Once scalacheck supports better Function1 arbitrary
// instances this can be removed.
implicit def function1Arbitrary: Arbitrary[(Int) => Boolean] =
Arbitrary(getArbitrary[Int].map(x => (input) => input < x))

implicit def constArbitrary[A, B](implicit A: Arbitrary[A]): Arbitrary[Const[A, B]] =
Arbitrary(A.arbitrary.map(Const[A, B]))

Expand Down

0 comments on commit 6cec86b

Please sign in to comment.