Skip to content

Commit 072b3b3

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 072b3b3

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.collect.ImmutableList.toImmutableList;
21+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
2022
import static com.google.common.truth.Fact.fact;
2123
import static com.google.common.truth.Fact.simpleFact;
24+
import static java.util.Arrays.stream;
2225

2326
import com.google.common.annotations.GwtIncompatible;
27+
import com.google.common.collect.ImmutableList;
28+
import com.google.common.collect.ImmutableSet;
2429
import java.util.regex.Matcher;
2530
import java.util.regex.Pattern;
2631
import org.jspecify.annotations.Nullable;
@@ -87,6 +92,26 @@ public void contains(@Nullable CharSequence string) {
8792
}
8893
}
8994

95+
/** Checks that the actual value contains the given sequences. */
96+
public void containsAllOf(@Nullable CharSequence... strings) {
97+
checkNotNull(strings);
98+
ImmutableSet<String> expected =
99+
stream(strings).map(charSeq -> checkNotNull(charSeq).toString()).collect(toImmutableSet());
100+
checkArgument(expected.size() == strings.length, "duplicate strings in expected");
101+
if (actual == null) {
102+
failWithActual("expected a string that contains all of", expected);
103+
return;
104+
}
105+
ImmutableList<String> missing =
106+
expected.stream()
107+
.filter(string -> !checkNotNull(actual).contains(string))
108+
.collect(toImmutableList());
109+
if (!missing.isEmpty()) {
110+
failWithoutActual(
111+
fact("expected to contain all of", expected), butWas(), fact("missing", missing));
112+
}
113+
}
114+
90115
/** Checks that the actual value does not contain the given sequence. */
91116
public void doesNotContain(@Nullable CharSequence string) {
92117
checkNotNull(string);
@@ -321,6 +346,34 @@ public void contains(@Nullable CharSequence string) {
321346
}
322347
}
323348

349+
/** Checks that the actual value contains the given sequences. */
350+
public void containsAllOf(@Nullable CharSequence... strings) {
351+
checkNotNull(strings);
352+
ImmutableSet<String> expected =
353+
stream(strings)
354+
.map(charSeq -> checkNotNull(charSeq).toString())
355+
.collect(toImmutableSet());
356+
checkArgument(expected.size() == strings.length, "duplicate strings in expected");
357+
if (actual == null) {
358+
failWithoutActual(
359+
fact("expected a string that contains all of", expected),
360+
butWas(),
361+
simpleFact("(case is ignored)"));
362+
return;
363+
}
364+
ImmutableList<String> missing =
365+
expected.stream()
366+
.filter(string -> !containsIgnoreCase(checkNotNull(actual), string))
367+
.collect(toImmutableList());
368+
if (!missing.isEmpty()) {
369+
failWithoutActual(
370+
fact("expected to contain all of", expected),
371+
butWas(),
372+
fact("missing", missing),
373+
simpleFact("(case is ignored)"));
374+
}
375+
}
376+
324377
/** Checks that the actual value does not contain the given sequence (while ignoring case). */
325378
public void doesNotContain(@Nullable CharSequence string) {
326379
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)