Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.
Open
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
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());
}
}