-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
clarify law testing's dependency on cats-testkit and scalatest #2245
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a8eae14
clarify law testing's dependency on cats-testkit and scalatest
kailuowang 6889139
Update lawtesting.md
kailuowang 5dca108
Update lawtesting.md
kailuowang b63abdf
Update lawtesting.md
kailuowang 3311271
Update lawtesting.md
kailuowang 329e5ae
fixing tut compilation
kailuowang a534cee
Update lawtesting.md
kailuowang 8e5e758
Update lawtesting.md
kailuowang 7e2f9de
Update lawtesting.md
kailuowang fd77b9c
Update lawtesting.md
kailuowang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,23 +7,25 @@ section: "typeclasses" | |
# 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. | ||
Cats uses `catalysts` and [discipline](https://github.com/typelevel/discipline) to help test instances with laws. To test type class | ||
laws from Cats against your instances, you need to add a `cats-laws` dependency. | ||
If you are using `ScalaTests`, Cats also ships with `cats-testkit`, which 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. | ||
First up, you will need to specify dependencies on `cats-laws` in your `build.sbt` file (or `cats-testkit` if you | ||
are using `ScalaTest`). | ||
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.1" % Test, | ||
"org.typelevel" %% "cats-testkit" % "1.0.1"% Test, | ||
"org.typelevel" %% "cats-laws" % "1.1.0" % Test, //or `cats-testkit` if you are using ScalaTest | ||
"com.github.alexarchambault" %% "scalacheck-shapeless_1.13" % "1.1.6" % Test | ||
) | ||
``` | ||
|
||
|
||
## Example: Testing a Functor instance | ||
|
||
We'll begin by creating a data type and its Functor instance. | ||
|
@@ -50,8 +52,8 @@ For simplicity we'll just use `Eq.fromUniversalEquals`: | |
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`. | ||
Then we can begin to write our law tests. If you are using `ScalaTest` you can inherit from `cats.tests.CatsSuite` | ||
from `cats-testkit`. `CatsSuite` extends the standard ScalaTest `FunSuite` as well as `Matchers`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you do if you're not using ScalaTest? Not sure this helps. |
||
Furthermore it also pulls in all of cats instances and syntax, so there's no need to import from `cats.implicits._`. | ||
|
||
```tut:book | ||
|
@@ -62,8 +64,11 @@ 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.*`. | ||
The key to testing laws is the `checkAll` function provided by [discipline](https://github.com/typelevel/discipline), | ||
which takes a name for your test and a Discipline ruleset. | ||
Cats has defined rulesets for all type class laws in `cats.laws.discipline.*`. As of now, [discipline](https://github.com/typelevel/discipline) provides this helper `checkAll` only for `ScalaTest` and `Spec2`, | ||
but it should not be hard for you to write your own `checkAll` for your test framework of choice following this | ||
[example](https://github.com/typelevel/discipline/blob/master/shared/src/main/scala/scalatest/Discipline.scala). | ||
|
||
So for our example we will want to import `cats.laws.discipline.FunctorTests` and call `checkAll` with it. | ||
Before we do so, however, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
catalysts
relevant here?