Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add stringContainsInAnyOrder matcher #347

Open
wants to merge 1 commit into
base: v2.3-candidates
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,34 @@ 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.
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);
}
}