Skip to content

Commit f0a747b

Browse files
committed
Add tests and run prePR
1 parent cf96a0c commit f0a747b

File tree

13 files changed

+199
-14
lines changed

13 files changed

+199
-14
lines changed

core/src/main/scala/cats/instances/try.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,31 @@ trait TryInstances extends TryInstances1 {
122122

123123
override def as[A, B](ta: Try[A], b: B): Try[B] =
124124
ta match {
125-
case Success(_) => Success(b)
125+
case Success(_) => Success(b)
126126
case err: Failure[?] => castFailure[B](err)
127127
}
128128

129129
override def tupleLeft[A, B](ta: Try[A], b: B): Try[(B, A)] =
130130
ta match {
131-
case Success(a) => Success((b, a))
131+
case Success(a) => Success((b, a))
132132
case err: Failure[?] => castFailure[(B, A)](err)
133133
}
134134

135135
override def tupleRight[A, B](ta: Try[A], b: B): Try[(A, B)] =
136136
ta match {
137-
case Success(a) => Success((a, b))
137+
case Success(a) => Success((a, b))
138138
case err: Failure[?] => castFailure[(A, B)](err)
139139
}
140140

141141
override def fproduct[A, B](ta: Try[A])(f: A => B): Try[(A, B)] =
142142
ta match {
143-
case Success(a) => Success((a, f(a)))
143+
case Success(a) => Success((a, f(a)))
144144
case err: Failure[?] => castFailure[(A, B)](err)
145145
}
146146

147147
override def fproductLeft[A, B](ta: Try[A])(f: A => B): Try[(B, A)] =
148148
ta match {
149-
case Success(a) => Success((f(a), a))
149+
case Success(a) => Success((f(a), a))
150150
case err: Failure[?] => castFailure[(B, A)](err)
151151
}
152152

@@ -198,7 +198,7 @@ trait TryInstances extends TryInstances1 {
198198
override def unzip[A, B](fab: Try[(A, B)]): (Try[A], Try[B]) =
199199
fab match {
200200
case Success((a, b)) => (Success(a), Success(b))
201-
case err: Failure[?] => (castFailure[A](err), castFailure[B](err))
201+
case err: Failure[?] => (castFailure[A](err), castFailure[B](err))
202202
}
203203

204204
override def isEmpty[A](fa: Try[A]): Boolean = fa.isFailure

tests/shared/src/test/scala/cats/tests/EitherSuite.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ class EitherSuite extends CatsSuite {
119119
}
120120
}
121121

122+
test("functor default methods match map-based implementations") {
123+
val F = Functor[Either[Int, *]]
124+
forAll { (fa: Either[Int, String], b: Long, f: String => Double) =>
125+
assert(F.as(fa, b) === F.map(fa)(_ => b))
126+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
127+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
128+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
129+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
130+
assert(F.void(fa) === F.map(fa)(_ => ()))
131+
}
132+
forAll { (fab: Either[Int, (String, Long)]) =>
133+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
134+
}
135+
}
136+
122137
test("map2Eval is lazy") {
123138
val bomb: Eval[Either[String, Int]] = Later(sys.error("boom"))
124139
val x: Either[String, Int] = Left("l")

tests/shared/src/test/scala/cats/tests/IorSuite.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package cats.tests
2323

24-
import cats.{Bitraverse, MonadError, Semigroupal, Show, Traverse}
24+
import cats.{Bitraverse, Functor, MonadError, Semigroupal, Show, Traverse}
2525
import cats.data.{EitherT, Ior, NonEmptyChain, NonEmptyList, NonEmptySet, NonEmptyVector}
2626
import cats.kernel.{Eq, Semigroup}
2727
import cats.kernel.laws.discipline.{OrderTests, SemigroupTests}
@@ -77,6 +77,21 @@ class IorSuite extends CatsSuite {
7777
}
7878
}
7979

80+
test("functor default methods match map-based implementations") {
81+
val F = Functor[Ior[String, *]]
82+
forAll { (fa: Ior[String, Int], b: String, f: Int => Long) =>
83+
assert(F.as(fa, b) === F.map(fa)(_ => b))
84+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
85+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
86+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
87+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
88+
assert(F.void(fa) === F.map(fa)(_ => ()))
89+
}
90+
forAll { (fab: Ior[String, (Int, String)]) =>
91+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
92+
}
93+
}
94+
8095
test("onlyLeftOrRight") {
8196
forAll { (i: Int Ior String) =>
8297
assert(i.onlyLeft.map(Left(_)).orElse(i.onlyRight.map(Right(_))) === (i.onlyLeftOrRight))

tests/shared/src/test/scala/cats/tests/ListSuite.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package cats.tests
2323

24-
import cats.{Align, Alternative, CoflatMap, Eval, Monad, Semigroupal, Traverse, TraverseFilter}
24+
import cats.{Align, Alternative, CoflatMap, Eval, Functor, Monad, Semigroupal, Traverse, TraverseFilter}
2525
import cats.data.{NonEmptyList, ZipList}
2626
import cats.laws.discipline.{
2727
AlignTests,
@@ -81,6 +81,21 @@ class ListSuite extends CatsSuite {
8181
assert(List.empty[Int].toNel === None)
8282
}
8383

84+
test("functor default methods match map-based implementations") {
85+
val F = Functor[List]
86+
forAll { (fa: List[Int], b: String, f: Int => Long) =>
87+
assert(F.as(fa, b) === F.map(fa)(_ => b))
88+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
89+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
90+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
91+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
92+
assert(F.void(fa) === F.map(fa)(_ => ()))
93+
}
94+
forAll { (fab: List[(Int, String)]) =>
95+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
96+
}
97+
}
98+
8499
test("groupByNel should be consistent with groupBy")(
85100
forAll { (fa: List[Int], f: Int => Int) =>
86101
assert((fa.groupByNel(f).map { case (k, v) => (k, v.toList) }: Map[Int, List[Int]]) === fa.groupBy(f))

tests/shared/src/test/scala/cats/tests/MapSuite.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package cats.tests
2323

24-
import cats.{Align, Eval, FlatMap, FunctorFilter, MonoidK, Semigroupal, Show, UnorderedTraverse}
24+
import cats.{Align, Eval, FlatMap, Functor, FunctorFilter, MonoidK, Semigroupal, Show, UnorderedTraverse}
2525
import cats.arrow.Compose
2626
import cats.kernel.{CommutativeMonoid, Monoid}
2727
import cats.kernel.instances.StaticMethods.wrapMutableMap
@@ -81,6 +81,21 @@ class MapSuite extends CatsSuite {
8181
}
8282
}
8383

84+
test("functor default methods match map-based implementations") {
85+
val F = Functor[Map[Int, *]]
86+
forAll { (fa: Map[Int, Int], b: String, f: Int => Long) =>
87+
assert(F.as(fa, b) === F.map(fa)(_ => b))
88+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
89+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
90+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
91+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
92+
assert(F.void(fa) === F.map(fa)(_ => ()))
93+
}
94+
forAll { (fab: Map[Int, (Int, String)]) =>
95+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
96+
}
97+
}
98+
8499
{
85100
val m = wrapMutableMap(scala.collection.mutable.Map(1 -> "one", 2 -> "two"))
86101
checkAll("WrappedMutableMap", SerializableTests.serializable(m))

tests/shared/src/test/scala/cats/tests/OptionSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import cats.{
3333
CoflatMap,
3434
CommutativeMonad,
3535
Eval,
36+
Functor,
3637
Later,
3738
MonadError,
3839
Semigroupal,
@@ -76,6 +77,21 @@ class OptionSuite extends CatsSuite {
7677
}
7778
}
7879

80+
test("functor default methods match map-based implementations") {
81+
val F = Functor[Option]
82+
forAll { (fa: Option[Int], b: String, f: Int => Long) =>
83+
assert(F.as(fa, b) === F.map(fa)(_ => b))
84+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
85+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
86+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
87+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
88+
assert(F.void(fa) === F.map(fa)(_ => ()))
89+
}
90+
forAll { (fab: Option[(Int, String)]) =>
91+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
92+
}
93+
}
94+
7995
// The following tests check laws which are a different formulation of
8096
// laws that are checked. Since these laws are more or less duplicates of
8197
// existing laws, we don't check them for all types that have the relevant

tests/shared/src/test/scala/cats/tests/QueueSuite.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package cats.tests
2323

24-
import cats.{Alternative, CoflatMap, Eval, Monad, Semigroupal, Traverse, TraverseFilter}
24+
import cats.{Alternative, CoflatMap, Eval, Functor, Monad, Semigroupal, Traverse, TraverseFilter}
2525
import cats.laws.discipline.{
2626
AlternativeTests,
2727
CoflatMapTests,
@@ -36,6 +36,7 @@ import cats.laws.discipline.arbitrary.*
3636
import cats.syntax.show.*
3737
import scala.collection.immutable.Queue
3838
import cats.syntax.eq.*
39+
import org.scalacheck.Prop.forAll
3940

4041
class QueueSuite extends CatsSuite {
4142
checkAll("Queue[Int]", SemigroupalTests[Queue].semigroupal[Int, Int, Int])
@@ -66,6 +67,21 @@ class QueueSuite extends CatsSuite {
6667
assert(Queue.empty[Int].show === "Queue()")
6768
}
6869

70+
test("functor default methods match map-based implementations") {
71+
val F = Functor[Queue]
72+
forAll { (fa: Queue[Int], b: String, f: Int => Long) =>
73+
assert(F.as(fa, b) === F.map(fa)(_ => b))
74+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
75+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
76+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
77+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
78+
assert(F.void(fa) === F.map(fa)(_ => ()))
79+
}
80+
forAll { (fab: Queue[(Int, String)]) =>
81+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
82+
}
83+
}
84+
6985
test("traverse is stack-safe") {
7086
val queue = (0 until 100000).foldLeft(Queue.empty[Int])(_ :+ _)
7187
val sumAll = Traverse[Queue]

tests/shared/src/test/scala/cats/tests/SeqSuite.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package cats.tests
2323

24-
import cats.{Align, Alternative, CoflatMap, Eval, Monad, Semigroupal, Traverse, TraverseFilter}
24+
import cats.{Align, Alternative, CoflatMap, Eval, Functor, Monad, Semigroupal, Traverse, TraverseFilter}
2525
import cats.data.ZipSeq
2626
import cats.laws.discipline.{
2727
AlignTests,
@@ -76,6 +76,21 @@ class SeqSuite extends CatsSuite {
7676
}
7777
}
7878

79+
test("functor default methods match map-based implementations") {
80+
val F = Functor[Seq]
81+
forAll { (fa: Seq[Int], b: String, f: Int => Long) =>
82+
assert(F.as(fa, b) === F.map(fa)(_ => b))
83+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
84+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
85+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
86+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
87+
assert(F.void(fa) === F.map(fa)(_ => ()))
88+
}
89+
forAll { (fab: Seq[(Int, String)]) =>
90+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
91+
}
92+
}
93+
7994
test("traverse is stack-safe") {
8095
val seq = (0 until 100000).toSeq
8196
val sumAll = Traverse[Seq]

tests/shared/src/test/scala/cats/tests/SortedMapSuite.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package cats.tests
2323

24-
import cats.{Align, FlatMap, MonoidK, Semigroupal, Show, Traverse, TraverseFilter}
24+
import cats.{Align, FlatMap, Functor, MonoidK, Semigroupal, Show, Traverse, TraverseFilter}
2525
import cats.kernel.{CommutativeMonoid, Monoid}
2626
import cats.kernel.laws.discipline.{CommutativeMonoidTests, HashTests, MonoidTests}
2727
import cats.laws.discipline.{
@@ -73,6 +73,21 @@ class SortedMapSuite extends CatsSuite {
7373
}
7474
}
7575

76+
test("functor default methods match map-based implementations") {
77+
val F = Functor[SortedMap[Int, *]]
78+
forAll { (fa: SortedMap[Int, Int], b: String, f: Int => Long) =>
79+
assert(F.as(fa, b) === F.map(fa)(_ => b))
80+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
81+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
82+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
83+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
84+
assert(F.void(fa) === F.map(fa)(_ => ()))
85+
}
86+
forAll { (fab: SortedMap[Int, (Int, String)]) =>
87+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
88+
}
89+
}
90+
7691
checkAll("Hash[SortedMap[Int, String]]", HashTests[SortedMap[Int, String]].hash)
7792
checkAll("CommutativeMonoid[SortedMap[String, Int]]",
7893
CommutativeMonoidTests[SortedMap[String, Int]].commutativeMonoid

tests/shared/src/test/scala/cats/tests/TrySuite.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package cats.tests
2323

24-
import cats.{CoflatMap, Eval, Later, Monad, MonadThrow, Semigroupal, Traverse}
24+
import cats.{CoflatMap, Eval, Functor, Later, Monad, MonadThrow, Semigroupal, Traverse}
2525
import cats.kernel.{Eq, Monoid, Semigroup}
2626
import cats.kernel.laws.discipline.{MonoidTests, SemigroupTests}
2727
import cats.laws.{ApplicativeLaws, CoflatMapLaws, FlatMapLaws, MonadLaws}
@@ -109,6 +109,21 @@ class TrySuite extends CatsSuite {
109109
}
110110
}
111111

112+
test("functor default methods match map-based implementations") {
113+
val F = Functor[Try]
114+
forAll { (fa: Try[Int], b: String, f: Int => Long) =>
115+
assert(F.as(fa, b) === F.map(fa)(_ => b))
116+
assert(F.tupleLeft(fa, b) === F.map(fa)(a => (b, a)))
117+
assert(F.tupleRight(fa, b) === F.map(fa)(a => (a, b)))
118+
assert(F.fproduct(fa)(f) === F.map(fa)(a => (a, f(a))))
119+
assert(F.fproductLeft(fa)(f) === F.map(fa)(a => (f(a), a)))
120+
assert(F.void(fa) === F.map(fa)(_ => ()))
121+
}
122+
forAll { (fab: Try[(Int, String)]) =>
123+
assert(F.unzip(fab) === (F.map(fab)(_._1), F.map(fab)(_._2)))
124+
}
125+
}
126+
112127
// The following tests check laws which are a different formulation of
113128
// laws that are checked. Since these laws are more or less duplicates of
114129
// existing laws, we don't check them for all types that have the relevant

0 commit comments

Comments
 (0)