我正在做一些Specs测试,我试图理解什么是“必须”函数,以及它做了什么。
我在specs源代码中找不到它的声明或实现,我正在尝试理解它的作用。
下面是它的一些用法示例:
"hello world".size must be equalTo(11)
"hello world" must be matching("h.* w.*")
stack.push(11) must throwAn[Error]
在我看来,“必须”接受一个函数作为参数,但我想知道“必须”的实际签名,以及它对参数的作用。
有谁能给我指个方向吗?
谢谢!
发布于 2011-03-24 02:13:51
至少在Specs2.0中,你会在org.specs2.matcher.MustExpectable中找到必须的定义
/**
* 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)
}
特征在这里声明相关的隐式转换:
/**
* 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
发布于 2011-03-24 02:13:28
这里有关于MustMatchers的文档,还有关于must
和should
用法的详细解释。
https://stackoverflow.com/questions/5409615
复制相似问题