diff --git a/idea/testData/libraries/navigation/fromLibSource/Foo.kt b/idea/testData/libraries/navigation/fromLibSource/Foo.kt new file mode 100644 index 00000000000..0ca6244f3cb --- /dev/null +++ b/idea/testData/libraries/navigation/fromLibSource/Foo.kt @@ -0,0 +1,3 @@ +package a + +class Foo \ No newline at end of file diff --git a/idea/testData/libraries/navigation/fromLibSource/usage.kt b/idea/testData/libraries/navigation/fromLibSource/usage.kt new file mode 100644 index 00000000000..4e40d48a15c --- /dev/null +++ b/idea/testData/libraries/navigation/fromLibSource/usage.kt @@ -0,0 +1,6 @@ +package b + +import a.* + +fun doSomething(f: Foo, t: Thread, s: String) { +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/libraries/NavigateFromLibrarySourcesTest.kt b/idea/tests/org/jetbrains/jet/plugin/libraries/NavigateFromLibrarySourcesTest.kt new file mode 100644 index 00000000000..178fe9f214d --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/libraries/NavigateFromLibrarySourcesTest.kt @@ -0,0 +1,93 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.libraries + +import com.intellij.testFramework.LightProjectDescriptor +import org.jetbrains.jet.plugin.JdkAndMockLibraryProjectDescriptor +import org.jetbrains.jet.plugin.PluginTestCaseBase +import com.intellij.openapi.roots.ModuleRootManager +import com.intellij.openapi.roots.LibraryOrderEntry +import com.intellij.openapi.roots.OrderRootType +import com.intellij.openapi.vfs.VirtualFileManager +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiClass +import org.jetbrains.jet.lang.psi.JetClass +import kotlin.test.assertEquals +import kotlin.test.fail +import org.jetbrains.kotlin.util.sure +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import org.jetbrains.jet.lang.psi.JetClassOrObject +import org.jetbrains.jet.asJava.LightClassUtil +import org.jetbrains.jet.asJava.KotlinLightClass +import kotlin.test.assertTrue + +public class NavigateFromLibrarySourcesTest: LightCodeInsightFixtureTestCase() { + public fun testJdkClass() { + checkNavigationFromLibrarySource("Thread", "java.lang.Thread") + } + + public fun testOurKotlinClass() { + checkNavigationFromLibrarySource("Foo", "a.Foo") + } + + public fun testBuiltinClass() { + checkNavigationFromLibrarySource("String", "kotlin.String") + } + + // This test is not exactly for navigation, but separating it to another class doesn't worth it. + public fun testNoLightClassForLibrarySource() { + val navigationElement = navigationElementForReferenceInLibrarySource("Foo") + assertTrue(navigationElement is JetClassOrObject, "Foo should navigate to JetClassOrObject") + val lightClass = LightClassUtil.getPsiClass(navigationElement as JetClassOrObject) + assertTrue(lightClass !is KotlinLightClass, "Do not create Kotlin Light Class for file from library sources") + } + + private fun checkNavigationFromLibrarySource(referenceText: String, targetFqName: String) { + checkNavigationElement(navigationElementForReferenceInLibrarySource(referenceText), targetFqName) + } + + private fun navigationElementForReferenceInLibrarySource(referenceText: String): PsiElement { + val libraryOrderEntry = ModuleRootManager.getInstance(myModule!!).getOrderEntries().first { it is LibraryOrderEntry } + val libSourcesRoot = libraryOrderEntry.getUrls(OrderRootType.SOURCES)[0] + val vf = VirtualFileManager.getInstance().findFileByUrl(libSourcesRoot + "/usage.kt")!! + val psiFile = getPsiManager().findFile(vf)!! + val indexOf = psiFile.getText()!!.indexOf(referenceText) + val reference = psiFile.findReferenceAt(indexOf) + return reference.sure("Couldn't find reference").resolve().sure("Couldn't resolve reference").getNavigationElement()!! + } + + override fun getProjectDescriptor(): LightProjectDescriptor { + return JdkAndMockLibraryProjectDescriptor(PluginTestCaseBase.getTestDataPathBase() + "/libraries/navigation/fromLibSource", true) + } + + private fun checkNavigationElement(element: PsiElement, expectedFqName: String) { + when (element) { + is PsiClass -> { + assertEquals(expectedFqName, element.getQualifiedName()) + } + is JetClass -> { + val name = element.getFqName() + assert(name != null) + assertEquals(expectedFqName, name!!.asString()) + } + else -> { + fail("Navigation element should be JetClass or PsiClass: " + element.javaClass + ", " + element.getText()) + } + } + } + +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/libraries/NavigateToStdlibSourceRegressionTest.java b/idea/tests/org/jetbrains/jet/plugin/libraries/NavigateToStdlibSourceRegressionTest.java index b1042979a58..0925dcee241 100644 --- a/idea/tests/org/jetbrains/jet/plugin/libraries/NavigateToStdlibSourceRegressionTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/libraries/NavigateToStdlibSourceRegressionTest.java @@ -42,67 +42,6 @@ public class NavigateToStdlibSourceRegressionTest extends NavigateToLibraryRegre assertEquals("Test.kt", navigationElement.getContainingFile().getName()); } - public void testJavaClass() throws IOException { - doNavigationInSourcesTest("libraries/stdlib/src/kotlin/collections/Iterators.kt", "Enumeration", "java.util.Enumeration"); - } - - public void testKotlinClass() throws IOException { - doNavigationInSourcesTest("libraries/stdlib/src/kotlin/collections/Stream.kt", "AbstractIterator", "kotlin.support.AbstractIterator"); - } - - public void testClassWithJavaAnalog() throws IOException { - doNavigationInSourcesTest("libraries/stdlib/src/kotlin/collections/AbstractIterator.kt", "Iterator", "kotlin.Iterator"); - } - - public void testNavigationInKotlinBuiltIns() throws IOException { - doNavigationInSourcesTest("libraries/stdlib/src/generated/_Arrays.kt", "Array", "kotlin.Array"); - } - - private void doNavigationInSourcesTest(@NotNull String path, @NotNull String element, @NotNull String expectedFqName) throws IOException { - PsiElement navigationElement = getNavigationElement(path, element); - checkNavigationElement(navigationElement, expectedFqName); - } - - protected PsiElement getNavigationElement(String path, String element) { - File file = new File(path); - PsiFile psiFile = getPsiFileForFileFromSources(file); - String text = psiFile.getText(); - int index = text.indexOf(element); - assertNotSame("Cannot find text '" + element + "' in file " + path, -1, index); - while (Character.isLetter(text.charAt(index - 1))) { - index = text.indexOf(element, index + 1); - } - PsiReference ref = psiFile.findReferenceAt(index); - assertNotNull("Cannot find reference at " + index + ", " + - text.substring(index - 20, index) + "" + text.substring(index, index + 20), ref); - PsiElement resolvedElement = ref.resolve(); - assertNotNull("Cannot resolve reference: " + ref.getElement().getText(), resolvedElement); - return resolvedElement.getNavigationElement(); - } - - @NotNull - private PsiFile getPsiFileForFileFromSources(@NotNull File file) { - VirtualFile virtualFile = VfsUtil.findFileByIoFile(file, false); - assertNotNull("Cannot find virtual file for " + file.getAbsolutePath(), virtualFile); - PsiFile psiFile = getPsiManager().findFile(virtualFile); - assertNotNull("Cannot find psi file for " + virtualFile.getCanonicalPath(), psiFile); - return psiFile; - } - - private static void checkNavigationElement(@NotNull PsiElement element, @NotNull String expectedName) { - if (element instanceof PsiClass) { - assertEquals(expectedName, ((PsiClass) element).getQualifiedName()); - } - else if (element instanceof JetClass) { - FqName name = ((JetClass) element).getFqName(); - assert name != null; - assertEquals(expectedName, name.asString()); - } - else { - fail("Navigation element should be JetClass or PsiClass: " + element.getClass() + ", " + element.getText()); - } - } - @Override protected void tearDown() throws Exception { // Workaround for IDEA's bug during tests. diff --git a/idea/tests/org/jetbrains/jet/plugin/lightClasses/LightClassForLibrarySourceFileTest.java b/idea/tests/org/jetbrains/jet/plugin/lightClasses/LightClassForLibrarySourceFileTest.java deleted file mode 100644 index bbc9e13115a..00000000000 --- a/idea/tests/org/jetbrains/jet/plugin/lightClasses/LightClassForLibrarySourceFileTest.java +++ /dev/null @@ -1,35 +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.lightClasses; - -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiElement; -import org.jetbrains.jet.asJava.KotlinLightClass; -import org.jetbrains.jet.asJava.LightClassUtil; -import org.jetbrains.jet.lang.psi.JetClassOrObject; -import org.jetbrains.jet.plugin.libraries.NavigateToStdlibSourceRegressionTest; - -public class LightClassForLibrarySourceFileTest extends NavigateToStdlibSourceRegressionTest { - - public void testLightClassForFileFromLibrarySource() throws Exception { - PsiElement navigationElement = getNavigationElement("libraries/stdlib/src/kotlin/collections/Stream.kt", "AbstractIterator"); - assertTrue("AbstractIterator should navigate to JetClassOrObject", navigationElement instanceof JetClassOrObject); - PsiClass lightClass = LightClassUtil.getPsiClass((JetClassOrObject) navigationElement); - assertTrue("Do not create Kotlin Light Class for file from library sources", !(lightClass instanceof KotlinLightClass)); - } - -}