8

I have the following test

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

There is a scalatest idiomatic way to do this?

Kara
  • 6,115
  • 16
  • 50
  • 57
raisercostin
  • 8,777
  • 5
  • 67
  • 76

1 Answers1

13

I found that there is some normalization for case insensitive comparisons at 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)

I created the following normalizer

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"
  }

The test is now

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)
Pascalius
  • 14,024
  • 4
  • 40
  • 38
raisercostin
  • 8,777
  • 5
  • 67
  • 76
  • 1
    for multi-line strings, you may want to use ```def normalized(s: String): String = s.replaceAll("(?s)\\s+", " ").trim``` – botkop Dec 28 '17 at 14:08