Skip to content

Commit

Permalink
Moved Chunk generators to standalone trait and increased coverage of …
Browse files Browse the repository at this point in the history
…constructors
  • Loading branch information
mpilquist committed Feb 5, 2019
1 parent 8901752 commit e44d6a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
33 changes: 33 additions & 0 deletions core/shared/src/test/scala/fs2/ChunkGen.scala
@@ -0,0 +1,33 @@
package fs2

import cats.data.Chain
import cats.laws.discipline.arbitrary._
import org.scalacheck._
import Arbitrary.arbitrary
import scala.reflect.ClassTag

trait ChunkGen {

implicit def arbChunk[A: ClassTag](implicit A: Arbitrary[A]): Arbitrary[Chunk[A]] = Arbitrary(
Gen.frequency(
1 -> Chunk.empty[A],
5 -> A.arbitrary.map(a => Chunk.singleton(a)),
10 -> Gen.listOf(A.arbitrary).map(as => Chunk.vector(as.toVector)),
10 -> Gen.listOf(A.arbitrary).map(as => Chunk.indexedSeq(as.toIndexedSeq)),
10 -> Gen.listOf(A.arbitrary).map(Chunk.seq),
10 -> arbitrary[Chain[A]].map(Chunk.chain),
10 -> Gen.listOf(A.arbitrary).map(as => Chunk.buffer(collection.mutable.Buffer.empty[A] ++ as)),
10 -> Gen.listOf(A.arbitrary).map(as => Chunk.array(as.toArray)),
10 -> (for {
as <- Gen.listOf(A.arbitrary)
offset <- Gen.chooseNum(0, as.size / 2)
len <- Gen.chooseNum(0, as.size - offset)
} yield Chunk.boxed(as.toArray, offset, len))
)
)

implicit def cogenChunk[A: Cogen]: Cogen[Chunk[A]] =
Cogen[List[A]].contramap(_.toList)
}

object ChunkGen extends ChunkGen
14 changes: 1 addition & 13 deletions core/shared/src/test/scala/fs2/TestUtil.scala
Expand Up @@ -10,7 +10,7 @@ import scala.concurrent.duration._
import cats.effect.IO
import cats.implicits._

object TestUtil extends TestUtilPlatform {
object TestUtil extends TestUtilPlatform with ChunkGen {

def runLogF[A](s: Stream[IO,A]): Future[Vector[A]] = (IO.shift(executionContext) >> s.compile.toVector).unsafeToFuture

Expand All @@ -27,18 +27,6 @@ object TestUtil extends TestUtilPlatform {
case NonFatal(e) => ()
}

implicit def arbChunk[A](implicit A: Arbitrary[A]): Arbitrary[Chunk[A]] = Arbitrary(
Gen.frequency(
10 -> Gen.listOf(A.arbitrary).map(as => Chunk.indexedSeq(as.toVector)),
10 -> Gen.listOf(A.arbitrary).map(Chunk.seq),
5 -> A.arbitrary.map(a => Chunk.singleton(a)),
1 -> Chunk.empty[A]
)
)

implicit def cogenChunk[A: Cogen]: Cogen[Chunk[A]] =
Cogen[List[A]].contramap(_.toList)

/** Newtype for generating test cases. Use the `tag` for labeling properties. */
case class PureStream[+A](tag: String, get: Stream[Pure,A])
implicit def arbPureStream[A:Arbitrary] = Arbitrary(PureStream.gen[A])
Expand Down

0 comments on commit e44d6a9

Please sign in to comment.