diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index 412f6d1111b..1866fe39408 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -32,6 +32,7 @@ import org.jetbrains.jet.completion.AbstractJavaWithLibCompletionTest; import org.jetbrains.jet.completion.AbstractJetJSCompletionTest; import org.jetbrains.jet.completion.AbstractKeywordCompletionTest; import org.jetbrains.jet.editor.quickDoc.AbstractJetQuickDocProviderTest; +import org.jetbrains.jet.findUsages.AbstractJetFindUsagesTest; import org.jetbrains.jet.jvm.compiler.*; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveDescriptorRendererTest; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparingTest; @@ -430,6 +431,48 @@ public class GenerateTests { AbstractResolveBaseTest.class, testModel("idea/testData/resolve/references", "doTest") ); + + generateTest( + "idea/tests/", + "JetFindUsagesTest", + AbstractJetFindUsagesTest.class, + testModelWithFileName( + "idea/testData/findUsages/findClassUsages/javaClassUsages", "Server.kt", "testFindClassJavaUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findClassUsages/kotlinClassUsages1", "Server.kt", "testFindClassKotlinUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findClassUsages/kotlinClassUsages2", "Server.kt", "testFindClassKotlinUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findClassUsages/kotlinClassUsages3", "Server.kt", "testFindClassKotlinUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findObjectUsages/javaObjectUsages", "Server.kt", "testFindObjectJavaUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findObjectUsages/kotlinObjectUsages", "Server.kt", "testFindObjectKotlinUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findMethodUsages/javaMethodUsages", "Server.kt", "testFindMethodJavaUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findMethodUsages/kotlinMethodUsages", "Server.kt", "testFindMethodKotlinUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findPropertyUsages/javaPropertyUsages", "Server.kt", "testFindPropertyJavaUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages", "Server.kt", "testFindPropertyKotlinUsages" + ), + testModelWithFileName( + "idea/testData/findUsages/findWithFilteringImports", "Server.kt", "testFindWithFilteringImports" + ), + testModelWithFileName( + "idea/testData/findUsages/unresolvedAnnotation", "Server.kt", "testFindUsagesUnresolvedAnnotation" + ) + ); } private static SimpleTestClassModel testModel(@NotNull String rootPath) { @@ -448,6 +491,10 @@ public class GenerateTests { return new SimpleTestClassModel(new File(rootPath), false, Pattern.compile("^(.+)$"), methodName); } + private static SimpleTestClassModel testModelWithFileName(@NotNull String rootPath, @NotNull String fileName, @NotNull String methodName) { + return new SimpleTestClassModel(new File(rootPath), true, Pattern.compile("^(.*)" + fileName + "$"), methodName); + } + private static SimpleTestClassModel testModel( @NotNull String rootPath, boolean recursive, diff --git a/idea/testData/findUsages/findClassUsages/javaUsages/Client.java b/idea/testData/findUsages/findClassUsages/javaClassUsages/Client.java similarity index 100% rename from idea/testData/findUsages/findClassUsages/javaUsages/Client.java rename to idea/testData/findUsages/findClassUsages/javaClassUsages/Client.java diff --git a/idea/testData/findUsages/findClassUsages/javaUsages/Server.kt b/idea/testData/findUsages/findClassUsages/javaClassUsages/Server.kt similarity index 100% rename from idea/testData/findUsages/findClassUsages/javaUsages/Server.kt rename to idea/testData/findUsages/findClassUsages/javaClassUsages/Server.kt diff --git a/idea/testData/findUsages/findClassUsages/javaClassUsages/results.txt b/idea/testData/findUsages/findClassUsages/javaClassUsages/results.txt new file mode 100644 index 00000000000..a6a1b5a7518 --- /dev/null +++ b/idea/testData/findUsages/findClassUsages/javaClassUsages/results.txt @@ -0,0 +1 @@ +Field declaration (4: 13) private Server myServer; \ No newline at end of file diff --git a/idea/testData/findUsages/findClassUsages/kotlinUsages1/Client.kt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages1/Client.kt similarity index 100% rename from idea/testData/findUsages/findClassUsages/kotlinUsages1/Client.kt rename to idea/testData/findUsages/findClassUsages/kotlinClassUsages1/Client.kt diff --git a/idea/testData/findUsages/findClassUsages/kotlinUsages1/Server.kt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages1/Server.kt similarity index 100% rename from idea/testData/findUsages/findClassUsages/kotlinUsages1/Server.kt rename to idea/testData/findUsages/findClassUsages/kotlinClassUsages1/Server.kt diff --git a/idea/testData/findUsages/findClassUsages/kotlinClassUsages1/results.txt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages1/results.txt new file mode 100644 index 00000000000..ce29933b911 --- /dev/null +++ b/idea/testData/findUsages/findClassUsages/kotlinClassUsages1/results.txt @@ -0,0 +1,12 @@ +Class/object property type (6: 21) var nextServer: Server? = null +Function return types (14: 26) fun getNextServer(): Server? { +Function return types (32: 21) fun Any.asServer(): Server? { +Import directive (3: 15) import server.Server +Local variable type (10: 21) val server: Server = s +Nested class/object (7: 16) val name = Server.NAME +Parameter type (24: 19) fun Client.bar(s: Server) { +Parameter type (9: 16) fun foo(s: Server) { +Super type qualifier (19: 15) super.work() +Supertype (5: 15) class Client: Server { +Target type of 'as' operation (33: 40) return if (this is Server) this as Server else null +Target type of 'is' operation (33: 24) return if (this is Server) this as Server else null \ No newline at end of file diff --git a/idea/testData/findUsages/findClassUsages/kotlinUsages2/Client.kt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages2/Client.kt similarity index 100% rename from idea/testData/findUsages/findClassUsages/kotlinUsages2/Client.kt rename to idea/testData/findUsages/findClassUsages/kotlinClassUsages2/Client.kt diff --git a/idea/testData/findUsages/findClassUsages/kotlinUsages2/Server.kt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages2/Server.kt similarity index 100% rename from idea/testData/findUsages/findClassUsages/kotlinUsages2/Server.kt rename to idea/testData/findUsages/findClassUsages/kotlinClassUsages2/Server.kt diff --git a/idea/testData/findUsages/findClassUsages/kotlinClassUsages2/results.txt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages2/results.txt new file mode 100644 index 00000000000..d0e55620990 --- /dev/null +++ b/idea/testData/findUsages/findClassUsages/kotlinClassUsages2/results.txt @@ -0,0 +1,2 @@ +Annotation (5: 1) X("") +Import directive (3: 15) import server.X \ No newline at end of file diff --git a/idea/testData/findUsages/findClassUsages/kotlinUsages3/Client.kt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages3/Client.kt similarity index 100% rename from idea/testData/findUsages/findClassUsages/kotlinUsages3/Client.kt rename to idea/testData/findUsages/findClassUsages/kotlinClassUsages3/Client.kt diff --git a/idea/testData/findUsages/findClassUsages/kotlinUsages3/Server.kt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages3/Server.kt similarity index 100% rename from idea/testData/findUsages/findClassUsages/kotlinUsages3/Server.kt rename to idea/testData/findUsages/findClassUsages/kotlinClassUsages3/Server.kt diff --git a/idea/testData/findUsages/findClassUsages/kotlinClassUsages3/results.txt b/idea/testData/findUsages/findClassUsages/kotlinClassUsages3/results.txt new file mode 100644 index 00000000000..5a384bbe749 --- /dev/null +++ b/idea/testData/findUsages/findClassUsages/kotlinClassUsages3/results.txt @@ -0,0 +1,4 @@ +Import directive (3: 15) import server.Data +Type argument (9: 16) val c = Client() +Type constraint (5: 17) class Client where X: Data { +Type constraint (5: 35) class Client where X: Data { \ No newline at end of file diff --git a/idea/testData/findUsages/findMethodUsages/javaUsages/Client.java b/idea/testData/findUsages/findMethodUsages/javaMethodUsages/Client.java similarity index 100% rename from idea/testData/findUsages/findMethodUsages/javaUsages/Client.java rename to idea/testData/findUsages/findMethodUsages/javaMethodUsages/Client.java diff --git a/idea/testData/findUsages/findMethodUsages/javaUsages/Server.kt b/idea/testData/findUsages/findMethodUsages/javaMethodUsages/Server.kt similarity index 100% rename from idea/testData/findUsages/findMethodUsages/javaUsages/Server.kt rename to idea/testData/findUsages/findMethodUsages/javaMethodUsages/Server.kt diff --git a/idea/testData/findUsages/findMethodUsages/javaMethodUsages/results.txt b/idea/testData/findUsages/findMethodUsages/javaMethodUsages/results.txt new file mode 100644 index 00000000000..9e5e5aa96f5 --- /dev/null +++ b/idea/testData/findUsages/findMethodUsages/javaMethodUsages/results.txt @@ -0,0 +1 @@ +Unclassified usage (6: 16) server.processRequest(); \ No newline at end of file diff --git a/idea/testData/findUsages/findMethodUsages/kotlinUsages/Client.kt b/idea/testData/findUsages/findMethodUsages/kotlinMethodUsages/Client.kt similarity index 100% rename from idea/testData/findUsages/findMethodUsages/kotlinUsages/Client.kt rename to idea/testData/findUsages/findMethodUsages/kotlinMethodUsages/Client.kt diff --git a/idea/testData/findUsages/findMethodUsages/kotlinUsages/Server.kt b/idea/testData/findUsages/findMethodUsages/kotlinMethodUsages/Server.kt similarity index 100% rename from idea/testData/findUsages/findMethodUsages/kotlinUsages/Server.kt rename to idea/testData/findUsages/findMethodUsages/kotlinMethodUsages/Server.kt diff --git a/idea/testData/findUsages/findMethodUsages/kotlinMethodUsages/results.txt b/idea/testData/findUsages/findMethodUsages/kotlinMethodUsages/results.txt new file mode 100644 index 00000000000..6af85d2954b --- /dev/null +++ b/idea/testData/findUsages/findMethodUsages/kotlinMethodUsages/results.txt @@ -0,0 +1,3 @@ +Callable reference (6: 23) val methodRef = ::processRequest() +Function call (10: 9) processRequest() +Import directive (3: 15) import server.processRequest \ No newline at end of file diff --git a/idea/testData/findUsages/findObjectUsages/javaUsages/Client.java b/idea/testData/findUsages/findObjectUsages/javaObjectUsages/Client.java similarity index 100% rename from idea/testData/findUsages/findObjectUsages/javaUsages/Client.java rename to idea/testData/findUsages/findObjectUsages/javaObjectUsages/Client.java diff --git a/idea/testData/findUsages/findObjectUsages/javaUsages/Server.kt b/idea/testData/findUsages/findObjectUsages/javaObjectUsages/Server.kt similarity index 100% rename from idea/testData/findUsages/findObjectUsages/javaUsages/Server.kt rename to idea/testData/findUsages/findObjectUsages/javaObjectUsages/Server.kt diff --git a/idea/testData/findUsages/findObjectUsages/javaObjectUsages/results.txt b/idea/testData/findUsages/findObjectUsages/javaObjectUsages/results.txt new file mode 100644 index 00000000000..e73d8bd4760 --- /dev/null +++ b/idea/testData/findUsages/findObjectUsages/javaObjectUsages/results.txt @@ -0,0 +1,2 @@ +Class static member access (7: 28) println("foo = " + O.instance$.getFoo()) +Usage in import (3: 15) import server.O \ No newline at end of file diff --git a/idea/testData/findUsages/findObjectUsages/kotlinUsages/Client.kt b/idea/testData/findUsages/findObjectUsages/kotlinObjectUsages/Client.kt similarity index 100% rename from idea/testData/findUsages/findObjectUsages/kotlinUsages/Client.kt rename to idea/testData/findUsages/findObjectUsages/kotlinObjectUsages/Client.kt diff --git a/idea/testData/findUsages/findObjectUsages/kotlinUsages/Server.kt b/idea/testData/findUsages/findObjectUsages/kotlinObjectUsages/Server.kt similarity index 100% rename from idea/testData/findUsages/findObjectUsages/kotlinUsages/Server.kt rename to idea/testData/findUsages/findObjectUsages/kotlinObjectUsages/Server.kt diff --git a/idea/testData/findUsages/findObjectUsages/kotlinObjectUsages/results.txt b/idea/testData/findUsages/findObjectUsages/kotlinObjectUsages/results.txt new file mode 100644 index 00000000000..f88980bb98a --- /dev/null +++ b/idea/testData/findUsages/findObjectUsages/kotlinObjectUsages/results.txt @@ -0,0 +1,3 @@ +Import directive (3: 15) import server.O +Receiver (7: 26) println("foo = ${O.foo}") +Value read (8: 19) val obj = O \ No newline at end of file diff --git a/idea/testData/findUsages/findPropertyUsages/javaUsages/Client.java b/idea/testData/findUsages/findPropertyUsages/javaPropertyUsages/Client.java similarity index 100% rename from idea/testData/findUsages/findPropertyUsages/javaUsages/Client.java rename to idea/testData/findUsages/findPropertyUsages/javaPropertyUsages/Client.java diff --git a/idea/testData/findUsages/findPropertyUsages/javaUsages/Server.kt b/idea/testData/findUsages/findPropertyUsages/javaPropertyUsages/Server.kt similarity index 100% rename from idea/testData/findUsages/findPropertyUsages/javaUsages/Server.kt rename to idea/testData/findUsages/findPropertyUsages/javaPropertyUsages/Server.kt diff --git a/idea/testData/findUsages/findPropertyUsages/javaPropertyUsages/results.txt b/idea/testData/findUsages/findPropertyUsages/javaPropertyUsages/results.txt new file mode 100644 index 00000000000..83ef31a8291 --- /dev/null +++ b/idea/testData/findUsages/findPropertyUsages/javaPropertyUsages/results.txt @@ -0,0 +1 @@ +Unclassified usage (7: 40) println("foo = " + O.instance$.getFoo()) \ No newline at end of file diff --git a/idea/testData/findUsages/findPropertyUsages/kotlinUsages/Client.kt b/idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages/Client.kt similarity index 100% rename from idea/testData/findUsages/findPropertyUsages/kotlinUsages/Client.kt rename to idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages/Client.kt diff --git a/idea/testData/findUsages/findPropertyUsages/kotlinUsages/Server.kt b/idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages/Server.kt similarity index 100% rename from idea/testData/findUsages/findPropertyUsages/kotlinUsages/Server.kt rename to idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages/Server.kt diff --git a/idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages/results.txt b/idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages/results.txt new file mode 100644 index 00000000000..7968c8e56cd --- /dev/null +++ b/idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages/results.txt @@ -0,0 +1,3 @@ +Import directive (3: 15) import server.foo +Receiver (8: 35) println("length: ${server.foo.length()}") +Selector (7: 33) println("foo = ${server.foo}") \ No newline at end of file diff --git a/idea/testData/findUsages/findWithFilteringImports/Usages.kt b/idea/testData/findUsages/findWithFilteringImports/Client.kt similarity index 100% rename from idea/testData/findUsages/findWithFilteringImports/Usages.kt rename to idea/testData/findUsages/findWithFilteringImports/Client.kt diff --git a/idea/testData/findUsages/findWithFilteringImports/Test.kt b/idea/testData/findUsages/findWithFilteringImports/Server.kt similarity index 100% rename from idea/testData/findUsages/findWithFilteringImports/Test.kt rename to idea/testData/findUsages/findWithFilteringImports/Server.kt diff --git a/idea/testData/findUsages/findWithFilteringImports/results.txt b/idea/testData/findUsages/findWithFilteringImports/results.txt new file mode 100644 index 00000000000..518e09c26b4 --- /dev/null +++ b/idea/testData/findUsages/findWithFilteringImports/results.txt @@ -0,0 +1 @@ +Instantiation (5: 9) val a = Test() \ No newline at end of file diff --git a/idea/testData/findUsages/unresolvedAnnotation/results.txt b/idea/testData/findUsages/unresolvedAnnotation/results.txt new file mode 100644 index 00000000000..1881962a8c1 --- /dev/null +++ b/idea/testData/findUsages/unresolvedAnnotation/results.txt @@ -0,0 +1 @@ +Usage in extends/implements clause (1: 29) public class Client extends Foo {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/findUsages/AbstractJetFindUsagesTest.java b/idea/tests/org/jetbrains/jet/findUsages/AbstractJetFindUsagesTest.java new file mode 100644 index 00000000000..57e56488325 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/findUsages/AbstractJetFindUsagesTest.java @@ -0,0 +1,186 @@ +/* + * 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.findUsages; + +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.base.Predicates; +import com.google.common.collect.Collections2; +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; +import com.intellij.openapi.extensions.Extensions; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.PsiComment; +import com.intellij.psi.PsiElement; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; +import com.intellij.usageView.UsageInfo; +import com.intellij.usages.UsageInfo2UsageAdapter; +import com.intellij.usages.impl.rules.UsageType; +import com.intellij.usages.impl.rules.UsageTypeProvider; +import com.intellij.usages.rules.UsageFilteringRule; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.psi.JetClass; +import org.jetbrains.jet.lang.psi.JetFunction; +import org.jetbrains.jet.lang.psi.JetObjectDeclarationName; +import org.jetbrains.jet.lang.psi.JetProperty; +import org.jetbrains.jet.plugin.JetLightProjectDescriptor; +import org.jetbrains.jet.plugin.PluginTestCaseBase; +import org.jetbrains.jet.plugin.findUsages.JetImportFilteringRule; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +public abstract class AbstractJetFindUsagesTest extends LightCodeInsightFixtureTestCase { + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JetLightProjectDescriptor.INSTANCE; + } + + @Override + public void setUp() throws Exception { + super.setUp(); + myFixture.setTestDataPath(PluginTestCaseBase.getTestDataPathBase() + "/findUsages"); + } + + public void testFindClassJavaUsages(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, true, JetClass.class); + } + + public void testFindClassKotlinUsages(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, false, JetClass.class); + } + + public void testFindUsagesUnresolvedAnnotation(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, true, JetClass.class); + } + + public void testFindMethodJavaUsages(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, true, JetFunction.class); + } + + public void testFindMethodKotlinUsages(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, false, JetFunction.class); + } + + public void testFindPropertyJavaUsages(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, true, JetProperty.class); + } + + public void testFindPropertyKotlinUsages(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, false, JetProperty.class); + } + + public void testFindObjectJavaUsages(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, true, JetObjectDeclarationName.class); + } + + public void testFindObjectKotlinUsages(@NotNull String path) throws Exception { + doTestWithoutFiltering(path, false, JetObjectDeclarationName.class); + } + + public void testFindWithFilteringImports(@NotNull String path) throws Exception { + doTest(path, false, JetClass.class, Lists.newArrayList(new JetImportFilteringRule())); + } + + private void doTestWithoutFiltering( + String path, + boolean searchInJava, + Class caretElementClass + ) throws Exception { + doTest(path, searchInJava, caretElementClass, Collections.emptyList()); + } + + private void doTest( + String path, + boolean searchInJava, + Class caretElementClass, + Collection filters + ) throws IOException { + String rootPath = path.substring(0, path.lastIndexOf("/") + 1); + + myFixture.configureByFiles(path, rootPath + "Client." + (searchInJava ? "java" : "kt")); + T caretElement = PsiTreeUtil.getParentOfType(myFixture.getElementAtCaret(), caretElementClass, false); + assertNotNull(String.format("Element with type '%s' wasn't found at caret position", caretElementClass), caretElement); + + Collection usageInfos = myFixture.findUsages(caretElement); + + Collection filteredUsages = getUsageAdapters(filters, usageInfos); + + Function convertToString = new Function() { + @Override + public String apply(@Nullable UsageInfo2UsageAdapter usageAdapter) { + assert usageAdapter != null; + return getUsageType(usageAdapter.getElement()) + + " " + Joiner.on("").join(Arrays.asList(usageAdapter.getPresentation().getText())); + } + }; + + Collection finalUsages = Ordering.natural().sortedCopy(Collections2.transform(filteredUsages, convertToString)); + String expectedText = FileUtil.loadFile(new File(rootPath + "results.txt")); + assertOrderedEquals(finalUsages, Ordering.natural().sortedCopy(StringUtil.split(expectedText, "\n"))); + } + + private static Collection getUsageAdapters( + final Collection filters, + Collection usageInfos + ) { + return Collections2.filter( + Collections2.transform(usageInfos, new Function() { + @Override + public UsageInfo2UsageAdapter apply(@Nullable UsageInfo usageInfo) { + assert (usageInfo != null); + + UsageInfo2UsageAdapter usageAdapter = new UsageInfo2UsageAdapter(usageInfo); + for (UsageFilteringRule filter : filters) { + if (!filter.isVisible(usageAdapter)) { + return null; + } + } + + return usageAdapter; + } + }), + Predicates.notNull()); + } + + @Nullable + private static UsageType getUsageType(PsiElement element) { + if (element == null) return null; + + if (PsiTreeUtil.getParentOfType(element, PsiComment.class, false) != null) { + return UsageType.COMMENT_USAGE; + } + + UsageTypeProvider[] providers = Extensions.getExtensions(UsageTypeProvider.EP_NAME); + for (UsageTypeProvider provider : providers) { + UsageType usageType = provider.getUsageType(element); + if (usageType != null) { + return usageType; + } + } + + return UsageType.UNCLASSIFIED; + } +} diff --git a/idea/tests/org/jetbrains/jet/findUsages/JetFindUsagesTest.java b/idea/tests/org/jetbrains/jet/findUsages/JetFindUsagesTest.java index 33e8c702c07..5637a665378 100644 --- a/idea/tests/org/jetbrains/jet/findUsages/JetFindUsagesTest.java +++ b/idea/tests/org/jetbrains/jet/findUsages/JetFindUsagesTest.java @@ -16,251 +16,192 @@ package org.jetbrains.jet.findUsages; -import com.google.common.base.Function; -import com.google.common.base.Joiner; -import com.google.common.base.Predicates; -import com.google.common.collect.Collections2; -import com.google.common.collect.Lists; -import com.google.common.collect.Ordering; -import com.intellij.openapi.extensions.Extensions; -import com.intellij.psi.PsiComment; -import com.intellij.psi.PsiElement; -import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.testFramework.LightProjectDescriptor; -import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; -import com.intellij.usageView.UsageInfo; -import com.intellij.usages.UsageInfo2UsageAdapter; -import com.intellij.usages.impl.rules.UsageType; -import com.intellij.usages.impl.rules.UsageTypeProvider; -import com.intellij.usages.rules.UsageFilteringRule; -import com.intellij.util.ArrayUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.psi.JetClass; -import org.jetbrains.jet.lang.psi.JetFunction; -import org.jetbrains.jet.lang.psi.JetObjectDeclarationName; -import org.jetbrains.jet.lang.psi.JetProperty; -import org.jetbrains.jet.plugin.JetLightProjectDescriptor; -import org.jetbrains.jet.plugin.PluginTestCaseBase; -import org.jetbrains.jet.plugin.findUsages.JetImportFilteringRule; +import junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestSuite; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; +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; -public class JetFindUsagesTest extends LightCodeInsightFixtureTestCase { - @NotNull - @Override - protected LightProjectDescriptor getProjectDescriptor() { - return JetLightProjectDescriptor.INSTANCE; - } +import org.jetbrains.jet.findUsages.AbstractJetFindUsagesTest; - @Override - public void setUp() throws Exception { - super.setUp(); - myFixture.setTestDataPath(PluginTestCaseBase.getTestDataPathBase() + "/findUsages"); - } - - public void testFindClassJavaUsages() { - doTestWithoutFiltering( - Lists.newArrayList("findClassUsages/javaUsages/Server.kt", "findClassUsages/javaUsages/Client.java"), - JetClass.class, - Lists.newArrayList("Field declaration (4: 13) private Server myServer;")); - } - - public void testFindClassKotlinUsages1() { - doTestWithoutFiltering( - Lists.newArrayList("findClassUsages/kotlinUsages1/Server.kt", "findClassUsages/kotlinUsages1/Client.kt"), - JetClass.class, - Lists.newArrayList( - "Class/object property type (6: 21) var nextServer: Server? = null", - "Function return types (14: 26) fun getNextServer(): Server? {", - "Function return types (32: 21) fun Any.asServer(): Server? {", - "Import directive (3: 15) import server.Server", - "Local variable type (10: 21) val server: Server = s", - "Nested class/object (7: 16) val name = Server.NAME", - "Parameter type (24: 19) fun Client.bar(s: Server) {", - "Parameter type (9: 16) fun foo(s: Server) {", - "Super type qualifier (19: 15) super.work()", - "Supertype (5: 15) class Client: Server {", - "Target type of 'as' operation (33: 40) return if (this is Server) this as Server else null", - "Target type of 'is' operation (33: 24) return if (this is Server) this as Server else null" - )); - } - - public void testFindClassKotlinUsages2() { - doTestWithoutFiltering( - Lists.newArrayList("findClassUsages/kotlinUsages2/Server.kt", "findClassUsages/kotlinUsages2/Client.kt"), - JetClass.class, - Lists.newArrayList( - "Annotation (5: 1) X(\"\")", - "Import directive (3: 15) import server.X" - )); - } - - public void testFindClassKotlinUsages3() { - doTestWithoutFiltering( - Lists.newArrayList("findClassUsages/kotlinUsages3/Server.kt", "findClassUsages/kotlinUsages3/Client.kt"), - JetClass.class, - Lists.newArrayList( - "Import directive (3: 15) import server.Data", - "Type argument (9: 16) val c = Client()", - "Type constraint (5: 17) class Client where X: Data {", - "Type constraint (5: 35) class Client where X: Data {" - )); - } - - public void testFindUsagesUnresolvedAnnotation() { - doTestWithoutFiltering( - Lists.newArrayList("unresolvedAnnotation/Server.kt", "unresolvedAnnotation/Client.java"), - JetClass.class, - Lists.newArrayList("Usage in extends/implements clause (1: 29) public class Client extends Foo {}")); - } - - public void testFindMethodJavaUsages() { - doTestWithoutFiltering( - Lists.newArrayList("findMethodUsages/javaUsages/Server.kt", "findMethodUsages/javaUsages/Client.java"), - JetFunction.class, - Lists.newArrayList("Unclassified usage (6: 16) server.processRequest();")); - } - - public void testFindMethodKotlinUsages() { - doTestWithoutFiltering( - Lists.newArrayList("findMethodUsages/kotlinUsages/Server.kt", "findMethodUsages/kotlinUsages/Client.kt"), - JetFunction.class, - Lists.newArrayList( - "Callable reference (6: 23) val methodRef = ::processRequest()", - "Function call (10: 9) processRequest()", - "Import directive (3: 15) import server.processRequest" - )); - } - - public void testFindPropertyJavaUsages() { - doTestWithoutFiltering( - Lists.newArrayList("findPropertyUsages/javaUsages/Server.kt", "findPropertyUsages/javaUsages/Client.java"), - JetProperty.class, - Lists.newArrayList( - "Unclassified usage (7: 40) println(\"foo = \" + O.instance$.getFoo())" - )); - } - - public void testFindPropertyKotlinUsages() { - doTestWithoutFiltering( - Lists.newArrayList("findPropertyUsages/kotlinUsages/Server.kt", "findPropertyUsages/kotlinUsages/Client.kt"), - JetProperty.class, - Lists.newArrayList( - "Import directive (3: 15) import server.foo", - "Receiver (8: 35) println(\"length: ${server.foo.length()}\")", - "Selector (7: 33) println(\"foo = ${server.foo}\")" - )); - } - - public void testFindObjectJavaUsages() { - doTestWithoutFiltering( - Lists.newArrayList("findObjectUsages/javaUsages/Server.kt", "findObjectUsages/javaUsages/Client.java"), - JetObjectDeclarationName.class, - Lists.newArrayList( - "Class static member access (7: 28) println(\"foo = \" + O.instance$.getFoo())", - "Usage in import (3: 15) import server.O" - )); - } - - public void testFindObjectKotlinUsages() { - doTestWithoutFiltering( - Lists.newArrayList("findObjectUsages/kotlinUsages/Server.kt", "findObjectUsages/kotlinUsages/Client.kt"), - JetObjectDeclarationName.class, - Lists.newArrayList( - "Import directive (3: 15) import server.O", - "Receiver (7: 26) println(\"foo = ${O.foo}\")", - "Value read (8: 19) val obj = O" - )); - } - - public void testFindWithFilteringImports() { - doTest(Lists.newArrayList("findWithFilteringImports/Test.kt", "findWithFilteringImports/Usages.kt"), - JetClass.class, - 2, - Lists.newArrayList(new JetImportFilteringRule()), - Lists.newArrayList("Instantiation (5: 9) val a = Test()")); - } - - private void doTestWithoutFiltering( - Collection files, - Class caretElementClass, - Collection expectedUsages - ) { - doTest(files, caretElementClass, expectedUsages.size(), Collections.emptyList(), expectedUsages); - } - - private void doTest( - Collection files, - Class caretElementClass, - int numberOfNotFilteredUsages, - Collection filters, - Collection expectedUsages - ) { - myFixture.configureByFiles(ArrayUtil.toObjectArray(files, String.class)); - T caretElement = PsiTreeUtil.getParentOfType(myFixture.getElementAtCaret(), caretElementClass, false); - assertNotNull(String.format("Element with type '%s' wasn't found at caret position", caretElementClass), caretElement); - - Collection usageInfos = myFixture.findUsages(caretElement); - - assertEquals(numberOfNotFilteredUsages, usageInfos.size()); - - Collection filteredUsages = getUsageAdapters(filters, usageInfos); - - Function convertToString = new Function() { - @Override - public String apply(@Nullable UsageInfo2UsageAdapter usageAdapter) { - assert usageAdapter != null; - return getUsageType(usageAdapter.getElement()) - + " " + Joiner.on("").join(Arrays.asList(usageAdapter.getPresentation().getText())); - } - }; - - Collection finalUsages = Ordering.natural().sortedCopy(Collections2.transform(filteredUsages, convertToString)); - assertOrderedEquals(finalUsages, Ordering.natural().sortedCopy(expectedUsages)); - } - - private static Collection getUsageAdapters( - final Collection filters, - Collection usageInfos - ) { - return Collections2.filter( - Collections2.transform(usageInfos, new Function() { - @Override - public UsageInfo2UsageAdapter apply(@Nullable UsageInfo usageInfo) { - assert (usageInfo != null); - - UsageInfo2UsageAdapter usageAdapter = new UsageInfo2UsageAdapter(usageInfo); - for (UsageFilteringRule filter : filters) { - if (!filter.isVisible(usageAdapter)) { - return null; - } - } - - return usageAdapter; - } - }), - Predicates.notNull()); - } - - @Nullable - private static UsageType getUsageType(PsiElement element) { - if (element == null) return null; - - if (PsiTreeUtil.getParentOfType(element, PsiComment.class, false) != null) { - return UsageType.COMMENT_USAGE; +/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@InnerTestClasses({JetFindUsagesTest.JavaClassUsages.class, JetFindUsagesTest.KotlinClassUsages1.class, JetFindUsagesTest.KotlinClassUsages2.class, JetFindUsagesTest.KotlinClassUsages3.class, JetFindUsagesTest.JavaObjectUsages.class, JetFindUsagesTest.KotlinObjectUsages.class, JetFindUsagesTest.JavaMethodUsages.class, JetFindUsagesTest.KotlinMethodUsages.class, JetFindUsagesTest.JavaPropertyUsages.class, JetFindUsagesTest.KotlinPropertyUsages.class, JetFindUsagesTest.FindWithFilteringImports.class, JetFindUsagesTest.UnresolvedAnnotation.class}) +public class JetFindUsagesTest extends AbstractJetFindUsagesTest { + @TestMetadata("idea/testData/findUsages/findClassUsages/javaClassUsages") + public static class JavaClassUsages extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindClassJavaUsages("idea/testData/findUsages/findClassUsages/javaClassUsages/Server.kt"); } - - UsageTypeProvider[] providers = Extensions.getExtensions(UsageTypeProvider.EP_NAME); - for (UsageTypeProvider provider : providers) { - UsageType usageType = provider.getUsageType(element); - if (usageType != null) { - return usageType; - } + + public void testAllFilesPresentInJavaClassUsages() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findClassUsages/javaClassUsages"), Pattern.compile("^(.*)Server.kt$"), true); } - - return UsageType.UNCLASSIFIED; + + } + + @TestMetadata("idea/testData/findUsages/findClassUsages/kotlinClassUsages1") + public static class KotlinClassUsages1 extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindClassKotlinUsages("idea/testData/findUsages/findClassUsages/kotlinClassUsages1/Server.kt"); + } + + public void testAllFilesPresentInKotlinClassUsages1() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findClassUsages/kotlinClassUsages1"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findClassUsages/kotlinClassUsages2") + public static class KotlinClassUsages2 extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindClassKotlinUsages("idea/testData/findUsages/findClassUsages/kotlinClassUsages2/Server.kt"); + } + + public void testAllFilesPresentInKotlinClassUsages2() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findClassUsages/kotlinClassUsages2"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findClassUsages/kotlinClassUsages3") + public static class KotlinClassUsages3 extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindClassKotlinUsages("idea/testData/findUsages/findClassUsages/kotlinClassUsages3/Server.kt"); + } + + public void testAllFilesPresentInKotlinClassUsages3() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findClassUsages/kotlinClassUsages3"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findObjectUsages/javaObjectUsages") + public static class JavaObjectUsages extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindObjectJavaUsages("idea/testData/findUsages/findObjectUsages/javaObjectUsages/Server.kt"); + } + + public void testAllFilesPresentInJavaObjectUsages() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findObjectUsages/javaObjectUsages"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findObjectUsages/kotlinObjectUsages") + public static class KotlinObjectUsages extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindObjectKotlinUsages("idea/testData/findUsages/findObjectUsages/kotlinObjectUsages/Server.kt"); + } + + public void testAllFilesPresentInKotlinObjectUsages() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findObjectUsages/kotlinObjectUsages"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findMethodUsages/javaMethodUsages") + public static class JavaMethodUsages extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindMethodJavaUsages("idea/testData/findUsages/findMethodUsages/javaMethodUsages/Server.kt"); + } + + public void testAllFilesPresentInJavaMethodUsages() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findMethodUsages/javaMethodUsages"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findMethodUsages/kotlinMethodUsages") + public static class KotlinMethodUsages extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindMethodKotlinUsages("idea/testData/findUsages/findMethodUsages/kotlinMethodUsages/Server.kt"); + } + + public void testAllFilesPresentInKotlinMethodUsages() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findMethodUsages/kotlinMethodUsages"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findPropertyUsages/javaPropertyUsages") + public static class JavaPropertyUsages extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindPropertyJavaUsages("idea/testData/findUsages/findPropertyUsages/javaPropertyUsages/Server.kt"); + } + + public void testAllFilesPresentInJavaPropertyUsages() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findPropertyUsages/javaPropertyUsages"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages") + public static class KotlinPropertyUsages extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindPropertyKotlinUsages("idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages/Server.kt"); + } + + public void testAllFilesPresentInKotlinPropertyUsages() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findPropertyUsages/kotlinPropertyUsages"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/findWithFilteringImports") + public static class FindWithFilteringImports extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindWithFilteringImports("idea/testData/findUsages/findWithFilteringImports/Server.kt"); + } + + public void testAllFilesPresentInFindWithFilteringImports() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/findWithFilteringImports"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + @TestMetadata("idea/testData/findUsages/unresolvedAnnotation") + public static class UnresolvedAnnotation extends AbstractJetFindUsagesTest { + @TestMetadata("Server.kt") + public void test() throws Exception { + testFindUsagesUnresolvedAnnotation("idea/testData/findUsages/unresolvedAnnotation/Server.kt"); + } + + public void testAllFilesPresentInUnresolvedAnnotation() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/findUsages/unresolvedAnnotation"), Pattern.compile("^(.*)Server.kt$"), true); + } + + } + + public static Test suite() { + TestSuite suite = new TestSuite("JetFindUsagesTest"); + suite.addTestSuite(JavaClassUsages.class); + suite.addTestSuite(KotlinClassUsages1.class); + suite.addTestSuite(KotlinClassUsages2.class); + suite.addTestSuite(KotlinClassUsages3.class); + suite.addTestSuite(JavaObjectUsages.class); + suite.addTestSuite(KotlinObjectUsages.class); + suite.addTestSuite(JavaMethodUsages.class); + suite.addTestSuite(KotlinMethodUsages.class); + suite.addTestSuite(JavaPropertyUsages.class); + suite.addTestSuite(KotlinPropertyUsages.class); + suite.addTestSuite(FindWithFilteringImports.class); + suite.addTestSuite(UnresolvedAnnotation.class); + return suite; } }