-
Notifications
You must be signed in to change notification settings - Fork 378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Matcher checking a string matches a regular expression #36
Comments
Here the one (i found it on google page of hamcrest) - https://github.com/derari/cthul/wiki/Matchers#string-matchers |
There's also http://code.google.com/p/hamcrest-text-patterns/ |
hamcrest-text-patterns does the opposite thing than I asked for. Ok, cthul looks nice, however I think that matching against a regex is such a core functionality that you should not require users to use third-party libraries. This would be the same bizzare situation as with C++ failing to provide a standard regex implementation. I think that when you write some test code, you constantly come across writing assertions making use of regular expressions. See how RSpec constructed its documentations: regexes are emphasised there. As cthul is MIT-licensed, I suggest you copy the regex-related classes to hamcrest-library, adding cthul authors to the contributor list (to fulfil the legal licence requirements). |
In what way does hamcrest-text-patterns do the opposite thing to what you asked for? It is a hamcrest matcher that matches strings against regular expressions. What do you want? |
Ok, you are right, the section "Matching" shows regex-like matching, however, if I understand correctly, you need to use library builders to construct a matcher and you are not allowed to use normal regular expressions given as strings. I find this way of writing assertions way to verbose in many cases. |
Derbeth is right. Hamcrest ought to have a simple, non-verbose regex matcher. There are many good reasons to and no good reasons not to. Matcher<String> regexMatcher = Matchers.matchesRegex([insert regex expression here]); Quick. Easy. Simple. No third party integrations required, no matter how clean. |
Is there an objection to implementing this, or just no commits / pull requests for it? |
No objection. There used to be a RegexMatcher in Hamcrest but it got removed because it made supporting different JDK versions difficult. But that was a very long time ago; Hamcrest no longer supports JDK versions without java.util.Pattern. |
I end up writing one every single project I work on. |
Any idea when there might be a release with this matcher in? So far, i've copied and pasted it into two projects, and i'm going to die of shame if i have to do it again! |
@tomwhoiscontrary Maybe soon #65 |
Assuming there is no matcher for regular expressions, you may consider to rewrite your assertion to something like this: assertThat("Expected result matches regular expression",
expected.matches(regularExpression),
is(true)); |
This would be awesome, one of the great thing about Hamcrest is the clear diagnostics. |
@jm2dev I don't understand your point. Where does the actual value go? Also, if it fails, you just get an expected: true, was: false. |
+1 |
It turns out this was released as part of |
Is Hamcrest 2 actually a thing? I thought it was an abortive sally. |
There doesn't seem to be much documentation, but it's published to Maven Central. I was able to put it & the new JUnit integration in The main issue was I couldn't |
For followers, confirm there is a |
There is no such method, can you please let me know which version hamcrest you are refering? |
I cannot find any class allowing me to write my tests like:
I think that Hamcrest Library should offer matchesReges() together with containsString(), endsWith() and startsWith().
Is there a matcher like this available in the master branch? If not, I could write one (something similar to Hamcrest Regex Matcher) and create a pull request.
The text was updated successfully, but these errors were encountered: