|
17 | 17 |
|
18 | 18 | import static com.google.common.base.Preconditions.checkArgument;
|
19 | 19 | 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; |
20 | 22 | import static com.google.common.truth.Fact.fact;
|
21 | 23 | import static com.google.common.truth.Fact.simpleFact;
|
| 24 | +import static java.util.Arrays.stream; |
22 | 25 |
|
23 | 26 | import com.google.common.annotations.GwtIncompatible;
|
| 27 | +import com.google.common.collect.ImmutableList; |
| 28 | +import com.google.common.collect.ImmutableSet; |
24 | 29 | import java.util.regex.Matcher;
|
25 | 30 | import java.util.regex.Pattern;
|
26 | 31 | import org.jspecify.annotations.Nullable;
|
@@ -87,6 +92,26 @@ public void contains(@Nullable CharSequence string) {
|
87 | 92 | }
|
88 | 93 | }
|
89 | 94 |
|
| 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 | + |
90 | 115 | /** Checks that the actual value does not contain the given sequence. */
|
91 | 116 | public void doesNotContain(@Nullable CharSequence string) {
|
92 | 117 | checkNotNull(string);
|
@@ -321,6 +346,34 @@ public void contains(@Nullable CharSequence string) {
|
321 | 346 | }
|
322 | 347 | }
|
323 | 348 |
|
| 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 | + |
324 | 377 | /** Checks that the actual value does not contain the given sequence (while ignoring case). */
|
325 | 378 | public void doesNotContain(@Nullable CharSequence string) {
|
326 | 379 | checkNotNull(string);
|
|
0 commit comments