/python/testSrc/com/jetbrains/python/codeInsight/PyGotoTypeDeclarationTest.kt

https://github.com/JetBrains/intellij-community · Kotlin · 134 lines · 100 code · 23 blank · 11 comment · 0 complexity · 4206a2b6c5f41e82ec660829c5c6e3df MD5 · raw file

  1. // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
  2. package com.jetbrains.python.codeInsight
  3. import com.intellij.codeInsight.navigation.actions.GotoTypeDeclarationAction
  4. import com.intellij.psi.PsiElement
  5. import com.jetbrains.python.PythonFileType
  6. import com.jetbrains.python.fixtures.PyTestCase
  7. import com.jetbrains.python.psi.PyClass
  8. import com.jetbrains.python.psi.PyFile
  9. import com.jetbrains.python.psi.impl.PyBuiltinCache
  10. class PyGotoTypeDeclarationTest : PyTestCase() {
  11. // PY-41452
  12. fun testSimple() {
  13. val type = findSymbolType(
  14. "class Foo:\n" +
  15. " pass\n" +
  16. "foo = Foo()\n" +
  17. "f<caret>oo"
  18. )
  19. assertEquals((myFixture.file as PyFile).findTopLevelClass("Foo"), type)
  20. }
  21. // PY-41452
  22. fun testGeneric() {
  23. val type = findSymbolType(
  24. "from typing import List\n" +
  25. "foo: List[int] = undefined\n" +
  26. "f<caret>oo"
  27. )
  28. assertEquals(PyBuiltinCache.getInstance(myFixture.file).listType?.pyClass, type)
  29. }
  30. // PY-41452
  31. fun testUnion() {
  32. val types = findSymbolTypes(
  33. "from typing import Union\n" +
  34. "def foo() -> Union[str, int]\n" +
  35. " pass\n" +
  36. "b<caret>ar = foo()"
  37. )
  38. val cache = PyBuiltinCache.getInstance(myFixture.file)
  39. assertContainsElements(types, cache.intType?.pyClass, cache.strType?.pyClass)
  40. }
  41. // PY-41452
  42. fun testUnionOfTypingLiterals() {
  43. val type = findSymbolType(
  44. "from typing import Literal\n" +
  45. "def foo() -> Literal[\"a\", \"b\"]\n" +
  46. " pass\n" +
  47. "b<caret>ar = foo()"
  48. )
  49. assertEquals(PyBuiltinCache.getInstance(myFixture.file).strType?.pyClass, type)
  50. }
  51. // PY-41452
  52. fun testModule() {
  53. val type = findSymbolType("import typing\ntyp<caret>ing")
  54. assertEquals("types.ModuleType", (type as PyClass).qualifiedName)
  55. }
  56. // PY-41452
  57. fun testTypingNewType() {
  58. val type = findSymbolType(
  59. "from typing import NewType\n" +
  60. "NT = NewType(\"NT\", int)\n" +
  61. "def foo(b<caret>ar: NT) -> None:\n" +
  62. " pass"
  63. )
  64. assertEquals((myFixture.file as PyFile).findTopLevelAttribute("NT"), type)
  65. }
  66. // PY-41452
  67. fun testNamedTupleClass() {
  68. val type = findSymbolType(
  69. "from typing import NamedTuple\n" +
  70. "class MyNT(NamedTuple):\n" +
  71. " field_1: int\n" +
  72. " field_2: str\n" +
  73. "my_n<caret>t: MyNT = undefined"
  74. )
  75. assertEquals((myFixture.file as PyFile).findTopLevelClass("MyNT"), type)
  76. }
  77. // PY-41452
  78. fun testNamedTupleTarget() {
  79. val type = findSymbolType(
  80. "from typing import NamedTuple\n" +
  81. "Employee = NamedTuple('Employee', [('name', str), ('id', int)])\n" +
  82. "my_n<caret>t: Employee = undefined"
  83. )
  84. assertEquals((myFixture.file as PyFile).findTopLevelAttribute("Employee"), type)
  85. }
  86. // PY-41452
  87. fun testTypedDictClass() {
  88. val type = findSymbolType(
  89. "from typing import TypedDict\n" +
  90. "class MyDict(TypedDict):\n" +
  91. " field_1: int\n" +
  92. " field_2: str\n" +
  93. "my_d<caret>ict: MyDict = undefined"
  94. )
  95. assertEquals((myFixture.file as PyFile).findTopLevelClass("MyDict"), type)
  96. }
  97. // PY-41452
  98. fun testTypedDictTarget() {
  99. val type = findSymbolType(
  100. "from typing import TypedDict\n" +
  101. "Movie = TypedDict('Movie', {'name': str, 'year': int})\n" +
  102. "my_d<caret>ict: Movie = undefined"
  103. )
  104. assertEquals((myFixture.file as PyFile).findTopLevelAttribute("Movie"), type)
  105. }
  106. private fun findSymbolType(text: String): PsiElement = findSymbolTypes(text).single()
  107. private fun findSymbolTypes(text: String): List<PsiElement> {
  108. myFixture.configureByText(PythonFileType.INSTANCE, text)
  109. return GotoTypeDeclarationAction.findSymbolTypes(myFixture.editor, myFixture.caretOffset)?.asList() ?: emptyList()
  110. }
  111. }