From 398b27545cb479a848b4f6a7af85c2dd28bffc18 Mon Sep 17 00:00:00 2001 From: Marat Khabibullin Date: Fri, 18 Oct 2019 14:45:53 +0300 Subject: [PATCH] Add "Quick Type Definition" tests for Java and Ruby GitOrigin-RevId: 787c15e679ae1273dfff94732f7e4d8c3e054255 --- .../NoDefinitionForClass.java | 4 ++ .../NoDefinitionForInt.java | 6 +++ .../NoDefinitionForMethod.java | 4 ++ .../NoDefinitionForType.java | 4 ++ .../showTypeDefinition/Reference.java | 6 +++ .../actions/ShowTypeDefinitionActionTest.kt | 44 +++++++++++++++++++ .../hint/actions/ShowTypeDefinitionAction.kt | 19 +++++++- 7 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForClass.java create mode 100644 java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForInt.java create mode 100644 java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForMethod.java create mode 100644 java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForType.java create mode 100644 java/java-tests/testData/codeInsight/showTypeDefinition/Reference.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/hint/actions/ShowTypeDefinitionActionTest.kt diff --git a/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForClass.java b/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForClass.java new file mode 100644 index 000000000000..28d45d69e740 --- /dev/null +++ b/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForClass.java @@ -0,0 +1,4 @@ +public class Main { + public static void main(String[] args) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForInt.java b/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForInt.java new file mode 100644 index 000000000000..ba1d30c9ae7d --- /dev/null +++ b/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForInt.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + int num = 1; + System.out.println(num); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForMethod.java b/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForMethod.java new file mode 100644 index 000000000000..bd90f8903a47 --- /dev/null +++ b/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForMethod.java @@ -0,0 +1,4 @@ +public class Main { + public static void main(String[] args) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForType.java b/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForType.java new file mode 100644 index 000000000000..96f338856921 --- /dev/null +++ b/java/java-tests/testData/codeInsight/showTypeDefinition/NoDefinitionForType.java @@ -0,0 +1,4 @@ +public class Main { + public static void main(String[] args) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/showTypeDefinition/Reference.java b/java/java-tests/testData/codeInsight/showTypeDefinition/Reference.java new file mode 100644 index 000000000000..f77df8ef04e7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/showTypeDefinition/Reference.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + String str = "hello"; + System.out.println(str); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/hint/actions/ShowTypeDefinitionActionTest.kt b/java/java-tests/testSrc/com/intellij/codeInsight/hint/actions/ShowTypeDefinitionActionTest.kt new file mode 100644 index 000000000000..eebdf42b4f7d --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/hint/actions/ShowTypeDefinitionActionTest.kt @@ -0,0 +1,44 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.codeInsight.hint.actions + +import com.intellij.JavaTestUtil +import com.intellij.psi.PsiClass +import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase + +class ShowTypeDefinitionActionTest : JavaCodeInsightFixtureTestCase() { + override fun getBasePath(): String { + return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/showTypeDefinition/" + } + + fun testReference() { + myFixture.configureByFile(getTestName(false) + ".java") + val definitions = showDefinitions() + assertEquals(1, definitions.size) + val psiClass = definitions[0] as? PsiClass + assertNotNull(psiClass) + assertEquals("String", psiClass!!.name) + } + + fun testNoDefinitionForInt() { + doTestNoDefinitions() + } + + fun testNoDefinitionForClass() { + doTestNoDefinitions() + } + + fun testNoDefinitionForMethod() { + doTestNoDefinitions() + } + + fun testNoDefinitionForType() { + doTestNoDefinitions() + } + + private fun doTestNoDefinitions() { + myFixture.configureByFile(getTestName(false) + ".java") + assertEmpty(showDefinitions()) + } + + private fun showDefinitions() = ShowTypeDefinitionAction.runForTests(myFixture.editor.contentComponent) +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/codeInsight/hint/actions/ShowTypeDefinitionAction.kt b/platform/lang-impl/src/com/intellij/codeInsight/hint/actions/ShowTypeDefinitionAction.kt index 4073f4687bca..1dc444ce31cb 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hint/actions/ShowTypeDefinitionAction.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/hint/actions/ShowTypeDefinitionAction.kt @@ -5,6 +5,7 @@ import com.intellij.codeInsight.CodeInsightBundle import com.intellij.codeInsight.documentation.DocumentationManager import com.intellij.codeInsight.hint.* import com.intellij.codeInsight.navigation.actions.TypeDeclarationProvider +import com.intellij.ide.DataManager import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.application.ReadAction @@ -22,9 +23,11 @@ import com.intellij.util.Processor import org.jetbrains.annotations.TestOnly import java.awt.Component import com.intellij.util.containers.ContainerUtil +import org.jetbrains.annotations.TestOnly +import java.awt.Component import kotlin.streams.asSequence -class ShowTypeDefinitionAction : ShowImplementationsAction() { +open class ShowTypeDefinitionAction : ShowImplementationsAction() { override fun getSessionFactories(): List = listOf(TypeDefinitionsViewSessionFactory) override fun getPopupTitle(session: ImplementationViewSession): String { @@ -100,4 +103,18 @@ class ShowTypeDefinitionAction : ShowImplementationsAction() { } } } + + companion object { + @TestOnly + fun runForTests(context: Component): List { + val showTypeDefinitionAction = ShowTypeDefinitionActionForTest() + showTypeDefinitionAction.performForContext(DataManager.getInstance().getDataContext(context)) + return showTypeDefinitionAction.definitions.get().map { element -> (element as PsiImplementationViewElement).psiElement } + } + + private class ShowTypeDefinitionActionForTest(val definitions: Ref> = Ref()) : ShowTypeDefinitionAction() { + override fun showImplementations(session: ImplementationViewSession, invokedFromEditor: Boolean, invokedByShortcut: Boolean) = + definitions.set(session.implementationElements) + } + } } \ No newline at end of file