Skip to content

Commit 98bf5b5

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 98bf5b5

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,25 @@ 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+
assert actual != null; // For nullness checker.
106+
ImmutableList<String> missing =
107+
expected.stream().filter(string -> !actual.contains(string)).collect(toImmutableList());
108+
if (!missing.isEmpty()) {
109+
failWithoutActual(
110+
fact("expected to contain all of", expected), butWas(), fact("missing", missing));
111+
}
112+
}
113+
90114
/** Checks that the actual value does not contain the given sequence. */
91115
public void doesNotContain(@Nullable CharSequence string) {
92116
checkNotNull(string);
@@ -321,6 +345,35 @@ public void contains(@Nullable CharSequence string) {
321345
}
322346
}
323347

348+
/** Checks that the actual value contains the given sequences. */
349+
public void containsAllOf(@Nullable CharSequence... strings) {
350+
checkNotNull(strings);
351+
ImmutableSet<String> expected =
352+
stream(strings)
353+
.map(charSeq -> checkNotNull(charSeq).toString())
354+
.collect(toImmutableSet());
355+
checkArgument(expected.size() == strings.length, "duplicate strings in expected");
356+
if (actual == null) {
357+
failWithoutActual(
358+
fact("expected a string that contains all of", expected),
359+
butWas(),
360+
simpleFact("(case is ignored)"));
361+
return;
362+
}
363+
assert actual != null; // For nullness checker.
364+
ImmutableList<String> missing =
365+
expected.stream()
366+
.filter(string -> !containsIgnoreCase(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)