Closed renbinden closed 10 years ago
Hi Ross,
Thanks for the PR. I think this is useful functionality, but I'm a bit concerned about the diagnostic error messages that this produces. Because the implementation here uses assertTrue
I think the only error message you'll get is null
. I would prefer if it used something like the matchesPattern in the upcoming hamcrest version. See this pull request for details:
Hi Richard,
Hamcrest doesn't seem to have a repository for snapshot builds, though of course one option is waiting for the next version to be released. I don't mind waiting till the next Hamcrest version is out, expect.that(string.matches(regex)).is(true)
works for now.
Another option is specifying the error message with JUnit, so we get something a little more friendly:
public StringExpectation matches(String regex) {
assertTrue("String does not match regex", objectUnderTest.matches(regex));
return this;
}
It would also be a possibility to allow fail messages to be specified, not sure if this is currently possible?
Happy to take the String error message approach, but I think adding
both objectUnderTest
and regex
to the error message is a good idea, to help people diagnose
the problem.
You can currently specify fail messages when doing expect.failure()
. I
would be happy to accept patches adding more overloads with error messages
in appropriate places.
Apologies for the delay - I only just spotted that you had updated the pull request. Thanks for the patch.
This PR adds a method to the
StringExpectation
class to test whether aString
matches a given regular expression.