use of org.opensolaris.opengrok.analysis.JFlexSymbolMatcher in project OpenGrok by OpenGrok.
the class PerlSymbolTokenizerTest method testOffsetAttribute.
/**
* Helper method for {@link #testOffsetAttribute()} that runs the test on
* one single implementation class with the specified input text and
* expected tokens.
*/
private void testOffsetAttribute(Class<? extends JFlexSymbolMatcher> klass, String inputText, String[] expectedTokens) throws Exception {
JFlexSymbolMatcher matcher = klass.getConstructor(Reader.class).newInstance(new StringReader(inputText));
JFlexTokenizer tokenizer = new JFlexTokenizer(matcher);
CharTermAttribute term = tokenizer.addAttribute(CharTermAttribute.class);
OffsetAttribute offset = tokenizer.addAttribute(OffsetAttribute.class);
int count = 0;
while (tokenizer.incrementToken()) {
assertTrue("too many tokens", count < expectedTokens.length);
String expected = expectedTokens[count];
// 0-based offset to accord with String[]
assertEquals("term" + count, expected, term.toString());
assertEquals("start" + count, inputText.indexOf(expected), offset.startOffset());
assertEquals("end" + count, inputText.indexOf(expected) + expected.length(), offset.endOffset());
count++;
}
assertEquals("wrong number of tokens", expectedTokens.length, count);
}
Aggregations