diff options
Diffstat (limited to 'python/testSrc/com/jetbrains/python')
9 files changed, 383 insertions, 12 deletions
diff --git a/python/testSrc/com/jetbrains/python/PyFillParagraphTest.java b/python/testSrc/com/jetbrains/python/PyFillParagraphTest.java index 0a26253aec56..287c65f9e8a1 100644 --- a/python/testSrc/com/jetbrains/python/PyFillParagraphTest.java +++ b/python/testSrc/com/jetbrains/python/PyFillParagraphTest.java @@ -22,6 +22,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.command.CommandProcessor; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.jetbrains.python.fixtures.PyTestCase; /** @@ -62,7 +63,8 @@ public class PyFillParagraphTest extends PyTestCase { } public void testEnter() { - final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings(); + final CommonCodeStyleSettings settings = + CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings().getCommonSettings(PythonLanguage.getInstance()); int oldValue = settings.RIGHT_MARGIN; settings.RIGHT_MARGIN = 80; try { diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index 36805442a1ea..35a97e03efae 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -21,10 +21,12 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.fixtures.PyTestCase; import com.jetbrains.python.formatter.PyCodeStyleSettings; import com.jetbrains.python.psi.LanguageLevel; import com.jetbrains.python.psi.PyElementGenerator; +import com.jetbrains.python.psi.PyStatement; import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher; /** @@ -273,19 +275,19 @@ public class PyFormatterTest extends PyTestCase { } public void testWrapDefinitionWithLongLine() { // IDEA-92081 - settings().RIGHT_MARGIN = 30; + settings().setRightMargin(PythonLanguage.getInstance(), 30); settings().WRAP_LONG_LINES = true; doTest(); } public void testWrapAssignment() { // PY-8572 - settings().RIGHT_MARGIN = 120; + settings().setRightMargin(PythonLanguage.getInstance(), 120); settings().WRAP_LONG_LINES = false; doTest(); } public void testIndentInSlice() { // PY-8572 - settings().RIGHT_MARGIN = 120; + settings().setRightMargin(PythonLanguage.getInstance(), 120); settings().WRAP_LONG_LINES = false; doTest(); } @@ -352,7 +354,7 @@ public class PyFormatterTest extends PyTestCase { } public void testWrapInBinaryExpression() { // PY-9032 - settings().RIGHT_MARGIN = 80; + settings().setRightMargin(PythonLanguage.getInstance(), 80); doTest(true); } @@ -370,7 +372,7 @@ public class PyFormatterTest extends PyTestCase { } public void testWrapImports() { // PY-9163 - settings().RIGHT_MARGIN = 80; + settings().setRightMargin(PythonLanguage.getInstance(), 80); doTest(); } @@ -424,6 +426,26 @@ public class PyFormatterTest extends PyTestCase { myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.py"); } + /** + * This test merely checks that call to {@link com.intellij.psi.codeStyle.CodeStyleManager#reformat(com.intellij.psi.PsiElement)} + * is possible for Python sources. + */ + public void testReformatOfSingleElementPossible() { + myFixture.configureByFile("formatter/" + getTestName(true) + ".py"); + WriteCommandAction.runWriteCommandAction(myFixture.getProject(), new Runnable() { + @Override + public void run() { + final PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); + assertNotNull(elementAtCaret); + final PyStatement statement = PsiTreeUtil.getParentOfType(elementAtCaret, PyStatement.class, false); + assertNotNull(statement); + final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myFixture.getProject()); + codeStyleManager.reformat(statement); + } + }); + myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.py"); + } + private CodeStyleSettings settings() { return CodeStyleSettingsManager.getInstance().getSettings(myFixture.getProject()); } diff --git a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java index c36372d9ba64..e49b0eaee6c8 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java @@ -251,6 +251,23 @@ public class PyQuickFixTest extends PyTestCase { PyBundle.message("QFIX.statement.effect.introduce.variable"), true, true); } + public void testReplacePrintEnd() { + runWithLanguageLevel(LanguageLevel.PYTHON34, new Runnable() { + @Override + public void run() { + doInspectionTest("ReplacePrintEnd.py", PyStatementEffectInspection.class, PyBundle.message("QFIX.statement.effect"), true, true); + }}); + } + + public void testReplacePrintComment() { + runWithLanguageLevel(LanguageLevel.PYTHON34, new Runnable() { + @Override + public void run() { + doInspectionTest("ReplacePrintComment.py", PyStatementEffectInspection.class, PyBundle.message("QFIX.statement.effect"), true, + true); + }}); + } + public void testUnresolvedWith() { // PY-2083 setLanguageLevel(LanguageLevel.PYTHON25); doInspectionTest("UnresolvedWith.py", PyUnresolvedReferencesInspection.class, @@ -296,6 +313,11 @@ public class PyQuickFixTest extends PyTestCase { PyBundle.message("QFIX.default.argument"), true, true); } + public void testDefaultArgumentEmptyList() { + doInspectionTest("DefaultArgumentEmptyList.py", PyDefaultArgumentInspection.class, + PyBundle.message("QFIX.default.argument"), true, true); + } + public void testPyArgumentEqualDefault() { //PY-3125 doInspectionTest("ArgumentEqualDefault.py", PyArgumentEqualDefaultInspection.class, PyBundle.message("QFIX.remove.argument.equal.default"), true, true); diff --git a/python/testSrc/com/jetbrains/python/PyTypeTest.java b/python/testSrc/com/jetbrains/python/PyTypeTest.java index b0e41bcffddc..b6ed1090edad 100644 --- a/python/testSrc/com/jetbrains/python/PyTypeTest.java +++ b/python/testSrc/com/jetbrains/python/PyTypeTest.java @@ -835,6 +835,37 @@ public class PyTypeTest extends PyTestCase { "expr = (1,) + (True, 'spam') + ()"); } + public void testConstructorUnification() { + doTest("C[int]", + "class C(object):\n" + + " def __init__(self, x):\n" + + " '''\n" + + " :type x: T\n" + + " :rtype: C[T]\n" + + " '''\n" + + " pass\n" + + "\n" + + "expr = C(10)\n"); + } + + public void testGenericClassMethodUnification() { + doTest("int", + "class C(object):\n" + + " def __init__(self, x):\n" + + " '''\n" + + " :type x: T\n" + + " :rtype: C[T]\n" + + " '''\n" + + " pass\n" + + " def foo(self):\n" + + " '''\n" + + " :rtype: T\n" + + " '''\n" + + " pass\n" + + "\n" + + "expr = C(10).foo()\n"); + } + private static TypeEvalContext getTypeEvalContext(@NotNull PyExpression element) { return TypeEvalContext.userInitiated(element.getContainingFile()).withTracing(); } diff --git a/python/testSrc/com/jetbrains/python/PyTypingTest.java b/python/testSrc/com/jetbrains/python/PyTypingTest.java new file mode 100644 index 000000000000..9513795cb5b7 --- /dev/null +++ b/python/testSrc/com/jetbrains/python/PyTypingTest.java @@ -0,0 +1,267 @@ +/* + * Copyright 2000-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 com.jetbrains.python; + +import com.intellij.testFramework.LightProjectDescriptor; +import com.jetbrains.python.documentation.PythonDocumentationProvider; +import com.jetbrains.python.fixtures.PyTestCase; +import com.jetbrains.python.psi.LanguageLevel; +import com.jetbrains.python.psi.PyExpression; +import com.jetbrains.python.psi.types.PyType; +import com.jetbrains.python.psi.types.TypeEvalContext; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Tests for a type system based on mypy's typing module. + * + * @author vlan + */ +public class PyTypingTest extends PyTestCase { + @Nullable + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return ourPy3Descriptor; + } + + @Override + public void setUp() throws Exception { + super.setUp(); + setLanguageLevel(LanguageLevel.PYTHON32); + } + + @Override + public void tearDown() throws Exception { + setLanguageLevel(null); + super.tearDown(); + } + + public void testClassType() { + doTest("Foo", + "class Foo:" + + " pass\n" + + "\n" + + "def f(expr: Foo):\n" + + " pass\n"); + } + + public void testClassReturnType() { + doTest("Foo", + "class Foo:" + + " pass\n" + + "\n" + + "def f() -> Foo:\n" + + " pass\n" + + "\n" + + "expr = f()\n"); + + } + + public void testNoneType() { + doTest("None", + "def f(expr: None):\n" + + " pass\n"); + } + + public void testNoneReturnType() { + doTest("None", + "def f() -> None:\n" + + " return 0\n" + + "expr = f()\n"); + } + + public void testUnionType() { + doTest("int | str", + "from typing import Union\n" + + "\n" + + "def f(expr: Union[int, str]):\n" + + " pass\n"); + } + + public void testBuiltinList() { + doTest("list", + "from typing import List\n" + + "\n" + + "def f(expr: List):\n" + + " pass\n"); + } + + public void testBuiltinListWithParameter() { + doTest("list[int]", + "from typing import List\n" + + "\n" + + "def f(expr: List[int]):\n" + + " pass\n"); + } + + public void testBuiltinDictWithParameters() { + doTest("dict[str, int]", + "from typing import Dict\n" + + "\n" + + "def f(expr: Dict[str, int]):\n" + + " pass\n"); + } + + public void testBuiltinTuple() { + doTest("tuple", + "from typing import Tuple\n" + + "\n" + + "def f(expr: Tuple):\n" + + " pass\n"); + } + + public void testBuiltinTupleWithParameters() { + doTest("(int, str)", + "from typing import Tuple\n" + + "\n" + + "def f(expr: Tuple[int, str]):\n" + + " pass\n"); + } + + public void testAnyType() { + doTest("unknown", + "from typing import Any\n" + + "\n" + + "def f(expr: Any):\n" + + " pass\n"); + } + + public void testGenericType() { + doTest("A", + "from typing import typevar\n" + + "\n" + + "T = typevar('A')\n" + + "\n" + + "def f(expr: T):\n" + + " pass\n"); + } + + public void testGenericBoundedType() { + doTest("T <= int | str", + "from typing import typevar\n" + + "\n" + + "T = typevar('T', values=(int, str))\n" + + "\n" + + "def f(expr: T):\n" + + " pass\n"); + } + + public void testParameterizedClass() { + doTest("C[int]", + "from typing import Generic, typevar\n" + + "\n" + + "T = typevar('T')\n" + + "\n" + + "class C(Generic[T]):\n" + + " def __init__(self, x: T):\n" + + " pass\n" + + "\n" + + "expr = C(10)\n"); + } + + public void testParameterizedClassMethod() { + doTest("int", + "from typing import Generic, typevar\n" + + "\n" + + "T = typevar('T')\n" + + "\n" + + "class C(Generic[T]):\n" + + " def __init__(self, x: T):\n" + + " pass\n" + + " def foo(self) -> T:\n" + + " pass\n" + + "\n" + + "expr = C(10).foo()\n"); + } + + public void testParameterizedClassInheritance() { + doTest("int", + "from typing import Generic, typevar\n" + + "\n" + + "T = typevar('T')\n" + + "\n" + + "class B(Generic[T]):\n" + + " def foo(self) -> T:\n" + + " pass\n" + + "class C(B[T]):\n" + + " def __init__(self, x: T):\n" + + " pass\n" + + "\n" + + "expr = C(10).foo()\n"); + } + + public void testAnyStrUnification() { + doTest("bytes", + "from typing import AnyStr\n" + + "\n" + + "def foo(x: AnyStr) -> AnyStr:\n" + + " pass\n" + + "\n" + + "expr = foo(b'bar')\n"); + } + + public void testAnyStrForUnknown() { + doTest("str | bytes", + "from typing import AnyStr\n" + + "\n" + + "def foo(x: AnyStr) -> AnyStr:\n" + + " pass\n" + + "\n" + + "def bar(x):\n" + + " expr = foo(x)\n"); + } + + public void testFunctionType() { + doTest("(int, str) -> str", + "from typing import Function\n" + + "\n" + + "def foo(expr: Function[[int, str], str]):\n" + + " pass\n"); + } + + public void testTypeInStringLiteral() { + doTest("C", + "class C:\n" + + " def foo(self, expr: 'C'):\n" + + " pass\n"); + } + + public void testQualifiedTypeInStringLiteral() { + doTest("str", + "import typing\n" + + "\n" + + "def foo(x: 'typing.AnyStr') -> typing.AnyStr:\n" + + " pass\n" + + "\n" + + "expr = foo('bar')\n"); + } + + private void doTest(@NotNull String expectedType, @NotNull String text) { + myFixture.copyDirectoryToProject("typing", ""); + myFixture.configureByText(PythonFileType.INSTANCE, text); + final PyExpression expr = myFixture.findElementByText("expr", PyExpression.class); + final TypeEvalContext codeAnalysis = TypeEvalContext.codeAnalysis(expr.getContainingFile()); + final TypeEvalContext userInitiated = TypeEvalContext.userInitiated(expr.getContainingFile()).withTracing(); + assertType(expectedType, expr, codeAnalysis, "code analysis"); + assertType(expectedType, expr, userInitiated, "user initiated"); + } + + private static void assertType(String expectedType, PyExpression expr, TypeEvalContext context, String contextName) { + final PyType actual = context.getType(expr); + final String actualType = PythonDocumentationProvider.getTypeName(actual, context); + assertEquals("Failed in " + contextName + " context", expectedType, actualType); + } +} diff --git a/python/testSrc/com/jetbrains/python/PyWrapTest.java b/python/testSrc/com/jetbrains/python/PyWrapTest.java index 05c21b8af21a..4146391ea5d8 100644 --- a/python/testSrc/com/jetbrains/python/PyWrapTest.java +++ b/python/testSrc/com/jetbrains/python/PyWrapTest.java @@ -17,6 +17,7 @@ package com.jetbrains.python; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.jetbrains.python.fixtures.PyTestCase; /** @@ -30,17 +31,19 @@ public class PyWrapTest extends PyTestCase { protected void setUp() throws Exception { super.setUp(); final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings(); + final CommonCodeStyleSettings pythonSettings = settings.getCommonSettings(PythonLanguage.getInstance()); myOldWrap = settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN; - myOldMargin = settings.RIGHT_MARGIN; + myOldMargin = pythonSettings.RIGHT_MARGIN; settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN = true; - settings.RIGHT_MARGIN = 80; + pythonSettings.RIGHT_MARGIN = 80; } @Override protected void tearDown() throws Exception { final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings(); + final CommonCodeStyleSettings pythonSettings = settings.getCommonSettings(PythonLanguage.getInstance()); settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN = myOldWrap; - settings.RIGHT_MARGIN = myOldMargin; + pythonSettings.RIGHT_MARGIN = myOldMargin; super.tearDown(); } @@ -71,9 +74,10 @@ public class PyWrapTest extends PyTestCase { public void testWrapRightMargin() { final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings(); - int oldValue = settings.RIGHT_MARGIN; + final CommonCodeStyleSettings pythonSettings = settings.getCommonSettings(PythonLanguage.getInstance()); + int oldValue = pythonSettings.RIGHT_MARGIN; boolean oldMarginValue = settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN; - settings.RIGHT_MARGIN = 100; + pythonSettings.RIGHT_MARGIN = 100; settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN = true; try { final String testName = "wrap/" + getTestName(true); @@ -84,7 +88,7 @@ public class PyWrapTest extends PyTestCase { myFixture.checkResultByFile(testName + ".after.py"); } finally { - settings.RIGHT_MARGIN = oldValue; + pythonSettings.RIGHT_MARGIN = oldValue; settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN = oldMarginValue; } } diff --git a/python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java b/python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java index 99da2a43aea1..8a237b7dfb01 100644 --- a/python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java +++ b/python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java @@ -111,6 +111,7 @@ public abstract class PyTestCase extends UsefulTestCase { final PythonLanguageLevelPusher levelPusher = Extensions.findExtension(FilePropertyPusher.EP_NAME, PythonLanguageLevelPusher.class); levelPusher.flushLanguageLevelCache(); super.tearDown(); + clearFields(this); } @Nullable diff --git a/python/testSrc/com/jetbrains/python/intentions/PyIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyIntentionTest.java index 2891d819cf43..058da63e4d01 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyIntentionTest.java @@ -259,6 +259,11 @@ public class PyIntentionTest extends PyTestCase { doTest("Convert to 'import sys'"); } + // PY-11074 + public void testImportToImportFrom() { + doTest("Convert to 'from __builtin__ import ...'"); + } + public void testTypeInDocstring() { doDocReferenceTest(); } diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java index 4f02da1038ff..9a764b94169d 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java @@ -18,6 +18,9 @@ package com.jetbrains.python.refactoring; import com.intellij.codeInsight.TargetElementUtilBase; import com.intellij.openapi.util.Comparing; import com.intellij.psi.PsiElement; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.jetbrains.python.PythonLanguage; import com.jetbrains.python.fixtures.PyTestCase; import com.jetbrains.python.refactoring.inline.PyInlineLocalHandler; @@ -81,7 +84,21 @@ public class PyInlineLocalTest extends PyTestCase { doTest(); } + // PY-12401 public void testComment() { doTest(); } + + // PY-13114 + public void testReferenceInParenthesis() { + doTest(); + } + + // PY-12409 + public void testResultExceedsRightMargin() { + final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myFixture.getProject()); + settings.WRAP_LONG_LINES = true; + settings.setRightMargin(PythonLanguage.getInstance(), 80); + doTest(); + } } |