Skip to content

Commit

Permalink
Question: a non-consuming Matcher #52
Browse files Browse the repository at this point in the history
  • Loading branch information
agavrilov76 committed May 27, 2018
1 parent 5acbe1f commit 181de05
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
71 changes: 71 additions & 0 deletions expectit-core/src/main/java/net/sf/expectit/matcher/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result> nonConsuming(final Matcher<Result> matcher) {
return new Matcher<Result>() {
@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();
}
};
}
};
}

}
19 changes: 19 additions & 0 deletions expectit-core/src/test/java/net/sf/expectit/MatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 181de05

Please sign in to comment.