ArgumentPatternMatcher { public static List<String> arguments(Pattern pattern, String state) { final List<String> argumentsValues = new ArrayList<>(); Matcher matcher = pattern.matcher(state); if (matcher.lookingAt()) { for (int i = 1; i <= matcher.groupCount(); i++) { int startIndex = matcher.start(i); if (startIndex == -1) { argumentsValues.add(""); } else { argumentsValues.add(matcher.group(i)); } } } return argumentsValues; } ArgumentPatternMatcher(); static List<String> arguments(Pattern pattern, String state);  }
@Test public void should_extract_arguments_if_number() { List<String> arguments = ArgumentPatternMatcher.arguments(Pattern.compile("I have (\\d+) cukes in my belly"), "I have 36 cukes in my belly"); assertThat(arguments).containsExactly("36"); }
@Test public void should_extract_arguments_if_list() { List<String> arguments = ArgumentPatternMatcher.arguments(Pattern.compile("The following animals: (.*)"), "The following animals: dog, cat, mouse"); assertThat(arguments).containsExactly("dog, cat, mouse"); }
@Test public void should_extract_multiple_arguments() { List<String> arguments = ArgumentPatternMatcher.arguments(Pattern.compile("I have (\\d+) cukes in my (\\w+)"), "I have 36 cukes in my belly"); assertThat(arguments).containsExactly("36", "belly"); }
@Test public void should_not_extract_anything_if_no_matches() { List<String> arguments = ArgumentPatternMatcher.arguments(Pattern.compile("The following animals: (.*)"), "I have 36 cukes in my belly"); assertThat(arguments).isEmpty(); }