我正在尝试检查案例类列表是否包含一个特定实例,但是当我尝试这样做时,出现以下错误:
[info] Compiling 1 Scala source to /home/matt/Documents/transledge/app/target/scala-2.9.2/test-classes...
[error] /home/matt/Documents/transledge/app/src/test/scala/com/transledge/drewes/parser_suite.scala:40: overloaded method value should with alternatives:
[error] (notWord: ParserSuite.this.NotWord)ParserSuite.this.ResultOfNotWordForSeq[com.transledge.Instruction,List[com.transledge.Instruction]] <and>
[error] (haveWord: ParserSuite.this.HaveWord)ParserSuite.this.ResultOfHaveWordForSeq[com.transledge.Instruction] <and>
[error] (beWord: ParserSuite.this.BeWord)ParserSuite.this.ResultOfBeWordForAnyRef[List[com.transledge.Instruction]] <and>
[error] (rightMatcher: org.scalatest.matchers.Matcher[List[com.transledge.Instruction]])Unit
[error] cannot be applied to (org.scalatest.matchers.Matcher[Traversable[com.transledge.AddNode]])
[error] parsing(square_node, input) should contain(AddNode("foo"))
[error] ^
[error] one error found
[error] (test:compile) Compilation failed
[error] Total time: 3 s, completed 21-Feb-2013 15:15:04
有问题的测试套件是:
import org.scalatest.FunSpec
import scala.util.parsing.combinator._
import com.transledge.drewes.{Parser => DrewesParser}
import com.transledge._
import org.scalatest.matchers.ShouldMatchers
class ParserSuite extends DrewesParser with FunSpec with ShouldMatchers {
def parsing[A](parser: Parser[A], input: String): A = parse(parser, input).get
// snipping other tests
describe("square_node") {
val input = """\squarenode{foo}(1cm, 2cm)"""
it("should create a node") {
parsing(square_node, input) should contain(AddNode("foo")) // Line 40
}
}
}
AddNode
/的定义Instruction
非常基本:
package com.transledge
abstract class Instruction
case class AddNode(id: String) extends Instruction
这是解析器的简化定义:
package com.transledge.drewes
import scala.util.parsing.combinator._
import com.transledge._
trait Parser extends RegexParsers {
def node_id: Parser[String] = "[a-zA-Z\\-_:0-9]+".r
def node_name: Parser[String] = ("{" ~> node_id <~ "}") | node_id
def point: Parser[String] = "[^,()]+".r
def position: Parser[(String, String)] = "(" ~> point ~ "," ~ point <~ ")" ^^ { case a ~ "," ~ b => (a.trim, b.trim) }
def square_node: Parser[List[Instruction]] = "\\squarenode" ~> node_name ~ position ^^ { case name ~ position => List(AddNode(name)) }
}
我对此的理解是,Scala 编译器应该使用该变体should(rightMatcher: Matcher[List[T]])
,但正在获取一个实例Traversable
而不是 a List
,并且作为包含Traversable
的特征,不能在预期的地方使用。List
Traversable
List
那么如何检查列表是否包含该元素?