Skip to content

Commit

Permalink
Issue cucumber#466
Browse files Browse the repository at this point in the history
  • Loading branch information
Luxor committed Mar 15, 2013
1 parent c906b4e commit 896e412
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public JdkPatternArgumentMatcher(Pattern pattern) {

public List<Argument> argumentsFrom(String stepName) {
Matcher matcher = pattern.matcher(stepName);
if (matcher.matches()) {
List<Argument> arguments = new ArrayList<Argument>();
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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class JdkPatternArgumentMatcherTest {
@Test
Expand All @@ -30,6 +31,22 @@ public void shouldDealWithUnicodeEverywhere() throws UnsupportedEncodingExceptio
assertVariables("Jæ (.+) ålsker (.+) lændet", "Jæ vø ålsker døtte lændet", "vø", 3, "døtte", 13);
}

@Test
public void shouldDealWithUnAnchoredPattern() throws UnsupportedEncodingException {
assertVariables("^I wait for (.+) and (.+) seconds",
"I wait for 3 and 4 seconds to be sure",
"3", 11, "4", 17
);
}

@Test
public void shouldDealWithAnchoredPattern() {
JdkPatternArgumentMatcher matcher = new JdkPatternArgumentMatcher(Pattern.compile("^I wait for (.+) seconds$"));

assertNull(matcher.argumentsFrom("I wait for 30 seconds to be sure"));
assertEquals(1, matcher.argumentsFrom("I wait for 30 seconds").size());
}

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

0 comments on commit 896e412

Please sign in to comment.