From f80c6cd0b31865ada2c77df0e159bb94646e1888 Mon Sep 17 00:00:00 2001 From: Danny van Bruggen Date: Wed, 27 Sep 2017 22:55:18 +0200 Subject: Check that a union type must have at least two elements --- .../ast/validator/Java7ValidatorTest.java | 34 ++++++++++++++++++++-- .../com/github/javaparser/utils/TestUtils.java | 11 +++---- 2 files changed, 37 insertions(+), 8 deletions(-) (limited to 'javaparser-testing') 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,23 +64,44 @@ public class Java7ValidatorTest { } @Test - public void nobinaryIntegerLiterals() { + public void binaryIntegerLiterals() { ParseResult result = javaParser.parse(EXPRESSION, provider("0b01")); assertNoProblems(result); } @Test - public void noUnderscoresInIntegerLiterals() { + public void underscoresInIntegerLiterals() { ParseResult result = javaParser.parse(EXPRESSION, provider("1_000_000")); assertNoProblems(result); } @Test - public void noMultiCatch() { + public void multiCatch() { ParseResult result = javaParser.parse(STATEMENT, provider("try{}catch(Abc|Def e){}")); assertNoProblems(result); } + @Test + public void multiCatchWithoutElements() { + UnionType unionType = new UnionType(); + + List 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 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 result = javaParser.parse(STATEMENT, provider("a(() -> 1);")); 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 actual = result.getProblems().stream().map(Problem::toString).collect(Collectors.toSet()); + assertProblems(result.getProblems(), expectedArg); + } + + public static void assertProblems(List result, String... expectedArg) { + Set actual = result.stream().map(Problem::toString).collect(Collectors.toSet()); Set expected = new HashSet<>(); expected.addAll(Arrays.asList(expectedArg)); assertCollections(expected, actual); -- cgit v1.2.3