diff options
author | Jay Arthanareeswaran | 2019-03-18 13:46:58 +0000 |
---|---|---|
committer | Jay Arthanareeswaran | 2019-03-18 15:29:07 +0000 |
commit | 02d5a930beb4813367fc08986e6c2813a4f82fe0 (patch) | |
tree | e7936f5dbcbb116456572884c955bafcf57e7fda | |
parent | b0ea2537d4d56aca29790d69668f4176dbb83a83 (diff) | |
download | eclipse.jdt.core-02d5a930beb4813367fc08986e6c2813a4f82fe0.tar.gz eclipse.jdt.core-02d5a930beb4813367fc08986e6c2813a4f82fe0.tar.xz eclipse.jdt.core-02d5a930beb4813367fc08986e6c2813a4f82fe0.zip |
Change-Id: I0642ee03c21e369f2805aa7498b50e923fc44da4
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
2 files changed, 248 insertions, 3 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); + } + } } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java index d35c6cbb1c..6abaae5768 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingSwitchExpressionsTest.java @@ -29,6 +29,7 @@ import org.eclipse.jdt.core.dom.CompilationUnit; 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.StringLiteral; import org.eclipse.jdt.core.dom.SwitchCase; import org.eclipse.jdt.core.dom.SwitchExpression; import org.eclipse.jdt.core.dom.SwitchStatement; @@ -37,6 +38,7 @@ import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jdt.core.dom.VariableDeclarationStatement; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ListRewrite; + import junit.framework.Test; public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest { @@ -109,7 +111,7 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest { ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); - listRewrite.insertLast(caseStatement1, null); + listRewrite.insertAt(caseStatement1, 0, null); listRewrite.insertLast(statement1, null); listRewrite.insertLast(caseStatement2, null); } @@ -778,4 +780,72 @@ public class ASTRewritingSwitchExpressionsTest extends ASTRewritingTest { buf.append("}\n"); assertEqualString(preview, buf.toString()); } + @SuppressWarnings("rawtypes") + public void _testSwitchExpressions_05_since_12() throws Exception { + IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); + StringBuilder builder= new StringBuilder(); + builder.append("package test1;\n"); + builder.append("public class X {\n"); + builder.append(" public String foo(int i) {\n" + + " String ret = switch(i%2) {\n" + + " case 0 -> \"even\";\n" + + " default -> \"\";\n" + + " };\n" + + " return ret;"); + builder.append(" }\n"); + builder.append("}\n"); + ICompilationUnit cu= pack1.createCompilationUnit("X.java", builder.toString(), false, null); + + CompilationUnit astRoot= createAST(cu); + ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); + + assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); + TypeDeclaration type= findTypeDeclaration(astRoot, "X"); + MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); + Block block= methodDecl.getBody(); + List blockStatements= block.statements(); + AST ast= astRoot.getAST(); + assertEquals("incorrect no of statements", 2, blockStatements.size()); + { // insert a case + VariableDeclarationStatement varStatement= (VariableDeclarationStatement) blockStatements.get(0); + List fragments = varStatement.fragments(); + assertEquals("Incorrect no of fragments", 1, fragments.size()); + VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(0); + SwitchExpression initializer = (SwitchExpression) fragment.getInitializer(); + List statements= initializer.statements(); + assertEquals("incorrect Number of statements", 4, statements.size()); + + SwitchCase cse1 = (SwitchCase) statements.get(0); + rewrite.set(cse1, SwitchCase.SWITCH_LABELED_RULE_PROPERTY, Boolean.FALSE, null); + SwitchCase cse2 = (SwitchCase) statements.get(2); + rewrite.set(cse2, SwitchCase.SWITCH_LABELED_RULE_PROPERTY, Boolean.FALSE, null); + + ListRewrite listRewrite= rewrite.getListRewrite(initializer, SwitchExpression.STATEMENTS_PROPERTY); + SwitchCase caseStatement1= ast.newSwitchCase(); + caseStatement1.setSwitchLabeledRule(false); + caseStatement1.expressions().add(ast.newNumberLiteral("1")); + BreakStatement statement1= ast.newBreakStatement(); + StringLiteral literal1 = ast.newStringLiteral(); + literal1.setLiteralValue("odd"); + statement1.setExpression(literal1); + statement1.setImplicit(true); + listRewrite.insertAt(caseStatement1, 2, null); + listRewrite.insertAt(statement1, 3, null); + } + + String preview= evaluateRewrite(cu, rewrite); + builder= new StringBuilder(); + builder.append("package test1;\n"); + builder.append("public class X {\n"); + builder.append(" public String foo(int i) {\n" + + " String ret = switch(i%2) {\n" + + " case 0 : \"even\";\n" + + " case 1 : \"odd\";\n" + + " default : \"\";\n" + + " };\n" + + " return ret;"); + builder.append(" }\n"); + builder.append("}\n"); + assertEqualString(preview, builder.toString()); + } } |