Skip to content

Commit 7e9fc7a

Browse files
junyan59Google Java Core Libraries
authored and
Google Java Core Libraries
committedJun 27, 2024·
Make StringSubject.matches suggest using containsMatch if matches(x) 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
1 parent af140d6 commit 7e9fc7a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎core/src/main/java/com/google/common/truth/StringSubject.java

+10
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public void matches(@Nullable String regex) {
129129
fact("expected to match", regex),
130130
fact("but was", actual),
131131
simpleFact("Looks like you want to use .isEqualTo() for an exact equality assertion."));
132+
} else if (Platform.containsMatch(actual, regex)) {
133+
failWithoutActual(
134+
fact("expected to match", regex),
135+
fact("but was", actual),
136+
simpleFact("Did you mean to call containsMatch() instead of match()?"));
132137
} else {
133138
failWithActual("expected to match", regex);
134139
}
@@ -149,6 +154,11 @@ public void matches(@Nullable Pattern regex) {
149154
simpleFact(
150155
"If you want an exact equality assertion you can escape your regex with"
151156
+ " Pattern.quote()."));
157+
} else if (regex.matcher(actual).find()) {
158+
failWithoutActual(
159+
fact("expected to match", regex),
160+
fact("but was", actual),
161+
simpleFact("Did you mean to call containsMatch() instead of match()?"));
152162
} else {
153163
failWithActual("expected to match", regex);
154164
}

‎core/src/test/java/com/google/common/truth/StringSubjectTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ public void stringMatchesStringLiteralFail() {
216216
.contains("Looks like you want to use .isEqualTo() for an exact equality assertion.");
217217
}
218218

219+
@Test
220+
public void stringMatchesStringLiteralFailButContainsMatchSuccess() {
221+
expectFailureWhenTestingThat("aba").matches("[b]");
222+
assertFailureValue("expected to match", "[b]");
223+
assertFailureValue("but was", "aba");
224+
assertThat(expectFailure.getFailure())
225+
.factKeys()
226+
.contains("Did you mean to call containsMatch() instead of match()?");
227+
}
228+
219229
@Test
220230
@GwtIncompatible("Pattern")
221231
public void stringMatchesPattern() {
@@ -249,6 +259,17 @@ public void stringMatchesPatternLiteralFail() {
249259
+ " Pattern.quote().");
250260
}
251261

262+
@Test
263+
@GwtIncompatible("Pattern")
264+
public void stringMatchesPatternLiteralFailButContainsMatchSuccess() {
265+
expectFailureWhenTestingThat("aba").matches(Pattern.compile("[b]"));
266+
assertFailureValue("expected to match", "[b]");
267+
assertFailureValue("but was", "aba");
268+
assertThat(expectFailure.getFailure())
269+
.factKeys()
270+
.contains("Did you mean to call containsMatch() instead of match()?");
271+
}
272+
252273
@Test
253274
public void stringDoesNotMatchString() {
254275
assertThat("abcaqadev").doesNotMatch(".*aaa.*");

0 commit comments

Comments
 (0)
Please sign in to comment.