Scala案例类测试

时间:2015-02-21 06:14:41

标签: scala testing case-class

我使用案例类Point和LineSegment定义了一个密封的特征形状,并且具有LineSegment类的行为,它告诉我两个LineSegments是否相交,但是当我尝试测试该行为时...它说它无法识别它测试套件。

sealed trait Shape
case class Point(x: Int, y: Int) extends Shape {
//require(x != null & y !=null, "null values for point")
}

case class LineSegment(point1: Point, point2: Point)  extends Shape {
 require(point1 != point2)

 def intersect(lineSegment1: LineSegment, lineSegment2:LineSegment): 
 Boolean ={
// val A1, A2, B1, B2, C1, C2
val A1 = lineSegment1.point2.y - lineSegment1.point1.y
val B1 = lineSegment1.point1.x - lineSegment1.point2.x
val C1 = A1 * lineSegment1.point1.x + B1 * lineSegment1.point1.y

val A2 = lineSegment2.point2.y - lineSegment2.point1.y
val B2 = lineSegment2.point1.x - lineSegment2.point2.x
val C2 = A2 * lineSegment2.point1.x + B2 * lineSegment2.point1.y

val det = A1 * B2 - A2 * B1
if(det == 0)
  return false
else
  return true

}

}

这是我正在尝试的测试

import model.Point
import model.LineSegment
import org.scalatest.{FlatSpec, ShouldMatchers}

 class LineIntersectionTest extends FlatSpec with ShouldMatchers{
 val point1 = Point(20,30);
 val point2 = Point(10,35);
 val point3 = Point(100,10);
 val point4 = Point(16,45);

 val lineSegment1 = LineSegment(point1,point2);
 val lineSegment2 = LineSegment(point3,point4);

  "LineSegmentIntersection Behaviour" should "work for a line segment"  
  in {
  intersect(lineSegment1, lineSegment2) should be (true)
   }}

但是我得到一个错误无法解析符号相交,无法解析符号应该..它不能识别方法相交

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

您正在使用相交,就像它是LineIntersectionTest类上的函数一样。以下应该有效:

lineSegment1.intersect(lineSegment1, lineSegment2) should be (true)

但是我可能会使用不同的实现。也许:

object LineUtils {
  def intersect(l1: LineSegment, l2: LineSegment): Boolean
}

// Usage
LineUtils.insersect(lineSegment1, lineSegment2)