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

Port FoldableSuite to Scala 3 #374

Merged
merged 2 commits into from
Aug 29, 2021
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
3 changes: 2 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version = "2.7.5"
version = "3.0.0"
runner.dialect = "scala3"
align.preset = none
maxColumn = 120
includeNoParensInSelectChains = false
Expand Down
102 changes: 102 additions & 0 deletions core/src/test/scala-3/cats/derived/FoldableSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2015 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats
package derived

import cats.laws.discipline.{FoldableTests, SerializableTests}
import cats.syntax.all.*
import org.scalacheck.Arbitrary
import scala.compiletime.*

class FoldableSuite extends KittensSuite:
import FoldableSuite.*
import TestDefns.*

inline def foldableTests[F[_]]: FoldableTests[F] =
FoldableTests[F](summonInline)

inline def testFoldable(inline context: String): Unit =
checkAll(s"$context.Foldable[IList]", foldableTests[IList].foldable[Int, Long])
checkAll(s"$context.Foldable[Tree]", foldableTests[Tree].foldable[Int, Long])
checkAll(s"$context.Foldable[GenericAdt]", foldableTests[GenericAdt].foldable[Int, Long])
checkAll(s"$context.Foldable[OptList]", foldableTests[OptList].foldable[Int, Long])
checkAll(s"$context.Foldable[ListSnoc]", foldableTests[ListSnoc].foldable[Int, Long])
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 is Serializable", SerializableTests.serializable(summonInline[Foldable[Tree]]))

locally {
import auto.foldable.given
testFoldable("auto")
}

locally {
import semiInstances.given
testFoldable("semiauto")
}

end FoldableSuite

object FoldableSuite:
import TestDefns.*

type OptList[A] = Option[List[A]]
type ListSnoc[A] = List[Snoc[A]]
type AndChar[A] = (A, Char)
type BoxNel[A] = Box[Nel[A]]

object semiInstances:
given Foldable[IList] = semiauto.foldable
given Foldable[Tree] = semiauto.foldable
given Foldable[GenericAdt] = semiauto.foldable
given Foldable[OptList] = semiauto.foldable
given Foldable[ListSnoc] = semiauto.foldable
given Foldable[AndChar] = semiauto.foldable
given Foldable[Interleaved] = semiauto.foldable
given Foldable[BoxNel] = semiauto.foldable

final case class Nel[+A](head: A, tail: List[A])
object Nel:
given [A: Eq]: Eq[Nel[A]] =
(x, y) => x.head === y.head && x.tail === y.tail

given [A: Arbitrary]: Arbitrary[Nel[A]] =
Arbitrary(for
head <- Arbitrary.arbitrary[A]
tail <- Arbitrary.arbitrary[List[A]]
yield Nel(head, tail))

given Foldable[Nel] with
def foldLeft[A, B](fa: Nel[A], b: B)(f: (B, A) => B) = fa.tail.foldl(b)(f)
def foldRight[A, B](fa: Nel[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]) = fa.tail.foldr(lb)(f)

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

enum Many[+A] derives Foldable:
case Naught
case More(value: A, rest: Many[A])

enum AtMostOne[+A] derives Foldable:
case Naught
case Single(value: A)

enum AtLeastOne[+A] derives Foldable:
case Single(value: A)
case More(value: A, rest: Option[AtLeastOne[A]])

end FoldableSuite
17 changes: 0 additions & 17 deletions core/src/test/scala-3/cats/derived/FoldableTests.scala

This file was deleted.

91 changes: 91 additions & 0 deletions core/src/test/scala-3/cats/derived/FunctorSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2015 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats
package derived

import cats.laws.discipline.*
import cats.laws.discipline.eq.*
import scala.compiletime.*

class FunctorSuite extends KittensSuite:
import FunctorSuite.*
import TestDefns.*

given ExhaustiveCheck[Predicate[Boolean]] =
ExhaustiveCheck.instance(List(_ => true, _ => false, identity, !_))

inline def functorTests[F[_]]: FunctorTests[F] =
FunctorTests[F](summonInline)

inline def testFunctor(inline context: String): Unit =
checkAll(s"$context.Functor[IList]", functorTests[IList].functor[Int, String, Long])
checkAll(s"$context.Functor[Tree]", functorTests[Tree].functor[Int, String, Long])
checkAll(s"$context.Functor[GenericAdt]", functorTests[GenericAdt].functor[Int, String, Long])
checkAll(s"$context.Functor[OptList]", functorTests[OptList].functor[Int, String, Long])
checkAll(s"$context.Functor[ListSnoc]", functorTests[ListSnoc].functor[Int, String, Long])
// FIXME: Testing `functorTests[AndChar].functor[Int, String, Long]` causes a ClassCastException
checkAll(s"$context.Functor[AndChar]", functorTests[AndChar].functor[Int, String, Int])
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 is Serializable", SerializableTests.serializable(summonInline[Functor[Tree]]))

locally {
import auto.functor.given
testFunctor("auto")
}

locally {
import semiInstances.given
testFunctor("semiauto")
}

end FunctorSuite

object FunctorSuite:
import TestDefns.*

type OptList[A] = Option[List[A]]
type ListSnoc[A] = List[Snoc[A]]
type AndChar[A] = (A, Char)
type Predicate[A] = A => Boolean
type NestedPred[A] = Predicate[Predicate[A]]

object semiInstances:
given Functor[IList] = semiauto.functor
given Functor[Tree] = semiauto.functor
given Functor[GenericAdt] = semiauto.functor
given Functor[OptList] = semiauto.functor
given Functor[ListSnoc] = semiauto.functor
given Functor[AndChar] = semiauto.functor
given Functor[Interleaved] = semiauto.functor
given Functor[NestedPred] = semiauto.functor

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

enum Many[+A] derives Functor:
case Naught
case More(value: A, rest: Many[A])

enum AtMostOne[+A] derives Functor:
case Naught
case Single(value: A)

enum AtLeastOne[+A] derives Functor:
case Single(value: A)
case More(value: A, rest: Option[AtLeastOne[A]])

end FunctorSuite
18 changes: 0 additions & 18 deletions core/src/test/scala-3/cats/derived/FunctorTests.scala

This file was deleted.

77 changes: 0 additions & 77 deletions core/src/test/scala-3/cats/derived/functor.scala

This file was deleted.