-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #259 - Add project to test cats related code
- Loading branch information
Showing
5 changed files
with
321 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package effectie.testing.cats | ||
|
||
import cats.Monad | ||
import hedgehog.{Gen, Range} | ||
|
||
/** @author Kevin Lee | ||
* @since 2021-08-04 | ||
*/ | ||
object Gens { | ||
|
||
def genInt(from: Int, to: Int): Gen[Int] = | ||
Gen.int(Range.linear(from, to)) | ||
|
||
def genIntFromMinToMax: Gen[Int] = Gens.genInt(Int.MinValue, Int.MaxValue) | ||
|
||
def genIntToInt: Gen[Int => Int] = | ||
/* It has some hard-coded functions for now until Hedgehog has Gen[A => B] | ||
* Reference: https://github.com/hedgehogqa/scala-hedgehog/issues/90 | ||
*/ | ||
Gen.element1[Int => Int]( | ||
identity[Int], | ||
x => x + x, | ||
x => x - x, | ||
x => x * x, | ||
x => x + 100, | ||
x => x - 100, | ||
x => x * 100 | ||
) | ||
|
||
def genAToMonadA[F[_]: Monad, A](genF: Gen[A => A]): Gen[A => F[A]] = | ||
genF.map(f => x => Monad[F].pure(f(x))) | ||
|
||
def genFA[F[_]: Monad, A](genA: Gen[A]): Gen[F[A]] = | ||
genA.map(a => Monad[F].pure(a)) | ||
|
||
} |
134 changes: 134 additions & 0 deletions
134
test4cats/src/main/scala/effectie/testing/cats/Laws.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package effectie.testing.cats | ||
|
||
import cats.{Applicative, Eq, Functor, Monad} | ||
|
||
/** @author Kevin Lee | ||
* @since 2021-08-04 | ||
*/ | ||
object Laws { | ||
trait FunctorLaws { | ||
/* Functors must preserve identity morphisms | ||
* fmap id = id | ||
*/ | ||
def identity[F[_], A](fa: F[A])( | ||
implicit F: Functor[F], | ||
FA: Eq[F[A]] | ||
): Boolean = | ||
FA.eqv( | ||
F.map(fa)(scala.Predef.identity), | ||
fa | ||
) | ||
|
||
/* Functors preserve composition of morphisms | ||
* fmap (f . g) == fmap f . fmap g | ||
*/ | ||
def composition[F[_]: Functor, A, B, C](fa: F[A], f: B => C, g: A => B)( | ||
implicit F: Functor[F], | ||
FC: Eq[F[C]] | ||
): Boolean = | ||
FC.eqv( | ||
F.map(fa)(f compose g), | ||
F.map(F.map(fa)(g))(f) | ||
) | ||
} | ||
object FunctorLaws extends FunctorLaws | ||
|
||
trait ApplicativeLaws extends FunctorLaws { | ||
/* Identity | ||
* pure id <*> v = v | ||
*/ | ||
def identityAp[F[_]: Applicative, A](fa: => F[A])( | ||
implicit F: Functor[F], | ||
FA: Eq[F[A]] | ||
): Boolean = | ||
FA.eqv( | ||
Applicative[F].ap[A, A](Applicative[F].pure(scala.Predef.identity))(fa), | ||
fa | ||
) | ||
|
||
/* Homomorphism | ||
* pure f <*> pure x = pure (f x) | ||
*/ | ||
def homomorphism[F[_]: Applicative, A, B](f: A => B, a: => A)( | ||
implicit F: Functor[F], | ||
FB: Eq[F[B]] | ||
): Boolean = | ||
FB.eqv( | ||
Applicative[F].ap(Applicative[F].pure(f))(Applicative[F].pure(a)), | ||
Applicative[F].pure(f(a)) | ||
) | ||
|
||
/* Interchange | ||
* u <*> pure y = pure ($ y) <*> u | ||
*/ | ||
def interchange[F[_], A, B](a: => A, f: F[A => B])( | ||
implicit F: Applicative[F], | ||
FB: Eq[F[B]] | ||
): Boolean = | ||
FB.eqv( | ||
F.ap[A, B](f)(F.pure(a)), | ||
F.ap[A => B, B](F.pure(g => g(a)))(f) | ||
) | ||
|
||
/* Composition | ||
* pure (.) <*> u <*> v <*> w = u <*> (v <*> w) | ||
*/ | ||
def compositionAp[F[_], A, B, C](fa: F[A], f: F[B => C], g: F[A => B])( | ||
implicit F: Applicative[F], | ||
FC: Eq[F[C]] | ||
): Boolean = | ||
FC.eqv( | ||
F.ap[A, C]( | ||
F.ap[A => B, A => C]( | ||
F.ap[B => C, (A => B) => (A => C)]( | ||
F.pure(bc => ab => bc compose ab) | ||
)(f) | ||
)(g) | ||
)(fa), | ||
F.ap[B, C](f)( | ||
F.ap[A, B](g)(fa) | ||
) | ||
) | ||
} | ||
object ApplicativeLaws extends ApplicativeLaws | ||
|
||
trait MonadLaws extends ApplicativeLaws { | ||
/* | ||
* return a >>= f === f a | ||
*/ | ||
def leftIdentity[F[_], A, B](a: A, f: A => F[B])( | ||
implicit F: Monad[F], | ||
FB: Eq[F[B]] | ||
): Boolean = | ||
FB.eqv( | ||
F.flatMap(F.pure(a))(f), | ||
f(a) | ||
) | ||
|
||
/* | ||
* m >>= return === m | ||
*/ | ||
def rightIdentity[F[_], A](fa: F[A])( | ||
implicit F: Monad[F], | ||
FA: Eq[F[A]] | ||
): Boolean = | ||
FA.eqv( | ||
F.flatMap(fa)(F.pure(_: A)), | ||
fa | ||
) | ||
|
||
/* | ||
* (m >>= f) >>= g === m >>= (\x -> f x >>= g) | ||
*/ | ||
def associativity[F[_], A, B, C](fa: F[A], f: A => F[B], g: B => F[C])( | ||
implicit F: Monad[F], | ||
FC: Eq[F[C]] | ||
): Boolean = | ||
FC.eqv( | ||
F.flatMap(F.flatMap(fa)(f))(g), | ||
F.flatMap(fa)(x => F.flatMap(f(x))(g)) | ||
) | ||
} | ||
object MonadLaws extends MonadLaws | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
test4cats/src/main/scala/effectie/testing/cats/MonadSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package effectie.testing.cats | ||
|
||
import cats.{Eq, Monad} | ||
import hedgehog._ | ||
|
||
/** @author Kevin Lee | ||
* @since 2021-08-04 | ||
*/ | ||
object MonadSpec { | ||
def testLaws[F[_]: Monad](implicit eqF: Eq[F[Int]]): Property = | ||
Specs | ||
.monadLaws | ||
.laws[F]( | ||
Gens.genFA[F, Int](Gens.genInt(Int.MinValue, Int.MaxValue)), | ||
Gens.genIntFromMinToMax, | ||
Gens.genIntToInt, | ||
Gens.genAToMonadA(Gens.genIntToInt) | ||
) | ||
} |
Oops, something went wrong.