The string includes many substrings in ScalaTest Matchers

I need to check that one line contains many substrings. Next works

string should include ("seven") string should include ("eight") string should include ("nine") 

but it takes three almost duplicated lines. I'm looking for something like

 string should contain allOf ("seven", "eight", "nine") 

however, this will not work ... The statement just fails, while the string contains these substrings for sure.

How can I execute such a statement on one line?

+7
string scala matcher scalatest
source share
2 answers

Try the following:

 string should (include("seven") and include("eight") and include("nine")) 
+8
source share

You can always create your own connector:

 it should "..." in { "str asd dsa ddsd" should includeAllOf ("r as", "asd", "dd") } def includeAllOf(expectedSubstrings: String*): Matcher[String] = new Matcher[String] { def apply(left: String): MatchResult = MatchResult(expectedSubstrings forall left.contains, s"""String "$left" did not include all of those substrings: ${expectedSubstrings.map(s => s""""$s"""").mkString(", ")}""", s"""String "$left" contained all of those substrings: ${expectedSubstrings.map(s => s""""$s"""").mkString(", ")}""") } 

See http://www.scalatest.org/user_guide/using_matchers#usingCustomMatchers for more details.

+5
source share

All Articles