diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index 46b67625f8a..3e1cc6b5a01 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -31,6 +31,7 @@ import org.jetbrains.jet.jvm.compiler.*; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveDescriptorRendererTest; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparingTest; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveTest; +import org.jetbrains.jet.plugin.codeInsight.codeTransformations.AbstractCodeTransformationTest; import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTest; import org.jetbrains.jet.plugin.folding.AbstractKotlinFoldingTest; import org.jetbrains.jet.plugin.hierarchy.AbstractHierarchyTest; @@ -259,6 +260,14 @@ public class GenerateTests { testModel("idea/testData/codeInsight/surroundWith/functionLiteral", "doTestWithFunctionLiteralSurrounder") ); + generateTest( + "idea/tests/", + "CodeTransformationsTestGenerated", + AbstractCodeTransformationTest.class, + testModel("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression", "doTestIfStatementWithAssignmentsToExpression"), + testModel("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement", "doTestAssignmentWithIfExpressionToStatement") + ); + generateTest( "idea/tests/", "HierarchyTestGenerated", diff --git a/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt new file mode 100644 index 00000000000..3202a0646e5 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt @@ -0,0 +1,21 @@ +fun test(n: Int): String { + var res: String + + if (n == 1) { + res = if (3 > 2) { + println("***") + "one" + } else { + println("***") + "???" + } + } else if (n == 2) { + println("***") + res = "two" + } else { + println("***") + res = "too many" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt.after b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt.after new file mode 100644 index 00000000000..b30ef836b9d --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt.after @@ -0,0 +1,21 @@ +fun test(n: Int): String { + var res: String + + if (n == 1) { + if (3 > 2) { + println("***") + res = "one" + } else { + println("***") + res = "???" + } + } else if (n == 2) { + println("***") + res = "two" + } else { + println("***") + res = "too many" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt new file mode 100644 index 00000000000..d5ae1e4a2b0 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt @@ -0,0 +1,21 @@ +fun test(n: Int): String { + var res: String + + res = if (n == 1) { + if (3 > 2) { + println("***") + "one" + } else { + println("***") + "???" + } + } else if (n == 2) { + println("***") + "two" + } else { + println("***") + "too many" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt.after b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt.after new file mode 100644 index 00000000000..f7be6ee23aa --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt.after @@ -0,0 +1,21 @@ +fun test(n: Int): String { + var res: String + + if (n == 1) { + if (3 > 2) { + println("***") + res = "one" + } else { + println("***") + res = "???" + } + } else if (n == 2) { + println("***") + res = "two" + } else { + println("***") + res = "too many" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt new file mode 100644 index 00000000000..7307c997714 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt @@ -0,0 +1,13 @@ +fun test(n: Int): String { + var res: String + + res = if (n == 1) { + println("***") + "one" + } else { + println("***") + "two" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt.after b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt.after new file mode 100644 index 00000000000..c64e90844c9 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt.after @@ -0,0 +1,13 @@ +fun test(n: Int): String { + var res: String + + if (n == 1) { + println("***") + res = "one" + } else { + println("***") + res = "two" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIfWithoutAssignment.kt b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIfWithoutAssignment.kt new file mode 100644 index 00000000000..3a76ff13628 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIfWithoutAssignment.kt @@ -0,0 +1,12 @@ +// IS_APPLICABLE: false +fun test(n: Int): String { + var res: String + + if (n == 1) { + "one" + } else { + "two" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt new file mode 100644 index 00000000000..b30ef836b9d --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt @@ -0,0 +1,21 @@ +fun test(n: Int): String { + var res: String + + if (n == 1) { + if (3 > 2) { + println("***") + res = "one" + } else { + println("***") + res = "???" + } + } else if (n == 2) { + println("***") + res = "two" + } else { + println("***") + res = "too many" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt.after b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt.after new file mode 100644 index 00000000000..3202a0646e5 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt.after @@ -0,0 +1,21 @@ +fun test(n: Int): String { + var res: String + + if (n == 1) { + res = if (3 > 2) { + println("***") + "one" + } else { + println("***") + "???" + } + } else if (n == 2) { + println("***") + res = "two" + } else { + println("***") + res = "too many" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt new file mode 100644 index 00000000000..f7be6ee23aa --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt @@ -0,0 +1,21 @@ +fun test(n: Int): String { + var res: String + + if (n == 1) { + if (3 > 2) { + println("***") + res = "one" + } else { + println("***") + res = "???" + } + } else if (n == 2) { + println("***") + res = "two" + } else { + println("***") + res = "too many" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt.after b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt.after new file mode 100644 index 00000000000..d5ae1e4a2b0 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt.after @@ -0,0 +1,21 @@ +fun test(n: Int): String { + var res: String + + res = if (n == 1) { + if (3 > 2) { + println("***") + "one" + } else { + println("***") + "???" + } + } else if (n == 2) { + println("***") + "two" + } else { + println("***") + "too many" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt new file mode 100644 index 00000000000..c64e90844c9 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt @@ -0,0 +1,13 @@ +fun test(n: Int): String { + var res: String + + if (n == 1) { + println("***") + res = "one" + } else { + println("***") + res = "two" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt.after b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt.after new file mode 100644 index 00000000000..7307c997714 --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt.after @@ -0,0 +1,13 @@ +fun test(n: Int): String { + var res: String + + res = if (n == 1) { + println("***") + "one" + } else { + println("***") + "two" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutElse.kt b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutElse.kt new file mode 100644 index 00000000000..6c03979c77c --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutElse.kt @@ -0,0 +1,11 @@ +// IS_APPLICABLE: false +fun test(n: Int): String { + var res: String = "" + + if (n == 1) { + println("***") + res = "one" + } + + return res +} \ No newline at end of file diff --git a/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutTerminatingAssignment.kt b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutTerminatingAssignment.kt new file mode 100644 index 00000000000..9278328138f --- /dev/null +++ b/idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutTerminatingAssignment.kt @@ -0,0 +1,14 @@ +// IS_APPLICABLE: false +fun test(n: Int): String { + var res: String + + if (n == 1) { + res = "one" + println("***") + } else { + println("***") + res = "two" + } + + return res +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AbstractCodeTransformationTest.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AbstractCodeTransformationTest.java new file mode 100644 index 00000000000..ba5099b361b --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AbstractCodeTransformationTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.codeInsight.codeTransformations; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.testFramework.LightCodeInsightTestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.InTextDirectivesUtils; + +import java.io.File; + +public abstract class AbstractCodeTransformationTest extends LightCodeInsightTestCase { + public void doTestIfStatementWithAssignmentsToExpression(@NotNull String path) throws Exception { + doTest(path, new IfStatementWithAssignmentsToExpressionIntention()); + } + + public void doTestAssignmentWithIfExpressionToStatement(@NotNull String path) throws Exception { + doTest(path, new AssignmentWithIfExpressionToStatementIntention()); + } + + private void doTest(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception { + configureByFile(path); + + String fileText = FileUtil.loadFile(new File(path)); + String isApplicableString = InTextDirectivesUtils.findStringWithPrefix("// IS_APPLICABLE: ", fileText); + boolean isApplicableExpected = isApplicableString == null || isApplicableString.equals("true"); + + assert isApplicableExpected == intentionAction.isAvailable(getProject(), getEditor(), getFile()) + : "isAvailable() for " + intentionAction.getClass() + " should return " + isApplicableExpected; + if (isApplicableExpected) { + invokeAndCheck(path, intentionAction); + } + } + + private void invokeAndCheck(@NotNull String path, @NotNull IntentionAction intentionAction) { + intentionAction.invoke(getProject(), getEditor(), getFile()); + checkResultByFile(path + ".after"); + } + + @NotNull + @Override + protected String getTestDataPath() { + return ""; + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationsTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationsTestGenerated.java new file mode 100644 index 00000000000..1617edda4e1 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationsTestGenerated.java @@ -0,0 +1,102 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.codeInsight.codeTransformations; + +import junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestSuite; + +import java.io.File; +import java.util.regex.Pattern; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.test.InnerTestClasses; +import org.jetbrains.jet.test.TestMetadata; + +import org.jetbrains.jet.plugin.codeInsight.codeTransformations.AbstractCodeTransformationTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@InnerTestClasses({CodeTransformationsTestGenerated.IfStatementWithAssignmentsToExpression.class, CodeTransformationsTestGenerated.AssignmentWithIfExpressionToStatement.class}) +public class CodeTransformationsTestGenerated extends AbstractCodeTransformationTest { + @TestMetadata("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression") + public static class IfStatementWithAssignmentsToExpression extends AbstractCodeTransformationTest { + public void testAllFilesPresentInIfStatementWithAssignmentsToExpression() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("innerIfTransformed.kt") + public void testInnerIfTransformed() throws Exception { + doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt"); + } + + @TestMetadata("nestedIfs.kt") + public void testNestedIfs() throws Exception { + doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt"); + } + + @TestMetadata("simpleIf.kt") + public void testSimpleIf() throws Exception { + doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt"); + } + + @TestMetadata("simpleIfWithoutElse.kt") + public void testSimpleIfWithoutElse() throws Exception { + doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutElse.kt"); + } + + @TestMetadata("simpleIfWithoutTerminatingAssignment.kt") + public void testSimpleIfWithoutTerminatingAssignment() throws Exception { + doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutTerminatingAssignment.kt"); + } + + } + + @TestMetadata("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement") + public static class AssignmentWithIfExpressionToStatement extends AbstractCodeTransformationTest { + public void testAllFilesPresentInAssignmentWithIfExpressionToStatement() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("innerIfTransformed.kt") + public void testInnerIfTransformed() throws Exception { + doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt"); + } + + @TestMetadata("nestedIfs.kt") + public void testNestedIfs() throws Exception { + doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt"); + } + + @TestMetadata("simpleIf.kt") + public void testSimpleIf() throws Exception { + doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt"); + } + + @TestMetadata("simpleIfWithoutAssignment.kt") + public void testSimpleIfWithoutAssignment() throws Exception { + doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIfWithoutAssignment.kt"); + } + + } + + public static Test suite() { + TestSuite suite = new TestSuite("CodeTransformationsTestGenerated"); + suite.addTestSuite(IfStatementWithAssignmentsToExpression.class); + suite.addTestSuite(AssignmentWithIfExpressionToStatement.class); + return suite; + } +}