From 998d2c5ec839f303f9d0c9e16780f779b54d065c Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Fri, 21 Feb 2014 13:56:56 +0200 Subject: [PATCH] Add tests for JetRefactoringUtil.selectExpression Add tests for JetRefactoringUtil.selectExpression, which is invoked by e.g. extract variable when some text is selected. Test cases under idea/testData/expressionSelection/ should contain Kotlin files with the usual tags, with the expected result of selectExpression in the last comment of the file. --- .../jet/generators/tests/GenerateTests.kt | 5 ++ .../expressionSelection/binaryExpr.kt | 2 + .../expressionSelection/noExpression.kt | 2 + .../AbstractExpressionSelectionTest.java | 52 +++++++++++++++++++ .../ExpressionSelectionTestGenerated.java | 49 +++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 idea/testData/expressionSelection/binaryExpr.kt create mode 100644 idea/testData/expressionSelection/noExpression.kt create mode 100644 idea/tests/org/jetbrains/jet/plugin/AbstractExpressionSelectionTest.java create mode 100644 idea/tests/org/jetbrains/jet/plugin/ExpressionSelectionTestGenerated.java diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index d76c48d2d3f..ecd08f80e8d 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -95,6 +95,7 @@ import org.jetbrains.jet.checkers.AbstractJetDiagnosticsTestWithStdLib import org.jetbrains.jet.plugin.codeInsight.AbstractInsertImportOnPasteTest import org.jetbrains.jet.resolve.AbstractReferenceToJavaWithWrongFileStructureTest import org.jetbrains.jet.plugin.navigation.AbstractKotlinGotoTest +import org.jetbrains.jet.plugin.AbstractExpressionSelectionTest fun main(args: Array) { System.setProperty("java.awt.headless", "true") @@ -483,6 +484,10 @@ fun main(args: Array) { testClass(javaClass()) { model("smartSelection", testMethod = "doTestSmartSelection", pattern = """^([^\.]+)\.kt$""") } + + testClass(javaClass()) { + model("expressionSelection", testMethod = "doTestExpressionSelection", pattern = """^([^\.]+)\.kt$""") + } } testGroup("j2k/tests/test", "j2k/tests/testData") { diff --git a/idea/testData/expressionSelection/binaryExpr.kt b/idea/testData/expressionSelection/binaryExpr.kt new file mode 100644 index 00000000000..6a347968ade --- /dev/null +++ b/idea/testData/expressionSelection/binaryExpr.kt @@ -0,0 +1,2 @@ +val f = 40.5 + 1.5 +// 40.5 + 1.5 diff --git a/idea/testData/expressionSelection/noExpression.kt b/idea/testData/expressionSelection/noExpression.kt new file mode 100644 index 00000000000..4c1d30df105 --- /dev/null +++ b/idea/testData/expressionSelection/noExpression.kt @@ -0,0 +1,2 @@ +val f = 42.0 +/* */ diff --git a/idea/tests/org/jetbrains/jet/plugin/AbstractExpressionSelectionTest.java b/idea/tests/org/jetbrains/jet/plugin/AbstractExpressionSelectionTest.java new file mode 100644 index 00000000000..c967a729d02 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/AbstractExpressionSelectionTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2010-2014 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; + +import com.intellij.testFramework.LightCodeInsightTestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.lang.psi.JetExpression; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.plugin.refactoring.JetRefactoringUtil; + +public abstract class AbstractExpressionSelectionTest extends LightCodeInsightTestCase { + + public void doTestExpressionSelection(@NotNull String path) throws Exception { + configureByFile(path); + final String expectedExpression = JetTestUtils.getLastCommentInFile((JetFile) getFile()); + + try { + JetRefactoringUtil.selectExpression(getEditor(), getFile(), new JetRefactoringUtil.SelectExpressionCallback() { + @Override + public void run(@Nullable JetExpression expression) { + assertNotNull("Selected expression mustn't be null", expression); + assertEquals(expectedExpression, expression.getText()); + } + }); + } + catch (JetRefactoringUtil.IntroduceRefactoringException e) { + assertEquals(expectedExpression, ""); + } + } + + @NotNull + @Override + protected String getTestDataPath() { + return ""; + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/ExpressionSelectionTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/ExpressionSelectionTestGenerated.java new file mode 100644 index 00000000000..fedf61d0ca9 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/ExpressionSelectionTestGenerated.java @@ -0,0 +1,49 @@ +/* + * Copyright 2010-2014 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; + +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.AbstractExpressionSelectionTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/expressionSelection") +public class ExpressionSelectionTestGenerated extends AbstractExpressionSelectionTest { + public void testAllFilesPresentInExpressionSelection() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/expressionSelection"), Pattern.compile("^([^\\.]+)\\.kt$"), true); + } + + @TestMetadata("binaryExpr.kt") + public void testBinaryExpr() throws Exception { + doTestExpressionSelection("idea/testData/expressionSelection/binaryExpr.kt"); + } + + @TestMetadata("noExpression.kt") + public void testNoExpression() throws Exception { + doTestExpressionSelection("idea/testData/expressionSelection/noExpression.kt"); + } + +}