Search code examples
scalascalatestscalaz

Scala & ScalaTest operator === conflict


I want to test operator === of Scalaz using ScalaTest. I have written simply test:

class ComparisonTest extends FunSuite {

  test("=== operator of Scalaz") {
    assert(1 === 1) // i want to check/test === operator of Scalaz
  }
}

Unfortunately within assert of my test, scala picked operator === from ScalaTest. It did not help if I import explicitly:

import scalaz.Scalaz._
import scalaz._

I also tried:

import scalaz.syntax.EqualOps
assert(new EqualOps[Int](1).===(1))  }

but it did not compile:

Error:(10, 12) constructor EqualOps in class EqualOps cannot be accessed in class ComparisonTest
    assert(new EqualOps[Int](1).===(1))  }

Is there any way to do such test of Scalaz ===operator within test of FunSuite? (maybe somhow disable === of ScalaTest)


Solution

  • For new EqualOps[Int](1).===(1)) error, you can use implicitly[Equal[Int]].equal(1, 2) to implicitly get Scalaz Equal implementation.

    For disable scalatest Equalizer implicit conversion, you can try to override convertToEqualizer method and remove implicit method modifier.

    Example:

    class ComparisonTest extends FunSuite {
      override def convertToEqualizer[T](left: T): Equalizer[T] = new Equalizer(left)
    
      import scalaz._
      import Scalaz._
    
      test("=== operator of Scalaz") {
        assert(1 === 1) // i want to check/test === operator of Scalaz
      }
    }
    

    this is a tricky way to achieve this, best way maybe need scalatest change Equalizer inject way, like by import, and we can unimport the implicit like: import Predef.{any2stringadd}