Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecs2 - equality testing in scala
    text
    copied!<p>I have been testing my code with Specs2 for some time now and in my test I wanted to ensure that two objects of some type compare correctly:</p> <pre><code>"compare based on carrier value" in { val b0 = new BitField32(343948) val b0prim = new BitField32(343948) val b1 = new BitField32(112) b0prim should be equalTo(b0) b1 should not be equalTo(b0) } </code></pre> <p>When this test was executed, I got an error</p> <pre><code>[error] x compare based on carrier value [error] 'core.BitField32@17805bd5' is not equal to 'core.BitField32@6c0d0900' (BitField32Spec.scala:34) [error] Expected: ...ld32@[6c0d0900] [error] Actual: ...ld32@[17805bd5] </code></pre> <p>Which seemed reasonable, since my class didn't have any methods for comparison. So I tried a couple of things that came to mind: implementing <code>Ordered</code> and providing a variety of comparison methods:</p> <pre><code>/** * mutable 32bit Bitfield */ class BitField32(var carrier: Int) extends Ordered[BitField32]{ override def equals(that: Any) = true def canEqual(that: Any) = true def compare(that: BitField32) = this.carrier - that.carrier def ===(that: BitField32) = this.carrier == that.carrier def ==(that: BitField32) = this.carrier == that.carrier //(...) } </code></pre> <p>But I still keep getting the same error. I tried to read specs2 sources to see how the equalTo matcher works, but I wasn't any wiser afterwards. </p> <p>Any idea how I have to modify my class for the equality to be picked up by (stock) specs2 matchers?</p> <p>BitField32 isn't a case class by design - I want it to be mutable.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload