File tree 2 files changed +31
-0
lines changed
main/java/com/google/common/truth
test/java/com/google/common/truth
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -129,6 +129,11 @@ public void matches(@Nullable String regex) {
129
129
fact ("expected to match" , regex ),
130
130
fact ("but was" , actual ),
131
131
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()?" ));
132
137
} else {
133
138
failWithActual ("expected to match" , regex );
134
139
}
@@ -149,6 +154,11 @@ public void matches(@Nullable Pattern regex) {
149
154
simpleFact (
150
155
"If you want an exact equality assertion you can escape your regex with"
151
156
+ " 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()?" ));
152
162
} else {
153
163
failWithActual ("expected to match" , regex );
154
164
}
Original file line number Diff line number Diff line change @@ -216,6 +216,16 @@ public void stringMatchesStringLiteralFail() {
216
216
.contains ("Looks like you want to use .isEqualTo() for an exact equality assertion." );
217
217
}
218
218
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
+
219
229
@ Test
220
230
@ GwtIncompatible ("Pattern" )
221
231
public void stringMatchesPattern () {
@@ -249,6 +259,17 @@ public void stringMatchesPatternLiteralFail() {
249
259
+ " Pattern.quote()." );
250
260
}
251
261
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
+
252
273
@ Test
253
274
public void stringDoesNotMatchString () {
254
275
assertThat ("abcaqadev" ).doesNotMatch (".*aaa.*" );
You can’t perform that action at this time.
0 commit comments