Skip to content

Commit 7c56121

Browse files
java-team-github-botGoogle Java Core Libraries
authored andcommitted
Add a StringSubject.containsAll method, with a case-insensitive variation.
RELNOTES=Adds a StringSubject.containsAll method. PiperOrigin-RevId: 788927984
1 parent c24d253 commit 7c56121

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

core/src/main/java/com/google/common/truth/StringSubject.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.common.truth.Fact.simpleFact;
2222

2323
import com.google.common.annotations.GwtIncompatible;
24+
import com.google.common.collect.ImmutableSet;
2425
import java.util.regex.Matcher;
2526
import java.util.regex.Pattern;
2627
import org.jspecify.annotations.Nullable;
@@ -87,6 +88,27 @@ public void contains(@Nullable CharSequence string) {
8788
}
8889
}
8990

91+
/** Checks that the actual value contains the given sequences. */
92+
public void containsAllOf(@Nullable CharSequence... strings) {
93+
checkNotNull(strings);
94+
ImmutableSet<String> expected = ImmutableSet.copyOf(strings);
95+
checkArgument(expected.size() == strings.length, "duplicate strings in expected");
96+
if (actual == null) {
97+
failWithActual("expected a string that contains all of", expected);
98+
return;
99+
}
100+
List<String> missing = new ArrayList<>();
101+
for (String string : expected) {
102+
if (!actual.contains(string)) {
103+
missing.add(string);
104+
}
105+
}
106+
if (!missing.isEmpty()) {
107+
failWithoutActual(
108+
fact("expected to contain all of", expected), butWas(), fact("missing", missing));
109+
}
110+
}
111+
90112
/** Checks that the actual value does not contain the given sequence. */
91113
public void doesNotContain(@Nullable CharSequence string) {
92114
checkNotNull(string);
@@ -321,6 +343,33 @@ public void contains(@Nullable CharSequence string) {
321343
}
322344
}
323345

346+
/** Checks that the actual value contains the given sequences. */
347+
public void containsAllOf(@Nullable CharSequence... strings) {
348+
checkNotNull(strings);
349+
ImmutableSet<String> expected = ImmutableSet.copyOf(strings);
350+
checkArgument(expected.size() == strings.length, "duplicate strings in expected");
351+
if (actual == null) {
352+
failWithoutActual(
353+
fact("expected a string that contains all of", expected),
354+
butWas(),
355+
simpleFact("(case is ignored)"));
356+
return;
357+
}
358+
List<String> missing = new ArrayList<>();
359+
for (String string : expected) {
360+
if (!containsIgnoreCase(actual, string)) {
361+
missing.add(string);
362+
}
363+
}
364+
if (!missing.isEmpty()) {
365+
failWithoutActual(
366+
fact("expected to contain all of", expected),
367+
butWas(),
368+
fact("missing", missing),
369+
simpleFact("(case is ignored)"));
370+
}
371+
}
372+
324373
/** Checks that the actual value does not contain the given sequence (while ignoring case). */
325374
public void doesNotContain(@Nullable CharSequence string) {
326375
checkNotNull(string);

core/src/test/java/com/google/common/truth/StringSubjectTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ public void containsFail() {
112112
assertFailureValue(e, "expected to contain", "d");
113113
}
114114

115+
@Test
116+
public void containsAllOf() {
117+
assertThat("abc").containsAllOf("a", "c", "b");
118+
}
119+
120+
@Test
121+
public void containsAllOfFail() {
122+
AssertionError e =
123+
expectFailure(whenTesting -> whenTesting.that("abc").containsAllOf("a", "-", "b", "d"));
124+
assertFailureValue(e, "expected to contain all of", "[a, -, b, d]");
125+
assertFailureValue(e, "missing", "[-, d]");
126+
}
127+
115128
@Test
116129
public void doesNotContain() {
117130
assertThat("abc").doesNotContain("d");
@@ -184,6 +197,7 @@ public void endsWithFail() {
184197
@Test
185198
public void emptyStringTests() {
186199
assertThat("").contains("");
200+
assertThat("").containsAllOf("");
187201
assertThat("").startsWith("");
188202
assertThat("").endsWith("");
189203
assertThat("a").contains("");
@@ -533,6 +547,21 @@ public void containsIgnoringCaseFailBecauseNullSubject() {
533547
assertThat(e).factKeys().contains("(case is ignored)");
534548
}
535549

550+
@Test
551+
public void containsAllOfIgnoringCase() {
552+
assertThat("AbCd").ignoringCase().containsAllOf("a", "c", "b");
553+
}
554+
555+
@Test
556+
public void containsAllOfIgnoringCaseFail() {
557+
AssertionError e =
558+
expectFailure(
559+
whenTesting ->
560+
whenTesting.that("AbC").ignoringCase().containsAllOf("a", "-", "b", "d"));
561+
assertFailureValue(e, "expected to contain all of", "[a, -, b, d]");
562+
assertFailureValue(e, "missing", "[-, d]");
563+
}
564+
536565
@Test
537566
public void doesNotContainIgnoringCase() {
538567
assertThat("äbc").ignoringCase().doesNotContain("Äc");

0 commit comments

Comments
 (0)