加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 18:53:53 所属栏目:安全 来源:网络整理
导读:我有以下测试 "Matchers" should "ignore whitespace if configured so" in { " aaa n nr bbb".replaceAll("s+"," ") shouldBe "naaa bbb".replaceAll("s+"," ") } 有一种最神奇的惯用方法吗? 解决方法 我发现在 http://www.scalatest.org/user_gui
我有以下测试

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

有一种最神奇的惯用方法吗?

解决方法

我发现在 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 nr bbb " should equal("naaa bbb      t")(after being whiteSpaceNormalised)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读