首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ScalaTest测试是否排序

ScalaTest测试是否排序
EN

Stack Overflow用户
提问于 2013-07-29 17:46:15
回答 2查看 2.8K关注 0票数 3

为什么不使用ScalaTest 2.0.M5b进行编译?

代码语言:javascript
复制
import org.scalatest.matchers.MatchResult
import org.scalatest.matchers.BeMatcher
import org.scalatest.matchers.ShouldMatchers._

def sorted[T <% Ordered[T]] = new BeMatcher[Seq[T]] {
  override def apply(s: Seq[T]) =
    MatchResult(
        s match {
          case Seq(h, t@_*) => s.zip(t).forall{ case (x,y) => x < y }
          case _ => true
        },
        s + " was not sorted",
        s + " was sorted")
}

val s = Seq(1, 2, 3)
s should be (sorted[Int])

这是我得到的错误:

代码语言:javascript
复制
overloaded method value should with alternatives: (beWord: NewCollectionsSpec.this.BeWord)NewCollectionsSpec.this.ResultOfBeWordForAnyRef[scala.collection.GenSeq[Int]] <and> (notWord: 
 NewCollectionsSpec.this.NotWord)NewCollectionsSpec.this.ResultOfNotWordForAnyRef[scala.collection.GenSeq[Int]] <and> (haveWord: 
 NewCollectionsSpec.this.HaveWord)NewCollectionsSpec.this.ResultOfHaveWordForSeq[Int] <and> (rightMatcher: org.scalatest.matchers.Matcher[scala.collection.GenSeq[Int]])Unit cannot be applied to 
 (org.scalatest.matchers.Matcher[Seq[Int]])
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-29 18:52:41

s: Seq[Int]被隐式转换为SeqShouldWrapper[GenSeq[Int]]。它没有与should兼容的Matcher[Seq[Int]]替代方案。但它有def should(rightMatcher: Matcher[GenSeq[T]])。因此,如果更改matcher类型约束,所有内容都会编译:

代码语言:javascript
复制
def sorted[T <% Ordered[T]] = new BeMatcher[GenSeq[T]] {
  def apply(s: GenSeq[T]) =
    MatchResult(
      s match {
        case Seq(h, t@_*) => s.zip(t).forall {
          case (x, y) => x < y
        }
        case _ => true
      },
      s + " was not sorted",
      s + " was sorted")
}
票数 1
EN

Stack Overflow用户

发布于 2016-02-21 19:09:14

Scalatest已经提供了这样一个匹配器:

代码语言:javascript
复制
seq shouldBe sorted

注意,类似的代码

代码语言:javascript
复制
seq should be sorted

不编译。

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

https://stackoverflow.com/questions/17930718

复制
相关文章

相似问题

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