Scalatest: Detailed reporting on case class inequality

Created on 17 Nov 2014  Â·  4Comments  Â·  Source: scalatest/scalatest

Assume you are comparing two deeply nested case classes, is there a way to output the field(s) in which they differ?

Simple example, given 2 classes:

case class Foo(a: Int, b: Int, c: Bar)
case class Bar(x: Int, y: Int)

and the instances:

val left = Foo(3, 7, Bar(7, 5))
val right = Foo(3, 7, Bar(6, 5))

A desirable output would be „left.c.x does not equal right.c.x“. Is this possible today?

Most helpful comment

Possibly interesting for this: https://github.com/xdotai/diff

All 4 comments

I stumbled upon a similar problem today. I haven't found such functionality in ScalaTest, as the standard definition of Equality only permits Boolean results, and something like a scalaz Validation would be needed to track details of inequalities.
There is one project that can at least help with debugging:
https://github.com/stacycurl/delta
It allows you to do diffing on nested structures (see tests for some examples).
Unfortunately I couldn't get your nested example to compile, only a version which compares Bars:

import sjc.delta.Delta._
import sjc.delta.Delta.std.int._
import sjc.delta.Delta.hlist._
import sjc.delta.Delta.generic._

case class Foo(a: Int, b: Int, c: Bar)
case class Bar(x: Int, y: Int)
val left = Foo(3, 7, Bar(7, 5))
val right = Foo(3, 7, Bar(6, 5))
left.c.delta(right.c)

Hope this helps a little :)

Even for non-nested case classes it would greatly help to show some kind of diff/delta or at least put a newline after "was not equal to" so that the difference can be more easily spotted. Or can this be already achieved?

Just to give an update on this feature request, we are planning to add a "Differ" to ScalaTest that has three methods that can be used to do diffs. There's a trait here to capture the idea:

https://github.com/scalatest/scalatest/blob/8e5943d25cd3b8467b64ccefe4268932d95bed02/scalactic/src/main/scala/org/scalactic/Differ.scala

I think the Anys should be As though. But the idea is that a Differ[T] can optionally be mixed into Equality[T] and made implicit. That way the diffing behavior for a type is provided as a type class that is pulled into the assert or should/must expression along with an Equality. Since Equality is already being pulled in, this won't add any extra implicit lookup, so should not affect the compile time. Also, the diffs are not really needed generally, just for equality comparisons, so the assertions and matchers that take Equality already limit that implicit lookup well. We will add the Differ, if it exists, to the exception going to the IDE or other reporters.

Possibly interesting for this: https://github.com/xdotai/diff

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seratch picture seratch  Â·  7Comments

japgolly picture japgolly  Â·  5Comments

bblfish picture bblfish  Â·  5Comments

el-dom picture el-dom  Â·  7Comments

TomasMikula picture TomasMikula  Â·  3Comments