Search in sources :

Example 6 with CsmToken

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

the class DifferenceTest method calculateDifferenceAIsEmpty.

@Test
public void calculateDifferenceAIsEmpty() {
    Node n1 = new FieldDeclaration();
    Node n2 = new MethodDeclaration();
    LexicalDifferenceCalculator.CalculatedSyntaxModel a = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Collections.emptyList());
    LexicalDifferenceCalculator.CalculatedSyntaxModel b = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Arrays.asList(new CsmToken(GeneratedJavaParserConstants.LPAREN), new CsmChild(n1), new CsmToken(GeneratedJavaParserConstants.RPAREN), new CsmChild(n2)));
    Difference diff = Difference.calculate(a, b);
    assertEquals(4, diff.getElements().size());
    assertEquals(added(new CsmToken(GeneratedJavaParserConstants.LPAREN)), diff.getElements().get(0));
    assertEquals(added(new CsmChild(n1)), diff.getElements().get(1));
    assertEquals(added(new CsmToken(GeneratedJavaParserConstants.RPAREN)), diff.getElements().get(2));
    assertEquals(added(new CsmChild(n2)), diff.getElements().get(3));
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Example 7 with CsmToken

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

the class LexicalDifferenceCalculatorTest method annotationDeclarationModifiersExampleModified.

@Test
public void annotationDeclarationModifiersExampleModified() throws IOException {
    considerExample("AnnotationDeclaration_Example1_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.MODIFIERS, EnumSet.noneOf(Modifier.class), EnumSet.of(Modifier.PUBLIC));
    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) Modifier(com.github.javaparser.ast.Modifier) Test(org.junit.Test)

Example 8 with CsmToken

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

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

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

the class DifferenceTest method calculateDifferenceBIsEmpty.

@Test
public void calculateDifferenceBIsEmpty() {
    Node n1 = new FieldDeclaration();
    Node n2 = new MethodDeclaration();
    LexicalDifferenceCalculator.CalculatedSyntaxModel a = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Arrays.asList(new CsmToken(GeneratedJavaParserConstants.LPAREN), new CsmChild(n1), new CsmToken(GeneratedJavaParserConstants.RPAREN), new CsmChild(n2)));
    LexicalDifferenceCalculator.CalculatedSyntaxModel b = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Collections.emptyList());
    Difference diff = Difference.calculate(a, b);
    assertEquals(4, diff.getElements().size());
    assertEquals(removed(new CsmToken(GeneratedJavaParserConstants.LPAREN)), diff.getElements().get(0));
    assertEquals(removed(new CsmChild(n1)), diff.getElements().get(1));
    assertEquals(removed(new CsmToken(GeneratedJavaParserConstants.RPAREN)), diff.getElements().get(2));
    assertEquals(removed(new CsmChild(n2)), diff.getElements().get(3));
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Node(com.github.javaparser.ast.Node) 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