首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Scala规范中,什么是“必须”函数?

在Scala规范中,什么是“必须”函数?
EN

Stack Overflow用户
提问于 2011-03-24 02:04:35
回答 2查看 1.6K关注 0票数 3

我正在做一些Specs测试,我试图理解什么是“必须”函数,以及它做了什么。

我在specs源代码中找不到它的声明或实现,我正在尝试理解它的作用。

下面是它的一些用法示例:

代码语言:javascript
运行
复制
"hello world".size must be equalTo(11)
"hello world" must be matching("h.* w.*")
stack.push(11) must throwAn[Error]

在我看来,“必须”接受一个函数作为参数,但我想知道“必须”的实际签名,以及它对参数的作用。

有谁能给我指个方向吗?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-24 02:13:51

至少在Specs2.0中,你会在org.specs2.matcher.MustExpectable中找到必须的定义

代码语言:javascript
运行
复制
/**
 * 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)
}

特征在这里声明相关的隐式转换:

代码语言:javascript
运行
复制
/**
 * 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
票数 6
EN

Stack Overflow用户

发布于 2011-03-24 02:13:28

这里有关于MustMatchers的文档,还有关于mustshould用法的详细解释。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5409615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档