比较两个字符串时如何使scalatest匹配器忽略空格?

时间:2015-06-09 10:11:54

标签: scala scalatest

我有以下测试

  "Matchers" should "ignore whitespace if configured so" in {
    " aaa \n \n\r bbb".replaceAll("\\s+", " ") shouldBe "\naaa bbb".replaceAll("\\s+", " ")
  }

有一种最常用的惯用方法吗?

1 个答案:

答案 0 :(得分:11)

我发现在http://www.scalatest.org/user_guide/using_matchers

进行不区分大小写的比较时存在一些规范化
import org.scalatest.Matchers._
import org.scalactic.Explicitly._
import org.scalactic.StringNormalizations._
"Hi" should equal ("hi") (after being lowerCased)

我创建了以下规范化程序

import org.scalactic._
val whiteSpaceNormalised: Uniformity[String] =
  new AbstractStringUniformity {
    /**Returns the string with all consecutive white spaces reduced to a single space.*/
    def normalized(s: String): String = s.replaceAll("\\s+", " ")
    override def toString: String = "whiteSpaceNormalised"
  }

现在测试

import org.scalatest.Matchers._
import org.scalactic.Explicitly._
import org.scalactic.StringNormalizations._
" aaa \n \n\r bbb " should equal("\naaa bbb      \t")(after being whiteSpaceNormalised)