-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add stringContainsInAnyOrder matcher
resolves #346
- Loading branch information
1 parent
3929ae3
commit 151c0ee
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
hamcrest/src/main/java/org/hamcrest/text/StringContainsInAnyOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.hamcrest.text; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
|
||
import java.util.Arrays; | ||
|
||
public class StringContainsInAnyOrder extends TypeSafeMatcher<String> { | ||
private final Iterable<String> substrings; | ||
|
||
public StringContainsInAnyOrder(Iterable<String> substrings) { | ||
this.substrings = substrings; | ||
} | ||
|
||
@Override | ||
public boolean matchesSafely(String s) { | ||
for (String substring : substrings) { | ||
if (!s.contains(substring) ) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public void describeMismatchSafely(String item, Description mismatchDescription) { | ||
mismatchDescription.appendText("was \"").appendText(item).appendText("\""); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("a string containing ") | ||
.appendValueList("", ", ", "", substrings) | ||
.appendText(" in any order"); | ||
} | ||
|
||
/** | ||
* Creates a matcher of {@link String} that matches when the examined string contains all of | ||
* the specified substrings. | ||
* For example: | ||
* <pre>assertThat("mybarbaz", stringContainsInAnyOrder(Arrays.asList("bar", "foo")))</pre> | ||
* fails as "foo" doesn't exist in the string "mybarbaz" | ||
* | ||
* @param substrings | ||
* the substrings that must be contained within matching strings | ||
*/ | ||
public static Matcher<String> stringContainsInAnyOrder(Iterable<String> substrings) { | ||
return new StringContainsInAnyOrder(substrings); | ||
} | ||
|
||
/** | ||
* Creates a matcher of {@link String} that matches when the examined string contains all of | ||
* the specified substrings. | ||
* For example: | ||
* <pre>assertThat("mybarbaz", stringContainsInOrder("bar", "foo"))</pre> | ||
* fails as "foo" doesn't exist in the string "mybarbaz" | ||
* | ||
* @param substrings | ||
* the substrings that must be contained within matching strings | ||
*/ | ||
public static Matcher<String> stringContainsInAnyOrder(String... substrings) { | ||
return new StringContainsInAnyOrder(Arrays.asList(substrings)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
hamcrest/src/test/java/org/hamcrest/text/StringContainsInAnyOrderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.hamcrest.text; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.hamcrest.text.StringContainsInAnyOrder.stringContainsInAnyOrder; | ||
|
||
import org.hamcrest.AbstractMatcherTest; | ||
import org.hamcrest.Matcher; | ||
|
||
|
||
public class StringContainsInAnyOrderTest extends AbstractMatcherTest { | ||
final StringContainsInAnyOrder matcher = new StringContainsInAnyOrder(asList("a", "b", "c")); | ||
|
||
@Override | ||
protected Matcher<?> createMatcher() { | ||
return matcher; | ||
} | ||
|
||
public void testMatchesOnlyIfStringContainsGivenSubstringsInTheSameOrder() { | ||
assertMatches("substrings in order", matcher, "abcccccc"); | ||
assertMatches("substrings out of order", matcher, "cab"); | ||
assertMatches("substrings separated", matcher, "1c2a3b"); | ||
|
||
assertDoesNotMatch("no substrings in string", matcher, "xyz"); | ||
assertDoesNotMatch("substring missing", matcher, "ac"); | ||
assertDoesNotMatch("empty string", matcher, ""); | ||
} | ||
|
||
public void testHasAReadableDescription() { | ||
assertDescription("a string containing \"a\", \"b\", \"c\" in any order", matcher); | ||
} | ||
} |