This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
/
FunctorLawsCheck.scala
44 lines (37 loc) · 1.87 KB
/
FunctorLawsCheck.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Algebra.Show._
import Algebra.{->, Box, Id, Show, Symbol, Tree}
import ArbitraryImplicits._
import FunctorModule.FunctorInstances._
import FunctorModule.{Functor, FunctorLaws, FunctorLawsNoSyntax}
import org.scalacheck.Prop.forAll
import org.scalacheck.{Properties, _}
sealed abstract class FunctorLawsCheck[F[_] : Functor, A, B](name: String)(
implicit
AFI: Arbitrary[F[A]],
AAB: Arbitrary[A => B],
ABB: Arbitrary[B => Boolean]
) extends Properties(s"$name Functor Laws Check") {
val laws = FunctorLaws[F]
val lawsNoSyntax = FunctorLawsNoSyntax[F]
property(" Functor's Map Preserves Identity") = forAll {
(fa: F[A]) => laws.mapPreservesIdentity(fa)
}
property(" Functor's Map Preserves Composition") = forAll {
(fa: F[A], f: A => B, g: B => Boolean) =>
laws.mapPreservesComposition(fa)(f)(g)
}
property(" Functor's Map Preserves Identity No Syntax") = forAll {
(fa: F[A]) => lawsNoSyntax.mapPreservesIdentity(fa)
}
property(" Functor's Map Preserves Composition No Syntax") = forAll {
(fa: F[A], f: A => B, g: B => Boolean) =>
lawsNoSyntax.mapPreservesComposition(fa)(f)(g)
}
}
object IdFunctorLawsCheck extends FunctorLawsCheck[Id, String, Int]("Id")
object ListFunctorLawsCheck extends FunctorLawsCheck[List, Show[Symbol], Box[String]]("List")
object OptionFunctorLawsCheck extends FunctorLawsCheck[Option, Tree[Box[Int]], Boolean]("Option")
object EitherFunctorLawsCheck extends FunctorLawsCheck[Either[String, *], Box[Int], Symbol]("Either")
object IntFunctionFunctorLawsCheck extends FunctorLawsCheck[Int -> *, Box[String], Tree[Int]]("Function From Int")
object StringFunctionFunctorLawsCheck extends FunctorLawsCheck[String -> *, Tree[String], Box[Int]]("Function From String")
object TreeFunctorLawsCheck extends FunctorLawsCheck[Tree, String, Box[Box[Int]]]("Tree")