2016-06-17 25 views
7

'daki birçok alt dizeyi içerir Bir dizenin birçok alt dizgi içerdiğini kontrol etmem gerekiyor. Aşağıdakiler, neredeyse üç tane kopyalanmış satırı aldığındaDize, ScalaTest Matchers

çalışır. dize kesin bu alt dizeleri içeren ederken

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

ancak bu çalışmıyor gibi bir şey arıyorum ... onaylama işlemi sadece başarısız olur.

Tek bir satırda bu tür bir onaylamayı nasıl yapabilirim?

cevap

8

bu deneyin:

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

Hep özel eşleyici oluşturabilirsiniz:

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

fazla ayrıntı için http://www.scalatest.org/user_guide/using_matchers#usingCustomMatchers bakınız.