gpt4 book ai didi

ScalaTest 测试序列是否已排序

转载 作者:行者123 更新时间:2023-12-04 17:54:09 27 4
gpt4 key购买 nike

为什么这不能用 ScalaTest 2.0.M5b 编译?

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])

这是我得到的错误:
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]])

最佳答案

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

seq shouldBe sorted

请注意,类似的代码
seq should be sorted

不编译。

关于ScalaTest 测试序列是否已排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17930718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com