Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce compilation warnings in tests #4083

Merged
merged 9 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cats.tests

import cats.kernel.{Eq, Order}
import cats.laws.discipline.{ExhaustiveCheck, MiniInt}
import cats.laws.discipline.MiniInt._
import cats.laws.discipline.eq._
import cats.laws.discipline.DeprecatedEqInstances
import cats.kernel.{Eq, Order}
import org.scalacheck.Arbitrary

trait ScalaVersionSpecificFoldableSuite
Expand Down Expand Up @@ -55,6 +55,7 @@ trait ScalaVersionSpecificAlgebraInvariantSuite {
}

// This version-specific instance is required since 2.12 and below do not have parseString on the Numeric class
@annotation.nowarn("cat=deprecation")
implicit protected def eqFractional[A: Eq: Arbitrary]: Eq[Fractional[A]] = {
import DeprecatedEqInstances.catsLawsEqForFn1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cats.tests

class FoldableLazyListSuite extends FoldableSuite[LazyList]("lazyList") {
def iterator[T](list: LazyList[T]): Iterator[T] = list.iterator
}
21 changes: 10 additions & 11 deletions tests/src/test/scala-2.13+/cats/tests/ScalaVersionSpecific.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package cats.tests

import cats.{Eval, Foldable, Id, Now}
import cats._
import cats.data.NonEmptyLazyList
import cats.laws.discipline.{ExhaustiveCheck, MiniInt, NonEmptyParallelTests, ParallelTests}
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.DeprecatedEqInstances
import cats.syntax.either._
import cats.syntax.foldable._
import cats.syntax.parallel._
import cats.syntax.traverse._
import cats.syntax.eq._
import org.scalacheck.Prop._
import cats.kernel.{Eq, Order}
import cats.laws.discipline.ExhaustiveCheck
import cats.laws.discipline.MiniInt
import cats.laws.discipline.NonEmptyParallelTests
import cats.laws.discipline.ParallelTests
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.eq._
import cats.syntax.all._
import org.scalacheck.Arbitrary
import org.scalacheck.Prop._

trait ScalaVersionSpecificFoldableSuite { self: FoldableSuiteAdditional =>
test("Foldable[LazyList] monadic folds stack safety")(checkMonadicFoldsStackSafety(_.to(LazyList)))
Expand Down Expand Up @@ -57,7 +55,7 @@ trait ScalaVersionSpecificFoldableSuite { self: FoldableSuiteAdditional =>
}

test("Foldable[LazyList] laziness of foldM") {
assert(dangerous.foldM(0)((acc, a) => if (a < 2) Some(acc + a) else None) === None)
assert(dangerousLazyList.foldM(0)((acc, a) => if (a < 2) Some(acc + a) else None) === None)
}

def foldableLazyListWithDefaultImpl: Foldable[LazyList] =
Expand Down Expand Up @@ -220,6 +218,7 @@ trait ScalaVersionSpecificAlgebraInvariantSuite {
}

// This version-specific instance is required since 2.12 and below do not have parseString on the Numeric class
@annotation.nowarn("cat=deprecation")
implicit protected def eqFractional[A: Eq: Arbitrary]: Eq[Fractional[A]] = {
// This deprecated instance is required since there is not `ExhaustiveCheck` for any types for which a `Fractional`
// can easily be defined
Expand Down
4 changes: 3 additions & 1 deletion tests/src/test/scala/cats/tests/ChainSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,18 @@ class ChainSuite extends CatsSuite {
test("seq-like pattern match") {
Chain(1, 2, 3) match {
case Chain(a, b, c) => assert((a, b, c) === ((1, 2, 3)))
case other => fail(other.show)
}

Chain(1, 2, 3) match {
case h ==: t => assert((h, t) === 1 -> Chain(2, 3))
case other => fail(other.show)
}

Chain(1, 2, 3) match {
case init :== last => assert((init, last) === Chain(1, 2) -> 3)
case other => fail(other.show)
}

}

test("size is consistent with toList.size") {
Expand Down
57 changes: 31 additions & 26 deletions tests/src/test/scala/cats/tests/FoldableSuite.scala
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package cats.tests

import cats._
import cats.data.{Const, EitherK, IdT, Ior, Nested, NonEmptyList, NonEmptyStream, NonEmptyVector, OneAnd, Validated}
import cats.data._
import cats.instances.order._
import cats.kernel.{Eq, Monoid}
import cats.kernel.compat.scalaVersionSpecific._
import cats.laws.discipline.arbitrary._
import cats.syntax.alternative._
import cats.syntax.either._
import cats.syntax.foldable._
import cats.syntax.functor._
import cats.syntax.list._
import cats.syntax.reducible._
import cats.syntax.semigroupk._
import cats.syntax.all._
import org.scalacheck.Arbitrary
import scala.collection.immutable.{SortedMap, SortedSet}
import scala.util.Try
import cats.syntax.eq._
import org.scalacheck.Prop._

import scala.collection.immutable.SortedMap
import scala.collection.immutable.SortedSet
import scala.util.Try

@suppressUnusedImportWarningForScalaVersionSpecific
abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit
ArbFInt: Arbitrary[F[Int]],
Expand Down Expand Up @@ -168,7 +162,9 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit
// that might still be in use.
//
// https://github.com/typelevel/cats/pull/3278#discussion_r372841693
assert(fa.collectSomeFold(g) === (fa.toList.filter(f).fold(m.empty)(m.combine)))
@annotation.nowarn("cat=deprecation")
val obtained = fa.collectSomeFold(g)
assert(obtained === (fa.toList.filter(f).fold(m.empty)(m.combine)))
}
}

Expand Down Expand Up @@ -399,7 +395,10 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit

}

class FoldableSuiteAdditional extends CatsSuite with ScalaVersionSpecificFoldableSuite {
class FoldableSuiteAdditional
extends CatsSuite
with ScalaVersionSpecificFoldableSuite
with FoldableSuiteAdditionalStreamSpecific {

// exists method written in terms of foldRight
def contains[F[_]: Foldable, A: Eq](as: F[A], goal: A): Eval[Boolean] =
Expand Down Expand Up @@ -524,9 +523,6 @@ class FoldableSuiteAdditional extends CatsSuite with ScalaVersionSpecificFoldabl
test("Foldable[List] monadic folds stack safety")(checkMonadicFoldsStackSafety(_.toList))
test("Foldable[List].slidingN stack safety")(checkSlidingNStackSafety(_.toList))

test("Foldable[Stream] monadic folds stack safety")(checkMonadicFoldsStackSafety(_.toStream))
test("Foldable[Stream].slidingN stack safety")(checkSlidingNStackSafety(_.toStream))

test("Foldable[Vector] monadic folds stack safety")(checkMonadicFoldsStackSafety(_.toVector))
test("Foldable[Vector].slidingN stack safety")(checkSlidingNStackSafety(_.toVector))

Expand Down Expand Up @@ -554,6 +550,20 @@ class FoldableSuiteAdditional extends CatsSuite with ScalaVersionSpecificFoldabl
checkSlidingNStackSafety(xs => NonEmptyVector.fromVectorUnsafe(xs.toVector))
)

test("Foldable[List] doesn't break substitution") {
val result = List.range(0, 10).foldM(List.empty[Int])((accum, elt) => Eval.always(elt :: accum))

assert(result.value == result.value)
}
}

// `Stream` is deprecated since Scala 2.13 therefore all tests involving this type are
// gathered here to suppress deprecation warnings at once.
@annotation.nowarn("cat=deprecation")
sealed trait FoldableSuiteAdditionalStreamSpecific { self: FoldableSuiteAdditional =>
test("Foldable[Stream] monadic folds stack safety")(checkMonadicFoldsStackSafety(_.toStream))
test("Foldable[Stream].slidingN stack safety")(checkSlidingNStackSafety(_.toStream))

test("Foldable[NonEmptyStream] monadic folds stack safety")(
checkMonadicFoldsStackSafety(xs => NonEmptyStream(xs.head, xs.tail: _*))
)
Expand All @@ -565,8 +575,8 @@ class FoldableSuiteAdditional extends CatsSuite with ScalaVersionSpecificFoldabl
val F = Foldable[Stream]
def bomb[A]: A = sys.error("boom")
val dangerous = 0 #:: 1 #:: 2 #:: bomb[Int] #:: Stream.empty
def boom[A]: Stream[A] =
bomb[A] #:: Stream.empty
def boom[A]: Stream[A] = bomb[A] #:: Stream.empty

test("Foldable[Stream] doesn't blow up") {

// doesn't blow up - this also ensures it works for infinite streams.
Expand Down Expand Up @@ -661,12 +671,6 @@ class FoldableSuiteAdditional extends CatsSuite with ScalaVersionSpecificFoldabl
assert((1 #:: boom[Int]).findM[Id](_ > 0) == Some(1))
assert((1 #:: boom[Int]).collectFirstSomeM[Id, Int](Option.apply) == Some(1))
}

test("Foldable[List] doesn't break substitution") {
val result = List.range(0, 10).foldM(List.empty[Int])((accum, elt) => Eval.always(elt :: accum))

assert(result.value == result.value)
}
}

class FoldableListSuite extends FoldableSuite[List]("list") {
Expand All @@ -681,7 +685,8 @@ class FoldableSortedSetSuite extends FoldableSuite[SortedSet]("sortedSet") {
def iterator[T](set: SortedSet[T]): Iterator[T] = set.iterator
}

class FoldableStreamSuite extends FoldableSuite[Stream]("lazyList") {
@annotation.nowarn("cat=deprecation")
class FoldableStreamSuite extends FoldableSuite[Stream]("stream") {
def iterator[T](list: Stream[T]): Iterator[T] = list.iterator
}

Expand Down
20 changes: 10 additions & 10 deletions tests/src/test/scala/cats/tests/IndexedStateTSuite.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package cats.tests

import cats._
import cats.arrow.{Profunctor, Strong}
import cats.data.{EitherT, IndexedStateT, State, StateT}
import cats.kernel.Eq
import cats.laws.discipline._
import cats.arrow.Profunctor
import cats.arrow.Strong
import cats.data.EitherT
import cats.data.IndexedStateT
import cats.data.State
import cats.data.StateT
import cats.laws.discipline.SemigroupalTests.Isomorphisms
import cats.laws.discipline.eq._
import cats.laws.discipline._
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.eq._
import cats.platform.Platform
import cats.syntax.apply._
import cats.syntax.flatMap._
import cats.syntax.traverse._
import cats.syntax.eq._
import cats.syntax.all._
import org.scalacheck.Prop._
import org.scalacheck.Test.Parameters

Expand Down Expand Up @@ -332,7 +332,7 @@ class IndexedStateTSuite extends CatsSuite {
import cats.implicits.catsStdInstancesForOption
forAll { (initial: Int) =>
assert(StateT.fromState(state).run(initial).get === {
val (s, Some(result)) = state.run(initial).value
val (s, Some(result)) = state.run(initial).value: @unchecked // non-exhaustive match warning
(s, result)
})
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/test/scala/cats/tests/KleisliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class KleisliSuite extends CatsSuite {
// 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("")
assert(resL == resV)
assert(resL === resV)
}

test("traverse_ doesn't stack overflow with List + Eval") {
Expand Down
90 changes: 50 additions & 40 deletions tests/src/test/scala/cats/tests/ParallelSuite.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package cats.tests

import cats._
import cats.data._
import cats.data.NonEmptyList.ZipNonEmptyList
import cats.data._
import cats.kernel.compat.scalaVersionSpecific._
import cats.laws.discipline.{ApplicativeErrorTests, MiniInt, NonEmptyParallelTests, ParallelTests, SerializableTests}
import cats.laws.discipline.eq._
import cats.laws.discipline._
import cats.laws.discipline.arbitrary._
import cats.implicits._
import org.scalacheck.{Arbitrary, Gen}
import cats.laws.discipline.eq._
import cats.syntax.all._
import org.scalacheck.Arbitrary
import org.scalacheck.Gen
import org.scalacheck.Prop._

import scala.collection.immutable.SortedSet
import org.scalacheck.Prop._

@suppressUnusedImportWarningForScalaVersionSpecific
class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest with ScalaVersionSpecificParallelSuite {
class ParallelSuite
extends CatsSuite
with ApplicativeErrorForEitherTest
with ScalaVersionSpecificParallelSuite
with ParallelSuiteStreamSpecific {

test("ParSequence Either should accumulate errors") {
forAll { (es: List[Either[String, Int]]) =>
Expand Down Expand Up @@ -351,22 +356,6 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest with Sc
}
}

test("ParMap over Stream should be consistent with zip") {
forAll { (as: Stream[Int], bs: Stream[Int], cs: Stream[Int]) =>
val zipped = as
.zip(bs)
.map { case (a, b) =>
a + b
}
.zip(cs)
.map { case (a, b) =>
a + b
}

assert((as, bs, cs).parMapN(_ + _ + _) === zipped)
}
}

test("ParTupled of NonEmptyList should be consistent with ParMap of Tuple.apply") {
forAll { (fa: NonEmptyList[Int], fb: NonEmptyList[Int], fc: NonEmptyList[Int], fd: NonEmptyList[Int]) =>
assert((fa, fb, fc, fd).parTupled === ((fa, fb, fc, fd).parMapN(Tuple4.apply)))
Expand All @@ -391,12 +380,6 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest with Sc
}
}

test("ParTupled of Stream should be consistent with ParMap of Tuple.apply") {
forAll { (fa: Stream[Int], fb: Stream[Int], fc: Stream[Int], fd: Stream[Int]) =>
assert((fa, fb, fc, fd).parTupled === ((fa, fb, fc, fd).parMapN(Tuple4.apply)))
}
}

test("ParTupled of List should be consistent with zip") {
forAll { (fa: List[Int], fb: List[Int], fc: List[Int], fd: List[Int]) =>
assert((fa, fb, fc, fd).parTupled === fa.zip(fb).zip(fc).zip(fd).map { case (((a, b), c), d) => (a, b, c, d) })
Expand All @@ -409,12 +392,6 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest with Sc
}
}

test("ParTupled of Stream should be consistent with zip") {
forAll { (fa: Stream[Int], fb: Stream[Int], fc: Stream[Int], fd: Stream[Int]) =>
assert((fa, fb, fc, fd).parTupled === fa.zip(fb).zip(fc).zip(fd).map { case (((a, b), c), d) => (a, b, c, d) })
}
}

test("IorT leverages parallel effect instances when it exists") {
case class Marker(value: String) extends java.lang.Exception("marker") {
override def fillInStackTrace: Throwable = null
Expand Down Expand Up @@ -536,16 +513,11 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest with Sc
)
checkAll("NonEmptyParallel[Vector]", NonEmptyParallelTests[Vector].nonEmptyParallel[Int, String])
checkAll("NonEmptyParallel[List]", NonEmptyParallelTests[List].nonEmptyParallel[Int, String])
// Can't test Parallel here, as Applicative[ZipStream].pure doesn't terminate
checkAll("Parallel[Stream]", NonEmptyParallelTests[Stream].nonEmptyParallel[Int, String])

checkAll("NonEmptyParallel[NonEmptyVector]", NonEmptyParallelTests[NonEmptyVector].nonEmptyParallel[Int, String])

checkAll("NonEmptyParallel[NonEmptyList]", NonEmptyParallelTests[NonEmptyList].nonEmptyParallel[Int, String])

// TODO this doesn't infer?
checkAll("Parallel[NonEmptyStream]", ParallelTests[NonEmptyStream, OneAnd[ZipStream, *]].parallel[Int, String])

checkAll("Parallel[Id]", ParallelTests[Id].parallel[Int, String])

checkAll("NonEmptyParallel[NonEmptyList]", SerializableTests.serializable(NonEmptyParallel[NonEmptyList]))
Expand Down Expand Up @@ -573,6 +545,44 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest with Sc
val p1: NonEmptyParallel.Aux[Either[String, *], Validated[String, *]] = NonEmptyParallel[Either[String, *]]
val p2: NonEmptyParallel.Aux[NonEmptyList, ZipNonEmptyList] = NonEmptyParallel[NonEmptyList]
}
}

@annotation.nowarn("cat=deprecation")
sealed trait ParallelSuiteStreamSpecific { self: ParallelSuite =>

test("ParMap over Stream should be consistent with zip") {
forAll { (as: Stream[Int], bs: Stream[Int], cs: Stream[Int]) =>
val zipped = as
.zip(bs)
.map { case (a, b) =>
a + b
}
.zip(cs)
.map { case (a, b) =>
a + b
}

assert((as, bs, cs).parMapN(_ + _ + _) === zipped)
}
}

test("ParTupled of Stream should be consistent with ParMap of Tuple.apply") {
forAll { (fa: Stream[Int], fb: Stream[Int], fc: Stream[Int], fd: Stream[Int]) =>
assert((fa, fb, fc, fd).parTupled === ((fa, fb, fc, fd).parMapN(Tuple4.apply)))
}
}

test("ParTupled of Stream should be consistent with zip") {
forAll { (fa: Stream[Int], fb: Stream[Int], fc: Stream[Int], fd: Stream[Int]) =>
assert((fa, fb, fc, fd).parTupled === fa.zip(fb).zip(fc).zip(fd).map { case (((a, b), c), d) => (a, b, c, d) })
}
}

// Can't test Parallel here, as Applicative[ZipStream].pure doesn't terminate
checkAll("Parallel[Stream]", NonEmptyParallelTests[Stream].nonEmptyParallel[Int, String])

// TODO this doesn't infer?
checkAll("Parallel[NonEmptyStream]", ParallelTests[NonEmptyStream, OneAnd[ZipStream, *]].parallel[Int, String])

test("Parallel.apply should return an appropriately typed instance given both type parameters") {
val p1: Parallel.Aux[Either[String, *], Validated[String, *]] = Parallel[Either[String, *], Validated[String, *]]
Expand Down
Loading