Made some navigation tests independent on stdlib.

This commit is contained in:
Evgeny Gerashchenko
2014-09-25 16:40:06 +04:00
parent e53aca5496
commit d44926ffde
5 changed files with 102 additions and 96 deletions
@@ -0,0 +1,3 @@
package a
class Foo
@@ -0,0 +1,6 @@
package b
import a.*
fun doSomething(f: Foo, t: Thread, s: String) {
}
@@ -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())
}
}
}
}
@@ -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) + "<caret>" + 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.
@@ -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));
}
}