Comments (20)
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));
from javahamcrest.
I end up writing one every single project I work on.
from javahamcrest.
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).
from javahamcrest.
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.
The point is, I should be able write something like this:
Matcher<String> regexMatcher = Matchers.matchesRegex([insert regex expression here]);
Quick. Easy. Simple. No third party integrations required, no matter how clean.
from javahamcrest.
This would be awesome, one of the great thing about Hamcrest is the clear diagnostics.
from javahamcrest.
@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.
from javahamcrest.
Here the one (i found it on google page of hamcrest) - https://github.com/derari/cthul/wiki/Matchers#string-matchers
Also, u can find some other here - https://github.com/yandex-qatools/matchers-java
from javahamcrest.
For followers, confirm there is a MatchesPattern.matchesPattern(String regex)
and MatchesPattern.matchesPattern(Pattern pattern)
in 2.0, ex: assertThat(myString, matchesPattern("my regex"))
from javahamcrest.
There's also http://code.google.com/p/hamcrest-text-patterns/
from javahamcrest.
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?
from javahamcrest.
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.
from javahamcrest.
Is there an objection to implementing this, or just no commits / pull requests for it?
from javahamcrest.
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.
from javahamcrest.
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!
from javahamcrest.
@tomwhoiscontrary Maybe soon #65
from javahamcrest.
+1
from javahamcrest.
It turns out this was released as part of java-hamcrest
2.0.0.0 -- you'll also want to add hamcrest-junit
to your project if you upgrade from 1.3, and use its assertThat()
instead of the one in jUnit 4.
from javahamcrest.
Is Hamcrest 2 actually a thing? I thought it was an abortive sally.
from javahamcrest.
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 pom.xml
and get my project building.
The main issue was I couldn't import static org.junit.Assert.*;
anymore (for older test cases that still use assertEquals
, etc.) because the two different assertThat()
implementations got ambiguous.
from javahamcrest.
matchesRegex
There is no such method, can you please let me know which version hamcrest you are refering?
from javahamcrest.
Related Issues (20)
- containsInAnyOrder incorrectly identifies differences in identical collections HOT 1
- Hamcrest 3.0? HOT 2
- You should be able to specify a lambda to return a reason
- Conflicting license declarations HOT 1
- Not sure why assertThat() doesn't work in this case HOT 2
- 301 Moved Permanently
- assertThat(this.object, hasProperty("booleanName")) fails to match boolean types and renames the property HOT 1
- The matcher contains() is misleading HOT 2
- FR: Matching maps with various type HOT 1
- oss-fuzz integration
- Participitation in Hacktoberfest?
- HasProperty Matcher doesn't work with Java Records HOT 1
- assertThat(Int::class.java, typeCompatibleWith(Number::class.java)) in kotlin always fails
- hamcrest matching on actual empty list fails with nosuchmethoderror HOT 1
- Test output Alignment
- record version fails the hasProperty HOT 1
- has property fails with non public class.. not sure if this correct as per java standards of property
- Double "close-to" matcher that uses the ULP.
- GraalVM Native Image support HOT 3
- Inquiry about Project Activity and Future Plans HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javahamcrest.