Skip to content

Commit

Permalink
Merge pull request #726 from ceedubs/moar-streamingt-tests
Browse files Browse the repository at this point in the history
Add some StreamingT tests
  • Loading branch information
adelbertc committed Dec 8, 2015
2 parents 48286f2 + 44a560c commit 291568c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/cats/data/StreamingT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ object StreamingT extends StreamingTInstances {
Cons(a, fs)

/**
* Create a stream from an `F[StreamingT[F, A]]` value.
* Create a stream from a deferred `StreamingT[F, A]` value.
* Note: the extent to which this defers the value depends on the `pureEval`
* implementation of the `Applicative[F]` instance.
*/
def defer[F[_], A](s: => StreamingT[F, A])(implicit ev: Applicative[F]): StreamingT[F, A] =
Wait(ev.pureEval(Always(s)))
Expand Down
25 changes: 24 additions & 1 deletion tests/src/test/scala/cats/tests/StreamingTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests

import algebra.laws.OrderLaws

import cats.data.StreamingT
import cats.data.{Streaming, StreamingT}
import cats.laws.discipline.{CoflatMapTests, MonadCombineTests, SerializableTests}
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.eq._
Expand Down Expand Up @@ -147,6 +147,29 @@ class StreamingTTests extends CatsSuite {
}
}

test("unfold with Id consistent with Streaming.unfold") {
forAll { (o: Option[Long]) =>
val f: Long => Option[Long] = { x =>
val rng = new scala.util.Random(x)
if (rng.nextBoolean) Some(rng.nextLong)
else None
}

StreamingT.unfold[Id, Long](o)(f).toList should === (Streaming.unfold(o)(f).toList)
}
}

test("defer produces the same values") {
forAll { (xs: StreamingT[Option, Int]) =>
StreamingT.defer(xs) should === (xs)
}
}

test("defer isn't eager if the pureEval impl isn't") {
def s: StreamingT[Eval, Int] = throw new RuntimeException("blargh")
val x = StreamingT.defer[Eval, Int](s)
}

test("fromVector") {
forAll { (xs: Vector[Int]) =>
StreamingT.fromVector[Id, Int](xs).toList.toVector should === (xs)
Expand Down

0 comments on commit 291568c

Please sign in to comment.