From 5dc57fd0a9b529c4d815be3670ab5a1cc75e8a6e Mon Sep 17 00:00:00 2001 From: AOUDIA Date: Thu, 27 May 2021 12:43:05 +0200 Subject: [PATCH] feat: add stringContainsInAnyOrder matcher resolves #346 --- .../src/main/java/org/hamcrest/Matchers.java | 30 ++++++++- .../text/StringContainsInAnyOrder.java | 66 +++++++++++++++++++ .../text/StringContainsInAnyOrderTest.java | 31 +++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 hamcrest/src/main/java/org/hamcrest/text/StringContainsInAnyOrder.java create mode 100644 hamcrest/src/test/java/org/hamcrest/text/StringContainsInAnyOrderTest.java diff --git a/hamcrest/src/main/java/org/hamcrest/Matchers.java b/hamcrest/src/main/java/org/hamcrest/Matchers.java index d4f543ea..f6bcc6a4 100644 --- a/hamcrest/src/main/java/org/hamcrest/Matchers.java +++ b/hamcrest/src/main/java/org/hamcrest/Matchers.java @@ -1479,13 +1479,41 @@ public static Matcher 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: + *
assertThat("mybarbaz", stringContainsInAnyOrder(Arrays.asList("bar", "foo")))
+ * fails as "foo" doesn't exist in the string "mybarbaz" + * + * @param substrings + * the substrings that must be contained within matching strings + */ + public static Matcher stringContainsInAnyOrder(java.lang.Iterable 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: + *
assertThat("mybarbaz", stringContainsInOrder("bar", "foo"))
+ * fails as "foo" doesn't exist in the string "mybarbaz" + * + * @param substrings + * the substrings that must be contained within matching strings + */ + public static Matcher 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: *
assertThat("myfoobarbaz", stringContainsInOrder(Arrays.asList("bar", "foo")))
* fails as "foo" occurs before "bar" in the string "myfoobarbaz" - * + * * @param substrings * the substrings that must be contained within matching strings */ diff --git a/hamcrest/src/main/java/org/hamcrest/text/StringContainsInAnyOrder.java b/hamcrest/src/main/java/org/hamcrest/text/StringContainsInAnyOrder.java new file mode 100644 index 00000000..b0cc4424 --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/text/StringContainsInAnyOrder.java @@ -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 { + private final Iterable substrings; + + public StringContainsInAnyOrder(Iterable 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: + *
assertThat("mybarbaz", stringContainsInAnyOrder(Arrays.asList("bar", "foo")))
+ * fails as "foo" doesn't exist in the string "mybarbaz" + * + * @param substrings + * the substrings that must be contained within matching strings + */ + public static Matcher stringContainsInAnyOrder(Iterable 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: + *
assertThat("mybarbaz", stringContainsInOrder("bar", "foo"))
+ * fails as "foo" doesn't exist in the string "mybarbaz" + * + * @param substrings + * the substrings that must be contained within matching strings + */ + public static Matcher stringContainsInAnyOrder(String... substrings) { + return new StringContainsInAnyOrder(Arrays.asList(substrings)); + } +} diff --git a/hamcrest/src/test/java/org/hamcrest/text/StringContainsInAnyOrderTest.java b/hamcrest/src/test/java/org/hamcrest/text/StringContainsInAnyOrderTest.java new file mode 100644 index 00000000..ed1e10b2 --- /dev/null +++ b/hamcrest/src/test/java/org/hamcrest/text/StringContainsInAnyOrderTest.java @@ -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); + } +}