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

Add tests for all derived instances #519

Merged
merged 29 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7f9ef38
Simplify ApplicativeSuite
joroKr21 Sep 13, 2022
1b42e3b
Extend ApplicativeSuite with derived cases
joroKr21 Sep 13, 2022
78c824c
Extend ApplySuite with derived cases
joroKr21 Sep 13, 2022
2c8e124
Extends CommutativeMonoidSuite with derived cases
joroKr21 Sep 13, 2022
cf8ba4b
Extends CommutativeSemigroupSuite with derived cases
joroKr21 Sep 13, 2022
8f8a5ef
Extend ContravariantSuite with derived cases
joroKr21 Sep 13, 2022
c7d9d45
Extend EmptyKSuite with derived cases
joroKr21 Sep 13, 2022
4ebc912
Extend EmptySuite with derived cases
joroKr21 Sep 13, 2022
1ff44cc
Extend EqSuite with derived cases
joroKr21 Sep 13, 2022
74bddb1
Extend FoldableSuite with derived cases
joroKr21 Sep 13, 2022
c2e999c
Extend FunctorSuite with derived cases
joroKr21 Sep 13, 2022
fafe77d
Remove large ADT definitions
joroKr21 Sep 13, 2022
4d4b5be
Move some enums to ADT definitions
joroKr21 Sep 13, 2022
15bb1cd
Extend HashSuite with derived cases
joroKr21 Sep 13, 2022
da5b96e
Extend InvariantSuite with derived cases
joroKr21 Sep 13, 2022
5e6f47d
Extend MonoidKSuite with derived cases
joroKr21 Sep 13, 2022
84e7b61
Extend MonoidSuite with derived cases
joroKr21 Sep 13, 2022
06a2729
Extend NonEmptyTraverseSuite with derived instances
joroKr21 Sep 13, 2022
58a07c4
Extend OrderSuite with derived instances
joroKr21 Sep 13, 2022
7cbb5e1
Extend PartialOrderSuite with derived instances
joroKr21 Sep 13, 2022
7093149
Extend PureSuite with derived cases
joroKr21 Sep 13, 2022
aa0eb3d
Extend ReducibleSuite with derived instances
joroKr21 Sep 13, 2022
7c15eec
Extend SemigroupKSuite with derived cases
joroKr21 Sep 13, 2022
9039707
Extend SemigroupSuite with derived instances
joroKr21 Sep 13, 2022
91b7885
Extend ShowPrettySuite with derived instances
joroKr21 Sep 13, 2022
6f880bd
Extend ShowSuite with derived instances
joroKr21 Sep 13, 2022
3fd40f4
Extend TraverseSuite with derived instances
joroKr21 Sep 13, 2022
b574c6d
Refactor and rename ADT definitions
joroKr21 Sep 13, 2022
d5eb478
Simplify DerivedShowPretty
joroKr21 Sep 13, 2022
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
30 changes: 11 additions & 19 deletions core/src/main/scala-3/cats/derived/DerivedShowPretty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ Make sure that A satisfies one of the following conditions:
type DerivedShowPretty[A] = Derived[ShowPretty[A]]
object DerivedShowPretty:
opaque type Or[A] = A => List[String]
object Or extends OrInstances:
def apply[A](instance: A => List[String]): Or[A] = instance
object Or:
extension [A](or: Or[A]) def apply(a: A): List[String] = or(a)

sealed abstract class OrInstances:
inline given [A]: Or[A] = summonFrom {
case instance: Show[A] => Or((a: A) => instance.show(a).split(System.lineSeparator).toList)
case derived: DerivedShowPretty[A] => Or(derived.instance.showLines(_))
case instance: Show[A] => instance.show(_).split(System.lineSeparator).toList
case derived: DerivedShowPretty[A] => derived.instance.showLines(_)
}

inline def apply[A]: ShowPretty[A] =
Expand All @@ -48,23 +45,18 @@ object DerivedShowPretty:
val n = labels.size
if n <= 0 then List(s"$prefix()")
else
var lines: List[String] = List(")")
val inner = inst.project(a)(n - 1)([t] => (show: Or[t], x: t) => show.apply(x))
inner match
case Nil => lines = s" ${labels(n - 1)} = \"\"," :: lines
case h :: t => lines = s" ${labels(n - 1)} = $h" :: t.map(s => " " + s) ::: lines
var lines = List(")")
inst.project(a)(n - 1)([t] => (show: Or[t], x: t) => show.apply(x)) match
case Nil => lines ::= s" ${labels(n - 1)} = \"\","
case h :: t => lines :::= s" ${labels(n - 1)} = $h" :: t.map(s => " " + s)
var i = n - 2
while i >= 0 do
val inner = inst.project(a)(i)([t] => (show: Or[t], x: t) => show.apply(x))
inner match
case Nil => lines = s" ${labels(i)} = \"\"," :: lines
case v :: Nil => lines = s" ${labels(i)} = $v," :: lines
inst.project(a)(i)([t] => (show: Or[t], x: t) => show.apply(x)) match
case Nil => lines ::= s" ${labels(i)} = \"\","
case v :: Nil => lines ::= s" ${labels(i)} = $v,"
case h :: t => lines = s" ${labels(i)} = $h" :: t.init.map(s => " " + s) ::: s" ${t.last}," :: lines
i -= 1

lines = s"$prefix(" :: lines

lines
s"$prefix(" :: lines

trait Coproduct[A](using inst: K0.CoproductInstances[Or, A]) extends ShowPretty[A]:
def showLines(a: A): List[String] =
Expand Down
Loading