Skip to content

Instantly share code, notes, and snippets.

@drstevens
Created November 27, 2012 06:01
Show Gist options
  • Save drstevens/4152627 to your computer and use it in GitHub Desktop.
Save drstevens/4152627 to your computer and use it in GitHub Desktop.
Provide more type safe equalizer implementation and provide a way to disable the implicit === conversion
package com.bifflabs
import org.scalatest.Assertions
/**
* Use me to disable the implicit providing === operator in FunSuite. This allows use of scalaz.Identity.===(...)
*/
trait IgnoringDefaultEqualizer {
self: Assertions =>
override def convertToEqualizer(any: Any) = throw new Exception("Do not call me")
}
trait MoreTypeSafeAssertions {
class TypeSaferEquality[T](left: T) {
def ====(right: T) =
if (left == right)
None
else {
Some("%s did not equal %s".format(left.toString , right.toString))
}
def =!=(right: T) =
if (left != right)
None
else {
Some("%s should not equals %s".format(left.toString , right.toString))
}
}
implicit def typeSaferEquality[T](t: T) = new TypeSaferEquality(t)
}
object MoreTypeSafeAssertions extends MoreTypeSafeAssertions
trait AssertionsWithScalazSupport {
class EvenMoreTypeSaferEquality[T](left: T)(implicit eq: scalaz.Equal[T]) {
def ====(right: T) =
if (eq.equal(left, right))
None
else {
Some("%s did not equal %s".format(left.toString , right.toString))
}
def =!=(right: T) =
if (!eq.equal(left, right))
None
else {
Some("%s should not equals %s".format(left.toString , right.toString))
}
}
implicit def evenMoreTypeSaferEquality[T](t: T)(implicit eq: scalaz.Equal[T] = scalaz.Scalaz.equalA) = new EvenMoreTypeSaferEquality(t)
}
object AssertionsWithScalazSupport extends AssertionsWithScalazSupport
package com.bifflabs
import org.scalatest.FunSuite
import scalaz._
import scalaz.Scalaz._
class TestHelpersSuiteWithScalazSupport extends FunSuite with IgnoringDefaultEqualizer {
import AssertionsWithScalazSupport._
test("1 should equal 1") {
assert(1 ==== 1)
}
test("Some[1] should not equal None") {
assert(some(1) =!= none)
}
test("Unfortunately now I sometimes need to help the compiler") {
val s: Option[Int] = Some(10)
val n: Option[Int] = None
// assert(Some(10) =!= None) // Does not compile
assert(s =!= n)
}
test("I can disable ScalaTest equalizer implicit") {
assert(1 === 1 ==== true)
}
}
class TestHelpersSuite extends FunSuite with IgnoringDefaultEqualizer {
import MoreTypeSafeAssertions._
test("1 should equal 1") {
assert(1 ==== 1)
}
test("Some[1] should not equal None") {
assert(some(1) =!= none)
}
test("Unfortunately now I sometimes need to help the compiler") {
val s: Option[Int] = Some(10)
val n: Option[Int] = None
// assert(Some(10) =!= None) // Does not compile
assert(s =!= n)
}
test("I can disable ScalaTest equalizer implicit") {
assert(1 === 1 ==== true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment