diff options
Diffstat (limited to 'javaparser-testing/src')
11 files changed, 163 insertions, 51 deletions
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/JavadocParserTest.java b/javaparser-testing/src/test/java/com/github/javaparser/JavadocParserTest.java index ea3d43e41..99e138e21 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/JavadocParserTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/JavadocParserTest.java @@ -104,7 +104,8 @@ public class JavadocParserTest { assertEquals(underTest, JavadocParser.parse(expectedText)); - assertEquals(underTest.getBlockTags().get(0).getTagName(), "unofficial"); + assertEquals(1, underTest.getBlockTags().size()); + assertEquals("unofficial", underTest.getBlockTags().get(0).getTagName()); } @Test diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ParserConfigurationTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ParserConfigurationTest.java index ab018ccf1..292634d72 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/ParserConfigurationTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/ParserConfigurationTest.java @@ -2,9 +2,14 @@ package com.github.javaparser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.stmt.Statement; +import com.github.javaparser.ast.validator.NoProblemsValidator; import org.junit.Test; +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; import static com.github.javaparser.Providers.provider; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -12,8 +17,18 @@ public class ParserConfigurationTest { @Test public void storeNoTokens() { ParseResult<CompilationUnit> result = new JavaParser(new ParserConfiguration().setStoreTokens(false)).parse(ParseStart.COMPILATION_UNIT, provider("class X{}")); - + assertFalse(result.getResult().get().getTokenRange().isPresent()); assertTrue(result.getResult().get().findAll(Node.class).stream().noneMatch(node -> node.getTokenRange().isPresent())); } + + @Test + public void noProblemsHere() { + ParseResult<Statement> result = + new JavaParser(new ParserConfiguration().setLanguageLevel(ANY)) + .parse(STATEMENT, provider("try{}")); + assertEquals(true, result.isSuccessful()); + } + + } diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/NodeListTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/NodeListTest.java index d8fe3d6c3..8e4d568bd 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/ast/NodeListTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/NodeListTest.java @@ -38,6 +38,8 @@ import java.util.List; import static com.github.javaparser.ast.NodeList.nodeList; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class NodeListTest { @@ -138,6 +140,46 @@ public class NodeListTest { } @Test + public void removeFirstNode() { + List<String> changes = new LinkedList<>(); + String code = "class A { int a; int b; int c; int d; int e; }"; + CompilationUnit cu = JavaParser.parse(code); + ClassOrInterfaceDeclaration cd = cu.getClassByName("A").get(); + cd.getMembers().register(createObserver(changes)); + + cd.getMembers().removeFirst(); + assertEquals(Arrays.asList("'int a;' REMOVAL in list at 0"), changes); + assertEquals(cd.getMembers().size(), 4); + + for (int i = 3; i >= 0; i--) { + assertTrue(cd.getMembers().removeFirst() != null); + assertEquals(cd.getMembers().size(), i); + } + + assertEquals(cd.getMembers().size(), 0); + } + + @Test + public void removeLastNode() { + List<String> changes = new LinkedList<>(); + String code = "class A { int a; int b; int c; int d; int e; }"; + CompilationUnit cu = JavaParser.parse(code); + ClassOrInterfaceDeclaration cd = cu.getClassByName("A").get(); + cd.getMembers().register(createObserver(changes)); + + cd.getMembers().removeLast(); + assertEquals(Arrays.asList("'int e;' REMOVAL in list at 4"), changes); + assertEquals(cd.getMembers().size(), 4); + + for (int i = 3; i >= 0; i--) { + assertTrue(cd.getMembers().removeLast() != null); + assertEquals(cd.getMembers().size(), i); + } + + assertEquals(cd.getMembers().size(), 0); + } + + @Test public void removeObject() { List<String> changes = new LinkedList<>(); String code = "class A { int a; int b; int c; int d; int e; }"; @@ -226,22 +268,22 @@ public class NodeListTest { assertEquals("[abc, bcd, cde]", list.toString()); } - + @Test public void addFirst() { final NodeList<Name> list = nodeList(new Name("abc"), new Name("bcd"), new Name("cde")); list.addFirst(new Name("xxx")); - + assertEquals("[xxx, abc, bcd, cde]", list.toString()); } - + @Test public void addLast() { final NodeList<Name> list = nodeList(new Name("abc"), new Name("bcd"), new Name("cde")); list.addLast(new Name("xxx")); - + assertEquals("[abc, bcd, cde, xxx]", list.toString()); } @@ -251,7 +293,7 @@ public class NodeListTest { final NodeList<Name> list = nodeList(new Name("abc"), n, new Name("cde")); list.addBefore(new Name("xxx"), n); - + assertEquals("[abc, xxx, bcd, cde]", list.toString()); } @@ -261,7 +303,7 @@ public class NodeListTest { final NodeList<Name> list = nodeList(new Name("abc"), n, new Name("cde")); list.addAfter(new Name("xxx"), n); - + assertEquals("[abc, bcd, xxx, cde]", list.toString()); } @@ -271,7 +313,7 @@ public class NodeListTest { final NodeList<Name> list = nodeList(abc, new Name("bcd"), new Name("cde")); list.addBefore(new Name("xxx"), abc); - + assertEquals("[xxx, abc, bcd, cde]", list.toString()); } @@ -281,7 +323,7 @@ public class NodeListTest { final NodeList<Name> list = nodeList(new Name("abc"), new Name("bcd"), cde); list.addAfter(new Name("xxx"), cde); - + assertEquals("[abc, bcd, cde, xxx]", list.toString()); } } diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/type/TypeTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/type/TypeTest.java index 77c3d97cb..aa2117291 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/ast/type/TypeTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/type/TypeTest.java @@ -6,11 +6,13 @@ import com.github.javaparser.ParseResult; import com.github.javaparser.ParserConfiguration; import com.github.javaparser.ast.expr.VariableDeclarationExpr; import com.github.javaparser.ast.validator.Java5Validator; +import com.github.javaparser.ast.validator.ProblemReporter; import org.junit.Test; import static com.github.javaparser.JavaParser.parseType; import static com.github.javaparser.JavaParser.parseVariableDeclarationExpr; import static com.github.javaparser.ParseStart.VARIABLE_DECLARATION_EXPR; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; import static com.github.javaparser.Providers.provider; import static org.junit.Assert.*; @@ -30,10 +32,11 @@ public class TypeTest { @Test public void primitiveTypeArgumentLenientValidator() { - ParserConfiguration config = new ParserConfiguration(); - config.setValidator(new Java5Validator() {{ + ParserConfiguration config = new ParserConfiguration() + .setLanguageLevel(ANY); + config.getPostProcessors().add(new Java5Validator() {{ remove(noPrimitiveGenericArguments); - }}); + }}.postProcessor()); ParseResult<VariableDeclarationExpr> result = new JavaParser(config).parse( VARIABLE_DECLARATION_EXPR, provider("List<long> x")); @@ -56,7 +59,7 @@ public class TypeTest { type.ifArrayType(t -> s[0] = t); assertNotNull(s[0]); } - + @Test public void issue1251() { final Type type = parseType("TypeUtilsTest<String>.Tester"); diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_0ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_0ValidatorTest.java index 09eb39d76..6606d2ec6 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_0ValidatorTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_0ValidatorTest.java @@ -91,7 +91,7 @@ public class Java1_0ValidatorTest { public void nonEmptyList() { ArrayCreationExpr expr = new ArrayCreationExpr(PrimitiveType.booleanType()); List<Problem> problems= new ArrayList<>(); - new Java1_0Validator().accept(expr, new ProblemReporter(problems)); + new Java1_0Validator().accept(expr, new ProblemReporter(problems::add)); assertEquals("ArrayCreationExpr.levels can not be empty.", problems.get(0).getMessage()); } } diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java index 6bf000d26..a2a03f837 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java @@ -5,7 +5,6 @@ import com.github.javaparser.ParseResult; import com.github.javaparser.ParserConfiguration; import com.github.javaparser.Problem; import com.github.javaparser.ast.CompilationUnit; -import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.type.ClassOrInterfaceType; @@ -13,19 +12,17 @@ import com.github.javaparser.ast.type.UnionType; import org.junit.Test; import java.util.*; -import java.util.function.Supplier; -import java.util.stream.Collectors; import static com.github.javaparser.ParseStart.COMPILATION_UNIT; import static com.github.javaparser.ParseStart.EXPRESSION; import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; import static com.github.javaparser.Providers.provider; -import static com.github.javaparser.utils.TestUtils.assertCollections; import static com.github.javaparser.utils.TestUtils.assertNoProblems; import static com.github.javaparser.utils.TestUtils.assertProblems; public class Java7ValidatorTest { - public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setValidator(new Java7Validator())); + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_7)); @Test public void generics() { @@ -86,7 +83,7 @@ public class Java7ValidatorTest { UnionType unionType = new UnionType(); List<Problem> problems = new ArrayList<>(); - javaParser.getParserConfiguration().getValidator().accept(unionType, new ProblemReporter(problems)); + new Java7Validator().accept(unionType, new ProblemReporter(problems::add)); assertProblems(problems, "UnionType.elements can not be empty."); } @@ -97,7 +94,7 @@ public class Java7ValidatorTest { unionType.getElements().add(new ClassOrInterfaceType()); List<Problem> problems = new ArrayList<>(); - javaParser.getParserConfiguration().getValidator().accept(unionType, new ProblemReporter(problems)); + new Java7Validator().accept(unionType, new ProblemReporter(problems::add)); assertProblems(problems, "Union type (multi catch) must have at least two elements."); } diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/ValidatorTest.java deleted file mode 100644 index be87ec18f..000000000 --- a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/ValidatorTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.javaparser.ast.validator; - -import com.github.javaparser.JavaParser; -import com.github.javaparser.ParseResult; -import com.github.javaparser.ParserConfiguration; -import com.github.javaparser.ast.stmt.Statement; -import org.junit.Test; - -import static com.github.javaparser.ParseStart.STATEMENT; -import static com.github.javaparser.Providers.provider; -import static org.junit.Assert.assertEquals; - -public class ValidatorTest { - public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setValidator(new Java9Validator())); - - @Test - public void noProblemsHere() { - ParseResult<Statement> result = - new JavaParser(new ParserConfiguration().setValidator(new NoProblemsValidator())) - .parse(STATEMENT, provider("try{}")); - assertEquals(true, result.isSuccessful()); - } - -} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/javadoc/JavadocTest.java b/javaparser-testing/src/test/java/com/github/javaparser/javadoc/JavadocTest.java index f102ba525..7377dfb86 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/javadoc/JavadocTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/javadoc/JavadocTest.java @@ -24,6 +24,8 @@ package com.github.javaparser.javadoc; import com.github.javaparser.JavaParser; import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.javadoc.description.JavadocDescription; +import com.github.javaparser.javadoc.description.JavadocDescriptionElement; +import com.github.javaparser.javadoc.description.JavadocInlineTag; import org.junit.Test; import static com.github.javaparser.utils.Utils.EOL; @@ -102,4 +104,35 @@ public class JavadocTest { Javadoc javadoc = JavaParser.parseJavadoc(comment); assertEquals(2, javadoc.getBlockTags().size()); } + + @Test + public void blockTagModificationWorks() { + Javadoc javadoc = new Javadoc(new JavadocDescription()); + + assertEquals(0, javadoc.getBlockTags().size()); + JavadocBlockTag blockTag = new JavadocBlockTag(JavadocBlockTag.Type.RETURN, "a value"); + javadoc.addBlockTag(blockTag); + + assertEquals(1, javadoc.getBlockTags().size()); + assertEquals(blockTag, javadoc.getBlockTags().get(0)); + + assertEquals(blockTag, javadoc.getBlockTags().remove(0)); + assertEquals(0, javadoc.getBlockTags().size()); + } + + @Test + public void descriptionModificationWorks() { + JavadocDescription description = new JavadocDescription(); + + assertEquals(0, description.getElements().size()); + + JavadocDescriptionElement inlineTag = new JavadocInlineTag("inheritDoc", JavadocInlineTag.Type.INHERIT_DOC, ""); + assertTrue(description.addElement(inlineTag)); + + assertEquals(1, description.getElements().size()); + assertEquals(inlineTag, description.getElements().get(0)); + + assertEquals(inlineTag, description.getElements().remove(0)); + assertEquals(0, description.getElements().size()); + } } diff --git a/javaparser-testing/src/test/java/com/github/javaparser/utils/SourceRootTest.java b/javaparser-testing/src/test/java/com/github/javaparser/utils/SourceRootTest.java index fff017182..a0bb50e2e 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/utils/SourceRootTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/utils/SourceRootTest.java @@ -6,10 +6,8 @@ import org.junit.Test; import java.io.File; import java.io.IOException; -import java.net.URISyntaxException; import java.nio.file.Path; import java.util.List; -import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -19,7 +17,7 @@ public class SourceRootTest { private final SourceRoot sourceRoot = new SourceRoot(root); @Test - public void parseTestDirectory() throws URISyntaxException, IOException { + public void parseTestDirectory() throws IOException { List<ParseResult<CompilationUnit>> parseResults = sourceRoot.tryToParse(); List<CompilationUnit> units = sourceRoot.getCompilationUnits(); diff --git a/javaparser-testing/src/test/java/com/github/javaparser/utils/TestUtils.java b/javaparser-testing/src/test/java/com/github/javaparser/utils/TestUtils.java index dc96f7c1a..0213c1191 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/utils/TestUtils.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/utils/TestUtils.java @@ -20,6 +20,7 @@ import static com.github.javaparser.Providers.provider; import static com.github.javaparser.utils.CodeGenerationUtils.f; import static com.github.javaparser.utils.Utils.EOL; import static com.github.javaparser.utils.Utils.normalizeEolInTextBlock; +import static java.util.Arrays.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; @@ -130,8 +131,7 @@ public class TestUtils { public static void assertProblems(List<Problem> result, String... expectedArg) { Set<String> actual = result.stream().map(Problem::toString).collect(Collectors.toSet()); - Set<String> expected = new HashSet<>(); - expected.addAll(Arrays.asList(expectedArg)); + Set<String> expected = new HashSet<>(asList(expectedArg)); assertCollections(expected, actual); } diff --git a/javaparser-testing/src/test/java/com/github/javaparser/version/Java10PostProcessorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/version/Java10PostProcessorTest.java new file mode 100644 index 000000000..87f7990a1 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/version/Java10PostProcessorTest.java @@ -0,0 +1,47 @@ +package com.github.javaparser.version; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.stmt.Statement; +import com.github.javaparser.ast.type.VarType; +import org.junit.Test; + +import java.util.List; + +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_10_PREVIEW; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Java10PostProcessorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_10_PREVIEW)); + + @Test + public void varIsAType() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var x=\"\";")); + + List<VarType> allVarTypes = result.getResult().get().findAll(VarType.class); + + assertEquals(1, allVarTypes.size()); + } + + @Test + public void varAllowedInLocalVariableDeclaration() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var a = 5;")); + assertNoProblems(result); + } + + @Test + public void varAllowedInForEach() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("for(var a : as){}")); + assertNoProblems(result); + } + + @Test + public void varAllowedInOldFor() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("for(var a = 5;a<9;a++){}")); + assertNoProblems(result); + } +} |