From 7c56121a52114772e900128ebff67ee973974f68 Mon Sep 17 00:00:00 2001 From: Google Java Core Libraries Date: Wed, 30 Jul 2025 09:22:08 -0700 Subject: [PATCH] Add a StringSubject.containsAll method, with a case-insensitive variation. RELNOTES=Adds a StringSubject.containsAll method. PiperOrigin-RevId: 788927984 --- .../google/common/truth/StringSubject.java | 49 +++++++++++++++++++ .../common/truth/StringSubjectTest.java | 29 +++++++++++ 2 files changed, 78 insertions(+) diff --git a/core/src/main/java/com/google/common/truth/StringSubject.java b/core/src/main/java/com/google/common/truth/StringSubject.java index f32a34db5..23a841f80 100644 --- a/core/src/main/java/com/google/common/truth/StringSubject.java +++ b/core/src/main/java/com/google/common/truth/StringSubject.java @@ -21,6 +21,7 @@ import static com.google.common.truth.Fact.simpleFact; import com.google.common.annotations.GwtIncompatible; +import com.google.common.collect.ImmutableSet; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jspecify.annotations.Nullable; @@ -87,6 +88,27 @@ public void contains(@Nullable CharSequence string) { } } + /** Checks that the actual value contains the given sequences. */ + public void containsAllOf(@Nullable CharSequence... strings) { + checkNotNull(strings); + ImmutableSet expected = ImmutableSet.copyOf(strings); + checkArgument(expected.size() == strings.length, "duplicate strings in expected"); + if (actual == null) { + failWithActual("expected a string that contains all of", expected); + return; + } + List missing = new ArrayList<>(); + for (String string : expected) { + if (!actual.contains(string)) { + missing.add(string); + } + } + if (!missing.isEmpty()) { + failWithoutActual( + fact("expected to contain all of", expected), butWas(), fact("missing", missing)); + } + } + /** Checks that the actual value does not contain the given sequence. */ public void doesNotContain(@Nullable CharSequence string) { checkNotNull(string); @@ -321,6 +343,33 @@ public void contains(@Nullable CharSequence string) { } } + /** Checks that the actual value contains the given sequences. */ + public void containsAllOf(@Nullable CharSequence... strings) { + checkNotNull(strings); + ImmutableSet expected = ImmutableSet.copyOf(strings); + checkArgument(expected.size() == strings.length, "duplicate strings in expected"); + if (actual == null) { + failWithoutActual( + fact("expected a string that contains all of", expected), + butWas(), + simpleFact("(case is ignored)")); + return; + } + List missing = new ArrayList<>(); + for (String string : expected) { + if (!containsIgnoreCase(actual, string)) { + missing.add(string); + } + } + if (!missing.isEmpty()) { + failWithoutActual( + fact("expected to contain all of", expected), + butWas(), + fact("missing", missing), + simpleFact("(case is ignored)")); + } + } + /** Checks that the actual value does not contain the given sequence (while ignoring case). */ public void doesNotContain(@Nullable CharSequence string) { checkNotNull(string); diff --git a/core/src/test/java/com/google/common/truth/StringSubjectTest.java b/core/src/test/java/com/google/common/truth/StringSubjectTest.java index 8f2640b53..cdcde0997 100644 --- a/core/src/test/java/com/google/common/truth/StringSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/StringSubjectTest.java @@ -112,6 +112,19 @@ public void containsFail() { assertFailureValue(e, "expected to contain", "d"); } + @Test + public void containsAllOf() { + assertThat("abc").containsAllOf("a", "c", "b"); + } + + @Test + public void containsAllOfFail() { + AssertionError e = + expectFailure(whenTesting -> whenTesting.that("abc").containsAllOf("a", "-", "b", "d")); + assertFailureValue(e, "expected to contain all of", "[a, -, b, d]"); + assertFailureValue(e, "missing", "[-, d]"); + } + @Test public void doesNotContain() { assertThat("abc").doesNotContain("d"); @@ -184,6 +197,7 @@ public void endsWithFail() { @Test public void emptyStringTests() { assertThat("").contains(""); + assertThat("").containsAllOf(""); assertThat("").startsWith(""); assertThat("").endsWith(""); assertThat("a").contains(""); @@ -533,6 +547,21 @@ public void containsIgnoringCaseFailBecauseNullSubject() { assertThat(e).factKeys().contains("(case is ignored)"); } + @Test + public void containsAllOfIgnoringCase() { + assertThat("AbCd").ignoringCase().containsAllOf("a", "c", "b"); + } + + @Test + public void containsAllOfIgnoringCaseFail() { + AssertionError e = + expectFailure( + whenTesting -> + whenTesting.that("AbC").ignoringCase().containsAllOf("a", "-", "b", "d")); + assertFailureValue(e, "expected to contain all of", "[a, -, b, d]"); + assertFailureValue(e, "missing", "[-, d]"); + } + @Test public void doesNotContainIgnoringCase() { assertThat("äbc").ignoringCase().doesNotContain("Äc");