diff options
author | Danny van Bruggen <hexagonaal@gmail.com> | 2017-09-27 22:55:18 +0200 |
---|---|---|
committer | Danny van Bruggen <hexagonaal@gmail.com> | 2017-09-27 22:55:18 +0200 |
commit | f80c6cd0b31865ada2c77df0e159bb94646e1888 (patch) | |
tree | 408d587dfd64ee9697d319994c3a813ddfff1258 /javaparser-testing | |
parent | 060ab1715fe8eeb652627706b8225af85e57b955 (diff) | |
download | javaparser-f80c6cd0b31865ada2c77df0e159bb94646e1888.tar.gz |
Check that a union type must have at least two elements
Diffstat (limited to 'javaparser-testing')
-rw-r--r-- | javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java | 34 | ||||
-rw-r--r-- | javaparser-testing/src/test/java/com/github/javaparser/utils/TestUtils.java | 11 |
2 files changed, 37 insertions, 8 deletions
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 1b7c0ec1f..6bf000d26 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 @@ -3,17 +3,24 @@ 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.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; +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.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; @@ -57,24 +64,45 @@ public class Java7ValidatorTest { } @Test - public void nobinaryIntegerLiterals() { + public void binaryIntegerLiterals() { ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("0b01")); assertNoProblems(result); } @Test - public void noUnderscoresInIntegerLiterals() { + public void underscoresInIntegerLiterals() { ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("1_000_000")); assertNoProblems(result); } @Test - public void noMultiCatch() { + public void multiCatch() { ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try{}catch(Abc|Def e){}")); assertNoProblems(result); } @Test + public void multiCatchWithoutElements() { + UnionType unionType = new UnionType(); + + List<Problem> problems = new ArrayList<>(); + javaParser.getParserConfiguration().getValidator().accept(unionType, new ProblemReporter(problems)); + + assertProblems(problems, "UnionType.elements can not be empty."); + } + + @Test + public void multiCatchWithOneElement() { + UnionType unionType = new UnionType(); + unionType.getElements().add(new ClassOrInterfaceType()); + + List<Problem> problems = new ArrayList<>(); + javaParser.getParserConfiguration().getValidator().accept(unionType, new ProblemReporter(problems)); + + assertProblems(problems, "Union type (multi catch) must have at least two elements."); + } + + @Test public void noLambdas() { ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("a(() -> 1);")); assertProblems(result, "(line 1,col 3) Lambdas are not supported."); 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 c225b6568..e5f75e561 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 @@ -11,10 +11,7 @@ import java.io.*; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -127,7 +124,11 @@ public class TestUtils { } public static void assertProblems(ParseResult<?> result, String... expectedArg) { - Set<String> actual = result.getProblems().stream().map(Problem::toString).collect(Collectors.toSet()); + assertProblems(result.getProblems(), expectedArg); + } + + 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)); assertCollections(expected, actual); |