|
21 | 21 | import static com.google.common.truth.Fact.simpleFact;
|
22 | 22 |
|
23 | 23 | import com.google.common.annotations.GwtIncompatible;
|
| 24 | +import com.google.common.collect.ImmutableSet; |
24 | 25 | import java.util.regex.Matcher;
|
25 | 26 | import java.util.regex.Pattern;
|
26 | 27 | import org.jspecify.annotations.Nullable;
|
@@ -87,6 +88,27 @@ public void contains(@Nullable CharSequence string) {
|
87 | 88 | }
|
88 | 89 | }
|
89 | 90 |
|
| 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 | + |
90 | 112 | /** Checks that the actual value does not contain the given sequence. */
|
91 | 113 | public void doesNotContain(@Nullable CharSequence string) {
|
92 | 114 | checkNotNull(string);
|
@@ -321,6 +343,33 @@ public void contains(@Nullable CharSequence string) {
|
321 | 343 | }
|
322 | 344 | }
|
323 | 345 |
|
| 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 | + |
324 | 373 | /** Checks that the actual value does not contain the given sequence (while ignoring case). */
|
325 | 374 | public void doesNotContain(@Nullable CharSequence string) {
|
326 | 375 | checkNotNull(string);
|
|
0 commit comments