문제

So at start i had

def parseB(string : String)(implicit context : Context) : Float = parseAll(gexp, string).get.eval(context).left.get

and then in the test

implicit var context = Context()
parseB("False") should be(false)
parseB("False") should not be(true)

then i wrote a custom matcher

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, "", "")
}

so my test turned to

"False" should reflectBoolean(false)

but

"False" should not reflectBoolean(true)

Breaks- Of course, i never said it could match negative. So how do i say it?

도움이 되었습니까?

해결책 2

The trick is to declare an implicit class to consider "reflect" as a method of the result of ("False"shouldnot) of type ResultOfNotWordForAny[String].

def parseB(string : String)(implicit context : Context) : Float = parseAll(gexp, string).get.eval(context).left.get

implicit var context = Context()
parseB("False") should be(false)
parseB("False") should not be(true)

// Declares an implicit class for applying after not. Note the use of '!='
implicit class ReflectShouldMatcher(s: ResultOfNotWordForAny[String]) {
  def reflect(value: Boolean) = s be(BeMatcher[String] {
    left => MatchResult(parseB(left) != value, "", "")
  })
}
// Declares an explicit method for applying after should. Note the use of '=='
def reflect(right: Boolean) = Matcher[String]{ left =>
            MatchResult(parseB(left) == right, "", "")}

// Now it works. Note that the second example can be written with or wo parentheses.
"False" should reflect(false)
"False" should not reflect true

다른 팁

You don't need to change your matcher, I think you just need some parentheses:

"False" should ReflectBooleanMatcher(false)
"False" should not(ReflectBooleanMatcher(true)) //works!

UPDATE

Per @Vidya's comments if you define reflectBoolean as:

def reflectBoolean(value: Boolean) = new ReflectBooleanMatcher(value)

Then you can use 'BDD' style syntax, and the parentheses make this work:

"False" should reflectBoolean(false)
"False" should not(reflectBoolean(true))

I'm just going to rip off the documentation:

trait CustomMatchers {    
  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, "", "")      
  }

  def reflectBoolean(value: Boolean) = new ReflectBooleanMatcher(value)
}

Now try using the reflectBoolean method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top