Auto generate test for rename

This commit is contained in:
Nikolay Krasko
2013-11-21 22:38:54 +04:00
committed by Nikolay Krasko
parent 6935ad4437
commit 87fac21310
12 changed files with 487 additions and 229 deletions
@@ -32,6 +32,9 @@ import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.impl.PsiFileFactoryImpl;
import com.intellij.rt.execution.junit.FileComparisonFailure;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.util.Function;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import junit.framework.TestCase;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -548,14 +551,8 @@ public class JetTestUtils {
String rootPath = testClassMetadata.value();
File rootFile = new File(rootPath);
Set<String> filePaths = collectPathsMetadata(testCaseClass);
Set<String> filePaths = Sets.newHashSet();
for (Method method : testCaseClass.getDeclaredMethods()) {
TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
if (testMetadata != null) {
filePaths.add(testMetadata.value());
}
}
File[] files = testDataDir.listFiles();
if (files != null) {
for (File file : files) {
@@ -564,20 +561,70 @@ public class JetTestUtils {
assertTestClassPresentByMetadata(testCaseClass, generatorClassFqName, file);
}
}
else {
if (filenamePattern.matcher(file.getName()).matches()) {
String relativePath = FileUtil.getRelativePath(rootFile, file);
if (!filePaths.contains(relativePath)) {
Assert.fail("Test data file missing from the generated test class: " +
file +
pleaseReRunGenerator(generatorClassFqName));
}
}
else if (filenamePattern.matcher(file.getName()).matches()) {
assertFilePathPresent(file, rootFile, filePaths, generatorClassFqName);
}
}
}
}
public static void assertAllTestsPresentInSingleGeneratedClass(
@NotNull Class<?> testCaseClass,
@NotNull final String generatorClassFqName,
@NotNull File testDataDir,
@NotNull final Pattern filenamePattern) {
TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
String rootPath = testClassMetadata.value();
final File rootFile = new File(rootPath);
final Set<String> filePaths = collectPathsMetadata(testCaseClass);
FileUtil.processFilesRecursively(testDataDir, new Processor<File>() {
@Override
public boolean process(File file) {
if (file.isFile() && filenamePattern.matcher(file.getName()).matches()) {
assertFilePathPresent(file, rootFile, filePaths, generatorClassFqName);
}
return true;
}
});
}
private static void assertFilePathPresent(File file, File rootFile, Set<String> filePaths, String generatorClassFqName) {
String path = FileUtil.getRelativePath(rootFile, file);
if (path != null) {
String relativePath = FileUtil.nameToCompare(path);
if (!filePaths.contains(relativePath)) {
Assert.fail("Test data file missing from the generated test class: " +
file +
pleaseReRunGenerator(generatorClassFqName));
}
}
}
private static Set<String> collectPathsMetadata(Class<?> testCaseClass) {
return ContainerUtil.newHashSet(
ContainerUtil.map(collectMethodsMetadata(testCaseClass), new Function<String, String>() {
@Override
public String fun(String pathData) {
return FileUtil.nameToCompare(pathData);
}
}));
}
private static Set<String> collectMethodsMetadata(Class<?> testCaseClass) {
Set<String> filePaths = Sets.newHashSet();
for (Method method : testCaseClass.getDeclaredMethods()) {
TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
if (testMetadata != null) {
filePaths.add(testMetadata.value());
}
}
return filePaths;
}
private static boolean containsTestData(File dir, Pattern filenamePattern) {
File[] files = dir.listFiles();
assert files != null;
@@ -35,6 +35,7 @@ import org.jetbrains.jet.editor.quickDoc.AbstractJetQuickDocProviderTest;
import org.jetbrains.jet.findUsages.AbstractJetFindUsagesTest;
import org.jetbrains.jet.formatter.AbstractJetFormatterTest;
import org.jetbrains.jet.generators.tests.generator.SimpleTestClassModel;
import org.jetbrains.jet.generators.tests.generator.SingleClassTestModel;
import org.jetbrains.jet.generators.tests.generator.TestClassModel;
import org.jetbrains.jet.generators.tests.generator.TestGenerator;
import org.jetbrains.jet.jvm.compiler.*;
@@ -57,6 +58,7 @@ import org.jetbrains.jet.plugin.navigation.JetAbstractGotoSuperTest;
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixMultiFileTest;
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest;
import org.jetbrains.jet.plugin.refactoring.inline.AbstractInlineTest;
import org.jetbrains.jet.plugin.refactoring.rename.AbstractRenameTest;
import org.jetbrains.jet.psi.AbstractJetPsiMatcherTest;
import org.jetbrains.jet.resolve.AbstractResolveBaseTest;
import org.jetbrains.jet.resolve.AbstractResolveTest;
@@ -540,6 +542,13 @@ public class GenerateTests {
AbstractDataFlowValueRenderingTest.class,
testModel("idea/testData/dataFlowValueRendering")
);
generateTest(
"idea/tests/",
"RenameTestGenerated",
AbstractRenameTest.class,
new SingleClassTestModel(new File("idea/testData/refactoring/rename"), Pattern.compile("^(.+)\\.test$"), "doTest")
);
}
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
@@ -0,0 +1,120 @@
/*
* 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.generators.tests.generator;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.utils.Printer;
import org.junit.Assert;
import java.io.File;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Pattern;
public class SingleClassTestModel implements TestClassModel {
private final File rootFile;
private final Pattern filenamePattern;
private final String doTestMethodName;
private final String testClassName;
private Collection<TestMethodModel> testMethods;
public SingleClassTestModel(@NotNull File rootFile, @NotNull Pattern filenamePattern, @NotNull String doTestMethodName) {
this.rootFile = rootFile;
this.filenamePattern = filenamePattern;
this.doTestMethodName = doTestMethodName;
this.testClassName = StringUtil.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(rootFile.getName()));
}
@NotNull
@Override
public final Collection<TestClassModel> getInnerTestClasses() {
return Collections.emptyList();
}
@NotNull
@Override
public Collection<TestMethodModel> getTestMethods() {
if (testMethods == null) {
final List<TestMethodModel> result = Lists.newArrayList();
result.add(new TestAllFilesPresentMethodModel());
FileUtil.processFilesRecursively(rootFile, new Processor<File>() {
@Override
public boolean process(File file) {
if (!file.isDirectory() && filenamePattern.matcher(file.getName()).matches()) {
result.addAll(getTestMethodsFromFile(file));
}
return true;
}
});
testMethods = result;
}
return testMethods;
}
protected Collection<TestMethodModel> getTestMethodsFromFile(File file) {
return Collections.<TestMethodModel>singletonList(new SimpleTestMethodModel(rootFile, file, doTestMethodName, filenamePattern));
}
@Override
public boolean isEmpty() {
// There's always one test for checking if all tests are present
return getTestMethods().size() <= 1;
}
@Override
public String getDataString() {
return JetTestUtils.getFilePath(rootFile);
}
@Override
public String getName() {
return testClassName;
}
private class TestAllFilesPresentMethodModel implements TestMethodModel {
@Override
public String getName() {
return "testAllFilesPresentIn" + testClassName;
}
@Override
public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) {
String assertTestsPresentStr = String.format(
"JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), \"%s\", new File(\"%s\"), Pattern.compile(\"%s\"));",
generatorClassFqName, JetTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()));
p.println(assertTestsPresentStr);
}
@Override
public String getDataString() {
return null;
}
}
}
@@ -0,0 +1 @@
// RENAME: JAVA_CLASS->testing.SomeClass->NewName
@@ -0,0 +1 @@
// RENAME: JAVA_CLASS->testing.SomeClass->NewName
@@ -0,0 +1 @@
// RENAME: KOTLIN_CLASS->testing.rename.First->Third
@@ -0,0 +1 @@
// RENAME: KOTLIN_CLASS->X->TestX
@@ -0,0 +1 @@
// RENAME: KOTLIN_FUNCTION->testing.rename.C.first->second
@@ -0,0 +1,229 @@
/*
* 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.refactoring.rename;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.refactoring.MultiFileTestCase;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.refactoring.rename.RenamePsiElementProcessor;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.util.Function;
import junit.framework.Assert;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.jet.InTextDirectivesUtils;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
import org.jetbrains.jet.utils.ExceptionUtils;
import java.io.File;
import java.util.Collections;
public abstract class AbstractRenameTest extends MultiFileTestCase {
private enum RenameType {
JAVA_CLASS,
JAVA_METHOD,
KOTLIN_CLASS,
KOTLIN_FUNCTION,
KOTLIN_PROPERTY,
}
public void doTest(String path) {
try {
String fileText = FileUtil.loadFile(new File(path));
String renameDirective = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// RENAME:");
Assert.assertNotNull("'// RENAME:' directive is expected for rename test file", renameDirective);
String[] strings = renameDirective.split("->");
Assert.assertTrue("'// RENAME:' directive should have at least AbstractRenameTest.RenameType parameter", strings.length > 0);
String renameTypeStr = strings[0];
RenameType type = RenameType.valueOf(renameTypeStr);
FqNameUnsafe fqNameUnsafe = new FqNameUnsafe(strings[1]);
switch (type) {
case JAVA_CLASS:
renameJavaClassTest(fqNameUnsafe.asString(), strings[2]);
break;
case JAVA_METHOD:
renameJavaMethodTest(fqNameUnsafe.asString(), strings[2], strings[3]);
break;
case KOTLIN_CLASS:
renameKotlinClassTest(fqNameUnsafe.toSafe(), strings[2]);
break;
case KOTLIN_FUNCTION:
renameKotlinFunctionTest(fqNameUnsafe.parent().toSafe(),fqNameUnsafe.shortName().asString(), strings[2]);
break;
case KOTLIN_PROPERTY:
renameKotlinPropertyTest(fqNameUnsafe.parent().toSafe(), fqNameUnsafe.shortName().asString(), strings[2]);
break;
}
}
catch (Exception e) {
throw ExceptionUtils.rethrow(e);
}
}
private void renameJavaClassTest(@NonNls final String qClassName, @NonNls final String newName) throws Exception {
doTest(new MultiFileTestCase.PerformAction() {
@Override
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
PsiClass aClass = myJavaFacade.findClass(qClassName, GlobalSearchScope.allScope(getProject()));
assertNotNull("Class " + qClassName + " not found", aClass);
PsiElement substitution = RenamePsiElementProcessor.forElement(aClass).substituteElementToRename(aClass, null);
new RenameProcessor(myProject, substitution, newName, true, true).run();
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
FileDocumentManager.getInstance().saveAllDocuments();
}
});
}
private void renameJavaMethodTest(final String className, final String methodSignature, final String newName) throws Exception {
doTest(new MultiFileTestCase.PerformAction() {
@Override
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
JavaPsiFacade manager = getJavaFacade();
PsiClass aClass = manager.findClass(className, GlobalSearchScope.moduleScope(myModule));
assertNotNull("Class " + className + " not found", aClass);
PsiMethod methodBySignature = aClass.findMethodBySignature(manager.getElementFactory().createMethodFromText(methodSignature + "{}", null), false);
assertNotNull("Method with signature '" + methodSignature + "' wasn't found in class " + className, methodBySignature);
new RenameProcessor(myProject, methodBySignature, newName, false, false).run();
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
FileDocumentManager.getInstance().saveAllDocuments();
}
});
}
private void renameKotlinFunctionTest(final FqName qClassName, final String oldMethodName, String newMethodName) throws Exception {
doTestWithKotlinRename(new Function<PsiFile, PsiElement>() {
@Override
public PsiElement fun(PsiFile file) {
BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) file)
.getBindingContext();
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName);
assertNotNull(classDescriptor);
JetScope scope = classDescriptor.getMemberScope(Collections.<TypeProjection>emptyList());
FunctionDescriptor methodDescriptor = scope.getFunctions(Name.identifier(oldMethodName)).iterator().next();
return BindingContextUtils.callableDescriptorToDeclaration(bindingContext, methodDescriptor);
}
}, newMethodName);
}
private void renameKotlinPropertyTest(final FqName qClassName, final String oldPropertyName, String newPropertyName) throws Exception {
doTestWithKotlinRename(new Function<PsiFile, PsiElement>() {
@Override
public PsiElement fun(PsiFile file) {
BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) file).getBindingContext();
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName);
assertNotNull(classDescriptor);
JetScope scope = classDescriptor.getMemberScope(Collections.<TypeProjection>emptyList());
VariableDescriptor propertyName = scope.getProperties(Name.identifier(oldPropertyName)).iterator().next();
return BindingContextUtils.descriptorToDeclaration(bindingContext, propertyName);
}
}, newPropertyName);
}
private void renameKotlinClassTest(@NonNls final FqName qClassName, @NonNls String newName) throws Exception {
doTestWithKotlinRename(new Function<PsiFile, PsiElement>() {
@Override
public PsiElement fun(PsiFile file) {
BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) file)
.getBindingContext();
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName);
assertNotNull(classDescriptor);
return BindingContextUtils.classDescriptorToDeclaration(bindingContext, classDescriptor);
}
}, newName);
}
private void doTestWithKotlinRename(@NonNls final Function<PsiFile, PsiElement> elementToRenameCallback, final @NonNls String newName) throws Exception {
doTest(new MultiFileTestCase.PerformAction() {
@Override
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
VirtualFile child = rootDir.findChild(getTestDirName() + ".kt");
assertNotNull(child);
Document document = FileDocumentManager.getInstance().getDocument(child);
assertNotNull(document);
PsiFile file = PsiDocumentManager.getInstance(getProject()).getPsiFile(document);
assertTrue(file instanceof JetFile);
PsiElement psiElement = elementToRenameCallback.fun(file);
assertNotNull(psiElement);
PsiElement substitution = RenamePsiElementProcessor.forElement(psiElement).substituteElementToRename(psiElement, null);
assert substitution != null;
new RenameProcessor(myProject, substitution, newName, true, true).run();
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
FileDocumentManager.getInstance().saveAllDocuments();
VirtualFileManager.getInstance().syncRefresh();
}
});
}
protected String getTestDirName() {
String testName = getTestName(true);
return testName.substring(0, testName.indexOf('_'));
}
@Override
protected void doTest(PerformAction performAction) throws Exception {
super.doTest(performAction, getTestDirName());
}
@Override
protected String getTestRoot() {
return "/refactoring/rename/";
}
@Override
protected String getTestDataPath() {
return PluginTestCaseBase.getTestDataPathBase();
}
}
@@ -1,76 +0,0 @@
/*
* 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.refactoring.rename;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.refactoring.MultiFileTestCase;
import com.intellij.refactoring.rename.RenameProcessor;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.testing.LocalFileSystemUtils;
public class RenameInJavaTest extends MultiFileTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
LocalFileSystemUtils.refreshPath(getTestDataPath() + getTestRoot());
}
public void testRenameJavaClass() throws Exception {
doTest("testing.SomeClass", "NewName");
}
public void todotestRenameJavaClassSamePackage() throws Exception {
doTest("testing.SomeClass", "NewName");
}
private void doTest(@NonNls final String qClassName, @NonNls final String newName) throws Exception {
doTest(new PerformAction() {
@Override
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
RenameInJavaTest.this.performAction(qClassName, newName);
}
});
}
private void performAction(String qClassName, String newName) throws Exception {
PsiClass aClass = myJavaFacade.findClass(qClassName, GlobalSearchScope.allScope(getProject()));
assertNotNull("Class " + qClassName + " not found", aClass);
new RenameProcessor(myProject, aClass, newName, true, true).run();
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
FileDocumentManager.getInstance().saveAllDocuments();
}
@Override
protected String getTestRoot() {
return "/refactoring/rename/";
}
@Override
protected String getTestDataPath() {
return PluginTestCaseBase.getTestDataPathBase();
}
}
@@ -1,137 +0,0 @@
/*
* 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.refactoring.rename;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.refactoring.MultiFileTestCase;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.util.Function;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
import org.jetbrains.jet.testing.LocalFileSystemUtils;
import java.io.File;
import java.util.Collections;
public class RenameInKotlinTest extends MultiFileTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
LocalFileSystemUtils.refreshPath(getTestDataPath() + getTestRoot());
LocalFileSystemUtils.refreshPath(getTestDataPath() + getTestRoot() + getTestName(true) + File.separator + "before");
LocalFileSystemUtils.refreshPath(getTestDataPath() + getTestRoot() + getTestName(true) + File.separator + "after");
}
public void testRenameKotlinClass() throws Exception {
doTestWithRenameClass(new FqName("testing.rename.First"), "Third");
}
public void testRenameKotlinMethod() throws Exception {
doTestWithRenameMethod(new FqName("testing.rename.C"), "first", "second");
}
public void testRenameKotlinClassConstructor() throws Exception {
doTestWithRenameClass(new FqName("X"), "TestX");
}
private void doTestWithRenameMethod(final FqName qClassName, final String oldMethodName, String newMethodName) throws Exception {
doTestWithRename(new Function<PsiFile, PsiElement>() {
@Override
public PsiElement fun(PsiFile file) {
BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) file)
.getBindingContext();
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName);
assertNotNull(classDescriptor);
JetScope scope = classDescriptor.getMemberScope(Collections.<TypeProjection>emptyList());
FunctionDescriptor methodDescriptor = scope.getFunctions(Name.identifier(oldMethodName)).iterator().next();
return BindingContextUtils.callableDescriptorToDeclaration(bindingContext, methodDescriptor);
}
}, newMethodName);
}
private void doTestWithRenameClass(@NonNls final FqName qClassName, @NonNls String newName) throws Exception {
doTestWithRename(new Function<PsiFile, PsiElement>() {
@Override
public PsiElement fun(PsiFile file) {
BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) file)
.getBindingContext();
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName);
assertNotNull(classDescriptor);
return BindingContextUtils.classDescriptorToDeclaration(bindingContext, classDescriptor);
}
}, newName);
}
private void doTestWithRename(@NonNls final Function<PsiFile, PsiElement> elementToRenameCallback, @NonNls final String newName) throws Exception {
doTest(new PerformAction() {
@Override
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
VirtualFile child = rootDir.findChild(getTestName(false) + ".kt");
if (child == null) {
return;
}
Document document = FileDocumentManager.getInstance().getDocument(child);
if (document == null) {
return;
}
PsiFile file = PsiDocumentManager.getInstance(getProject()).getPsiFile(document);
if (!(file instanceof JetFile)) {
return;
}
PsiElement psiElement = elementToRenameCallback.fun(file);
assertNotNull(psiElement);
new RenameProcessor(myProject, psiElement, newName, true, true).run();
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
FileDocumentManager.getInstance().saveAllDocuments();
VirtualFileManager.getInstance().syncRefresh();
}
});
}
@Override
protected String getTestRoot() {
return "/refactoring/rename/";
}
@Override
protected String getTestDataPath() {
return PluginTestCaseBase.getTestDataPathBase();
}
}
@@ -0,0 +1,61 @@
/*
* 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.refactoring.rename;
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.rename.AbstractRenameTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("idea/testData/refactoring/rename")
public class RenameTestGenerated extends AbstractRenameTest {
public void testAllFilesPresentInRename() throws Exception {
JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests",
new File("idea/testData/refactoring/rename"),
Pattern.compile("^(.+)\\.test$"));
}
@TestMetadata("renameJavaClass/renameJavaClass.test")
public void testRenameJavaClass_RenameJavaClass() throws Exception {
doTest("idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test");
}
@TestMetadata("renameKotlinClass/kotlinClass.test")
public void testRenameKotlinClass_KotlinClass() throws Exception {
doTest("idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test");
}
@TestMetadata("renameKotlinClassConstructor/renameKotlinConstructor.test")
public void testRenameKotlinClassConstructor_RenameKotlinConstructor() throws Exception {
doTest("idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test");
}
@TestMetadata("renameKotlinMethod/renameKotlinMethod.test")
public void testRenameKotlinMethod_RenameKotlinMethod() throws Exception {
doTest("idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test");
}
}