use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.
the class DifferenceTest method addingStatementToEmptyMethodBody.
@Test
public void addingStatementToEmptyMethodBody() {
String code = "class A { void foo(char p1, int p2) {} }";
considerCode(code);
Statement s = new ExpressionStmt(new BinaryExpr(new IntegerLiteralExpr("10"), new IntegerLiteralExpr("2"), BinaryExpr.Operator.PLUS));
MethodDeclaration m = cu.getClassByName("A").get().getMethodsByName("foo").get(0);
LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(m.getBody().get());
LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterListAddition(m.getBody().get(), ObservableProperty.STATEMENTS, 0, s);
Difference diff = Difference.calculate(csmOriginal, csmChanged);
int i = 0;
assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.added(new CsmIndent()), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.added(new CsmChild(s)), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.added(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.added(new CsmUnindent()), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
assertEquals(i, diff.getElements().size());
}
use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.
the class LexicalDifferenceCalculatorTest method differenceAfterddingStatementToEmptyBlock.
@Test
public void differenceAfterddingStatementToEmptyBlock() throws IOException {
LexicalDifferenceCalculator ldc = new LexicalDifferenceCalculator();
considerExample("ASimpleClassWithMoreFormatting_step3");
MethodDeclaration setter = cu.getClassByName("MyRenamedClass").get().getMethodsByName("setAField").get(0);
Statement assignStatement = new ExpressionStmt(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "aField"), new NameExpr("aField"), AssignExpr.Operator.ASSIGN));
Difference diff = ldc.calculateListAdditionDifference(ObservableProperty.STATEMENTS, setter.getBody().get().getStatements(), 0, assignStatement);
int index = 0;
assertEquals(Difference.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(index++));
assertEquals(Difference.DifferenceElement.kept(CsmElement.newline()), diff.getElements().get(index++));
assertEquals(Difference.DifferenceElement.added(CsmElement.indent()), diff.getElements().get(index++));
assertTrue(isAddedChild(diff.getElements().get(index++), ExpressionStmt.class));
assertEquals(Difference.DifferenceElement.added(CsmElement.newline()), diff.getElements().get(index++));
assertEquals(Difference.DifferenceElement.added(CsmElement.unindent()), diff.getElements().get(index++));
assertEquals(Difference.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(index++));
assertEquals(index, diff.getElements().size());
}
use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.
the class NodeWithStatements method addStatement.
public default T addStatement(int index, final Expression expr) {
Statement stmt = new ExpressionStmt(expr);
expr.setParentNode(stmt);
return addStatement(index, stmt);
}
use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.
the class ParseErrorRecoveryTest method bodystatementClosingBraceRecovery.
@Test
public void bodystatementClosingBraceRecovery() {
MethodDeclaration cu = parser.parse(ParseStart.CLASS_BODY, provider("int x(){X X X}")).getResult().get().asMethodDeclaration();
Statement xxx = cu.getBody().get();
assertEquals(UNPARSABLE, xxx.getParsed());
}
use of com.github.javaparser.ast.stmt.Statement in project javaparser by javaparser.
the class ParseErrorRecoveryTest method bodystatementSemicolonRecovery.
@Test
public void bodystatementSemicolonRecovery() {
MethodDeclaration cu = parser.parse(ParseStart.CLASS_BODY, provider("int x(){X X X;}")).getResult().get().asMethodDeclaration();
Statement xxx = cu.getBody().get().getStatements().get(0);
assertEquals(UNPARSABLE, xxx.getParsed());
}
Aggregations