Search in sources :

Example 11 with CsmToken

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

the class DifferenceTest method methodDeclarationAddingParameter.

@Test
public void methodDeclarationAddingParameter() {
    MethodDeclaration md = considerMd("public void foo(){}");
    Parameter newParameter = new Parameter(new ArrayType(PrimitiveType.intType()), new SimpleName("foo"));
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(md);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterListAddition(md, ObservableProperty.PARAMETERS, 0, newParameter);
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    int i = 0;
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.PUBLIC)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmChild(md.getType())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmChild(md.getName())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.LPAREN)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmChild(newParameter)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.RPAREN)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmChild(md.getBody().get())), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : ArrayType(com.github.javaparser.ast.type.ArrayType) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Test(org.junit.Test)

Example 12 with CsmToken

use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken 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 13 with CsmToken

use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken 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 14 with CsmToken

use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken 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)

Example 15 with CsmToken

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

the class LexicalDifferenceCalculatorTest method compilationUnitExampleWithPackageSet.

@Test
public void compilationUnitExampleWithPackageSet() {
    considerCode("class A {}");
    CsmElement element = ConcreteSyntaxModel.forClass(cu.getClass());
    PackageDeclaration packageDeclaration = new PackageDeclaration(new Name(new Name("foo"), "bar"));
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, cu, ObservableProperty.PACKAGE_DECLARATION, null, packageDeclaration);
    assertEquals(3, csmChanged.elements.size());
    assertEquals(new LexicalDifferenceCalculator.CsmChild(packageDeclaration), csmChanged.elements.get(0));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(cu.getType(0)), csmChanged.elements.get(1));
    assertEquals(new CsmToken(eolTokenKind()), csmChanged.elements.get(2));
}
Also used : 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

CsmToken (com.github.javaparser.printer.concretesyntaxmodel.CsmToken)18 Test (org.junit.Test)17 CsmElement (com.github.javaparser.printer.concretesyntaxmodel.CsmElement)14 CsmChild (com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild)10 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)5 Modifier (com.github.javaparser.ast.Modifier)3 Node (com.github.javaparser.ast.Node)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 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