Tests for Unresolved reference highlighting in Evaluate expression window
authorElizaveta Shashkova <Elizaveta.Shashkova@jetbrains.com>
Wed, 18 Feb 2015 17:36:41 +0000 (20:36 +0300)
committerElizaveta Shashkova <Elizaveta.Shashkova@jetbrains.com>
Wed, 18 Feb 2015 17:36:41 +0000 (20:36 +0300)
python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionBuiltins/evaluateExpressionBuiltins.py [new file with mode: 0644]
python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionClass/evaluateExpressionClass.py [new file with mode: 0644]
python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionInsideFunction/evaluateExpressionInsideFunction.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/fixtures/PyInspectionTestCase.java
python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java

diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionBuiltins/evaluateExpressionBuiltins.py b/python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionBuiltins/evaluateExpressionBuiltins.py
new file mode 100644 (file)
index 0000000..1bf13ed
--- /dev/null
@@ -0,0 +1,3 @@
+a = [1, 2, 3]
+b = 3
+c = a + b
\ No newline at end of file
diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionClass/evaluateExpressionClass.py b/python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionClass/evaluateExpressionClass.py
new file mode 100644 (file)
index 0000000..d602bc5
--- /dev/null
@@ -0,0 +1,5 @@
+s = 1
+class A:
+    def __init__(self):
+        self.a = 2
+        self.b = 3
\ No newline at end of file
diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionInsideFunction/evaluateExpressionInsideFunction.py b/python/testData/inspections/PyUnresolvedReferencesInspection/EvaluateExpressionInsideFunction/evaluateExpressionInsideFunction.py
new file mode 100644 (file)
index 0000000..18f0338
--- /dev/null
@@ -0,0 +1,4 @@
+a = 3
+def f():
+    s = 1
+    return s
\ No newline at end of file
index 717a579ce97b19d5d799ae598b8a343f05b1d888..2f388aa205571f271b1a14448d7ee27613cc2c6c 100644 (file)
@@ -42,7 +42,7 @@ public abstract class PyInspectionTestCase extends PyTestCase {
     myFixture.checkHighlighting(isWarning(), isInfo(), isWeakWarning());
   }
 
-  private String getTestDirectory(boolean lowercaseFirstLetter) {
+  protected String getTestDirectory(boolean lowercaseFirstLetter) {
     return "inspections/" + getInspectionClass().getSimpleName() + "/" + getTestName(lowercaseFirstLetter);
   }
 
index 327cecabf147f550ebc4752b4d2dc3f18ff041ab..85b60df49184b2a357f6579b10799c5b19029924 100644 (file)
@@ -17,9 +17,14 @@ package com.jetbrains.python.inspections;
 
 import com.intellij.codeInsight.intention.IntentionAction;
 import com.intellij.openapi.util.JDOMUtil;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.PsiElement;
+import com.intellij.xdebugger.impl.XSourcePositionImpl;
+import com.jetbrains.python.debugger.PyDebuggerEditorsProvider;
 import com.jetbrains.python.fixtures.PyInspectionTestCase;
 import com.jetbrains.python.inspections.unresolvedReference.PyUnresolvedReferencesInspection;
 import com.jetbrains.python.psi.LanguageLevel;
+import com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl;
 import org.jdom.Element;
 import org.jetbrains.annotations.NotNull;
 
@@ -412,6 +417,41 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase {
     myFixture.checkHighlighting(isWarning(), isInfo(), isWeakWarning());
   }
 
+  protected VirtualFile prepareFile() {
+    myFixture.copyDirectoryToProject(getTestDirectory(false), "");
+    return myFixture.configureByFile(getTestDirectory(false) + "/" + getTestName(true) + ".py").getVirtualFile();
+  }
+
+  protected void doEvaluateExpressionTest(@NotNull VirtualFile mainFile, @NotNull String expression, int lineNumber) {
+    PsiElement element = PyDebuggerEditorsProvider.getContextElement(myFixture.getProject(),
+                                                                     XSourcePositionImpl.create(mainFile, lineNumber));
+    final PyExpressionCodeFragmentImpl fragment = new PyExpressionCodeFragmentImpl(myFixture.getProject(), "fragment.py", expression, true);
+    fragment.setContext(element);
+    myFixture.configureFromExistingVirtualFile(fragment.getVirtualFile());
+    myFixture.enableInspections(getInspectionClass());
+    myFixture.checkHighlighting(isWarning(), isInfo(), isWeakWarning());
+  }
+
+  public void testEvaluateExpressionBuiltins() {
+    VirtualFile mainFile = prepareFile();
+    doEvaluateExpressionTest(mainFile, "len(a)", 2);
+    doEvaluateExpressionTest(mainFile, "a", 2);
+  }
+
+  public void testEvaluateExpressionInsideFunction() {
+    VirtualFile mainFile = prepareFile();
+    doEvaluateExpressionTest(mainFile, "a", 3);
+    doEvaluateExpressionTest(mainFile, "s", 3);
+  }
+
+  // PY-14309
+  public void testEvaluateExpressionClass() {
+    VirtualFile mainFile = prepareFile();
+    doEvaluateExpressionTest(mainFile, "s", 4);
+    doEvaluateExpressionTest(mainFile, "self", 4);
+    doEvaluateExpressionTest(mainFile, "self.a", 4);
+  }
+
   // PY-13554
   public void testDocstringTypeFromSubModule() {
     doMultiFileTest();