Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java')
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java179
1 files changed, 177 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java
index 8b0dfef6dc..e44740e17b 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter12Test.java
@@ -16,22 +16,32 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;
+import java.util.List;
+
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.Block;
+import org.eclipse.jdt.core.dom.BreakStatement;
import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Statement;
+import org.eclipse.jdt.core.dom.SwitchCase;
+import org.eclipse.jdt.core.dom.SwitchExpression;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.Type;
+import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import junit.framework.Test;
+@SuppressWarnings("rawtypes")
public class ASTConverter12Test extends ConverterTestSetup {
ICompilationUnit workingCopy;
@@ -64,7 +74,9 @@ public class ASTConverter12Test extends ConverterTestSetup {
this.workingCopy = null;
}
}
-
+ /*
+ * Test that a simple switch expression's return type holds the correct type
+ */
public void test0001() throws JavaModelException {
String contents =
" public class X {\n" +
@@ -110,7 +122,10 @@ public class ASTConverter12Test extends ConverterTestSetup {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
}
}
-
+ /*
+ * Test that a case statement with multiple cases is resolved correctly
+ * and has the correct source range
+ */
public void test0002() throws JavaModelException {
String contents =
"public class X {\n" +
@@ -159,6 +174,7 @@ public class ASTConverter12Test extends ConverterTestSetup {
assertEquals("Switch statement", node.getNodeType(), ASTNode.SWITCH_STATEMENT);
SwitchStatement switchStatement = (SwitchStatement) node;
checkSourceRange((Statement) switchStatement.statements().get(0), "case SATURDAY,SUNDAY ->", contents);
+ checkSourceRange((Statement) switchStatement.statements().get(2), "case MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY ->", contents);
} finally {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
}
@@ -217,4 +233,163 @@ public class ASTConverter12Test extends ConverterTestSetup {
javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
}
}
+ public void test0004() throws JavaModelException {
+ String contents =
+ "public class X {\n" +
+ " static enum Day {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, SATURDAY,SUNDAY}\n" +
+ " String bb(Day day) throws Exception {\n" +
+ " String today = switch (day) {\n" +
+ " case SATURDAY,SUNDAY:\n" +
+ " break \"Weekend day\";\n" +
+ " case MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY:\n" +
+ " break \"Week day\";\n" +
+ " default:\n" +
+ " break \"Any day\";\n" +
+ " };\n" +
+ " return today;\n" +
+ " }\n" +
+ "}" ;
+ this.workingCopy = getWorkingCopy("/Converter12/src/X.java", true/*resolve*/);
+ IJavaProject javaProject = this.workingCopy.getJavaProject();
+ String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
+ try {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
+ javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
+ ASTNode node = buildAST(
+ contents,
+ this.workingCopy);
+ assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
+ CompilationUnit compilationUnit = (CompilationUnit) node;
+ assertProblemsSize(compilationUnit, 0);
+ node = getASTNode(compilationUnit, 0, 1, 0);
+ assertEquals("Switch statement", node.getNodeType(), ASTNode.VARIABLE_DECLARATION_STATEMENT);
+ List fragments = ((VariableDeclarationStatement) node).fragments();
+ assertEquals("Incorrect no of fragments", 1, fragments.size());
+ node = (ASTNode) fragments.get(0);
+ assertEquals("Switch statement", node.getNodeType(), ASTNode.VARIABLE_DECLARATION_FRAGMENT);
+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) node;
+ Expression initializer = fragment.getInitializer();
+ assertEquals("incorrect type", ASTNode.SWITCH_EXPRESSION, initializer.getNodeType());
+ Expression expression = ((SwitchExpression) initializer).getExpression();
+ assertEquals("incorrect type", ASTNode.SIMPLE_NAME, expression.getNodeType());
+ assertEquals("incorrect name", "day", ((SimpleName) expression).getFullyQualifiedName());
+ List statements = ((SwitchExpression) initializer).statements();
+ assertEquals("incorrect no of statements", 6, statements.size());
+ BreakStatement brStmt = (BreakStatement) statements.get(1);
+ Expression expression2 = brStmt.getExpression();
+ assertNotNull("should not null", expression2);
+ assertEquals("incorrect node type", ASTNode.STRING_LITERAL, expression2.getNodeType());
+
+ //default case:
+ SwitchCase caseStmt = (SwitchCase) statements.get(4);
+ assertTrue("not default", caseStmt.isDefault());
+ brStmt = (BreakStatement) statements.get(5);
+ expression2 = brStmt.getExpression();
+ assertNotNull("should not null", expression2);
+ assertEquals("incorrect node type", ASTNode.STRING_LITERAL, expression2.getNodeType());
+
+ } finally {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
+ }
+ }
+ public void test0005() throws JavaModelException {
+ String contents =
+ "public class X {\n" +
+ " public String test001() {\n" +
+ " int i = 0;\n" +
+ " String ret = switch(i%2) {\n" +
+ " case 0 -> \"odd\";\n" +
+ " case 1 -> \"even\";\n" +
+ " default -> \"\";\n" +
+ " };\n" +
+ " return ret;\n" +
+ " }" +
+ "}" ;
+ this.workingCopy = getWorkingCopy("/Converter12/src/X.java", true/*resolve*/);
+ IJavaProject javaProject = this.workingCopy.getJavaProject();
+ String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
+ try {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
+ javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
+ ASTNode node = buildAST(
+ contents,
+ this.workingCopy);
+ assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
+ CompilationUnit compilationUnit = (CompilationUnit) node;
+ assertProblemsSize(compilationUnit, 0);
+ node = getASTNode(compilationUnit, 0, 0, 1);
+ assertEquals("Switch statement", node.getNodeType(), ASTNode.VARIABLE_DECLARATION_STATEMENT);
+ List fragments = ((VariableDeclarationStatement) node).fragments();
+ assertEquals("Incorrect no of fragments", 1, fragments.size());
+ node = (ASTNode) fragments.get(0);
+ assertEquals("Switch statement", node.getNodeType(), ASTNode.VARIABLE_DECLARATION_FRAGMENT);
+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) node;
+ Expression initializer = fragment.getInitializer();
+ assertEquals("incorrect type", ASTNode.SWITCH_EXPRESSION, initializer.getNodeType());
+ Expression expression = ((SwitchExpression) initializer).getExpression();
+ assertEquals("incorrect type", ASTNode.INFIX_EXPRESSION, expression.getNodeType());
+ List statements = ((SwitchExpression) initializer).statements();
+ assertEquals("incorrect no of statements", 6, statements.size());
+ BreakStatement brStmt = (BreakStatement) statements.get(1);
+ Expression expression2 = brStmt.getExpression();
+ assertNotNull("should not null", expression2);
+ assertEquals("incorrect node type", ASTNode.STRING_LITERAL, expression2.getNodeType());
+
+ //default case:
+ SwitchCase caseStmt = (SwitchCase) statements.get(4);
+ assertTrue("not default", caseStmt.isDefault());
+ brStmt = (BreakStatement) statements.get(5);
+ expression2 = brStmt.getExpression();
+ assertNotNull("should not null", expression2);
+ assertEquals("incorrect node type", ASTNode.STRING_LITERAL, expression2.getNodeType());
+
+ } finally {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
+ }
+ }
+ public void test0006() throws JavaModelException {
+ String contents =
+ "public class X {\n" +
+ " public String test001() {\n" +
+ " int i = 0;\n" +
+ " String ret = switch(i%2) {\n" +
+ " case 0 -> {return \"odd\"; }\n" +
+ " case 1 -> \"even\";\n" +
+ " default -> \"\";\n" +
+ " };\n" +
+ " return ret;\n" +
+ " }" +
+ "}" ;
+ this.workingCopy = getWorkingCopy("/Converter12/src/X.java", true/*resolve*/);
+ IJavaProject javaProject = this.workingCopy.getJavaProject();
+ String old = javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
+ try {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
+ javaProject.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
+ ASTNode node = buildAST(
+ contents,
+ this.workingCopy);
+ assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
+ CompilationUnit compilationUnit = (CompilationUnit) node;
+ assertProblemsSize(compilationUnit, 0);
+ node = getASTNode(compilationUnit, 0, 0, 1);
+ assertEquals("Switch statement", node.getNodeType(), ASTNode.VARIABLE_DECLARATION_STATEMENT);
+ List fragments = ((VariableDeclarationStatement) node).fragments();
+ assertEquals("Incorrect no of fragments", 1, fragments.size());
+ node = (ASTNode) fragments.get(0);
+ assertEquals("Switch statement", node.getNodeType(), ASTNode.VARIABLE_DECLARATION_FRAGMENT);
+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) node;
+ Expression initializer = fragment.getInitializer();
+ List statements = ((SwitchExpression) initializer).statements();
+ assertEquals("incorrect no of statements", 6, statements.size());
+ Block block = (Block) statements.get(1);
+ statements = block.statements();
+ assertEquals("incorrect no of statements", 1, statements.size());
+ Statement stmt = (Statement) statements.get(0);
+ assertEquals("incorrect node type", ASTNode.RETURN_STATEMENT, stmt.getNodeType());
+
+ } finally {
+ javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, old);
+ }
+ }
}

Back to the top