-
-
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
Add Contravariant instance for Eq and test Show instance. #1211
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,22 @@ object eq { | |
} | ||
} | ||
|
||
/** | ||
* Create an approximation of Eq[Eq[A]] by generating 100 values for A | ||
* and comparing the application of the two eqv functions | ||
*/ | ||
implicit def catsLawsEqForEq[A](implicit arbA: Arbitrary[(A, A)], booleanEq: Eq[Boolean]): Eq[Eq[A]] = new Eq[Eq[A]] { | ||
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. I don't really think we need the 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. That makes sense, I adapted |
||
def eqv(f: Eq[A], g: Eq[A]): Boolean = { | ||
val samples = List.fill(100)(arbA.arbitrary.sample).collect { | ||
case Some(a) => a | ||
case None => sys.error("Could not generate arbitrary values to compare two Eq[A]") | ||
} | ||
samples.forall { | ||
case (l, r) => booleanEq.eqv(f.eqv(l, r), g.eqv(l, r)) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Create an approximation of Eq[PartialOrder[A]] by generating 100 values for A | ||
* and comparing the application of the two compare functions | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cats | ||
package tests | ||
|
||
import cats.functor.Contravariant | ||
import cats.laws.discipline.arbitrary._ | ||
import cats.laws.discipline.{ContravariantTests, SerializableTests} | ||
import cats.laws.discipline.eq._ | ||
|
||
class ShowTests extends CatsSuite { | ||
checkAll("Contravariant[Show]", ContravariantTests[Show].contravariant[Int, Int, Int]) | ||
checkAll("Contravariant[Show]", SerializableTests.serializable(Contravariant[Show])) | ||
} |
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.
this seems a bit weak. You could do something like
a.hashCode == b.hashCode
. I think that is a lawful Eq that has a better chance of showing some bad behavior (although, if that is the case,Eq.on
is broken anyway, and the test should be there).