use of com.google.errorprone.VisitorState in project error-prone by google.
the class ASTHelpersTest method testCommentTokens.
@Test
public void testCommentTokens() {
writeFile("A.java", "public class A {", " Runnable theRunnable = new Runnable() {", " /**", " * foo", " */", " public void run() {", " /* bar1 */", " /* bar2 */", " System.err.println(\"Hi\");", " }", " // baz number 1", " // baz number 2", " };", "}");
TestScanner scanner = new TestScanner() {
@Override
public Void visitNewClass(NewClassTree tree, VisitorState state) {
setAssertionsComplete();
List<String> comments = new ArrayList<>();
for (ErrorProneToken t : state.getTokensForNode(tree)) {
if (!t.comments().isEmpty()) {
for (Comment c : t.comments()) {
Verify.verify(c.getSourcePos(0) >= 0);
comments.add(c.getText());
}
}
}
assertThat(comments).containsExactly("/**\n * foo\n */", "/* bar1 */", "/* bar2 */", "// baz number 1", "// baz number 2").inOrder();
return super.visitNewClass(tree, state);
}
};
tests.add(scanner);
assertCompiles(scanner);
}
use of com.google.errorprone.VisitorState in project error-prone by google.
the class ASTHelpersTest method expressionStatementMatches.
private Scanner expressionStatementMatches(final String expectedExpression, final Matcher<ExpressionTree> matcher) {
return new TestScanner() {
@Override
public Void visitExpressionStatement(ExpressionStatementTree node, VisitorState state) {
ExpressionTree expression = node.getExpression();
if (expression.toString().equals(expectedExpression)) {
assertMatch(node.getExpression(), state, matcher);
setAssertionsComplete();
}
return super.visitExpressionStatement(node, state);
}
};
}
use of com.google.errorprone.VisitorState in project error-prone by google.
the class AnnotationMatcherTest method nodeWithAnnotationMatches.
private Scanner nodeWithAnnotationMatches(final boolean shouldMatch, final AnnotationMatcher<Tree> toMatch) {
ScannerTest test = new ScannerTest() {
private boolean matched = false;
@Override
public Void visitAnnotation(AnnotationTree node, VisitorState visitorState) {
TreePath currPath = getCurrentPath().getParentPath();
Tree parent = currPath.getLeaf();
if (parent.getKind() == Kind.MODIFIERS) {
currPath = currPath.getParentPath();
parent = currPath.getLeaf();
}
visitorState = visitorState.withPath(currPath);
if (toMatch.matches(parent, visitorState)) {
matched = true;
}
visitorState = visitorState.withPath(getCurrentPath());
return super.visitAnnotation(node, visitorState);
}
@Override
public void assertDone() {
assertEquals(matched, shouldMatch);
}
};
tests.add(test);
return test;
}
use of com.google.errorprone.VisitorState in project error-prone by google.
the class DescendantOfAbstractTest method memberSelectMatches.
protected Scanner memberSelectMatches(final boolean shouldMatch, final DescendantOf toMatch) {
ScannerTest test = new ScannerTest() {
private boolean matched = false;
@Override
public Void visitMethodInvocation(MethodInvocationTree node, VisitorState visitorState) {
visitorState = visitorState.withPath(getCurrentPath());
ExpressionTree methodSelect = node.getMethodSelect();
if (toMatch.matches(methodSelect, visitorState)) {
matched = true;
}
return super.visitMethodInvocation(node, visitorState);
}
@Override
public void assertDone() {
assertEquals(matched, shouldMatch);
}
};
tests.add(test);
return test;
}
Aggregations