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 #516

Merged
merged 29 commits into from
Sep 13, 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
7a4ba0b
Simplify ApplicativeSuite
joroKr21 Sep 13, 2022
fe6afce
Extend ApplicativeSuite with derived cases
joroKr21 Sep 13, 2022
287eebd
Extend ApplySuite with derived cases
joroKr21 Sep 13, 2022
24acf9d
Extends CommutativeMonoidSuite with derived cases
joroKr21 Sep 13, 2022
fcb642d
Extends CommutativeSemigroupSuite with derived cases
joroKr21 Sep 13, 2022
790fce5
Extend ContravariantSuite with derived cases
joroKr21 Sep 13, 2022
0e0fdf2
Extend EmptyKSuite with derived cases
joroKr21 Sep 13, 2022
89391f8
Extend EmptySuite with derived cases
joroKr21 Sep 13, 2022
a8ada28
Extend EqSuite with derived cases
joroKr21 Sep 13, 2022
338d3d2
Extend FoldableSuite with derived cases
joroKr21 Sep 13, 2022
8d649f6
Extend FunctorSuite with derived cases
joroKr21 Sep 13, 2022
0a13d78
Remove large ADT definitions
joroKr21 Sep 13, 2022
02886eb
Move some enums to ADT definitions
joroKr21 Sep 13, 2022
bc1ede4
Extend HashSuite with derived cases
joroKr21 Sep 13, 2022
0ffbf9f
Extend InvariantSuite with derived cases
joroKr21 Sep 13, 2022
6d11ce9
Extend MonoidKSuite with derived cases
joroKr21 Sep 13, 2022
4409e85
Extend MonoidSuite with derived cases
joroKr21 Sep 13, 2022
2ddb032
Extend NonEmptyTraverseSuite with derived instances
joroKr21 Sep 13, 2022
fa4ca48
Extend OrderSuite with derived instances
joroKr21 Sep 13, 2022
43992b3
Extend PartialOrderSuite with derived instances
joroKr21 Sep 13, 2022
171073c
Extend PureSuite with derived cases
joroKr21 Sep 13, 2022
02f00dd
Extend ReducibleSuite with derived instances
joroKr21 Sep 13, 2022
8b100ae
Extend SemigroupKSuite with derived cases
joroKr21 Sep 13, 2022
17e9f0f
Extend SemigroupSuite with derived instances
joroKr21 Sep 13, 2022
1b0e8c7
Extend ShowPrettySuite with derived instances
joroKr21 Sep 13, 2022
321def5
Extend ShowSuite with derived instances
joroKr21 Sep 13, 2022
ac34f89
Extend TraverseSuite with derived instances
joroKr21 Sep 13, 2022
5acedf0
Refactor and rename ADT definitions
joroKr21 Sep 13, 2022
79d9e22
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