Skip to content

Commit

Permalink
fix(#988): Add Hamcrest matchesPattern matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Schlathoelter committed Sep 18, 2023
1 parent a9026ea commit 6846f62
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
@SuppressWarnings("unchecked")
public class HamcrestValidationMatcher implements ValidationMatcher, ControlExpressionParser {

private final List<String> matchers = Arrays.asList( "equalTo", "equalToIgnoringCase", "equalToIgnoringWhiteSpace", "is", "not", "containsString", "startsWith", "endsWith" );
private final List<String> matchers = Arrays.asList( "equalTo", "equalToIgnoringCase", "equalToIgnoringWhiteSpace", "is", "not", "containsString", "startsWith", "endsWith", "matchesPattern" );

private final List<String> collectionMatchers = Arrays.asList("hasSize", "hasItem", "hasItems", "contains", "containsInAnyOrder");

Expand Down Expand Up @@ -500,7 +500,7 @@ private static class Tokenizer {
/**
* Regular expression with three alternative parts (ored) to match:
* <ol>
* <li> `(sometext)` - Quoted parameter block of a matcher.</li>
* <li> (`sometext`) - Quoted parameter block of a matcher.</li>
* <li> 'sometext' - Quoted text used as a parameter to a string matcher.</li>
* <li> (unquotedtext) - Unquoted text used as a parameter to a string matcher. This expression is non-greedy, meaning the first closing bracket will terminate the match.</li>
* </ol>
Expand All @@ -513,7 +513,7 @@ private static class Tokenizer {
* Therefore, the regex expressions explicitly match the escaped quote -> \\\\'
*/
private static final Pattern TEXT_PARAMETER_PATTERN = Pattern.compile(
"(?<quoted1>\\('(?:[^']|\\\\')*[^\\\\]'\\))"
"(?<quoted1>\\('(?:[^']|\\\\')*[^\\\\]'\\))"
+ "|(?<quoted2>('(?:[^']|\\\\')*[^\\\\]'))"
+ "|(?<unquoted>\\(((?:[^']|\\\\')*?)[^\\\\]?\\))"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ public Object[][] testData() {
new Object[]{"foo", "unquoted text may not include brackets or commas", Collections.singletonList("anyOf(equalTo(unquoted text may not include brackets or commas), anyOf(isEmptyOrNullString()))")},
new Object[]{"foo", "quoted \\' text may not include brackets or commas", Collections.singletonList("anyOf(equalTo(quoted \\\\' text may not include brackets or commas), anyOf(isEmptyOrNullString()))")},
new Object[]{"foo", "value1", Collections.singletonList("anyOf(isEmptyOrNullString(),equalTo(value1))")},

new Object[]{"foo", "INSERT INTO todo_entries (id, title, description, done) values (1, 'Invite for meeting', 'Invite the group for a lunch meeting', 'false')",
Collections.singletonList("allOf(startsWith('INSERT INTO todo_entries (id, title, description, done)'))")},
new Object[]{"foo", "value1", Collections.singletonList("matchesPattern([^2345]*)")},
new Object[]{"foo", "value1 with quotes", Collections.singletonList("matchesPattern('[^2345]*')")},



};
Expand Down Expand Up @@ -237,6 +239,8 @@ public Object[][] testDataFailed() {
new Object[]{"foo", "notext containing a ' (quote) and a , (comma) ", Collections.singletonList("anyOf(equalTo('text containing a \\' (quote) and a , (comma) '), anyOf(isEmptyOrNullString()))")},
new Object[]{"foo", "notext containing a \\' (quote) and a , (comma) ", Collections.singletonList("anyOf(equalTo('text containing a \\\\' (quote) and a , (comma) '), anyOf(isEmptyOrNullString()))")},
new Object[]{"foo", "nounquoted text may not include brackets or commas", Collections.singletonList("anyOf(equalTo(unquoted text may not include brackets or commas), anyOf(isEmptyOrNullString()))")},
new Object[]{"foo", "value1", Collections.singletonList("matchesPattern([^12345]*)")},
new Object[]{"foo", "value1 with quotes", Collections.singletonList("matchesPattern('[^12345]*')")},

};
}
Expand Down

0 comments on commit 6846f62

Please sign in to comment.