Skip to content
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

fix(#988): Add Hamcrest matchesPattern matcher #989

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 +162,10 @@ 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,7 +236,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]*')")},
};
}
}
Loading