至少在 Specs2.0 中,你会在org.specs2.matcher.MustExpectable中找到 must 的定义
/**
* This kind of expectable can be followed by the verb must to apply a matcher:
*
* `1 must beEqualTo(1)`
*
* For convenience, several mustMatcher methods have also been defined as shortcuts to equivalent:
*
* `a must matcher`
*/
class MustExpectable[T] private[specs2] (tm: () => T) extends Expectable[T](tm) { outer =>
def must(m: =>Matcher[T]) = applyMatcher(m)
def must_==(other: =>T) = applyMatcher(new BeEqualTo(other))
def must_!=(other: =>T) = applyMatcher(new BeEqualTo(other).not)
}
object MustExpectable {
def apply[T](t: =>T) = new MustExpectable(() => t)
}
trait在MustExpectation
这里声明了相关的隐式转换:
/**
* This trait provides implicit definitions to transform any value into a MustExpectable
*/
trait MustExpectations extends Expectations {
implicit def akaMust[T](tm: Expectable[T]) = new MustExpectable(() => tm.value) {
override private[specs2] val desc = tm.desc
}
implicit def theValue[T](t: =>T): MustExpectable[T] = createMustExpectable(t)
implicit def theBlock(t: =>Nothing): MustExpectable[Nothing] = createMustExpectable(t)
protected def createMustExpectable[T](t: =>T) = MustExpectable(t)
}
object MustExpectations extends MustExpectations