Skip to content

Commit

Permalink
Make StringSubject.matches suggest using containsMatch if matches(x) …
Browse files Browse the repository at this point in the history
…fails but containsMatch(x) would have passed.

Fixes #791

Fixes #830

RELNOTES=Improved the failure message for `matches` to conditionally suggest using `containsMatch`.
COPYBARA_INTEGRATE_REVIEW=#830 from junyan59:stringMatches cc02dca
PiperOrigin-RevId: 647435705
  • Loading branch information
junyan59 authored and Google Java Core Libraries committed Jun 27, 2024
1 parent af140d6 commit 7e9fc7a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 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 @@ -129,6 +129,11 @@ public void matches(@Nullable String regex) {
fact("expected to match", regex),
fact("but was", actual),
simpleFact("Looks like you want to use .isEqualTo() for an exact equality assertion."));
} else if (Platform.containsMatch(actual, regex)) {
failWithoutActual(
fact("expected to match", regex),
fact("but was", actual),
simpleFact("Did you mean to call containsMatch() instead of match()?"));
} else {
failWithActual("expected to match", regex);
}
Expand All @@ -149,6 +154,11 @@ public void matches(@Nullable Pattern regex) {
simpleFact(
"If you want an exact equality assertion you can escape your regex with"
+ " Pattern.quote()."));
} else if (regex.matcher(actual).find()) {
failWithoutActual(
fact("expected to match", regex),
fact("but was", actual),
simpleFact("Did you mean to call containsMatch() instead of match()?"));
} else {
failWithActual("expected to match", regex);
}
Expand Down
21 changes: 21 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 @@ -216,6 +216,16 @@ public void stringMatchesStringLiteralFail() {
.contains("Looks like you want to use .isEqualTo() for an exact equality assertion.");
}

@Test
public void stringMatchesStringLiteralFailButContainsMatchSuccess() {
expectFailureWhenTestingThat("aba").matches("[b]");
assertFailureValue("expected to match", "[b]");
assertFailureValue("but was", "aba");
assertThat(expectFailure.getFailure())
.factKeys()
.contains("Did you mean to call containsMatch() instead of match()?");
}

@Test
@GwtIncompatible("Pattern")
public void stringMatchesPattern() {
Expand Down Expand Up @@ -249,6 +259,17 @@ public void stringMatchesPatternLiteralFail() {
+ " Pattern.quote().");
}

@Test
@GwtIncompatible("Pattern")
public void stringMatchesPatternLiteralFailButContainsMatchSuccess() {
expectFailureWhenTestingThat("aba").matches(Pattern.compile("[b]"));
assertFailureValue("expected to match", "[b]");
assertFailureValue("but was", "aba");
assertThat(expectFailure.getFailure())
.factKeys()
.contains("Did you mean to call containsMatch() instead of match()?");
}

@Test
public void stringDoesNotMatchString() {
assertThat("abcaqadev").doesNotMatch(".*aaa.*");
Expand Down

0 comments on commit 7e9fc7a

Please sign in to comment.