Skip to content

Commit

Permalink
Improve intercalate tests after review
Browse files Browse the repository at this point in the history
  • Loading branch information
peterneyens committed Jan 6, 2017
1 parent b859ea3 commit 755f9e9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
6 changes: 5 additions & 1 deletion core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,12 @@ import simulacrum.typeclass
* scala> import cats.implicits._
* scala> Foldable[List].intercalate(List("a","b","c"), "-")
* res0: String = a-b-c
* scala> Foldable[List].intercalate(List("a"), "-")
* res1: String = a
* scala> Foldable[List].intercalate(List.empty[String], "-")
* res2: String = ""
* scala> Foldable[Vector].intercalate(Vector(1,2,3), 1)
* res1: Int = 8
* res3: Int = 8
* }}}
*/
def intercalate[A](fa: F[A], a: A)(implicit A: Monoid[A]): A =
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/Reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ import simulacrum.typeclass
* scala> val nel = NonEmptyList.of("a", "b", "c")
* scala> Reducible[NonEmptyList].intercalate1(nel, "-")
* res0: String = a-b-c
* scala> Reducible[NonEmtpyList].intercalate1(NonEmptyList.of("a"), "-")
* res1: String = a
* }}}
*/
def intercalate1[A](fa: F[A], a: A)(implicit A: Semigroup[A]): A =
Expand Down
8 changes: 3 additions & 5 deletions tests/src/test/scala/cats/tests/FoldableTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.scalacheck.Arbitrary

import cats.instances.all._

abstract class FoldableCheck[F[_]: Foldable](name: String)(implicit ArbFInt: Arbitrary[F[Int]]) extends CatsSuite with PropertyChecks {
abstract class FoldableCheck[F[_]: Foldable](name: String)(implicit ArbFInt: Arbitrary[F[Int]], ArbFString: Arbitrary[F[String]]) extends CatsSuite with PropertyChecks {

def iterator[T](fa: F[T]): Iterator[T]

Expand Down Expand Up @@ -69,10 +69,8 @@ abstract class FoldableCheck[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}

test("intercalate") {
forAll { (fa: F[Int], a: Int) =>
val list = fa.toList
val sum = list.sum + (math.max(list.length - 1, 0) * a)
fa.intercalate(a) should === (sum)
forAll { (fa: F[String], a: String) =>
fa.intercalate(a) should === (fa.toList.mkString(a))
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion tests/src/test/scala/cats/tests/ReducibleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ReducibleTestsAdditional extends CatsSuite {

}

abstract class ReducibleCheck[F[_]: Reducible](name: String)(implicit ArbFInt: Arbitrary[F[Int]]) extends FoldableCheck[F](name) {
abstract class ReducibleCheck[F[_]: Reducible](name: String)(implicit ArbFInt: Arbitrary[F[Int]], ArbFString: Arbitrary[F[String]]) extends FoldableCheck[F](name) {
def range(start: Long, endInclusive: Long): F[Long]

test(s"Reducible[$name].reduceLeftM stack safety") {
Expand All @@ -80,4 +80,10 @@ abstract class ReducibleCheck[F[_]: Reducible](name: String)(implicit ArbFInt: A
fa.toList.toNel should === (Some(fa.toNonEmptyList))
}
}

test(s"Reducible[$name].intercalate1") {
forAll { (fa: F[String], a: String) =>
fa.intercalate1(a) === (fa.toList.mkString(a))
}
}
}

0 comments on commit 755f9e9

Please sign in to comment.