2016-06-17 4 views
7

한 문자열에 여러 하위 문자열이 포함되어 있는지 확인해야합니다. 다음 작품들문자열에 ScalaTest Matchers의 많은 하위 문자열이 포함되어 있음

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

그러나 3 개의 거의 중복 된 줄이 필요합니다.

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

그러나 이것은 작동하지 않습니다 ... 단, 어설 션은 확실히 이러한 부분 문자열을 포함하는 동안 실패합니다.

한 줄에 어떻게 그러한 어설 션을 수행 할 수 있습니까?

답변

8

이 시도 :

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

당신은 항상 정의 정규를 만들 수 있습니다

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(", ")}""") 
    } 

은 자세한 내용은 http://www.scalatest.org/user_guide/using_matchers#usingCustomMatchers를 참조하십시오.