Search in sources :

Example 6 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class LexicalDifferenceCalculatorTest method annotationDeclaratioJavadocExampleOriginal.

@Test
public void annotationDeclaratioJavadocExampleOriginal() throws IOException {
    considerExample("AnnotationDeclaration_Example3_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
    csm.removeIndentationElements();
    int i = 0;
    assertEquals(new CsmToken(GeneratedJavaParserConstants.PUBLIC), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.AT), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.INTERFACE), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getName()), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.LBRACE), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(0)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(1)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(2)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(3)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(4)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(5)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.RBRACE), csm.elements.get(i++));
    assertEquals(i, csm.elements.size());
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Test(org.junit.Test)

Example 7 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class LexicalDifferenceCalculatorTest method annotationDeclarationModifiersExampleOriginal.

@Test
public void annotationDeclarationModifiersExampleOriginal() throws IOException {
    considerExample("AnnotationDeclaration_Example1_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
    csm.removeIndentationElements();
    int i = 0;
    assertEquals(new CsmToken(GeneratedJavaParserConstants.AT), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.INTERFACE), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getName()), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.LBRACE), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(0)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(1)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(2)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(3)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(4)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(5)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.RBRACE), csm.elements.get(i++));
    assertEquals(i, csm.elements.size());
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Test(org.junit.Test)

Example 8 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class DifferenceTest method annotationDeclarationExampleWithJavadocAdded.

@Test
public void annotationDeclarationExampleWithJavadocAdded() throws IOException {
    considerExample("AnnotationDeclaration_Example3_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    JavadocComment comment = new JavadocComment("Cool this annotation!");
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.COMMENT, null, comment);
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    diff.removeIndentationElements();
    int i = 0;
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.PUBLIC)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.AT)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.INTERFACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getName())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(0))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(1))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(2))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(3))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(4))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(5))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Test(org.junit.Test)

Example 9 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class DifferenceTest method annotationDeclarationExampleWithModifierRemoved.

@Test
public void annotationDeclarationExampleWithModifierRemoved() throws IOException {
    considerExample("AnnotationDeclaration_Example3_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.MODIFIERS, EnumSet.of(Modifier.PUBLIC), EnumSet.noneOf(Modifier.class));
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    diff.removeIndentationElements();
    int i = 0;
    assertEquals(removed(new CsmToken(GeneratedJavaParserConstants.PUBLIC)), diff.getElements().get(i++));
    assertEquals(removed(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.AT)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.INTERFACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getName())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(0))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(1))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(2))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(3))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(4))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(5))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Modifier(com.github.javaparser.ast.Modifier) Test(org.junit.Test)

Example 10 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class DifferenceTest method compilationUnitExampleWithPackageSetDiff.

@Test
public void compilationUnitExampleWithPackageSetDiff() {
    considerCode("class A {}");
    CsmElement element = ConcreteSyntaxModel.forClass(cu.getClass());
    PackageDeclaration packageDeclaration = new PackageDeclaration(new Name(new Name("foo"), "bar"));
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, cu);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, cu, ObservableProperty.PACKAGE_DECLARATION, null, packageDeclaration);
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    assertEquals(3, diff.getElements().size());
    assertEquals(added(new CsmChild(packageDeclaration)), diff.getElements().get(0));
    assertEquals(kept(new CsmChild(cu.getType(0))), diff.getElements().get(1));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(2));
}
Also used : CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration) Test(org.junit.Test)

Aggregations

CsmElement (com.github.javaparser.printer.concretesyntaxmodel.CsmElement)14 CsmToken (com.github.javaparser.printer.concretesyntaxmodel.CsmToken)14 Test (org.junit.Test)13 CsmChild (com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild)6 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)5 Modifier (com.github.javaparser.ast.Modifier)3 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)3 PackageDeclaration (com.github.javaparser.ast.PackageDeclaration)2 com.github.javaparser (com.github.javaparser)1 GeneratedJavaParserConstants (com.github.javaparser.GeneratedJavaParserConstants)1 TokenTypes.eolTokenKind (com.github.javaparser.TokenTypes.eolTokenKind)1 DataKey (com.github.javaparser.ast.DataKey)1 Node (com.github.javaparser.ast.Node)1 NodeList (com.github.javaparser.ast.NodeList)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Comment (com.github.javaparser.ast.comments.Comment)1 NodeWithVariables (com.github.javaparser.ast.nodeTypes.NodeWithVariables)1 AstObserver (com.github.javaparser.ast.observer.AstObserver)1 ObservableProperty (com.github.javaparser.ast.observer.ObservableProperty)1 PropagatingAstObserver (com.github.javaparser.ast.observer.PropagatingAstObserver)1