Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satorg committed Nov 28, 2024
1 parent 40548ac commit a1b23d3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ trait ScalaVersionSpecificRegressionSuite { self: RegressionSuite =>
// shouldn't have ever evaluated validate(8)
checkAndResetCount(3)

assert(LazyList(1, 2, 6, 8).traverse_(validate) === (Either.left("6 is greater than 5")))
assert(LazyList(1, 2, 6, 8).traverseVoid(validate) === Either.left("6 is greater than 5"))
checkAndResetCount(3)
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/shared/src/test/scala/cats/tests/KleisliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,16 @@ class KleisliSuite extends CatsSuite {
assertEquals(program.run(A123), List((1, "2", true)))
}

test("traverse_ doesn't stack overflow") {
test("traverseVoid doesn't stack overflow") {
// see: https://github.com/typelevel/cats/issues/3947
val resL = (1 to 10000).toList.traverse_(_ => Kleisli.liftF[Id, String, Unit](())).run("")
val resV = (1 to 10000).toVector.traverse_(_ => Kleisli.liftF[Id, String, Unit](())).run("")
val resL = (1 to 10000).toList.traverseVoid(_ => Kleisli.liftF[Id, String, Unit](())).run("")
val resV = (1 to 10000).toVector.traverseVoid(_ => Kleisli.liftF[Id, String, Unit](())).run("")
assert(resL === resV)
}

test("traverse_ doesn't stack overflow with List + Eval") {
test("traverseVoid doesn't stack overflow with List + Eval") {
// see: https://github.com/typelevel/cats/issues/3947
(1 to 10000).toList.traverse_(_ => Kleisli.liftF[Eval, String, Unit](Eval.Unit)).run("").value
(1 to 10000).toList.traverseVoid(_ => Kleisli.liftF[Eval, String, Unit](Eval.Unit)).run("").value
}

/**
Expand Down
22 changes: 11 additions & 11 deletions tests/shared/src/test/scala/cats/tests/ParallelSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,24 @@ class ParallelSuite
}
}

test("ParTraverse_ identity should be equivalent to parSequence_") {
test("ParTraverseVoid identity should be equivalent to parSequenceVoid") {
forAll { (es: SortedSet[Either[String, Int]]) =>
assert(Parallel.parTraverse_(es)(identity) === (Parallel.parSequence_[SortedSet, Either[String, *], Int](es)))
assert(Parallel.parTraverseVoid(es)(identity) === Parallel.parSequenceVoid[SortedSet, Either[String, *], Int](es))
}
}

test("ParTraverse_ syntax should be equivalent to Parallel.parTraverse_") {
test("ParTraverseVoid syntax should be equivalent to Parallel.parTraverseVoid") {
forAll { (es: SortedSet[Either[String, Int]]) =>
assert(
Parallel.parTraverse_[SortedSet, Either[String, *], Either[String, Int], Int](es)(identity) === (es
.parTraverse_(identity))
Parallel.parTraverseVoid[SortedSet, Either[String, *], Either[String, Int], Int](es)(identity) ===
es.parTraverseVoid(identity)
)
}
}

test("ParSequence_ syntax should be equivalent to Parallel.parSequence_") {
test("ParSequenceVoid syntax should be equivalent to Parallel.parSequenceVoid") {
forAll { (es: SortedSet[Either[String, Int]]) =>
assert(Parallel.parSequence_[SortedSet, Either[String, *], Int](es) === (es.parSequence_))
assert(Parallel.parSequenceVoid[SortedSet, Either[String, *], Int](es) === es.parSequenceVoid)
}
}

Expand All @@ -105,9 +105,9 @@ class ParallelSuite
}
}

test("ParNonEmptyTraverse_ identity should be equivalent to parNonEmptySequence_") {
test("ParNonEmptyTraverseVoid identity should be equivalent to parNonEmptySequenceVoid") {
forAll { (es: NonEmptyList[Either[String, Int]]) =>
assert(Parallel.parNonEmptyTraverse_(es)(identity) === (Parallel.parNonEmptySequence_(es)))
assert(Parallel.parNonEmptyTraverseVoid(es)(identity) === Parallel.parNonEmptySequenceVoid(es))
}
}

Expand Down Expand Up @@ -310,10 +310,10 @@ class ParallelSuite
}
}

test("ParReplicateA_ should be equivalent to fill parSequence_") {
test("ParReplicateA_ should be equivalent to fill parSequenceVoid") {
forAll(Gen.choose(1, 20), Arbitrary.arbitrary[Either[String, String]]) {
(repetitions: Int, e: Either[String, String]) =>
assert(Parallel.parReplicateA_(repetitions, e) === Parallel.parSequence_(List.fill(repetitions)(e)))
assert(Parallel.parReplicateA_(repetitions, e) === Parallel.parSequenceVoid(List.fill(repetitions)(e)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/cats/tests/ReducibleSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ abstract class ReducibleSuite[F[_]: Reducible](name: String)(implicit
assert(out.toList === List(2, 4, 6, 9))
}

test(s"Reducible[$name].nonEmptyTraverse_ can breakout") {
test(s"Reducible[$name].nonEmptyTraverseVoid can breakout") {
val notAllEven = fromValues(2, 4, 6, 9, 10, 12, 14)
val out = mutable.ListBuffer[Int]()

notAllEven.nonEmptyTraverse_ { a => out += a; if (a % 2 == 0) Some(a) else None }
notAllEven.nonEmptyTraverseVoid { a => out += a; if (a % 2 == 0) Some(a) else None }

assert(out.toList === List(2, 4, 6, 9))
}
Expand Down
10 changes: 5 additions & 5 deletions tests/shared/src/test/scala/cats/tests/RegressionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,23 @@ class RegressionSuite extends CatsSuite with ScalaVersionSpecificRegressionSuite
assert(Vector(1, 2, 6, 8).traverse(validate) === (Either.left("6 is greater than 5")))
checkAndResetCount(3)

assert(List(1, 2, 6, 8).traverse_(validate) === (Either.left("6 is greater than 5")))
assert(List(1, 2, 6, 8).traverseVoid(validate) === Either.left("6 is greater than 5"))
checkAndResetCount(3)

{
@annotation.nowarn("cat=deprecation")
val obtained = Stream(1, 2, 6, 8).traverse_(validate)
val obtained = Stream(1, 2, 6, 8).traverseVoid(validate)
assert(obtained === Either.left("6 is greater than 5"))
}
checkAndResetCount(3)

assert(Vector(1, 2, 6, 8).traverse_(validate) === (Either.left("6 is greater than 5")))
assert(Vector(1, 2, 6, 8).traverseVoid(validate) === Either.left("6 is greater than 5"))
checkAndResetCount(3)

assert(NonEmptyList.of(1, 2, 6, 7, 8).traverse_(validate) === (Either.left("6 is greater than 5")))
assert(NonEmptyList.of(1, 2, 6, 7, 8).traverseVoid(validate) === Either.left("6 is greater than 5"))
checkAndResetCount(3)

assert(NonEmptyList.of(6, 7, 8).traverse_(validate) === (Either.left("6 is greater than 5")))
assert(NonEmptyList.of(6, 7, 8).traverseVoid(validate) === Either.left("6 is greater than 5"))
checkAndResetCount(1)
}

Expand Down
27 changes: 19 additions & 8 deletions tests/shared/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import cats.data.{
ValidatedNec,
ValidatedNel
}
import cats.syntax.OptionOps
import cats.syntax.all._

import scala.collection.immutable.{SortedMap, SortedSet}
Expand Down Expand Up @@ -155,11 +154,11 @@ object SyntaxSuite {
val a1: A = fz.foldMap(f3)

val f4 = mock[A => G[B]]
val gu0: G[Unit] = fa.traverse_(f4)
val gu0: G[Unit] = fa.traverseVoid(f4)

val fga = mock[F[G[A]]]
val gu1: G[Unit] = fga.sequence_
val ga: G[A] = fga.foldK
val gu1: G[Unit] = fga.sequenceVoid // NestedFoldableOps
val ga1: G[A] = fga.foldK // NestedFoldableOps

val f5 = mock[A => Boolean]
val oa: Option[A] = fa.find(f5)
Expand Down Expand Up @@ -201,7 +200,19 @@ object SyntaxSuite {
val gunit: G[F[A]] = fga.nonEmptySequence
}

def testParallel[M[_]: Parallel, T[_]: Traverse, A, B]: Unit = {
def testParallelFoldable[M[_]: Parallel, T[_]: Foldable, A, B](): Unit = {
val ta = mock[T[A]]
val tma = mock[T[M[A]]]
val famb = mock[A => M[B]]

val mu1 = ta.parTraverseVoid(famb)
val mu2 = tma.parSequenceVoid

// Suppress "unused local val" warnings and make sure the types were inferred correctly.
val _ = (mu1: M[Unit], mu2: M[Unit])
}

def testParallelTraverse[M[_]: Parallel, T[_]: Traverse, A, B](): Unit = {
val ta = mock[T[A]]
val f = mock[A => M[B]]
val mtb = ta.parTraverse(f)
Expand Down Expand Up @@ -348,7 +359,7 @@ object SyntaxSuite {
val mtab2 = tmab.parLeftSequence
}

def testParallelFoldable[T[_]: Foldable, M[_]: Parallel, A, B: Monoid]: Unit = {
def testParallelFoldableMonoid[T[_]: Foldable, M[_]: Parallel, A, B: Monoid](): Unit = {
val ta = mock[T[A]]
val f = mock[A => M[B]]
val mb = ta.parFoldMapA(f)
Expand Down Expand Up @@ -379,9 +390,9 @@ object SyntaxSuite {
val lb: Eval[B] = fa.reduceRightTo(f4)(f6)

val f7 = mock[A => G[B]]
val gu1: G[Unit] = fa.nonEmptyTraverse_(f7)
val gu1: G[Unit] = fa.nonEmptyTraverseVoid(f7)

val gu2: G[Unit] = fga.nonEmptySequence_
val gu2: G[Unit] = fga.nonEmptySequenceVoid
}

def testFunctor[F[_]: Functor, A, B]: Unit = {
Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/cats/tests/TraverseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ abstract class TraverseSuite[F[_]: Traverse](name: String)(implicit ArbFInt: Arb
}
}

test(s"Traverse[$name].traverse matches traverse_ with Option") {
test(s"Traverse[$name].traverse matches traverseVoid with Option") {
forAll { (fa: F[Int], fn: Int => Option[Int]) =>
assert(Applicative[Option].void(fa.traverse(fn)) == fa.traverse_(fn))
assert(Applicative[Option].void(fa.traverse(fn)) == fa.traverseVoid(fn))
}
}

Expand Down

0 comments on commit a1b23d3

Please sign in to comment.