diff options
Diffstat (limited to 'javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations')
11 files changed, 1171 insertions, 0 deletions
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/ArrayCreationLevelTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/ArrayCreationLevelTransformationsTest.java new file mode 100644 index 000000000..93cff347e --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/ArrayCreationLevelTransformationsTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast; + +import com.github.javaparser.ast.ArrayCreationLevel; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.expr.ArrayCreationExpr; +import com.github.javaparser.ast.expr.IntegerLiteralExpr; +import com.github.javaparser.ast.expr.Name; +import com.github.javaparser.ast.expr.NormalAnnotationExpr; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import com.github.javaparser.utils.Utils; +import org.junit.Test; + +import java.io.IOException; + +/** + * Transforming ArrayCreationLevel and verifying the LexicalPreservation works as expected. + */ +public class ArrayCreationLevelTransformationsTest extends AbstractLexicalPreservingTest { + + protected ArrayCreationLevel consider(String code) { + considerExpression("new int" + code); + ArrayCreationExpr arrayCreationExpr = expression.asArrayCreationExpr(); + return arrayCreationExpr.getLevels().get(0); + } + + // Dimension + + @Test + public void addingDimension() throws IOException { + ArrayCreationLevel it = consider("[]"); + it.setDimension(new IntegerLiteralExpr("10")); + assertTransformedToString("[10]", it); + } + + @Test + public void removingDimension() throws IOException { + ArrayCreationLevel it = consider("[10]"); + it.removeDimension(); + assertTransformedToString("[]", it); + } + + @Test + public void replacingDimension() throws IOException { + ArrayCreationLevel it = consider("[10]"); + it.setDimension(new IntegerLiteralExpr("12")); + assertTransformedToString("[12]", it); + } + + // Annotations + + @Test + public void addingAnnotation() throws IOException { + ArrayCreationLevel it = consider("[]"); + it.addAnnotation("myAnno"); + assertTransformedToString("@myAnno()"+ Utils.EOL+"[]", it); + } + + @Test + public void removingAnnotation() throws IOException { + ArrayCreationLevel it = consider("@myAnno []"); + it.getAnnotations().remove(0); + assertTransformedToString("[]", it); + } + + @Test + public void replacingAnnotation() throws IOException { + ArrayCreationLevel it = consider("@myAnno []"); + it.getAnnotations().set(0, new NormalAnnotationExpr(new Name("myOtherAnno"), new NodeList<>())); + assertTransformedToString("@myOtherAnno() []", it); + } + +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/CompilationUnitTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/CompilationUnitTransformationsTest.java new file mode 100644 index 000000000..304037829 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/CompilationUnitTransformationsTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast; + +import com.github.javaparser.ast.PackageDeclaration; +import com.github.javaparser.ast.expr.Name; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; + +import static com.github.javaparser.utils.Utils.EOL; + +/** + * Transforming CompilationUnit and verifying the LexicalPreservation works as expected. + */ +public class CompilationUnitTransformationsTest extends AbstractLexicalPreservingTest { + + // packageDeclaration + + @Test + public void addingPackageDeclaration() throws IOException { + considerCode("class A {}"); + cu.setPackageDeclaration(new PackageDeclaration(new Name(new Name("foo"), "bar"))); + assertTransformedToString("package foo.bar;"+ EOL + EOL + "class A {}", cu); + } + + @Test + public void removingPackageDeclaration() throws IOException { + considerCode("package foo.bar; class A {}"); + cu.removePackageDeclaration(); + assertTransformedToString("class A {}", cu); + } + + @Test + public void replacingPackageDeclaration() throws IOException { + considerCode("package foo.bar; class A {}"); + cu.setPackageDeclaration(new PackageDeclaration(new Name(new Name("foo2"), "baz"))); + assertTransformedToString("package foo2.baz;" + + EOL + EOL + + " class A {}", cu); + } + + // imports + + // types +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/AnnotationDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/AnnotationDeclarationTransformationsTest.java new file mode 100644 index 000000000..a2ffd40a1 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/AnnotationDeclarationTransformationsTest.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.body.AnnotationMemberDeclaration; +import com.github.javaparser.ast.type.PrimitiveType; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; +import java.util.EnumSet; + +import static org.junit.Assert.assertEquals; + +/** + * Transforming AnnotationDeclaration and verifying the LexicalPreservation works as expected. + */ +public class AnnotationDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + @Test + public void unchangedExamples() throws IOException { + assertUnchanged("AnnotationDeclaration_Example1"); + assertUnchanged("AnnotationDeclaration_Example3"); + assertUnchanged("AnnotationDeclaration_Example9"); + } + + // name + + @Test + public void changingName() throws IOException { + considerExample("AnnotationDeclaration_Example1_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().setName("NewName"); + assertTransformed("AnnotationDeclaration_Example1", cu); + } + + // modifiers + + @Test + public void addingModifiers() throws IOException { + considerExample("AnnotationDeclaration_Example1_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().setModifiers(EnumSet.of(Modifier.PUBLIC)); + assertTransformed("AnnotationDeclaration_Example2", cu); + } + + @Test + public void removingModifiers() throws IOException { + considerExample("AnnotationDeclaration_Example3_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().setModifiers(EnumSet.noneOf(Modifier.class)); + assertTransformed("AnnotationDeclaration_Example3", cu); + } + + @Test + public void replacingModifiers() throws IOException { + considerExample("AnnotationDeclaration_Example3_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().setModifiers(EnumSet.of(Modifier.PROTECTED)); + assertTransformed("AnnotationDeclaration_Example4", cu); + } + + // members + + @Test + public void addingMember() throws IOException { + considerExample("AnnotationDeclaration_Example3_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().addMember(new AnnotationMemberDeclaration(EnumSet.noneOf(Modifier.class), PrimitiveType.intType(), "foo", null)); + assertTransformed("AnnotationDeclaration_Example5", cu); + } + + @Test + public void removingMember() throws IOException { + considerExample("AnnotationDeclaration_Example3_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().getMember(2).remove(); + assertTransformed("AnnotationDeclaration_Example6", cu); + } + + @Test + public void replacingMember() throws IOException { + considerExample("AnnotationDeclaration_Example3_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().setMember(2, new AnnotationMemberDeclaration(EnumSet.noneOf(Modifier.class), PrimitiveType.intType(), "foo", null)); + assertTransformed("AnnotationDeclaration_Example7", cu); + } + + // javadoc + + @Test + public void addingJavadoc() throws IOException { + considerExample("AnnotationDeclaration_Example3_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().setJavadocComment("Cool this annotation!"); + assertTransformed("AnnotationDeclaration_Example8", cu); + } + + @Test + public void removingJavadoc() throws IOException { + considerExample("AnnotationDeclaration_Example9_original"); + boolean removed = cu.getAnnotationDeclarationByName("ClassPreamble").get().getJavadocComment().get().remove(); + assertEquals(true, removed); + assertTransformed("AnnotationDeclaration_Example9", cu); + } + + @Test + public void replacingJavadoc() throws IOException { + considerExample("AnnotationDeclaration_Example9_original"); + cu.getAnnotationDeclarationByName("ClassPreamble").get().setJavadocComment("Super extra cool this annotation!!!"); + assertTransformed("AnnotationDeclaration_Example10", cu); + } + +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/AnnotationMemberDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/AnnotationMemberDeclarationTransformationsTest.java new file mode 100644 index 000000000..6b5552807 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/AnnotationMemberDeclarationTransformationsTest.java @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.body.AnnotationMemberDeclaration; +import com.github.javaparser.ast.expr.IntegerLiteralExpr; +import com.github.javaparser.ast.expr.Name; +import com.github.javaparser.ast.expr.NormalAnnotationExpr; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.util.EnumSet; + +import static com.github.javaparser.utils.Utils.EOL; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Transforming AnnotationMemberDeclaration and verifying the LexicalPreservation works as expected. + */ +public class AnnotationMemberDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + protected AnnotationMemberDeclaration consider(String code) { + considerCode("@interface AD { " + code + " }"); + return cu.getAnnotationDeclarationByName("AD").get().getMember(0).asAnnotationMemberDeclaration(); + } + + // Name + + @Test + public void changingName() { + AnnotationMemberDeclaration md = consider("int foo();"); + md.setName("bar"); + assertTransformedToString("int bar();", md); + } + + // Type + + @Test + public void changingType() { + AnnotationMemberDeclaration md = consider("int foo();"); + md.setType("String"); + assertTransformedToString("String foo();", md); + } + + // Modifiers + + @Test + public void addingModifiers() { + AnnotationMemberDeclaration md = consider("int foo();"); + md.setModifiers(EnumSet.of(Modifier.PUBLIC)); + assertTransformedToString("public int foo();", md); + } + + @Test + public void removingModifiers() { + AnnotationMemberDeclaration md = consider("public int foo();"); + md.setModifiers(EnumSet.noneOf(Modifier.class)); + assertTransformedToString("int foo();", md); + } + + @Test + public void replacingModifiers() { + AnnotationMemberDeclaration md = consider("public int foo();"); + md.setModifiers(EnumSet.of(Modifier.PROTECTED)); + assertTransformedToString("protected int foo();", md); + } + + // Default value + + @Test + public void addingDefaultValue() { + AnnotationMemberDeclaration md = consider("int foo();"); + md.setDefaultValue(new IntegerLiteralExpr("10")); + assertTransformedToString("int foo() default 10;", md); + } + + @Test + public void removingDefaultValue() { + AnnotationMemberDeclaration md = consider("int foo() default 10;"); + assertEquals(true, md.getDefaultValue().get().remove()); + assertTransformedToString("int foo();", md); + } + + @Test + public void replacingDefaultValue() { + AnnotationMemberDeclaration md = consider("int foo() default 10;"); + md.setDefaultValue(new IntegerLiteralExpr("11")); + assertTransformedToString("int foo() default 11;", md); + } + + // Annotations + + @Test + public void addingAnnotation() { + AnnotationMemberDeclaration it = consider("int foo();"); + it.addAnnotation("myAnno"); + assertTransformedToString("@myAnno()" + EOL + "int foo();", it); + } + + @Test + public void addingTwoAnnotations() { + AnnotationMemberDeclaration it = consider("int foo();"); + it.addAnnotation("myAnno"); + it.addAnnotation("myAnno2"); + assertTransformedToString("@myAnno()" + EOL + "@myAnno2()" + EOL + "int foo();", it); + } + + @Test + public void removingAnnotationOnSomeLine() { + AnnotationMemberDeclaration it = consider("@myAnno int foo();"); + it.getAnnotations().remove(0); + assertTransformedToString("int foo();", it); + } + + @Test + public void removingAnnotationOnPrevLine() { + AnnotationMemberDeclaration it = consider("@myAnno" + EOL + "int foo();"); + it.getAnnotations().remove(0); + assertTransformedToString("int foo();", it); + } + + @Test + public void replacingAnnotation() { + AnnotationMemberDeclaration it = consider("@myAnno int foo();"); + it.getAnnotations().set(0, new NormalAnnotationExpr(new Name("myOtherAnno"), new NodeList<>())); + assertTransformedToString("@myOtherAnno() int foo();", it); + } + + // Javadoc + + @Test + public void addingJavadoc() { + AnnotationMemberDeclaration it = consider("int foo();"); + it.setJavadocComment("Cool this annotation!"); + assertTransformedToString("@interface AD { /** Cool this annotation!*/" + EOL + + "int foo(); }", it.getParentNode().get()); + } + + @Test + public void removingJavadoc() { + AnnotationMemberDeclaration it = consider("/** Cool this annotation!*/ int foo();"); + assertTrue(it.getJavadocComment().get().remove()); + assertTransformedToString("@interface AD { int foo(); }", it.getParentNode().get()); + } + + @Test + public void replacingJavadoc() { + AnnotationMemberDeclaration it = consider("/** Cool this annotation!*/ int foo();"); + it.setJavadocComment("Super extra cool this annotation!!!"); + assertTransformedToString("@interface AD { /** Super extra cool this annotation!!!*/ int foo(); }", it.getParentNode().get()); + } + +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/ClassOrInterfaceDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/ClassOrInterfaceDeclarationTransformationsTest.java new file mode 100644 index 000000000..1be80378d --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/ClassOrInterfaceDeclarationTransformationsTest.java @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.FieldDeclaration; +import com.github.javaparser.ast.body.VariableDeclarator; +import com.github.javaparser.ast.type.PrimitiveType; +import com.github.javaparser.ast.type.TypeParameter; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; +import java.util.EnumSet; + +import static com.github.javaparser.JavaParser.parseClassOrInterfaceType; +import static com.github.javaparser.utils.Utils.EOL; + +/** + * Transforming ClassOrInterfaceDeclaration and verifying the LexicalPreservation works as expected. + */ +public class ClassOrInterfaceDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + protected ClassOrInterfaceDeclaration consider(String code) { + considerCode(code); + return cu.getType(0).asClassOrInterfaceDeclaration(); + } + + // Name + + @Test + public void settingName() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A {}"); + cid.setName("B"); + assertTransformedToString("class B {}", cid); + } + + // isInterface + + @Test + public void classToInterface() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A {}"); + cid.setInterface(true); + assertTransformedToString("interface A {}", cid); + } + + @Test + public void interfaceToClass() throws IOException { + ClassOrInterfaceDeclaration cid = consider("interface A {}"); + cid.setInterface(false); + assertTransformedToString("class A {}", cid); + } + + // typeParameters + + @Test + public void addingTypeParameterWhenThereAreNone() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A {}"); + cid.addTypeParameter(new TypeParameter("T", new NodeList<>())); + assertTransformedToString("class A<T> {}", cid); + } + + @Test + public void addingTypeParameterAsFirstWhenThereAreSome() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A<U> {}"); + cid.getTypeParameters().addFirst(new TypeParameter("T", new NodeList<>())); + assertTransformedToString("class A<T, U> {}", cid); + } + + @Test + public void addingTypeParameterAsLastWhenThereAreSome() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A<U> {}"); + cid.addTypeParameter(new TypeParameter("T", new NodeList<>())); + assertTransformedToString("class A<U, T> {}", cid); + } + + // extendedTypes + + @Test + public void addingExtendedTypes() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A {}"); + cid.addExtendedType("Foo"); + assertTransformedToString("class A extends Foo {}", cid); + } + + @Test + public void removingExtendedTypes() throws IOException { + ClassOrInterfaceDeclaration cid = consider("public class A extends Foo {}"); + cid.getExtendedTypes().remove(0); + assertTransformedToString("public class A {}", cid); + } + + @Test + public void replacingExtendedTypes() throws IOException { + ClassOrInterfaceDeclaration cid = consider("public class A extends Foo {}"); + cid.getExtendedTypes().set(0, parseClassOrInterfaceType("Bar")); + assertTransformedToString("public class A extends Bar {}", cid); + } + + // implementedTypes + + @Test + public void addingImplementedTypes() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A {}"); + cid.addImplementedType("Foo"); + assertTransformedToString("class A implements Foo {}", cid); + } + + @Test + public void removingImplementedTypes() throws IOException { + ClassOrInterfaceDeclaration cid = consider("public class A implements Foo {}"); + cid.getImplementedTypes().remove(0); + assertTransformedToString("public class A {}", cid); + } + + @Test + public void replacingImplementedTypes() throws IOException { + ClassOrInterfaceDeclaration cid = consider("public class A implements Foo {}"); + cid.getImplementedTypes().set(0, parseClassOrInterfaceType("Bar")); + assertTransformedToString("public class A implements Bar {}", cid); + } + + // Modifiers + + @Test + public void addingModifiers() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A {}"); + cid.setModifiers(EnumSet.of(Modifier.PUBLIC)); + assertTransformedToString("public class A {}", cid); + } + + @Test + public void removingModifiers() throws IOException { + ClassOrInterfaceDeclaration cid = consider("public class A {}"); + cid.setModifiers(EnumSet.noneOf(Modifier.class)); + assertTransformedToString("class A {}", cid); + } + + @Test + public void replacingModifiers() throws IOException { + ClassOrInterfaceDeclaration cid = consider("public class A {}"); + cid.setModifiers(EnumSet.of(Modifier.PROTECTED)); + assertTransformedToString("protected class A {}", cid); + } + + // members + + @Test + public void addingField() throws IOException { + ClassOrInterfaceDeclaration cid = consider("class A {}"); + cid.addField("int", "foo"); + assertTransformedToString("class A {" + EOL + " int foo;" + EOL + "}", cid); + } + + @Test + public void removingField() throws IOException { + ClassOrInterfaceDeclaration cid = consider("public class A { int foo; }"); + cid.getMembers().remove(0); + assertTransformedToString("public class A {}", cid); + } + + @Test + public void replacingFieldWithAnotherField() throws IOException { + ClassOrInterfaceDeclaration cid = consider("public class A {float f;}"); + cid.getMembers().set(0, new FieldDeclaration(EnumSet.noneOf(Modifier.class), new VariableDeclarator(PrimitiveType.intType(), "bar"))); + assertTransformedToString("public class A {int bar;}", cid); + } + + // Annotations + + // Javadoc + +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/ConstructorDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/ConstructorDeclarationTransformationsTest.java new file mode 100644 index 000000000..6889a70ef --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/ConstructorDeclarationTransformationsTest.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.body.ConstructorDeclaration; +import com.github.javaparser.ast.body.Parameter; +import com.github.javaparser.ast.expr.SimpleName; +import com.github.javaparser.ast.type.ArrayType; +import com.github.javaparser.ast.type.PrimitiveType; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; +import java.util.EnumSet; + +/** + * Transforming ConstructorDeclaration and verifying the LexicalPreservation works as expected. + */ +public class ConstructorDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + protected ConstructorDeclaration consider(String code) { + considerCode("class A { " + code + " }"); + return cu.getType(0).getMembers().get(0).asConstructorDeclaration(); + } + + // Name + + @Test + public void settingName() throws IOException { + ConstructorDeclaration cd = consider("A(){}"); + cd.setName("B"); + assertTransformedToString("B(){}", cd); + } + + // JavaDoc + + // Modifiers + + @Test + public void addingModifiers() throws IOException { + ConstructorDeclaration cd = consider("A(){}"); + cd.setModifiers(EnumSet.of(Modifier.PUBLIC)); + assertTransformedToString("public A(){}", cd); + } + + @Test + public void removingModifiers() throws IOException { + ConstructorDeclaration cd = consider("public A(){}"); + cd.setModifiers(EnumSet.noneOf(Modifier.class)); + assertTransformedToString("A(){}", cd); + } + + @Test + public void replacingModifiers() throws IOException { + ConstructorDeclaration cd = consider("public A(){}"); + cd.setModifiers(EnumSet.of(Modifier.PROTECTED)); + assertTransformedToString("protected A(){}", cd); + } + + // Parameters + + @Test + public void addingParameters() throws IOException { + ConstructorDeclaration cd = consider("A(){}"); + cd.addParameter(PrimitiveType.doubleType(), "d"); + assertTransformedToString("A(double d){}", cd); + } + + @Test + public void removingOnlyParameter() throws IOException { + ConstructorDeclaration cd = consider("public A(double d){}"); + cd.getParameters().remove(0); + assertTransformedToString("public A(){}", cd); + } + + @Test + public void removingFirstParameterOfMany() throws IOException { + ConstructorDeclaration cd = consider("public A(double d, float f){}"); + cd.getParameters().remove(0); + assertTransformedToString("public A(float f){}", cd); + } + + @Test + public void removingLastParameterOfMany() throws IOException { + ConstructorDeclaration cd = consider("public A(double d, float f){}"); + cd.getParameters().remove(1); + assertTransformedToString("public A(double d){}", cd); + } + + @Test + public void replacingOnlyParameter() throws IOException { + ConstructorDeclaration cd = consider("public A(float f){}"); + cd.getParameters().set(0, new Parameter(new ArrayType(PrimitiveType.intType()), new SimpleName("foo"))); + assertTransformedToString("public A(int[] foo){}", cd); + } + + // ThrownExceptions + + // Body + + // Annotations +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/EnumConstantDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/EnumConstantDeclarationTransformationsTest.java new file mode 100644 index 000000000..dff71cc3c --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/EnumConstantDeclarationTransformationsTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.body.EnumConstantDeclaration; +import com.github.javaparser.ast.body.EnumDeclaration; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; + +/** + * Transforming EnumConstantDeclaration and verifying the LexicalPreservation works as expected. + */ +public class EnumConstantDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + protected EnumConstantDeclaration consider(String code) { + considerCode("enum A { " + code + " }"); + return cu.getType(0).asEnumDeclaration().getEntries().get(0); + } + + // Name + + @Test + public void settingName() throws IOException { + EnumConstantDeclaration ecd = consider("A"); + ecd.setName("B"); + assertTransformedToString("B", ecd); + } + + // Annotations + + // Javadoc + +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/EnumDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/EnumDeclarationTransformationsTest.java new file mode 100644 index 000000000..e2a8fa3df --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/EnumDeclarationTransformationsTest.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.body.EnumDeclaration; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; +import java.util.EnumSet; + +/** + * Transforming EnumDeclaration and verifying the LexicalPreservation works as expected. + */ +public class EnumDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + protected EnumDeclaration consider(String code) { + considerCode(code); + return cu.getType(0).asEnumDeclaration(); + } + + // Name + + @Test + public void settingName() throws IOException { + EnumDeclaration cid = consider("enum A { E1, E2 }"); + cid.setName("B"); + assertTransformedToString("enum B { E1, E2 }", cid); + } + + // implementedTypes + + // Modifiers + + @Test + public void addingModifiers() throws IOException { + EnumDeclaration ed = consider("enum A { E1, E2 }"); + ed.setModifiers(EnumSet.of(Modifier.PUBLIC)); + assertTransformedToString("public enum A { E1, E2 }", ed); + } + + @Test + public void removingModifiers() throws IOException { + EnumDeclaration ed = consider("public enum A { E1, E2 }"); + ed.setModifiers(EnumSet.noneOf(Modifier.class)); + assertTransformedToString("enum A { E1, E2 }", ed); + } + + @Test + public void replacingModifiers() throws IOException { + EnumDeclaration ed = consider("public enum A { E1, E2 }"); + ed.setModifiers(EnumSet.of(Modifier.PROTECTED)); + assertTransformedToString("protected enum A { E1, E2 }", ed); + } + + // members + + // Annotations + + // Javadoc + +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/FieldDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/FieldDeclarationTransformationsTest.java new file mode 100644 index 000000000..994f5d8b6 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/FieldDeclarationTransformationsTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.body.FieldDeclaration; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; +import java.util.EnumSet; + +/** + * Transforming FieldDeclaration and verifying the LexicalPreservation works as expected. + */ +public class FieldDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + protected FieldDeclaration consider(String code) { + considerCode("class A { " + code + " }"); + return cu.getType(0).getMembers().get(0).asFieldDeclaration(); + } + + // JavaDoc + + // Modifiers + + @Test + public void addingModifiers() { + FieldDeclaration it = consider("int A;"); + it.setModifiers(EnumSet.of(Modifier.PUBLIC)); + assertTransformedToString("public int A;", it); + } + + @Test + public void removingModifiers() { + FieldDeclaration it = consider("public int A;"); + it.setModifiers(EnumSet.noneOf(Modifier.class)); + assertTransformedToString("int A;", it); + } + + @Test + public void replacingModifiers() { + FieldDeclaration it = consider("int A;"); + it.setModifiers(EnumSet.of(Modifier.PROTECTED)); + assertTransformedToString("protected int A;", it); + } + + @Test + public void changingTypes() { + FieldDeclaration it = consider("int a, b;"); + assertTransformedToString("int a, b;", it); + it.getVariable(0).setType("Xyz"); + assertTransformedToString(" a, b;", it); + it.getVariable(1).setType("Xyz"); + assertTransformedToString("Xyz a, b;", it); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/InitializerDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/InitializerDeclarationTransformationsTest.java new file mode 100644 index 000000000..738acb36d --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/InitializerDeclarationTransformationsTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.body.InitializerDeclaration; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; + +/** + * Transforming InitializerDeclaration and verifying the LexicalPreservation works as expected. + */ +public class InitializerDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + protected InitializerDeclaration consider(String code) { + considerCode("class A { " + code + " }"); + return cu.getType(0).getMembers().get(0).asInitializerDeclaration(); + } + + // JavaDoc + + // Body + + // IsStatic + + @Test + public void instanceToStatic() throws IOException { + InitializerDeclaration it = consider("{ /*some comment*/ }"); + it.setStatic(true); + assertTransformedToString("static { /*some comment*/ }", it); + } + + @Test + public void staticToInstance() throws IOException { + InitializerDeclaration it = consider("static { /*some comment*/ }"); + it.setStatic(false); + assertTransformedToString("{ /*some comment*/ }", it); + } + + // Annotations +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/MethodDeclarationTransformationsTest.java b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/MethodDeclarationTransformationsTest.java new file mode 100644 index 000000000..1f94f3868 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/MethodDeclarationTransformationsTest.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.body.Parameter; +import com.github.javaparser.ast.expr.SimpleName; +import com.github.javaparser.ast.type.ArrayType; +import com.github.javaparser.ast.type.PrimitiveType; +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; +import org.junit.Test; + +import java.io.IOException; +import java.util.EnumSet; + +/** + * Transforming MethodDeclaration and verifying the LexicalPreservation works as expected. + */ +public class MethodDeclarationTransformationsTest extends AbstractLexicalPreservingTest { + + protected MethodDeclaration consider(String code) { + considerCode("class A { " + code + " }"); + return cu.getType(0).getMembers().get(0).asMethodDeclaration(); + } + + // Name + + @Test + public void settingName() throws IOException { + MethodDeclaration it = consider("void A(){}"); + it.setName("B"); + assertTransformedToString("void B(){}", it); + } + + // JavaDoc + + // Modifiers + + @Test + public void addingModifiers() throws IOException { + MethodDeclaration it = consider("void A(){}"); + it.setModifiers(EnumSet.of(Modifier.PUBLIC)); + assertTransformedToString("public void A(){}", it); + } + + @Test + public void removingModifiers() throws IOException { + MethodDeclaration it = consider("public void A(){}"); + it.setModifiers(EnumSet.noneOf(Modifier.class)); + assertTransformedToString("void A(){}", it); + } + + @Test + public void replacingModifiers() throws IOException { + MethodDeclaration it = consider("public void A(){}"); + it.setModifiers(EnumSet.of(Modifier.PROTECTED)); + assertTransformedToString("protected void A(){}", it); + } + + // Parameters + + @Test + public void addingParameters() throws IOException { + MethodDeclaration it = consider("void foo(){}"); + it.addParameter(PrimitiveType.doubleType(), "d"); + assertTransformedToString("void foo(double d){}", it); + } + + @Test + public void removingOnlyParameter() throws IOException { + MethodDeclaration it = consider("public void foo(double d){}"); + it.getParameters().remove(0); + assertTransformedToString("public void foo(){}", it); + } + + @Test + public void removingFirstParameterOfMany() throws IOException { + MethodDeclaration it = consider("public void foo(double d, float f){}"); + it.getParameters().remove(0); + assertTransformedToString("public void foo(float f){}", it); + } + + @Test + public void removingLastParameterOfMany() throws IOException { + MethodDeclaration it = consider("public void foo(double d, float f){}"); + it.getParameters().remove(1); + assertTransformedToString("public void foo(double d){}", it); + } + + @Test + public void replacingOnlyParameter() throws IOException { + MethodDeclaration it = consider("public void foo(float f){}"); + it.getParameters().set(0, new Parameter(new ArrayType(PrimitiveType.intType()), new SimpleName("foo"))); + assertTransformedToString("public void foo(int[] foo){}", it); + } + + // ThrownExceptions + + // Body + + // Annotations +} |