开发者

In Scala Specs, what is the "must" function?

开发者 https://www.devze.com 2023-02-19 12:09 出处:网络
I\'m working with some Specs tests and I\'m trying to understand what the \"must\" function is, and what it does.

I'm working with some Specs tests and I'm trying to understand what the "must" function is, and what it does.

I am unable to find its declaration or implementation anywhere in the specs source, and I'm trying to understand what it does.

Here are some example usages of it:

"hello world".size must be equalTo(11)
"hello world" must be matching("h.* w.*")
stack.push(11) must throwAn[Error]

It looks to me like "must" takes a function开发者_开发百科 as an argument, but I'd like to know the actual signature of "must", and what it does with its argument.

Can anyone point me in the right direction?

Thanks!


At least with Specs2.0, you will find the definition of must in 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)
}

The MustExpectation trait is here to declare the relevant implicit conversions:

/**
 * 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


There's the documentation on MustMatchers, there are detailed explanations on the usage of must and should.

0

精彩评论

暂无评论...
验证码 换一张
取 消