Skip to content

Commit

Permalink
Match multiple substrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Ouwens committed Mar 17, 2012
1 parent 3136dd0 commit 5360de0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ object BoiteMatchers {
def apply(left: Box[_]) =
BePropertyMatchResult(left.isInstanceOf[Failure], "failure")

def saying(message: String) =
def saying(messages: String*) =
new BePropertyMatcher[Box[_]] {
def apply(left: Box[_]) = {
val matches = left match {
case Failure(e) => e.getMessage contains message
case Failure(e) => messages forall { e.getMessage contains _ }
case _ => false
}
BePropertyMatchResult(matches, "failure with message \"" + message + "\"")
BePropertyMatchResult(matches, "failure saying " + messages.mkString("\"", "\", \"", "\""))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@ class FailMatcherTest extends FlatSpec with ShouldMatchers {
anEmpty should not be a (failure saying aMessage)
}

it should "match a partial message" in {
it should "match a substring" in {
aFailure should be a (failure saying "message")
}

it should "match several substrings" in {
aFailure should be a (failure saying ("this", "exception", "message"))
}

it should "not match if one substring doesn't match" in {
aFailure should not be a (failure saying ("this", "exception", "does not match"))
}

it should "give an appropriate error message" in {
val thrown = intercept[TestFailedException] {
anEmpty should be a (failure saying aMessage)
anEmpty should be a (failure saying ("this", "exception", "message"))
}
thrown.getMessage should include ("failure with message \"" + aMessage + "\"")
thrown.getMessage should include ("failure saying \"this\", \"exception\", \"message\"")
}
}

0 comments on commit 5360de0

Please sign in to comment.