"aka" 的 Scala specs2 匹配器

Scala specs2 matchers with "aka"

我想在使用 "aka" 提供标签时检查一个字符串是否包含另一个字符串。例如:

"31 west 23rd street, NY" aka "address"  must contain("11065")

失败

address '31 west 23rd street, NY' doesn't contain '11065'.

但是,我想指定 11066 是邮政编码。即:

"31 west 23rd street, NY" aka "address"  must contain("11065") aka "zip code"

这行不通。

知道如何实现吗? 我期望的所需结果是:

address '31 west 23rd street, NY' doesn't contain zip code '11065'.

下面是一个可能的解决方案,但我不喜欢它,因为它不是 spec2 原生的并且只支持字符串:

def contain(needle: String, aka: String) = new Matcher[String] {
  def apply[S <: String](b: Expectable[S]) = {
    result(needle != null && b.value != null && b.value.contains(needle),
      s"${b.description} contains $aka '$needle'",
      s"${b.description} doesn't contain $aka '$needle'", b)
  }
}

我认为没有适用于所有匹配器的解决方案。在这种情况下,您可以重复使用 aka 机器

def contain(expected: Expectable[String]): Matcher[String] = new Matcher[String] {
  def apply[S <: String](e: Expectable[S]): MatchResult[S] =
    result(e.value.contains(expected.value),
      s" ${e.value} contains ${expected.description} ${expected.value}",
      s" ${e.value} does not contain ${expected.description}",
      e)
}

"31 west 23rd street, NY" aka "address"  must contain("11065" aka "the zip code")

这显示

 31 west 23rd street, NY does not contain the zip code '11065'