重载等于运算符的断言对于三等于失败但是为双等于传递

时间:2015-04-09 20:06:47

标签: scala scalatest assertion case-class

我重载了一个case类的相等运算符:

case class Polygon(points: Seq[(Double, Double)]) extends Shape {
  def ==(that: Polygon): Boolean = { ... }
}

在断言中使用双等于时,我的单元测试通过了:

import org.scalatest.FunSpec

class ShapeSpec extends FunSpec {
  describe("Polygon") {
    it("is equal to all its rotations") {
      val p = Polygon(Seq( ... ))
      for { i ← 0 until p.points.size } {
        val q = Polygon(p.points.drop(i) ++ p.points.take(i))
        assert(p == q)
      }
    }
  }
}

但是当使用===而不是==时,相同的测试失败了:

[info] Polygon
[info] - is equal to all its rotations *** FAILED ***
[info]   Polygon(List((-1.0,-1.0), (4.0,-1.0), (4.0,2.0), (1.0,2.0),
         (1.0,1.0), (-1.0,1.0))) did not equal PolygonM(List((4.0,-1.0),
         (4.0,2.0), (1.0,2.0), (1.0,1.0), (-1.0,1.0), (-1.0,-1.0)))
         (ShapeSpec.scala:28)

为什么会这样?

1 个答案:

答案 0 :(得分:3)

你拼错了。 ;)

应该是:

override def equals(x: Any): Boolean