Two tests for resolved reference

This commit is contained in:
Nikolay Krasko
2012-09-07 15:18:46 +04:00
parent 4376c7a87c
commit 8fd52a9095
5 changed files with 99 additions and 21 deletions
+3
View File
@@ -0,0 +1,3 @@
open class Test
class SomeTest : <caret>Test()
@@ -0,0 +1,5 @@
package test1.test2
class Some()
val test : <caret>test1.test2.Some = Some()
@@ -21,17 +21,14 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.intellij.codeInsight.navigation.GotoImplementationHandler;
import com.intellij.codeInsight.navigation.GotoTargetHandler;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.UsefulTestCase;
import junit.framework.Assert;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.asJava.JetLightClass;
import org.jetbrains.jet.plugin.presentation.JetLightClassListCellRenderer;
import org.jetbrains.jet.testing.InTextDirectivesUtils;
import org.jetbrains.jet.testing.ReferenceUtils;
import java.util.Arrays;
import java.util.Collections;
@@ -40,7 +37,7 @@ import java.util.List;
/**
* @author Nikolay Krasko
*/
final class ImplementationTestUtils {
public final class ImplementationTestUtils {
private ImplementationTestUtils() {
}
@@ -59,22 +56,7 @@ final class ImplementationTestUtils {
@Override
public String apply(@Nullable PsiElement element) {
Assert.assertNotNull(element);
if (element instanceof JetLightClass) {
JetLightClass jetLightClass = (JetLightClass) element;
JetLightClassListCellRenderer renderer = new JetLightClassListCellRenderer();
String elementText = renderer.getElementText(jetLightClass);
String containerText = JetLightClassListCellRenderer.getContainerTextStatic(jetLightClass);
return (containerText != null) ? containerText + "." + elementText : elementText;
}
Assert.assertTrue(element instanceof NavigationItem);
ItemPresentation presentation = ((NavigationItem) element).getPresentation();
Assert.assertNotNull(presentation);
String presentableText = presentation.getPresentableText();
String locationString = presentation.getLocationString();
return locationString != null ? (locationString + "." + presentableText) : presentableText;
return ReferenceUtils.renderAsGotoImplementation(element);
}
});
@@ -22,7 +22,10 @@ import com.intellij.psi.PsiPolyVariantReference;
import com.intellij.psi.PsiReference;
import com.intellij.psi.ResolveResult;
import com.intellij.testFramework.LightCodeInsightTestCase;
import junit.framework.Assert;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.testing.ReferenceUtils;
import java.io.File;
@@ -34,10 +37,44 @@ public class ResolveBaseTest extends LightCodeInsightTestCase {
doMultiResolveTest();
}
public void testResolveClass() throws Exception {
doSingleResolveTest("(<root>).Test");
}
public void testResolvePackageInProperty() throws Exception {
doSingleResolveTest("test1");
}
public void testSeveralOverrides() throws Exception {
doMultiResolveTest();
}
protected void doSingleResolveTest(@Nullable String referenceToString) throws Exception {
final String testName = getTestName(false);
configureByFile(testName + ".kt");
int offset = getEditor().getCaretModel().getOffset();
final PsiReference psiReference = getFile().findReferenceAt(offset);
if (psiReference != null) {
PsiElement resolvedTo = psiReference.resolve();
if (resolvedTo != null) {
String resolvedToElementStr = ReferenceUtils.renderAsGotoImplementation(resolvedTo);
String notEqualMessage = String.format("Found reference to '%s', but '%s' was expected", resolvedToElementStr,
referenceToString != null ? referenceToString : "<null>");
assertEquals(notEqualMessage, referenceToString, resolvedToElementStr);
}
else {
Assert.assertNull(
String.format("Element %s wasn't resolved to anything, but %s was expected", psiReference, referenceToString),
referenceToString);
}
}
else {
Assert.assertNull(String.format("No reference found at offset: %s, but one resolved to %s was expected", offset, referenceToString),
referenceToString);
}
}
protected void doMultiResolveTest() throws Exception {
final String testName = getTestName(false);
configureByFile(testName + ".kt");
@@ -0,0 +1,51 @@
/*
* Copyright 2010-2012 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.testing;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.intellij.psi.PsiElement;
import junit.framework.Assert;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.asJava.JetLightClass;
import org.jetbrains.jet.plugin.presentation.JetLightClassListCellRenderer;
public final class ReferenceUtils {
private ReferenceUtils() {
}
public static String renderAsGotoImplementation(@NotNull PsiElement element) {
if (element instanceof JetLightClass) {
JetLightClass jetLightClass = (JetLightClass) element;
JetLightClassListCellRenderer renderer = new JetLightClassListCellRenderer();
String elementText = renderer.getElementText(jetLightClass);
String containerText = JetLightClassListCellRenderer.getContainerTextStatic(jetLightClass);
return (containerText != null) ? containerText + "." + elementText : elementText;
}
Assert.assertTrue(element instanceof NavigationItem);
ItemPresentation presentation = ((NavigationItem) element).getPresentation();
if (presentation == null) {
return element.getText();
}
String presentableText = presentation.getPresentableText();
String locationString = presentation.getLocationString();
return locationString != null ? (locationString + "." + presentableText) : presentableText;
}
}