dreamflasher
dreamflasher

Reputation: 1530

How do I get meaningful error messages for nested scala unit equality matchers?

I want to assert equality in a ScalaTest of case classes which contain an Array. (So the built-in equality matchers for case classes are not applicable.) Example:

case class Example(array: Array[Double], variable: Integer)

Test stub:

val a = Example(Array(0.1, 0.2), 1)
val b = Example(Array(0.1, 0.2), 1)
a should equal (b)

Fails as expected. So I implement an Equality trait:

implicit val exampleEq =
new Equality[Example] {
  def areEqual(left: Example, right: Any): Boolean =
    right match {
      case other: Example => {
        left.array should contain theSameElementsInOrderAs other.array
        left.variable should be other.variable
        true
      }
      case _ => false
    }
}

Which works. The other option is to implement the Equality trait with == at all places of the "should be" and in case it is false at one place return false, else true. The problem with both is that when running the test I get the error message that both "Example" objects are not equal (if they are not) but I would like to see in which element they differ.

How do I achieve this?

Thank you for your help!

[UPDATE] In practice Example contains multiple arrays and other fields, I changed the code accordingly.

Upvotes: 1

Views: 448

Answers (1)

Ed Staub
Ed Staub

Reputation: 15690

Considered using:

left.array should contain theSameElementsInOrderAs other.array

Reference: Working with "sequences".

Upvotes: 0

Related Questions