如何使用规范测试框架从Matcher [A]中编写Matcher [Iterable [A]]

时间:2010-06-05 06:16:20

标签: scala composition specs matcher

如果我有一个匹配器[A]如何创建一个匹配器[Iterable [A]],只有当Iterable的每个元素满足原始匹配器时才会满足。

class ExampleSpec extends Specification {
  def allSatisfy[A](m: => Matcher[A]): Matcher[Iterable[A]] = error("TODO")
  def notAllSatisfy[A](m: => Matcher[A]): Matcher[Iterable[A]] = allSatisfy(m).not    

   "allSatisfy" should {
     "Pass if all elements satisfy the expectation" in {
      List(1, 2, 3, 4) must allSatisfy(beLessThan(5))
    }

    "Fail if any elements do not satisfy the expectation" in {
      List(1, 2, 3, 5) must notAllSatisfy(beLessThan(5))
    }
  }
}

2 个答案:

答案 0 :(得分:5)

我当然不会声称自己是Specs专家,所以我的代码很可能会得到很大的改进。无论如何,我能够让它像这样工作:


class ExampleSpec extends Specification {
  def allSatisfy[A](m: Matcher[A]): Matcher[Iterable[A]] = new Matcher[Iterable[A]]() {
    def apply(v: => Iterable[A]) = {
      val iterable = v
      (iterable.forall(e => {println("checking el " + e); m(e)._1}), "all elements match", "not all elements match")
    }
  }

  def notAllSatisfy[A](m: => Matcher[A]): Matcher[Iterable[A]] = allSatisfy(m).not

  "allSatisfy" should {
    "Pass if all elements satisfy the expectation" in {
      List(1, 2, 3, 4) must allSatisfy(beLessThan(5))
    }

    "Fail if any elements do not satisfy the expectation" in {
      List(1, 2, 3, 5) must notAllSatisfy(beLessThan(5))
    }
  }
}

答案 1 :(得分:1)

我认为我已经开箱即用,但事实并非如此。我计划在接下来的日子里将其添加到规格中:

import org.specs._
import org.specs.matcher._
object SeqMatcher extends Specification {
  implicit def toSeqMatcher[T](m: Matcher[T]) = new ToSeqMatcher(m)
  class ToSeqMatcher[T](m: Matcher[T]) {
    def toSeq: Matcher[Seq[T]] = new Matcher[Seq[T]]() {
      type Res = (Boolean, String, String)
      def apply(v: =>Seq[T]) = ((true, "", "") /: v) { (res: Res, cur: T) =>
        def append(first: String, separator: String, second: String) = 
           first + (if (first.isEmpty) "" else separator) + second
        val currentRes = m(cur)
        if (currentRes._1) 
          (res._1 && currentRes._1, append(res._2, " and ", currentRes._2),  append(res._3, " and ", currentRes._2))
        else
          (res._1 && currentRes._1, append(res._2, " and ", currentRes._2),  append(res._2, " but ", currentRes._2))
      }
    }
  }
  List(1, 2, 6).toSeq must beLessThan(5).toSeq
}
SeqMatcher.reportSpecs

那将打印出来:

x example 1
    1 is less than 5 and 2 is less than 5 but 6 is less than 5 

敬请关注!

埃里克。