diff --git a/expectit-core/src/main/java/net/sf/expectit/matcher/Matchers.java b/expectit-core/src/main/java/net/sf/expectit/matcher/Matchers.java index 0851429..2293d8c 100644 --- a/expectit-core/src/main/java/net/sf/expectit/matcher/Matchers.java +++ b/expectit-core/src/main/java/net/sf/expectit/matcher/Matchers.java @@ -369,4 +369,75 @@ public String toString() { }; } + /** + * Make sure that the given match don't consume the data from the internal buffer. + * @param matcher the matcher + * @return the result + */ + public static Matcher nonConsuming(final Matcher matcher) { + return new Matcher() { + @Override + public Result matches(final String input, final boolean isEof) { + final Result result = matcher.matches(input, isEof); + return new Result() { + @Override + public int start() { + return result.start(); + } + + @Override + public int start(final int group) { + return result.start(group); + } + + @Override + public int end() { + return 0; + } + + @Override + public int end(final int group) { + result.end(group); + return 0; + } + + @Override + public String group() { + return result.group(); + } + + @Override + public String group(final int group) { + return result.group(group); + } + + @Override + public int groupCount() { + return result.groupCount(); + } + + @Override + public String getInput() { + return result.getInput(); + } + + @Override + public boolean isSuccessful() { + return result.isSuccessful(); + } + + @Override + public String getBefore() { + return result.getBefore(); + } + + @Override + public boolean canStopMatching() { + return result.canStopMatching(); + } + }; + } + }; + } + } diff --git a/expectit-core/src/test/java/net/sf/expectit/MatcherTest.java b/expectit-core/src/test/java/net/sf/expectit/MatcherTest.java index 22e4c68..22ae0e9 100644 --- a/expectit-core/src/test/java/net/sf/expectit/MatcherTest.java +++ b/expectit-core/src/test/java/net/sf/expectit/MatcherTest.java @@ -30,6 +30,7 @@ import static net.sf.expectit.matcher.Matchers.eof; import static net.sf.expectit.matcher.Matchers.exact; import static net.sf.expectit.matcher.Matchers.matches; +import static net.sf.expectit.matcher.Matchers.nonConsuming; import static net.sf.expectit.matcher.Matchers.regexp; import static net.sf.expectit.matcher.Matchers.sequence; import static net.sf.expectit.matcher.Matchers.startsWith; @@ -617,4 +618,22 @@ public void testStartsWith() throws IOException, InterruptedException { assertFalse(result2.isSuccessful()); assertFalse(result2.canStopMatching()); } + + @Test + public void testNonConsuming() throws IOException { + Result result = input.expect(LONG_TIMEOUT, nonConsuming(contains("b2"))); + assertTrue(result.isSuccessful()); + assertEquals(result.groupCount(), 0); + assertEquals(result.end(), 0); + assertEquals(result.start(), 2); + assertEquals(result.group(), "b2"); + assertEquals(result.end(0), 0); + assertEquals(result.start(0), 2); + assertEquals(result.group(0), "b2"); + assertEquals(result.getBefore(), "a1"); + checkIndexOutOfBound(result, 1); + + Result result2 = input.expect(LONG_TIMEOUT, contains("b2")); + assertTrue(result2.isSuccessful()); + } }