File tree Expand file tree Collapse file tree
main/java/com/google/common/truth
test/java/com/google/common/truth Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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.*" );
You can’t perform that action at this time.
0 commit comments