From d7aa8e7fc8f0c421edd72c7ac581397973a1155f Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 24 Feb 2014 18:18:07 +0400 Subject: [PATCH] Testing framework for Move refactoring --- .../com/intellij/refactoring/annotations.xml | 4 + .../jet/generators/tests/GenerateTests.kt | 5 + .../refactoring/move/AbstractJetMoveTest.kt | 129 ++++++++++++++++++ .../move/JetMoveTestGenerated.java | 39 ++++++ 4 files changed, 177 insertions(+) create mode 100644 idea/tests/org/jetbrains/jet/plugin/refactoring/move/AbstractJetMoveTest.kt create mode 100644 idea/tests/org/jetbrains/jet/plugin/refactoring/move/JetMoveTestGenerated.java diff --git a/annotations/com/intellij/refactoring/annotations.xml b/annotations/com/intellij/refactoring/annotations.xml index 46eab669011..e2d931e952d 100644 --- a/annotations/com/intellij/refactoring/annotations.xml +++ b/annotations/com/intellij/refactoring/annotations.xml @@ -1,4 +1,8 @@ + + + diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index ecd08f80e8d..45c5a11a75d 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -96,6 +96,7 @@ 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 +import org.jetbrains.jet.plugin.refactoring.move.AbstractJetMoveTest fun main(args: Array) { System.setProperty("java.awt.headless", "true") @@ -428,6 +429,10 @@ fun main(args: Array) { model("findUsages/java", pattern = """^(.+)\.0\.java$""") } + testClass(javaClass()) { + model("refactoring/move", extension = "test", singleClass = true) + } + testClass(javaClass()) { model("completion/weighers", pattern = """^([^\.]+)\.kt$""") } diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/move/AbstractJetMoveTest.kt b/idea/tests/org/jetbrains/jet/plugin/refactoring/move/AbstractJetMoveTest.kt new file mode 100644 index 00000000000..28004accc2a --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/refactoring/move/AbstractJetMoveTest.kt @@ -0,0 +1,129 @@ +/* + * 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.refactoring.move + +import com.intellij.openapi.util.io.FileUtil +import com.intellij.psi.PsiElement +import org.jetbrains.jet.plugin.PluginTestCaseBase +import java.io.File +import com.intellij.refactoring.BaseRefactoringProcessor.ConflictsInTestsException +import com.google.gson.JsonObject +import com.intellij.refactoring.MultiFileTestCase +import com.intellij.openapi.vfs.VirtualFile +import com.google.gson.JsonParser +import com.intellij.codeInsight.TargetElementUtilBase +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.editor.EditorFactory +import com.intellij.openapi.editor.Document +import com.intellij.openapi.application.ApplicationManager +import com.intellij.psi.PsiDocumentManager +import com.intellij.openapi.util.Computable +import org.jetbrains.jet.JetTestUtils +import com.intellij.psi.PsiFile +import com.intellij.psi.PsiManager + +public abstract class AbstractJetMoveTest : MultiFileTestCase() { + protected fun doTest(path: String) { + fun extractCaretOffset(doc: Document): Int { + return ApplicationManager.getApplication()!!.runWriteAction( + Computable { + val text = StringBuilder(doc.getText()) + val offset = text.indexOf("") + + if (offset >= 0) { + text.delete(offset, offset + "".length) + doc.setText(text.toString()) + } + + offset + } + )!! + } + + val config = JsonParser().parse(FileUtil.loadFile(File(path))) as JsonObject + + val action = MoveAction.valueOf(config.getString("type")) + + val testDir = path.substring(0, path.lastIndexOf("/")) + val mainFilePath = config.getNullableString("mainFile")!! + + val conflictFile = File(testDir + "/conflicts.txt") + doTest { rootDir, rootAfter -> + val mainFile = rootDir.findFileByRelativePath(mainFilePath)!! + val mainPsiFile = PsiManager.getInstance(getProject()!!).findFile(mainFile)!! + val document = FileDocumentManager.getInstance()!!.getDocument(mainFile)!! + val editor = EditorFactory.getInstance()!!.createEditor(document, getProject()!!)!! + + val caretOffset = extractCaretOffset(document) + val elementAtCaret = if (caretOffset >= 0) { + TargetElementUtilBase.getInstance()!!.findTargetElement( + editor, + TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED or TargetElementUtilBase.ELEMENT_NAME_ACCEPTED, + caretOffset + ) + } + else null + + try { + action.runRefactoring(mainPsiFile, elementAtCaret, config) + + assert(!conflictFile.exists()) + } + catch(e: ConflictsInTestsException) { + JetTestUtils.assertEqualsToFile(conflictFile, e.getMessages().sort().makeString("\n")) + } + finally { + PsiDocumentManager.getInstance(getProject()!!).commitAllDocuments() + FileDocumentManager.getInstance()?.saveAllDocuments() + + EditorFactory.getInstance()!!.releaseEditor(editor) + } + } + } + + protected fun getTestDirName(lowercaseFirstLetter : Boolean) : String { + val testName = getTestName(lowercaseFirstLetter) + return testName.substring(0, testName.lastIndexOf('_')).replace('_', '/') + } + + protected fun doTest(action : (VirtualFile, VirtualFile?) -> Unit) { + super.doTest(action, getTestDirName(true)) + } + + protected override fun getTestRoot() : String { + return "/refactoring/move/" + } + + protected override fun getTestDataPath() : String { + return PluginTestCaseBase.getTestDataPathBase() + } +} + +fun JsonObject.getString(name: String): String { + val member = getNullableString(name) + if (member == null) { + throw IllegalStateException("Member with name '$name' is expected in '$this'") + } + + return member +} + +fun JsonObject.getNullableString(name: String): String? = this[name]?.getAsString() + +enum class MoveAction { + abstract fun runRefactoring(mainFile: PsiFile, elementAtCaret: PsiElement?, config: JsonObject) +} diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/move/JetMoveTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/refactoring/move/JetMoveTestGenerated.java new file mode 100644 index 00000000000..9e3566cb38e --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/refactoring/move/JetMoveTestGenerated.java @@ -0,0 +1,39 @@ +/* + * 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.refactoring.move; + +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.refactoring.move.AbstractJetMoveTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/refactoring/move") +public class JetMoveTestGenerated extends AbstractJetMoveTest { + public void testAllFilesPresentInMove() throws Exception { + JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/refactoring/move"), Pattern.compile("^(.+)\\.test$")); + } + +}