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 law testing guide #1880

Merged
merged 6 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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 build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ lazy val docs = project
.settings(noPublishSettings)
.settings(docSettings)
.settings(commonJvmSettings)
.dependsOn(coreJVM, freeJVM)
.settings(libraryDependencies += "com.github.alexarchambault" %% "scalacheck-shapeless_1.13" % "1.1.5" % Compile)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would strongly discourage adding a third party dependency unless it is really essential. cats is quite low down the compile chain so the fewer dependencies it has that could get in the way of upgrades the better. And for thing like testing new scala compilers (eg 2.13.0.M1, native,etc) it is not the dependencies that a third party library brings into cats that is the issue, rather the dependencies needed to build that 3rd party library in the first place.

So I'm not saying it should not have any at all, but that we have to be careful.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a starting suggestion/discussion.....

The same is true for testkit, that I have suggested we split into two: a testkit that has no dependencies other than pure cats, and another testkit-scalatest that adds the scalatest dependency. The idea here is that we make the the base testkit independent to the testing frameworks. But what about specs2, should we also add a testkit-specs2, testkit-utest and so on?

And what about, say, cats-check. For sure we can't add that as a dependency.

So maybe now is a good time to address this. Maybe we need some kind of uber-cats repo that brings these things together?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You raise a good point, I was hesitant to add the dependency before. I can revert the last commit and make the Arbitrary instance explicit with a mention to scalacheck-shapeless.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should be very cautious about adding dependencies to the project. In this particular case, it's a dependency added to the docs generation project, so no downstream impact. That's why I didn't object it. However, now I realized that it does impact the docs build in scala 2.13.

"Making the Arbitrary instance explicit with a mention to scalacheck-shapeless" would make more sense to me.

.dependsOn(coreJVM, freeJVM, kernelLawsJVM, lawsJVM, testkitJVM)

lazy val cats = project.in(file("."))
.settings(moduleName := "root")
Expand Down
133 changes: 133 additions & 0 deletions docs/src/main/tut/typeclasses/lawtesting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Law testing

[Laws](https://typelevel.org/cats/typeclasses.html#laws) are an important part of cats.
Cats uses `catalysts` and `discipline` to help test instances with laws.
To make things easier, cats ships with `cats-testkit`, which makes use of `catalysts` and `discipline` and exposes `CatsSuite` based on ScalaTest.


## Getting started

First up, you will need to specify dependencies on `cats-laws` and `cats-testkit` in your `build.sbt` file.
To make things easier, we'll also include the `scalacheck-shapeless` library in this tutorial, so we don't have to manually write instances for ScalaCheck's `Arbitrary`.

```scala
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-laws" % "1.0.0-MF" % Test,
"org.typelevel" %% "cats-testkit" % "1.0.0-MF"% Test,
"com.github.alexarchambault" %% "scalacheck-shapeless_1.13" % "1.1.5" % Test
)
```

## Example: Testing a Functor instance

We'll begin by creating a data type and its Functor instance.
```tut:book
import cats._

sealed trait Tree[+A]
case object Leaf extends Tree[Nothing]
case class Node[A](p: A, left: Tree[A], right: Tree[A]) extends Tree[A]

object Tree {
implicit val functorTree: Functor[Tree] = new Functor[Tree] {
def map[A, B](tree: Tree[A])(f: A => B) = tree match {
case Leaf => Leaf
case Node(p, left, right) => Node(f(p), map(left)(f), map(right)(f))
}
}
}
```
We will also need to create an `Eq` instance, as most laws will need to compare values of a type to properly test for correctness.
For simplicity we'll just use `Eq.fromUniversalEquals`:

```tut:book
implicit def eqTree[A: Eq]: Eq[Tree[A]] = Eq.fromUniversalEquals
```

Then we can begin to write our law tests. Start by creating a new class in your `test` folder and inheriting from `cats.tests.CatsSuite`.
`CatsSuite` extends the standard ScalaTest `FunSuite` as well as `Matchers`.
Furthermore it also pulls in all of cats instances and syntax, so there's no need to import from `cats.implicits._`.

```tut:book
import cats.tests.CatsSuite

class TreeLawTests extends CatsSuite {

}
```

The key to testing laws is the `checkAll` function, which takes a name for your test and a Discipline ruleset.
Cats has defined rulesets for all type class laws in `cats.laws.discipline.*`.

So for our example we will want to import `cats.laws.discipline.FunctorTests` and call `checkAll` with it.
Before we do so, however,
we will have to bring our instances into scope as well as the derived `Arbitrary` instances from `scalacheck-shapeless`:


```tut:book
import Tree._

import org.scalacheck.Shapeless._

import cats.laws.discipline.FunctorTests

class TreeLawTests extends CatsSuite {
checkAll("Tree.FunctorLaws", FunctorTests[Tree].functor[Int, Int, String])
}
```

Now when we run `test` in our sbt console, ScalaCheck will test if the `Functor` laws hold for our `Tree` type.
You should see something like this:

```
[info] TreeLawTests:
[info] - Tree.FunctorLaws.functor.covariant composition
[info] - Tree.FunctorLaws.functor.covariant identity
[info] - Tree.FunctorLaws.functor.invariant composition
[info] - Tree.FunctorLaws.functor.invariant identity
[info] ScalaTest
[info] Run completed in 537 milliseconds.
[info] Total number of tests run: 4
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 4, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 4, Failed 0, Errors 0, Passed 4
[success] Total time: 1 s, completed Aug 31, 2017 2:19:22 PM
```

And voila, you've successfully proven that your data type upholds the Functor laws!

### Testing cats.kernel instances

For most of the type classes included in cats, the above will work great.
However, the law tests for the type classes inside `cats.kernel` are structured a bit differently.
These include `Semigroup`, `Monoid`, `Group` and `Semilattice`.
Instead of importing the laws from `cats.laws.discipline.*`, we have to import `cats.kernel.laws.GroupLaws`
and then call the corresponding method, e.g. `GroupLaws[Foo].monoid`, or `GroupLaws[Foo].semigroup`.

Let's test it out, by defining a `Semigroup` instance for our `Tree` type.

```tut:book
import cats.implicits._

implicit def semigroupTree[A: Semigroup]: Semigroup[Tree[A]] = new Semigroup[Tree[A]] {
def combine(x: Tree[A], y: Tree[A]) = (x, y) match {
case (Leaf, _) => Leaf
case (_, Leaf) => Leaf
case (Node(xp, xLeft, xRight), Node(yp, yLeft, yRight)) =>
Node(xp |+| yp, xLeft |+| yLeft, xRight |+| yRight)
}
}
```

Then we can again test the instance inside our class extending `CatsSuite`:

```tut:book
import cats.laws.discipline.FunctorTests
import cats.kernel.laws.GroupLaws

class TreeLawTests extends CatsSuite {
checkAll("Tree[Int].MonoidLaws", GroupLaws[Tree[Int]].semigroup)
checkAll("Tree.FunctorLaws", FunctorTests[Tree].functor[Int, Int, String])
}
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that with catalysts you can do something similar to how algebra/kernel calls the laws, which is something like:

...
import catalysts.macros.TypeTagM

// this can be added to the general test suite
implicit def groupLaws[A: Eq: Arbitrary]: GroupLaws[A] = GroupLaws[A]

....
laws[GroupLaws, Tree[Int]].check(_.semigroup)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this is worth mentioning? I'm not really sure if I can see the benefit 🤔