Skip to content

Commit

Permalink
Test scala 3 enums (#498)
Browse files Browse the repository at this point in the history
* Test with scala 3 enums

* More enum tests

* Fix typo

* Add other branches to test enums

* Simplify recursive Gen logic
  • Loading branch information
TimWSpence authored Jun 11, 2022
1 parent de620e8 commit 0bad32b
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/test/scala-3/cats/derived/ContravariantSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class ContravariantSuite extends KittensSuite:
s"$context.Contravariant[ListSnocF]",
contravariantTests[ListSnocF].contravariant[MiniInt, String, Boolean]
)
checkAll(
s"$context.Contravariant[EnumK1Contra]",
contravariantTests[EnumK1Contra].contravariant[MiniInt, String, Boolean]
)
checkAll(
s"$context.Contravariant is Serializable",
SerializableTests.serializable(summonInline[Contravariant[TreePred]])
Expand Down Expand Up @@ -96,6 +100,7 @@ object ContravariantSuite:
// given Contravariant[InterleavedPred] = semiauto.contravariant
given Contravariant[AndCharPred] = semiauto.contravariant
given Contravariant[ListSnocF] = semiauto.contravariant
given Contravariant[EnumK1Contra] = semiauto.contravariant

case class Single[A](value: A => Unit) derives Contravariant

Expand Down
2 changes: 2 additions & 0 deletions core/src/test/scala-3/cats/derived/FoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class FoldableSuite extends KittensSuite:
checkAll(s"$context.Foldable[AndChar]", foldableTests[AndChar].foldable[Int, Long])
checkAll(s"$context.Foldable[Interleaved]", foldableTests[Interleaved].foldable[Int, Long])
checkAll(s"$context.Foldable[BoxNel]", foldableTests[BoxNel].foldable[Int, Long])
checkAll(s"$context.Foldable[EnumK1]", foldableTests[EnumK1].foldable[Int, Long])
checkAll(s"$context.Foldable is Serializable", SerializableTests.serializable(summonInline[Foldable[Tree]]))

locally {
Expand Down Expand Up @@ -69,6 +70,7 @@ object FoldableSuite:
given Foldable[AndChar] = semiauto.foldable
given Foldable[Interleaved] = semiauto.foldable
given Foldable[BoxNel] = semiauto.foldable
given Foldable[EnumK1] = semiauto.foldable

final case class Nel[+A](head: A, tail: List[A])
object Nel:
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/scala-3/cats/derived/FunctorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class FunctorSuite extends KittensSuite:
checkAll(s"$context.Functor[AndChar]", functorTests[AndChar].functor[Int, String, Long])
checkAll(s"$context.Functor[Interleaved]", functorTests[Interleaved].functor[Int, String, Long])
checkAll(s"$context.Functor[NestedPred]", functorTests[NestedPred].functor[Boolean, Int, Boolean])
checkAll(s"$context.Functor[EnumK1]", functorTests[EnumK1].functor[Boolean, Int, Boolean])
checkAll(s"$context.Functor is Serializable", SerializableTests.serializable(summonInline[Functor[Tree]]))

locally {
Expand Down Expand Up @@ -72,6 +73,7 @@ object FunctorSuite:
given Functor[AndChar] = semiauto.functor
given Functor[Interleaved] = semiauto.functor
given Functor[NestedPred] = semiauto.functor
given Functor[EnumK1] = semiauto.functor

case class Single[A](value: A) derives Functor

Expand Down
2 changes: 2 additions & 0 deletions core/src/test/scala-3/cats/derived/HashSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class HashSuite extends KittensSuite:
// checkAll(s"$context.Hash[Interleaved[Int]]", hashTests[Interleaved[Int]].hash)
checkAll(s"$context.Hash[Tree[Int]]", hashTests[Tree[Int]].hash)
checkAll(s"$context.Hash[Recursive]", hashTests[Recursive].hash)
checkAll(s"$context.Hash[EnumK0]", hashTests[EnumK0].hash)
checkAll(s"$context.Hash is Serializable", SerializableTests.serializable(summonInline[Hash[Inner]]))

locally {
Expand All @@ -45,5 +46,6 @@ object HashSuite:
given Hash[Interleaved[Int]] = semiauto.hash
given Hash[Tree[Int]] = semiauto.hash
given Hash[Recursive] = semiauto.hash
given Hash[EnumK0] = semiauto.hash

end HashSuite
2 changes: 2 additions & 0 deletions core/src/test/scala-3/cats/derived/InvariantSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class InvariantSuite extends KittensSuite:
checkAll(s"$context.Invariant[AndCharF]", invariantTests[AndCharF].invariant[MiniInt, String, Boolean])
checkAll(s"$context.Invariant[ListSnoc", invariantTests[ListSnoc].invariant[MiniInt, String, Boolean])
checkAll(s"$context.Invariant[Bivariant]", invariantTests[Bivariant].invariant[MiniInt, String, Boolean])
checkAll(s"$context.Invariant[EnumK1Inv]", invariantTests[EnumK1Inv].invariant[MiniInt, String, Boolean])
checkAll(s"$context.Invariant is Serializable", SerializableTests.serializable(summonInline[Invariant[TreeF]]))

// TODO https://github.com/typelevel/kittens/issues/476
Expand Down Expand Up @@ -88,6 +89,7 @@ object InvariantSuite:
given Invariant[ListSnoc] = semiauto.invariant
given Invariant[Bivariant] = semiauto.invariant
given Invariant[IList] = semiauto.invariant
given Invariant[EnumK1Inv] = semiauto.invariant

case class Single[A](value: A) derives Invariant

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class NonEmptyTraverseSuite extends KittensSuite:
s"$context.NonEmptyTraverse[Interleaved]",
nonEmptyTraverseTests[Interleaved].nonEmptyTraverse[Option, Int, Int, Int, Int, Option, Option]
)
checkAll(
s"$context.NonEmptyTraverse[EnumK1]",
nonEmptyTraverseTests[EnumK1].nonEmptyTraverse[Option, Int, Int, Int, Int, Option, Option]
)
checkAll(
s"$context.NonEmptyTraverse is Serializable",
SerializableTests.serializable(summonInline[NonEmptyTraverse[Tree]])
Expand Down Expand Up @@ -88,5 +92,6 @@ object NonEmptyTraverseSuite:
given NonEmptyTraverse[NelAndOne] = semiauto.nonEmptyTraverse
given NonEmptyTraverse[VecAndNel] = semiauto.nonEmptyTraverse
given NonEmptyTraverse[Interleaved] = semiauto.nonEmptyTraverse
given NonEmptyTraverse[EnumK1] = semiauto.nonEmptyTraverse

end NonEmptyTraverseSuite
2 changes: 2 additions & 0 deletions core/src/test/scala-3/cats/derived/OrderSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class OrderSuite extends KittensSuite {
checkAll(s"$context.Order[Interleaved[Int]]", orderTests[Interleaved[Int]].order)
checkAll(s"$context.Order[Recursive]", orderTests[Recursive].order)
checkAll(s"$context.Order[GenericAdt[Int]]", orderTests[GenericAdt[Int]].order)
checkAll(s"$context.Order[EnumK0]", orderTests[EnumK0].order)
checkAll(s"$context.Order is Serializable", SerializableTests.serializable(summonInline[Order[Interleaved[Int]]]))
}

Expand All @@ -57,5 +58,6 @@ object OrderSuite {
given Order[Interleaved[Int]] = semiauto.order
given Order[Recursive] = semiauto.order
given Order[GenericAdt[Int]] = semiauto.order
given Order[EnumK0] = semiauto.order
}
}
2 changes: 2 additions & 0 deletions core/src/test/scala-3/cats/derived/PartialOrderSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PartialOrderSuite extends KittensSuite:
checkAll(s"$context.PartialOrder[Tree[Int]]", partialOrderTests[Tree[Int]].partialOrder)
checkAll(s"$context.PartialOrder[Recursive]", partialOrderTests[Recursive].partialOrder)
checkAll(s"$context.PartialOrder[Box[KeyValue]]", partialOrderTests[Box[KeyValue]].partialOrder)
checkAll(s"$context.PartialOrder[EnumK0]", partialOrderTests[EnumK0].partialOrder)
checkAll(
s"$context.PartialOrder is Serialiable",
SerializableTests.serializable(summonInline[PartialOrder[Tree[Int]]])
Expand Down Expand Up @@ -73,6 +74,7 @@ object PartialOrderSuite:
given PartialOrder[Tree[Int]] = semiauto.partialOrder
given PartialOrder[Recursive] = semiauto.partialOrder
given PartialOrder[Box[KeyValue]] = semiauto.partialOrder
given PartialOrder[EnumK0] = semiauto.partialOrder

final case class KeyValue(key: String, value: Int)
object KeyValue extends ((String, Int) => KeyValue):
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/scala-3/cats/derived/ReducibleSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ReducibleSuite extends KittensSuite:
checkAll(s"$context.Reducible[VecAndNel]", reducibleTests[VecAndNel].reducible[Option, Int, Long])
checkAll(s"$context.Reducible[Interleaved]", reducibleTests[Interleaved].reducible[Option, Int, Long])
checkAll(s"$context.Reducible[BoxZipper]", reducibleTests[BoxZipper].reducible[Option, Int, Long])
checkAll(s"$context.Reducible[EnumK1]", reducibleTests[EnumK1].reducible[Option, Int, Long])
checkAll(s"$context.Reducible is Serializable", SerializableTests.serializable(summonInline[Reducible[Tree]]))

locally {
Expand Down Expand Up @@ -69,6 +70,7 @@ object ReducibleSuite:
given Reducible[VecAndNel] = semiauto.reducible
given Reducible[Interleaved] = semiauto.reducible
given Reducible[BoxZipper] = semiauto.reducible
given Reducible[EnumK1] = semiauto.reducible

final case class Zipper[+A](left: List[A], focus: A, right: List[A])
object Zipper:
Expand Down
8 changes: 8 additions & 0 deletions core/src/test/scala-3/cats/derived/ShowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ class ShowSuite extends KittensSuite:
assertEquals(show(value), shown)
}

test(s"$context.Show[EnumK0]") {
val value: EnumK0 = EnumK0.LeafI(3)
val shown =
"LeafI(value = 3)"
assertEquals(show(value), shown)
}

test(s"$context.Show respects existing instances") {
val value = Box(Bogus(42))
val shown = "Box(content = Blah)"
Expand Down Expand Up @@ -108,5 +115,6 @@ object ShowSuite:
given Show[Interleaved[Int]] = semiauto.show
given Show[Tree[Int]] = semiauto.show
given Show[Box[Bogus]] = semiauto.show
given Show[EnumK0] = semiauto.show

end ShowSuite
5 changes: 5 additions & 0 deletions core/src/test/scala-3/cats/derived/TraverseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class TraverseSuite extends KittensSuite:
s"$context.Traverse[Interleaved]",
traverseTests[Interleaved].traverse[Int, Double, String, Long, Option, Option]
)
checkAll(
s"$context.Traverse[EnumK1]",
traverseTests[EnumK1].traverse[Int, Double, String, Long, Option, Option]
)
checkAll(s"$context.Traverse is Serializable", SerializableTests.serializable(summonInline[Traverse[Tree]]))

locally {
Expand Down Expand Up @@ -59,5 +63,6 @@ object TraverseSuite:
given Traverse[ListSnoc] = semiauto.traverse
given Traverse[AndChar] = semiauto.traverse
given Traverse[Interleaved] = semiauto.traverse
given Traverse[EnumK1] = semiauto.traverse

end TraverseSuite
128 changes: 128 additions & 0 deletions core/src/test/scala-3/cats/derived/adtdefns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,102 @@ import scala.annotation.tailrec

object TestDefns:

enum EnumK0:
case LeafS(value: String)
case LeafI(value: Int)

object EnumK0:
given Arbitrary[EnumK0] = Arbitrary(
Gen.oneOf(
Arbitrary.arbitrary[String].map(LeafS.apply),
Arbitrary.arbitrary[Int].map(LeafI.apply)
)
)

given Cogen[EnumK0] = Cogen[Either[String, Int]].contramap {
case LeafS(s) => Left(s)
case LeafI(i) => Right(i)
}

enum EnumK1[A]:
case Leaf(value: A)
case Rec(l: EnumK1[A], r: EnumK1[A])

object EnumK1:
given [A](using Arbitrary[A]): Arbitrary[EnumK1[A]] = Arbitrary(
Gen.recursive(rec =>
Gen.sized(size =>
val leaf = Arbitrary.arbitrary[A].map(Leaf.apply)
if size == 0 then leaf
else
Gen.resize(
size / 2,
Gen.oneOf(
leaf,
for
l <- rec
r <- rec
yield Rec(l, r)
)
)
)
)
)

enum EnumK1Contra[-A]:
case Leaf(value: A => Unit)
case Rec(l: EnumK1Contra[A], r: EnumK1Contra[A])

object EnumK1Contra:
given [A](using Arbitrary[A => Unit]): Arbitrary[EnumK1Contra[A]] = Arbitrary(
Gen.recursive(rec =>
Gen.sized(size =>
val leaf = Arbitrary.arbitrary[A => Unit].map(Leaf.apply)
if size == 0 then leaf
else
Gen.resize(
size / 2,
Gen.oneOf(
leaf,
for
l <- rec
r <- rec
yield Rec(l, r)
)
)
)
)
)

enum EnumK1Inv[A]:
case Leaf(cov: A, contra: A => Unit)
case Rec(l: EnumK1Inv[A], r: EnumK1Inv[A])

object EnumK1Inv:
given [A](using Arbitrary[A], Arbitrary[A => Unit]): Arbitrary[EnumK1Inv[A]] = Arbitrary(
Gen.recursive(rec =>
Gen.sized(size =>
val leaf =
for
cov <- Arbitrary.arbitrary[A]
contra <- Arbitrary.arbitrary[A => Unit]
yield Leaf(cov, contra)
if size == 0 then leaf
else
Gen.resize(
size / 2,
Gen.oneOf(
leaf,
for
l <- rec
r <- rec
yield Rec(l, r)
)
)
)
)
)

sealed trait Rgb
object Rgb:
case object Red extends Rgb
Expand Down Expand Up @@ -430,4 +526,36 @@ trait TestEqInstances:

given Eq[Outer] = Eq.fromUniversalEquals

given Eq[EnumK0] =
import EnumK0.*
Eq.instance {
case (LeafS(s1), LeafS(s2)) => s1 === s2
case (LeafI(i1), LeafI(i2)) => i1 === i2
case _ => false
}

given [A](using Eq[A]): Eq[EnumK1[A]] =
import EnumK1.*
Eq.instance {
case (Leaf(v1), Leaf(v2)) => v1 === v2
case (Rec(l1, r1), Rec(l2, r2)) => l1 === l2 && r1 === r2
case _ => false
}

given [A](using Eq[A => Unit]): Eq[EnumK1Contra[A]] =
import EnumK1Contra.*
Eq.instance {
case (Leaf(v1), Leaf(v2)) => v1 === v2
case (Rec(l1, r1), Rec(l2, r2)) => l1 === l2 && r1 === r2
case _ => false
}

given [A](using Eq[A], Eq[A => Unit]): Eq[EnumK1Inv[A]] =
import EnumK1Inv.*
Eq.instance {
case (Leaf(cov1, contra1), Leaf(cov2, contra2)) => cov1 === cov2 && contra1 === contra2
case (Rec(l1, r1), Rec(l2, r2)) => l1 === l2 && r1 === r2
case _ => false
}

end TestEqInstances

0 comments on commit 0bad32b

Please sign in to comment.