From 7e99a6bb452ffce9cd49284938e3155de8a5efcc Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 5 May 2016 12:01:03 +0300 Subject: [PATCH] Refactored AbstractCodeMoverTest --- .../kotlin/generators/tests/GenerateTests.kt | 4 +- .../moveUpDown/AbstractCodeMoverTest.kt | 57 ++++++++++--------- ...d.java => MoveStatementTestGenerated.java} | 38 ++++++------- 3 files changed, 51 insertions(+), 48 deletions(-) rename idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/{CodeMoverTestGenerated.java => MoveStatementTestGenerated.java} (98%) diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index c22a3680c5b..bea23af05d3 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -47,7 +47,7 @@ import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractCodeInsightActionT import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateHashCodeAndEqualsActionTest import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateTestSupportMethodActionTest import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateToStringActionTest -import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractCodeMoverTest +import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractMoveStatementTest import org.jetbrains.kotlin.idea.codeInsight.surroundWith.AbstractSurroundWithTest import org.jetbrains.kotlin.idea.codeInsight.unwrap.AbstractUnwrapRemoveTest import org.jetbrains.kotlin.idea.completion.test.* @@ -504,7 +504,7 @@ fun main(args: Array) { model("hierarchy/overrides", extension = null, recursive = false, testMethod = "doOverrideHierarchyTest") } - testClass() { + testClass() { model("codeInsight/moveUpDown/classBodyDeclarations", testMethod = "doTestClassBodyDeclaration") model("codeInsight/moveUpDown/closingBraces", testMethod = "doTestExpression") model("codeInsight/moveUpDown/expressions", testMethod = "doTestExpression") diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/AbstractCodeMoverTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/AbstractCodeMoverTest.kt index 3f311d8581a..4940c9ade02 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/AbstractCodeMoverTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/AbstractCodeMoverTest.kt @@ -16,10 +16,13 @@ package org.jetbrains.kotlin.idea.codeInsight.moveUpDown +import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementLeftAction +import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementRightAction import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementDownAction import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementUpAction import com.intellij.codeInsight.editorActions.moveUpDown.StatementUpDownMover import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.editor.actionSystem.EditorAction import com.intellij.openapi.extensions.Extensions import com.intellij.openapi.util.io.FileUtil import com.intellij.testFramework.LightCodeInsightTestCase @@ -32,7 +35,7 @@ import org.jetbrains.kotlin.test.InTextDirectivesUtils import org.jetbrains.kotlin.test.KotlinTestUtils import java.io.File -abstract class AbstractCodeMoverTest : LightCodeInsightTestCase() { +abstract class AbstractMoveStatementTest : AbstractCodeMoverTest() { protected fun doTestClassBodyDeclaration(path: String) { doTest(path, KotlinDeclarationMover::class.java) } @@ -41,53 +44,53 @@ abstract class AbstractCodeMoverTest : LightCodeInsightTestCase() { doTest(path, KotlinExpressionMover::class.java) } - private fun doTest(path: String, moverClass: Class) { + private fun doTest(path: String, defaultMoverClass: Class) { + doTest(path) { isApplicableExpected, direction -> + val movers = Extensions.getExtensions(StatementUpDownMover.STATEMENT_UP_DOWN_MOVER_EP) + val info = StatementUpDownMover.MoveInfo() + val actualMover = movers.firstOrNull { + it.checkAvailable(LightPlatformCodeInsightTestCase.getEditor(), LightPlatformCodeInsightTestCase.getFile(), info, direction == "down") + } ?: error("No mover found") + + assertEquals("Unmatched movers", defaultMoverClass.name, actualMover.javaClass.name) + assertEquals("Invalid applicability", isApplicableExpected, info.toMove2 != null) + } + } +} + +abstract class AbstractCodeMoverTest : LightCodeInsightTestCase() { + protected fun doTest(path: String, isApplicableChecker: (isApplicableExpected: Boolean, direction: String) -> Unit = { isApplicableExpected, direction -> }) { configureByFile(path) val fileText = FileUtil.loadFile(File(path), true) val direction = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// MOVE: ") + ?: error("No MOVE directive found") - var down = true - if ("up" == direction) { - down = false - } - else if ("down" == direction) { - down = true - } - else { - fail("Direction is not specified") + val action = when (direction) { + "up" -> MoveStatementUpAction() + "down" -> MoveStatementDownAction() + "left" -> MoveElementLeftAction() + "right" -> MoveElementRightAction() + else -> error("Unknown direction: $direction") } val isApplicableString = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// IS_APPLICABLE: ") val isApplicableExpected = isApplicableString == null || isApplicableString == "true" - val movers = Extensions.getExtensions(StatementUpDownMover.STATEMENT_UP_DOWN_MOVER_EP) - val info = StatementUpDownMover.MoveInfo() - var actualMover: StatementUpDownMover? = null - for (mover in movers) { - if (mover.checkAvailable(LightPlatformCodeInsightTestCase.getEditor(), LightPlatformCodeInsightTestCase.getFile(), info, down)) { - actualMover = mover - break - } - } - - assertTrue("No mover found", actualMover != null) - assertEquals("Unmatched movers", moverClass.name, actualMover!!.javaClass.name) - assertEquals("Invalid applicability", isApplicableExpected, info.toMove2 != null) + isApplicableChecker(isApplicableExpected, direction) if (isApplicableExpected) { - invokeAndCheck(fileText, path, down) + invokeAndCheck(fileText, path, action) } } - private fun invokeAndCheck(fileText: String, path: String, down: Boolean) { + private fun invokeAndCheck(fileText: String, path: String, action: EditorAction) { val codeStyleSettings = FormatSettingsUtil.getSettings() val configurator = FormatSettingsUtil.createConfigurator(fileText, codeStyleSettings) configurator.configureSettings() try { ApplicationManager.getApplication().runWriteAction { - val action = if (down) MoveStatementDownAction() else MoveStatementUpAction() action.actionPerformed(LightPlatformCodeInsightTestCase.getEditor(), LightPlatformCodeInsightTestCase.getCurrentEditorDataContext()) } diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/CodeMoverTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java similarity index 98% rename from idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/CodeMoverTestGenerated.java rename to idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java index b8eefe1fb60..46cd0ad3dd5 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/CodeMoverTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java @@ -28,11 +28,11 @@ import java.util.regex.Pattern; /** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ @SuppressWarnings("all") @RunWith(JUnit3RunnerWithInners.class) -public class CodeMoverTestGenerated extends AbstractCodeMoverTest { +public class MoveStatementTestGenerated extends AbstractMoveStatementTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class ClassBodyDeclarations extends AbstractCodeMoverTest { + public static class ClassBodyDeclarations extends AbstractMoveStatementTest { public void testAllFilesPresentInClassBodyDeclarations() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -40,7 +40,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/accessors") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Accessors extends AbstractCodeMoverTest { + public static class Accessors extends AbstractMoveStatementTest { @TestMetadata("accessor1.kt") public void testAccessor1() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/accessors/accessor1.kt"); @@ -73,7 +73,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Class extends AbstractCodeMoverTest { + public static class Class extends AbstractMoveStatementTest { public void testAllFilesPresentInClass() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -202,7 +202,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/classInitializer") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class ClassInitializer extends AbstractCodeMoverTest { + public static class ClassInitializer extends AbstractMoveStatementTest { public void testAllFilesPresentInClassInitializer() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/classInitializer"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -283,7 +283,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/enums") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Enums extends AbstractCodeMoverTest { + public static class Enums extends AbstractMoveStatementTest { public void testAllFilesPresentInEnums() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/enums"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -340,7 +340,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Function extends AbstractCodeMoverTest { + public static class Function extends AbstractMoveStatementTest { public void testAllFilesPresentInFunction() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -445,7 +445,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/functionAnchors") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class FunctionAnchors extends AbstractCodeMoverTest { + public static class FunctionAnchors extends AbstractMoveStatementTest { public void testAllFilesPresentInFunctionAnchors() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/functionAnchors"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -490,7 +490,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/property") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Property extends AbstractCodeMoverTest { + public static class Property extends AbstractMoveStatementTest { public void testAllFilesPresentInProperty() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/property"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -595,7 +595,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/propertyAnchors") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class PropertyAnchors extends AbstractCodeMoverTest { + public static class PropertyAnchors extends AbstractMoveStatementTest { public void testAllFilesPresentInPropertyAnchors() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/propertyAnchors"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -623,7 +623,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/closingBraces") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class ClosingBraces extends AbstractCodeMoverTest { + public static class ClosingBraces extends AbstractMoveStatementTest { public void testAllFilesPresentInClosingBraces() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/closingBraces"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -631,7 +631,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/closingBraces/for") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class For extends AbstractCodeMoverTest { + public static class For extends AbstractMoveStatementTest { public void testAllFilesPresentInFor() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/closingBraces/for"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -652,7 +652,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/closingBraces/function") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Function extends AbstractCodeMoverTest { + public static class Function extends AbstractMoveStatementTest { public void testAllFilesPresentInFunction() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/closingBraces/function"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -685,7 +685,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/closingBraces/if") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class If extends AbstractCodeMoverTest { + public static class If extends AbstractMoveStatementTest { public void testAllFilesPresentInIf() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/closingBraces/if"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -718,7 +718,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/closingBraces/nested") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Nested extends AbstractCodeMoverTest { + public static class Nested extends AbstractMoveStatementTest { public void testAllFilesPresentInNested() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/closingBraces/nested"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -739,7 +739,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/closingBraces/when") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class When extends AbstractCodeMoverTest { + public static class When extends AbstractMoveStatementTest { public void testAllFilesPresentInWhen() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/closingBraces/when"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -784,7 +784,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/closingBraces/while") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class While extends AbstractCodeMoverTest { + public static class While extends AbstractMoveStatementTest { public void testAllFilesPresentInWhile() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/closingBraces/while"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -818,7 +818,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/expressions") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class Expressions extends AbstractCodeMoverTest { + public static class Expressions extends AbstractMoveStatementTest { public void testAllFilesPresentInExpressions() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/expressions"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -1229,7 +1229,7 @@ public class CodeMoverTestGenerated extends AbstractCodeMoverTest { @TestMetadata("idea/testData/codeInsight/moveUpDown/parametersAndArguments") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) - public static class ParametersAndArguments extends AbstractCodeMoverTest { + public static class ParametersAndArguments extends AbstractMoveStatementTest { public void testAllFilesPresentInParametersAndArguments() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/parametersAndArguments"), Pattern.compile("^(.+)\\.kt$"), true); }