Search in sources :

Example 1 with CsmUnindent

use of com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent 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());
}
Also used : CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Statement(com.github.javaparser.ast.stmt.Statement) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) CsmIndent(com.github.javaparser.printer.concretesyntaxmodel.CsmIndent) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmUnindent(com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent) Test(org.junit.Test)

Aggregations

ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)1 Statement (com.github.javaparser.ast.stmt.Statement)1 CsmIndent (com.github.javaparser.printer.concretesyntaxmodel.CsmIndent)1 CsmToken (com.github.javaparser.printer.concretesyntaxmodel.CsmToken)1 CsmUnindent (com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent)1 CsmChild (com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild)1 Test (org.junit.Test)1