Rename: Implement automatic renamer for test classes

#KT-8512 Fixed
This commit is contained in:
Alexey Sedunov
2016-05-27 20:30:43 +03:00
parent 42b71ae90c
commit 188531017a
13 changed files with 157 additions and 10 deletions
+1
View File
@@ -182,6 +182,7 @@
###### New features
- [`KT-7851`](https://youtrack.jetbrains.com/issue/KT-7851) Respect naming conventions in automatic variable rename
- [`KT-8044`](https://youtrack.jetbrains.com/issue/KT-8044), [`KT-9432`](https://youtrack.jetbrains.com/issue/KT-9432) Support @JvmName annotation in rename refactoring
- [`KT-8512`](https://youtrack.jetbrains.com/issue/KT-8512) Support "Rename tests" options in Rename dialog
###### Issues fixed
- [`KT-8541`](https://youtrack.jetbrains.com/issue/KT-8541), [`KT-8786`](https://youtrack.jetbrains.com/issue/KT-8786) Do now show 'Rename overloads' options if target function has no overloads
@@ -189,8 +189,8 @@ public class ConfigLibraryUtil {
addLibrary(editor, module);
}
public static void configureLibrariesByDirective(@NotNull Module module, String rootPath, String fileText) {
for (String libraryInfo : InTextDirectivesUtils.findListWithPrefixes(fileText, "// CONFIGURE_LIBRARY: ")) {
public static void configureLibraries(@NotNull Module module, String rootPath, List<String> libraryInfos) {
for (String libraryInfo : libraryInfos) {
int i = libraryInfo.indexOf('@');
String libraryName = libraryInfo.substring(0, i);
String[] jarPaths = libraryInfo.substring(i + 1).split(";");
@@ -198,6 +198,29 @@ public class ConfigLibraryUtil {
}
}
public static void unconfigureLibrariesByName(@NotNull Module module, List<String> libraryNames) {
for (Iterator<String> iterator = libraryNames.iterator(); iterator.hasNext(); ) {
String libraryName = iterator.next();
if (removeLibrary(module, libraryName)) {
iterator.remove();
}
}
if (!libraryNames.isEmpty()) throw new AssertionError("Couldn't find the following libraries: " + libraryNames);
}
public static void unconfigureLibrariesByInfo(@NotNull Module module, List<String> libraryInfos) {
List<String> libraryNames = new ArrayList<String>();
for (String libraryInfo : libraryInfos) {
libraryNames.add(libraryInfo.substring(0, libraryInfo.indexOf('@')));
}
unconfigureLibrariesByName(module, libraryNames);
}
public static void configureLibrariesByDirective(@NotNull Module module, String rootPath, String fileText) {
configureLibraries(module, rootPath, InTextDirectivesUtils.findListWithPrefixes(fileText, "// CONFIGURE_LIBRARY: "));
}
public static void unconfigureLibrariesByDirective(@NotNull Module module, String fileText) {
List<String> libraryNames = new ArrayList<String>();
for (String libInfo : InTextDirectivesUtils.findListWithPrefixes(fileText, "// CONFIGURE_LIBRARY: ")) {
@@ -207,13 +230,6 @@ public class ConfigLibraryUtil {
libraryNames.add(libraryName);
}
for (Iterator<String> iterator = libraryNames.iterator(); iterator.hasNext(); ) {
String libraryName = iterator.next();
if (removeLibrary(module, libraryName)) {
iterator.remove();
}
}
if (!libraryNames.isEmpty()) throw new AssertionError("Couldn't find the following libraries: " + libraryNames);
unconfigureLibrariesByName(module, libraryNames);
}
}
+1
View File
@@ -434,6 +434,7 @@
<automaticRenamerFactory implementation="org.jetbrains.kotlin.idea.refactoring.rename.AutomaticVariableInJavaRenamerFactory"/>
<automaticRenamerFactory implementation="org.jetbrains.kotlin.idea.refactoring.rename.AutomaticInheritorRenamerFactory"/>
<automaticRenamerFactory implementation="org.jetbrains.kotlin.idea.refactoring.rename.AutomaticOverloadsRenamerFactory"/>
<automaticRenamerFactory implementation="org.jetbrains.kotlin.idea.refactoring.rename.KotlinAutomaticTestRenamerFactory"/>
<vetoRenameCondition implementation="org.jetbrains.kotlin.idea.refactoring.KotlinVetoRenameCondition"/>
<renameInputValidator implementation="org.jetbrains.kotlin.idea.refactoring.rename.KotlinDeclarationRenameInputValidator"/>
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2016 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.kotlin.idea.refactoring.rename
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.refactoring.rename.naming.AutomaticRenamer
import com.intellij.refactoring.rename.naming.AutomaticTestRenamerFactory
import com.intellij.usageView.UsageInfo
import org.jetbrains.kotlin.asJava.KtLightClass
import org.jetbrains.kotlin.asJava.toLightClass
import org.jetbrains.kotlin.psi.KtClassOrObject
class KotlinAutomaticTestRenamerFactory : AutomaticTestRenamerFactory() {
private fun getPsiClass(element: PsiElement): PsiClass? {
return when (element) {
is KtLightClass -> element
is KtClassOrObject -> element.toLightClass()
else -> null
}
}
override fun isApplicable(element: PsiElement): Boolean {
val psiClass = getPsiClass(element) ?: return false
return super.isApplicable(psiClass)
}
override fun createRenamer(element: PsiElement, newName: String?, usages: MutableCollection<UsageInfo>): AutomaticRenamer {
return super.createRenamer(getPsiClass(element)!!, newName, usages)
}
}
@@ -0,0 +1,10 @@
package test;
import org.junit.Test;
public class JBazBarNewFooTest {
@Test
void testFoo() {
}
}
@@ -0,0 +1,7 @@
package test;
import junit.framework.TestCase;
public class JFooBarNewBazTestCase extends TestCase {
}
@@ -0,0 +1,16 @@
package test
import junit.framework.TestCase
import org.junit.Test
class BarNew {
}
class FooBarNewBazTestCase : TestCase() {
}
class BazBarNewFooTest {
@Test fun testFoo() {}
}
@@ -0,0 +1,7 @@
{
"type": "MARKED_ELEMENT",
"mainFile": "test/test.kt",
"newName": "BarNew",
"withRuntime": "true",
"libraries": ["JUnit@lib/junit-4.12.jar"]
}
@@ -0,0 +1,10 @@
package test;
import org.junit.Test;
public class JBazBarFooTest {
@Test
void testFoo() {
}
}
@@ -0,0 +1,7 @@
package test;
import junit.framework.TestCase;
public class JFooBarBazTestCase extends TestCase {
}
@@ -0,0 +1,16 @@
package test
import junit.framework.TestCase
import org.junit.Test
class /*rename*/Bar {
}
class FooBarBazTestCase : TestCase() {
}
class BazBarFooTest {
@Test fun testFoo() {}
}
@@ -41,6 +41,7 @@ import com.intellij.refactoring.rename.RenameProcessor
import com.intellij.refactoring.rename.RenamePsiElementProcessor
import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory
import com.intellij.refactoring.util.CommonRefactoringUtil.RefactoringErrorHintException
import com.intellij.testFramework.PlatformTestUtil
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
@@ -99,6 +100,9 @@ abstract class AbstractRenameTest : KotlinMultiFileTestCase() {
ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk())
}
val libraryInfos = renameObject.getAsJsonArray("libraries")?.map { it.asString!! } ?: emptyList()
ConfigLibraryUtil.configureLibraries(myModule, PlatformTestUtil.getCommunityPath(), libraryInfos)
val fixtureClasses = renameObject.getAsJsonArray("fixtureClasses")?.map { it.asString } ?: emptyList()
try {
@@ -150,6 +154,7 @@ abstract class AbstractRenameTest : KotlinMultiFileTestCase() {
}
finally {
fixtureClasses.forEach { TestFixtureExtension.unloadFixture(it) }
ConfigLibraryUtil.unconfigureLibrariesByInfo(myModule, libraryInfos)
}
}
@@ -53,6 +53,12 @@ public class RenameTestGenerated extends AbstractRenameTest {
doTest(fileName);
}
@TestMetadata("automaticRenamerKotlinTestClass/automaticRenamerKotlinTestClass.test")
public void testAutomaticRenamerKotlinTestClass_AutomaticRenamerKotlinTestClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/automaticRenamerKotlinTestClass/automaticRenamerKotlinTestClass.test");
doTest(fileName);
}
@TestMetadata("automaticRenamerOverloads/package.test")
public void testAutomaticRenamerOverloads_Package() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/automaticRenamerOverloads/package.test");