1

I am learning scala, and want to write my tests with ===. However, I am curious if there is a way to do something like this:

assert(1 !=== 2)

I have tried the above, !==, and !(===)

Is there any way to get the descriptiveness of === and use negation?

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180

1 Answers1

3

ScalaTest doesn't have a !== method (it's actually in the source code and is commented out). You could implement your own analogue, like:

// Somewhere in the codebase
class ExtendedEqualizer(left: Any) {
  def !==(right: Any) = {
    if (left != right) None
    else Some("%s equaled to %s".format(left, right))
  }
}

object TestUtil {
  implicit def convertToExtendedEqualizer(left: Any) = new ExtendedEqualizer(left)
}

// In your test class
import TestUtil.convertToExtendedEqualizer

Then it becomes as simple to use as ===:

assert(3 !== 2+2)

Note that this is a simplified version of === that doesn't do deep array comparisons and doesn't generate a nice diff like ScalaTest does.

Alex Yarmula
  • 10,477
  • 5
  • 33
  • 32