Skip to content

Commit

Permalink
feat: add stringContainsInAnyOrder matcher
Browse files Browse the repository at this point in the history
resolves hamcrest#346
  • Loading branch information
aoudiamoncef committed May 27, 2021
1 parent 8522353 commit 5dc57fd
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
30 changes: 29 additions & 1 deletion hamcrest/src/main/java/org/hamcrest/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -1479,13 +1479,41 @@ public static Matcher<java.lang.String> matchesPattern(java.lang.String regex) {
return org.hamcrest.text.MatchesPattern.matchesPattern(regex);
}

/**
* 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<java.lang.String> stringContainsInAnyOrder(java.lang.Iterable<java.lang.String> substrings) {
return org.hamcrest.text.StringContainsInAnyOrder.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<java.lang.String> stringContainsInAnyOrder(java.lang.String... substrings) {
return org.hamcrest.text.StringContainsInAnyOrder.stringContainsInAnyOrder(substrings);
}

/**
* Creates a matcher of {@link String} that matches when the examined string contains all of
* the specified substrings, considering the order of their appearance.
* For example:
* <pre>assertThat("myfoobarbaz", stringContainsInOrder(Arrays.asList("bar", "foo")))</pre>
* fails as "foo" occurs before "bar" in the string "myfoobarbaz"
*
*
* @param substrings
* the substrings that must be contained within matching strings
*/
Expand Down
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));
}
}
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);
}
}

0 comments on commit 5dc57fd

Please sign in to comment.