JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

Feature/string contains in any order

Open tpraxl opened this issue 8 years ago • 3 comments

I kindly request to add stringContainsInAnyOrder as a complement to stringContainsInOrder. It matches if all of the substrings are contained within the subject, regardless of the order.

I'm aware that you are able to achieve the same with

assertThat("this is a string", allOf(
  contains("string"),
  contains("this"),
  contains("a")));

But there are cases when you need to work with an unknown list of strings, like String[] or List<String>.

For these cases, it would be nice to have

String[] tokens = classUnderTest.getTokens();
String subject = getTemplate();
assertThat(subject, stringContainsInAnyOrder(tokens))

tpraxl avatar Feb 22 '17 13:02 tpraxl

Just as another workaround for now, if you are on Java8:

assertThat(
    subject,
    allOf(Stream.of(tokens).map(Matchers::containsString).collect(Collectors.toList()))
);

yankee42 avatar Jul 16 '17 11:07 yankee42

@tpraxl looks a good idea, happy to look at getting merged if rebased from master as hamcrest-core and hamcrest-library has been merged into hamcrest so can't currently merge in at the moment.

nhojpatrick avatar Jun 28 '20 23:06 nhojpatrick

Going to try and kick start hamcrest, so if you want to get it merged, please rebase from the branch v2.3-candidates. Still trying to understand how has permissions to perform a release.

nhojpatrick avatar Feb 13 '22 12:02 nhojpatrick