diff options
author | Danny van Bruggen <hexagonaal@gmail.com> | 2018-02-05 21:21:02 +0100 |
---|---|---|
committer | Danny van Bruggen <hexagonaal@gmail.com> | 2018-02-05 21:21:02 +0100 |
commit | be10f7278344dacb0bcc43553579b5add828f9b7 (patch) | |
tree | f25210beef356f9c284fd848cf2bdf594fb8dcd4 /javaparser-testing/src/test/java/com | |
parent | 427ecca0f2cfb07a81cc6ed0ec6801c631b9d7bc (diff) | |
download | platform_external_javaparser-be10f7278344dacb0bcc43553579b5add828f9b7.tar.gz platform_external_javaparser-be10f7278344dacb0bcc43553579b5add828f9b7.tar.bz2 platform_external_javaparser-be10f7278344dacb0bcc43553579b5add828f9b7.zip |
Basic var support
Diffstat (limited to 'javaparser-testing/src/test/java/com')
7 files changed, 80 insertions, 126 deletions
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 ab4bb7498..33c9208cc 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.getTokens().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/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/Java7ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java index e72cd83ba..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 @@ -16,12 +16,13 @@ import java.util.*; 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.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() { @@ -82,7 +83,7 @@ public class Java7ValidatorTest { UnionType unionType = new UnionType(); List<Problem> problems = new ArrayList<>(); - javaParser.getParserConfiguration().getValidator().get().accept(unionType, new ProblemReporter(problems::add)); + new Java7Validator().accept(unionType, new ProblemReporter(problems::add)); assertProblems(problems, "UnionType.elements can not be empty."); } @@ -93,7 +94,7 @@ public class Java7ValidatorTest { unionType.getElements().add(new ClassOrInterfaceType()); List<Problem> problems = new ArrayList<>(); - javaParser.getParserConfiguration().getValidator().get().accept(unionType, new ProblemReporter(problems::add)); + 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/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..4cbb37743 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/version/Java10PostProcessorTest.java @@ -0,0 +1,51 @@ +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.CompilationUnit; +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.COMPILATION_UNIT; +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.ast.validator.Java1_1ValidatorTest.allModifiers; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Java10PostProcessorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_10)); + + @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); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/version/Java10ProcessorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/version/Java10ProcessorTest.java deleted file mode 100644 index 7445334d4..000000000 --- a/javaparser-testing/src/test/java/com/github/javaparser/version/Java10ProcessorTest.java +++ /dev/null @@ -1,92 +0,0 @@ -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.CompilationUnit; -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.COMPILATION_UNIT; -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.ast.validator.Java1_1ValidatorTest.allModifiers; -import static com.github.javaparser.utils.TestUtils.assertNoProblems; -import static com.github.javaparser.utils.TestUtils.assertProblems; -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class Java10ProcessorTest { - public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_10)); - - @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 underscoreIdentifiers() { - ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("a.b._.c.d = act(_, _ -> _);")); - assertProblems(result, - "(line 1,col 5) '_' is a reserved keyword.", - "(line 1,col 17) '_' is a reserved keyword.", - "(line 1,col 20) '_' is a reserved keyword.", - "(line 1,col 25) '_' is a reserved keyword." - ); - } - - @Test - public void moduleRequires() { - ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("module x{requires " + allModifiers + " a;}")); - assertProblems(result, - "(line 1,col 10) Can have only one of 'public', 'protected', 'private'.", - "(line 1,col 10) Can have only one of 'final', 'abstract'.", - "(line 1,col 10) Can have only one of 'native', 'strictfp'.", - "(line 1,col 10) 'transient' is not allowed here.", - "(line 1,col 10) 'volatile' is not allowed here.", - "(line 1,col 10) 'final' is not allowed here.", - "(line 1,col 10) 'synchronized' is not allowed here.", - "(line 1,col 10) 'default' is not allowed here.", - "(line 1,col 10) 'native' is not allowed here.", - "(line 1,col 10) 'private' is not allowed here.", - "(line 1,col 10) 'protected' is not allowed here.", - "(line 1,col 10) 'strictfp' is not allowed here.", - "(line 1,col 10) 'abstract' is not allowed here.", - "(line 1,col 10) 'public' is not allowed here." - ); - } - - @Test - public void interfaceMethod() { - ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X{" + allModifiers + "int x(){};}")); - assertProblems(result, - "(line 1,col 13) Can have only one of 'public', 'protected', 'private'.", - "(line 1,col 13) Can have only one of 'final', 'abstract'.", - "(line 1,col 13) Can have only one of 'native', 'strictfp'.", - "(line 1,col 13) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.", - "(line 1,col 13) 'transient' is not allowed here.", - "(line 1,col 13) 'volatile' is not allowed here.", - "(line 1,col 13) 'transitive' is not allowed here." - ); - } - - @Test - public void modules() { - ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("open module x {}")); - assertNoProblems(result); - } - - @Test - public void tryWithResourceReference() { - ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(a.b.c){}")); - assertNoProblems(result); - } - -} |