3

私はいくつかの仕様テストに取り組んでおり、「必須」機能とは何か、そしてそれが何をするのかを理解しようとしています。

スペックソースのどこにもその宣言や実装を見つけることができず、それが何をするのかを理解しようとしています。

使用例を次に示します。

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

「must」は引数として関数をとるように見えますが、「must」の実際の署名と、それが引数で何をするのかを知りたいです。

誰かが私を正しい方向に向けることができますか?

ありがとう!

4

2 に答える 2

6

少なくともSpecs2.0では、mustの定義は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)
}

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
于 2011-03-23T18:13:50.653 に答える
2

MustMatchersに関するドキュメントがあり、との使用法に関する詳細な説明がmustありshouldます。

于 2011-03-23T18:13:28.217 に答える