From d46e5f07c0773decb8a483847328b07334883031 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 24 Jan 2018 21:35:10 +0300 Subject: [PATCH] PY-16412 Add tests on import of necessary classes and symbols from typing --- .../AnnotationImportClassName/lib.py | 2 + .../AnnotationImportClassName/main.py | 5 + .../AnnotationImportClassName/main_after.py | 6 ++ .../AnnotationImportTypingAny/main.py | 3 + .../AnnotationImportTypingAny/main_after.py | 6 ++ .../AnnotationImportTypingOptional/lib.py | 4 + .../AnnotationImportTypingOptional/main.py | 6 ++ .../main_after.py | 8 ++ .../AnnotationImportTypingUnion/lib.py | 4 + .../AnnotationImportTypingUnion/main.py | 6 ++ .../AnnotationImportTypingUnion/main_after.py | 8 ++ .../PyAnnotateVariableTypeIntentionTest.java | 93 +++++++++++++------ 12 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/lib.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/main.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/main_after.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingAny/main.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingAny/main_after.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/lib.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/main.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/main_after.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/lib.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/main.py create mode 100644 python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/main_after.py diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/lib.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/lib.py new file mode 100644 index 000000000000..07d9e0efebfa --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/lib.py @@ -0,0 +1,2 @@ +class MyClass: + pass diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/main.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/main.py new file mode 100644 index 000000000000..e7e7d83b03f6 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/main.py @@ -0,0 +1,5 @@ +import lib + +def func(x: lib.MyClass): + var = x + var \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/main_after.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/main_after.py new file mode 100644 index 000000000000..cbec9728ed97 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportClassName/main_after.py @@ -0,0 +1,6 @@ +import lib +from lib import MyClass + +def func(x: lib.MyClass): + var: [MyClass] = x + var \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingAny/main.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingAny/main.py new file mode 100644 index 000000000000..417787357197 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingAny/main.py @@ -0,0 +1,3 @@ +def func(x): + var = x + var \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingAny/main_after.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingAny/main_after.py new file mode 100644 index 000000000000..bc54dd626ad4 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingAny/main_after.py @@ -0,0 +1,6 @@ +from typing import Any + + +def func(x): + var: [Any] = x + var \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/lib.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/lib.py new file mode 100644 index 000000000000..1a591276b252 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/lib.py @@ -0,0 +1,4 @@ +if True: + foo = 42 +else: + foo = None diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/main.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/main.py new file mode 100644 index 000000000000..7f8536e330e4 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/main.py @@ -0,0 +1,6 @@ +from lib import foo + + +def func(): + var = foo + var \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/main_after.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/main_after.py new file mode 100644 index 000000000000..0049c2ceccff --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingOptional/main_after.py @@ -0,0 +1,8 @@ +from typing import Optional + +from lib import foo + + +def func(): + var: [Optional[int]] = foo + var \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/lib.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/lib.py new file mode 100644 index 000000000000..a2f833e7dea3 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/lib.py @@ -0,0 +1,4 @@ +if True: + foo = 42 +else: + foo = 'spam' diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/main.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/main.py new file mode 100644 index 000000000000..7f8536e330e4 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/main.py @@ -0,0 +1,6 @@ +from lib import foo + + +def func(): + var = foo + var \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/main_after.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/main_after.py new file mode 100644 index 000000000000..1000cf807915 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/AnnotationImportTypingUnion/main_after.py @@ -0,0 +1,8 @@ +from typing import Union + +from lib import foo + + +def func(): + var: [Union[str, int]] = foo + var \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyAnnotateVariableTypeIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyAnnotateVariableTypeIntentionTest.java index db116eed5ed6..ba2358bac21e 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyAnnotateVariableTypeIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyAnnotateVariableTypeIntentionTest.java @@ -1,6 +1,7 @@ // Copyright 2000-2018 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.jetbrains.python.intentions; +import com.intellij.codeInsight.intention.IntentionAction; import com.jetbrains.python.PyBundle; import com.jetbrains.python.psi.LanguageLevel; import org.jetbrains.annotations.NotNull; @@ -59,47 +60,47 @@ public class PyAnnotateVariableTypeIntentionTest extends PyIntentionTestCase { } public void testAnnotationLocalSimpleAssignmentTarget() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationLocalSimpleAssignmentTargetInParentheses() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationLocalUnpackedAssignmentTarget() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationLocalChainedAssignmentTarget() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationLocalForTarget() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationLocalWithTarget() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationInstanceAttribute() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationInstanceAttributeDocstring() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationInstanceAttributeClassLevelAssignment() { - doTestAnnotation(); + doAnnotationTest(); } public void testAnnotationInstanceAttributeClassLevelAssignmentInAncestor() { - doTestAnnotation(); + doAnnotationTest(); } public void testTypeCommentInstanceAttribute() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentInstanceAttributePy3() { @@ -107,70 +108,102 @@ public class PyAnnotateVariableTypeIntentionTest extends PyIntentionTestCase { } public void testTypeCommentInstanceAttributeDocstring() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentInstanceAttributeClassLevelAssignment() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentInstanceAttributeClassLevelAssignmentInAncestor() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalSimpleAssignmentTarget() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalSimpleAssignmentTargetInParentheses() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalUnpackedAssignmentTarget() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentComplexUnpackedAssignmentTarget() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalChainedAssignmentTarget() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalForTarget() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalWithTarget() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalSimpleAssignmentTargetWithExistingComment() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalForTargetWithExistingComment() { - doTestTypeComment(); + doTypeCommentTest(); } public void testTypeCommentLocalWithTargetWithExistingComment() { - doTestTypeComment(); + doTypeCommentTest(); } - private void doTestAnnotation() { + public void testAnnotationImportTypingAny() { + doMultiFileAnnotationTest(); + } + + public void testAnnotationImportTypingUnion() { + doMultiFileAnnotationTest(); + } + + public void testAnnotationImportTypingOptional() { + doMultiFileAnnotationTest(); + } + + public void testAnnotationImportClassName() { + doMultiFileAnnotationTest(); + } + + private void doAnnotationTest() { doTest(LanguageLevel.PYTHON36); } - private void doTestTypeComment() { + private void doTypeCommentTest() { doTest(LanguageLevel.PYTHON27); } - private void doTest(@NotNull LanguageLevel languageLevel) { - doTest(PyBundle.message("INTN.annotate.types"), languageLevel); - } - private void doNegativeTest() { runWithLanguageLevel(LanguageLevel.PYTHON36, () -> doNegativeTest(PyBundle.message("INTN.annotate.types"))); } + + public void doMultiFileAnnotationTest() { + runWithLanguageLevel(LanguageLevel.PYTHON36, () -> { + doMultiFileTest(PyBundle.message("INTN.annotate.types")); + }); + } + + private void doMultiFileTest(@NotNull String hint) { + myFixture.copyDirectoryToProject(getTestName(false), ""); + myFixture.configureByFile("main.py"); + final IntentionAction intentionAction = myFixture.findSingleIntention(hint); + assertNotNull(intentionAction); + assertSdkRootsNotParsed(myFixture.getFile()); + myFixture.launchAction(intentionAction); + myFixture.checkResultByFile(getTestName(false) + "/main_after.py", true); + } + + private void doTest(@NotNull LanguageLevel languageLevel) { + doTest(PyBundle.message("INTN.annotate.types"), languageLevel); + } }