From 87fac213101d7920c00dac856cc10586616dd9af Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 21 Nov 2013 22:38:54 +0400 Subject: [PATCH] Auto generate test for rename --- .../tests/org/jetbrains/jet/JetTestUtils.java | 79 ++++-- .../jet/generators/tests/GenerateTests.java | 9 + .../tests/generator/SingleClassTestModel.java | 120 +++++++++ .../renameJavaClass/renameJavaClass.test | 1 + .../renameJavaClassSamePackage.testtodo | 1 + .../rename/renameKotlinClass/kotlinClass.test | 1 + .../renameKotlinConstructor.test | 1 + .../renameKotlinMethod.test | 1 + .../rename/AbstractRenameTest.java | 229 ++++++++++++++++++ .../refactoring/rename/RenameInJavaTest.java | 76 ------ .../rename/RenameInKotlinTest.java | 137 ----------- .../rename/RenameTestGenerated.java | 61 +++++ 12 files changed, 487 insertions(+), 229 deletions(-) create mode 100644 generators/src/org/jetbrains/jet/generators/tests/generator/SingleClassTestModel.java create mode 100644 idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test create mode 100644 idea/testData/refactoring/rename/renameJavaClassSamePackage/renameJavaClassSamePackage.testtodo create mode 100644 idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test create mode 100644 idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test create mode 100644 idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test create mode 100644 idea/tests/org/jetbrains/jet/plugin/refactoring/rename/AbstractRenameTest.java delete mode 100644 idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInJavaTest.java delete mode 100644 idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInKotlinTest.java create mode 100644 idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameTestGenerated.java diff --git a/compiler/tests/org/jetbrains/jet/JetTestUtils.java b/compiler/tests/org/jetbrains/jet/JetTestUtils.java index 53ea7177326..0e597d6bfb8 100644 --- a/compiler/tests/org/jetbrains/jet/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/jet/JetTestUtils.java @@ -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 filePaths = collectPathsMetadata(testCaseClass); - Set 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 filePaths = collectPathsMetadata(testCaseClass); + + FileUtil.processFilesRecursively(testDataDir, new Processor() { + @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 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 collectPathsMetadata(Class testCaseClass) { + return ContainerUtil.newHashSet( + ContainerUtil.map(collectMethodsMetadata(testCaseClass), new Function() { + @Override + public String fun(String pathData) { + return FileUtil.nameToCompare(pathData); + } + })); + } + + private static Set collectMethodsMetadata(Class testCaseClass) { + Set 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; diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java index 53cc7cdb03e..a19a92f03ab 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -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) { diff --git a/generators/src/org/jetbrains/jet/generators/tests/generator/SingleClassTestModel.java b/generators/src/org/jetbrains/jet/generators/tests/generator/SingleClassTestModel.java new file mode 100644 index 00000000000..e8032dc14de --- /dev/null +++ b/generators/src/org/jetbrains/jet/generators/tests/generator/SingleClassTestModel.java @@ -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 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 getInnerTestClasses() { + return Collections.emptyList(); + } + + @NotNull + @Override + public Collection getTestMethods() { + if (testMethods == null) { + final List result = Lists.newArrayList(); + + result.add(new TestAllFilesPresentMethodModel()); + + FileUtil.processFilesRecursively(rootFile, new Processor() { + @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 getTestMethodsFromFile(File file) { + return Collections.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; + } + } +} diff --git a/idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test b/idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test new file mode 100644 index 00000000000..2edc8b81ae1 --- /dev/null +++ b/idea/testData/refactoring/rename/renameJavaClass/renameJavaClass.test @@ -0,0 +1 @@ +// RENAME: JAVA_CLASS->testing.SomeClass->NewName \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameJavaClassSamePackage/renameJavaClassSamePackage.testtodo b/idea/testData/refactoring/rename/renameJavaClassSamePackage/renameJavaClassSamePackage.testtodo new file mode 100644 index 00000000000..e90ab6ce65c --- /dev/null +++ b/idea/testData/refactoring/rename/renameJavaClassSamePackage/renameJavaClassSamePackage.testtodo @@ -0,0 +1 @@ +// RENAME: JAVA_CLASS->testing.SomeClass->NewName diff --git a/idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test b/idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test new file mode 100644 index 00000000000..e42e956643c --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinClass/kotlinClass.test @@ -0,0 +1 @@ +// RENAME: KOTLIN_CLASS->testing.rename.First->Third \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test b/idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test new file mode 100644 index 00000000000..6d6d8d73f18 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinClassConstructor/renameKotlinConstructor.test @@ -0,0 +1 @@ +// RENAME: KOTLIN_CLASS->X->TestX \ No newline at end of file diff --git a/idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test b/idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test new file mode 100644 index 00000000000..f3d1379d9a0 --- /dev/null +++ b/idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test @@ -0,0 +1 @@ +// RENAME: KOTLIN_FUNCTION->testing.rename.C.first->second \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/AbstractRenameTest.java b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/AbstractRenameTest.java new file mode 100644 index 00000000000..47d11dd5279 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/AbstractRenameTest.java @@ -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() { + @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.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() { + @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.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() { + @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 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(); + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInJavaTest.java b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInJavaTest.java deleted file mode 100644 index 94a8f798df7..00000000000 --- a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInJavaTest.java +++ /dev/null @@ -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(); - } -} diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInKotlinTest.java b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInKotlinTest.java deleted file mode 100644 index 47b8933a465..00000000000 --- a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInKotlinTest.java +++ /dev/null @@ -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() { - @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.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() { - @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 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(); - } -} diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameTestGenerated.java new file mode 100644 index 00000000000..abef1646c1e --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameTestGenerated.java @@ -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"); + } + +}