Skip to content

Add a StringSubject.containsAll method, with a case-insensitive variation. #1547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions core/src/main/java/com/google/common/truth/StringSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> 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);
Expand Down Expand Up @@ -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<String> 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<String> 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);
Expand Down
29 changes: 29 additions & 0 deletions core/src/test/java/com/google/common/truth/StringSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -184,6 +197,7 @@ public void endsWithFail() {
@Test
public void emptyStringTests() {
assertThat("").contains("");
assertThat("").containsAllOf("");
assertThat("").startsWith("");
assertThat("").endsWith("");
assertThat("a").contains("");
Expand Down Expand Up @@ -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");
Expand Down
Loading