ScalaTest自己的匹配器,使用单词而不是

Ill*_*iax 3 scala scalatest

所以一开始我就有了

def parseB(string : String)(implicit context : Context) : Float = parseAll(gexp, string).get.eval(context).left.get
Run Code Online (Sandbox Code Playgroud)

然后在测试中

implicit var context = Context()
parseB("False") should be(false)
parseB("False") should not be(true)
Run Code Online (Sandbox Code Playgroud)

然后我写了一个自定义匹配器

case class ReflectBooleanMatcher(value : Boolean)(implicit context : Context) extends Matcher[String] with ExpressionParser{
  def parseB(string : String) : Boolean = parseAll(gexp, string).get.eval(context).right.get
  def apply (string : String) : MatchResult = 
      MatchResult(parseB(string) == value, "", "")
}
Run Code Online (Sandbox Code Playgroud)

所以我的测试转向了

"False" should reflectBoolean(false)
Run Code Online (Sandbox Code Playgroud)

"False" should not reflectBoolean(true)
Run Code Online (Sandbox Code Playgroud)

休息 - 当然,我从来没有说它可以匹配负面.那怎么说呢?

Noa*_*oah 5

您不需要更改匹配器,我认为您只需要一些括号:

"False" should ReflectBooleanMatcher(false)
"False" should not(ReflectBooleanMatcher(true)) //works!
Run Code Online (Sandbox Code Playgroud)

UPDATE

Per @ Vidya的评论,如果你定义reflectBoolean为:

def reflectBoolean(value: Boolean) = new ReflectBooleanMatcher(value)
Run Code Online (Sandbox Code Playgroud)

然后你可以使用'BDD'样式语法,括号使这个工作:

"False" should reflectBoolean(false)
"False" should not(reflectBoolean(true))
Run Code Online (Sandbox Code Playgroud)