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

Handle optional parameters correctly #558

Merged
merged 1 commit into from
Aug 13, 2013
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 @@ -19,7 +19,8 @@ public List<Argument> argumentsFrom(String stepName) {
if (matcher.lookingAt()) {
List<Argument> arguments = new ArrayList<Argument>(matcher.groupCount());
for (int i = 1; i <= matcher.groupCount(); i++) {
arguments.add(new Argument(matcher.start(i), matcher.group(i)));
int startIndex = matcher.start(i);
arguments.add(new Argument(startIndex == -1 ? null : startIndex, matcher.group(i)));
}
return arguments;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public void shouldDealWithAnchoredPattern() {
assertEquals(1, matcher.argumentsFrom("I wait for 30 seconds").size());
}

@Test
public void canHandleVariableNumberOfArguments() {
JdkPatternArgumentMatcher matcher = new JdkPatternArgumentMatcher(Pattern.compile("I wait for (.+) seconds|I wait for some time"));

List<Argument> arguments = matcher.argumentsFrom("I wait for 30 seconds to be sure");
List<Argument> optionalArguments = matcher.argumentsFrom("I wait for some time");

assertEquals(1, arguments.size());
assertEquals(1, optionalArguments.size());
assertNull(matcher.argumentsFrom("I wait for some time").get(0).getOffset());
assertNull(matcher.argumentsFrom("I wait for some time").get(0).getVal());
}

private void assertVariables(String regex, String string, String v1, Integer pos1, String v2, Integer pos2) throws UnsupportedEncodingException {
List<Argument> args = new JdkPatternArgumentMatcher(Pattern.compile(regex)).argumentsFrom(string);
assertEquals(2, args.size());
Expand Down