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

Hook Eq up to scalactic/scalatest's equality #522

Merged
merged 4 commits into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions state/src/test/scala/cats/state/StateTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ class StateTTests extends CatsSuite {
import StateTTests._

test("basic state usage"){
assert(add1.run(1).run == (2 -> 1))
add1.run(1).run should === (2 -> 1)
}

test("traversing state is stack-safe"){
val ns = (0 to 100000).toList
val x = ns.traverseU(_ => add1)
assert(x.runS(0).run == 100001)
x.runS(0).run should === (100001)
}

test("State.pure and StateT.pure are consistent")(check {
forAll { (s: String, i: Int) =>
val state: State[String, Int] = State.pure(i)
val stateT: State[String, Int] = StateT.pure(i)
state.run(s).run == stateT.run(s).run
state.run(s).run === stateT.run(s).run
}
})

test("Apply syntax is usable on State") {
val x = add1 *> add1
assert(x.runS(0).run == 2)
x.runS(0).run should === (2)
}

test("Singleton and instance inspect are consistent")(check {
forAll { (s: String, i: Int) =>
State.inspect[Int, String](_.toString).run(i).run ==
State.inspect[Int, String](_.toString).run(i).run ===
State.pure[Int, Unit](()).inspect(_.toString).run(i).run
}
})
Expand Down
29 changes: 29 additions & 0 deletions tests/shared/src/test/scala/cats/tests/CatsEquality.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cats
Copy link

Choose a reason for hiding this comment

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

I think this whole file would be useful in tests that use the cats library, and not just cats itself. So, if that is the case, consider moving the file to main, so it could be exported - i.e. tests/shared/src/main/scala/cats/tests/CatsEquality.scala

package tests

import org.scalactic._
import TripleEqualsSupport.AToBEquivalenceConstraint
import TripleEqualsSupport.BToAEquivalenceConstraint

// The code in this file was taken and only slightly modified from
// https://github.com/bvenners/equality-integration-demo
// Thanks for the great examples, Bill!

final class CatsEquivalence[T](T: Eq[T]) extends Equivalence[T] {
def areEquivalent(a: T, b: T): Boolean = T.eqv(a, b)
}

trait LowPriorityStrictCatsConstraints extends TripleEquals {
implicit def lowPriorityCatsCanEqual[A, B](implicit B: Eq[B], ev: A <:< B): CanEqual[A, B] =
new AToBEquivalenceConstraint[A, B](new CatsEquivalence(B), ev)
}

trait StrictCatsEquality extends LowPriorityStrictCatsConstraints {
override def convertToEqualizer[T](left: T): Equalizer[T] = super.convertToEqualizer[T](left)
implicit override def convertToCheckingEqualizer[T](left: T): CheckingEqualizer[T] = new CheckingEqualizer(left)
override def unconstrainedEquality[A, B](implicit equalityOfA: Equality[A]): CanEqual[A, B] = super.unconstrainedEquality[A, B]
implicit def catsCanEqual[A, B](implicit A: Eq[A], ev: B <:< A): CanEqual[A, B] =
new BToAEquivalenceConstraint[A, B](new CatsEquivalence(A), ev)
}

object StrictCatsEquality extends StrictCatsEquality
10 changes: 6 additions & 4 deletions tests/shared/src/test/scala/cats/tests/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats
package tests

import cats.std.AllInstances
import cats.syntax.AllSyntax
import cats.syntax.{AllSyntax, EqOps}
import org.scalatest.{ FunSuite, PropSpec, Matchers }
import org.scalatest.prop.PropertyChecks
import org.typelevel.discipline.scalatest.Discipline
Expand All @@ -16,13 +16,15 @@ import scala.util.{Failure, Success, Try}
* An opinionated stack of traits to improve consistency and reduce
* boilerplate in Cats tests.
*/
trait CatsSuite extends FunSuite with Matchers with Discipline with AllInstances with AllSyntax with TestInstances {
trait CatsSuite extends FunSuite with Matchers with Discipline with AllInstances with AllSyntax with TestInstances with StrictCatsEquality {
implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
PropertyCheckConfiguration(
minSuccessful = Platform.minSuccessful,
maxDiscardedFactor = Platform.maxDiscardedFactor)
// disable scalatest's ===
override def convertToEqualizer[T](left: T): Equalizer[T] = ???

// disable Eq syntax (by making `eqSyntax` not implicit), since it collides
// with scalactic's equality
override def eqSyntax[A: Eq](a: A): EqOps[A] = new EqOps[A](a)
Copy link

Choose a reason for hiding this comment

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

As per previous comment, consider moving this into its own trait in main, perhaps in conjunction with the other traits.
This would also mean that users need not import cats.syntax.EqOps

}

trait CatsProps extends PropSpec with Matchers with PropertyChecks with TestInstances {
Expand Down
2 changes: 1 addition & 1 deletion tests/shared/src/test/scala/cats/tests/EvalTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class EvalTests extends CatsSuite {
val (spooky, lz) = init(value)
(0 until n).foreach { _ =>
val result = lz.value
assert(result === value)
result should === (value)
spin ^= result.##
}
assert(spooky.counter == numEvals)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ class SpecificStreamingTTests extends CatsSuite {

val x = fa.flatMap(f).flatMap(g)
val y = fa.flatMap(a => f(a).flatMap(g))
assert(x === y)
x should === (y)
}
}
4 changes: 3 additions & 1 deletion tests/shared/src/test/scala/cats/tests/SyntaxTests.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cats
package tests

import cats.std.AllInstances
import cats.syntax.AllSyntax
import algebra.laws.GroupLaws
import cats.functor.{Invariant, Contravariant}
import cats.laws.discipline.SerializableTests
Expand All @@ -27,7 +29,7 @@ import scala.reflect.runtime.universe.TypeTag
*
* None of these tests should ever run, or do any runtime checks.
*/
class SyntaxTests extends CatsSuite with PropertyChecks {
class SyntaxTests extends AllInstances with AllSyntax {

// pretend we have a value of type A
def mock[A]: A = ???
Expand Down