mercredi 28 octobre 2015

Scala FunSuite difference between "should be" and "===" when comparing lists

I have a question about using FunSuite with Matchers.

I'm trying to compare to lists of objects so I tried doing this:

orders2 should be(tiOrdersList2)

where orders2 and tiOrdersList2 are scala.List[MyObject]

This match fails miserably, so I tried with

orders2.toSeq should be (tiOrdersList2.toSeq)

no luck

then I tried matching the single items

orders2(0) should be (tiOrdersList2(0))

no luck again

at the end I found that to make it work for the single object I had to use "===" and going back to the first case I managed to get this working:

orders2 === tiOrdersList2

I can't understand what is the difference here. I imagine that the "should be" matcher is stricter than "===" but I can't see why. Can anyone explain that to me?

NOTE: I want to point out that MyObject has various properties of different java based types, including float. could be that the reason?

UPDATE I checked the matcher with simple classes (see snippet below) and if I override the equals method on my own, also the "should be" works, so the main question is what kind of comparison is "===" doing?

import org.scalatest.{Matchers, FunSuite}
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class blahSuite extends FunSuite  with Matchers
{
    test("extractSessions") {
        new Object1 === (new Object1)
        new Object2 === (new Object2)
        new Object1 shouldEqual  (new Object1)
        new Object2 should be (new Object2)
    }

    class Object1 {
        val pippo: java.lang.String = "blah"
        val pappo:java.lang.Long = 0l
        override def toString(): String = {
            s"$pippo, $pappo"
        }
    }
    class Object2 {
        val pippo: java.lang.String = "blah"
        val pappo:java.lang.Long = 0l
        val pluto:java.lang.Float = 0.0f
        override def toString(): String = {
            s"$pippo, $pappo, $pluto"
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire