RichardWarburton / lambda-behave

A modern testing and behavioural specification framework for Java 8
http://richardwarburton.github.io/lambda-behave/
MIT License
253 stars 52 forks source link

Add method to check if a string matches a regular expression #81

Closed renbinden closed 10 years ago

renbinden commented 10 years ago

This PR adds a method to the StringExpectation class to test whether a String matches a given regular expression.

RichardWarburton commented 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:

https://github.com/hamcrest/JavaHamcrest/pull/57/files

renbinden commented 10 years ago

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?

RichardWarburton commented 10 years ago

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.

RichardWarburton commented 10 years ago

Apologies for the delay - I only just spotted that you had updated the pull request. Thanks for the patch.