mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
convert test to PyLightFixtureTestCase
This commit is contained in:
@@ -87,10 +87,10 @@ public class PyUnresolvedReferencesInspection extends LocalInspectionTool {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static List<LocalQuickFix> proposeImportFixes(final PyElement node, String ref_text) {
|
||||
static Collection<LocalQuickFix> proposeImportFixes(final PyElement node, String ref_text) {
|
||||
PsiFile exisitng_import_file = null; // if there's a matching existing import, this it the file it imports
|
||||
ImportFromExistingFix fix = null;
|
||||
List<LocalQuickFix> fixes = new ArrayList<LocalQuickFix>(2);
|
||||
Collection<LocalQuickFix> fixes = new HashSet<LocalQuickFix>(2);
|
||||
Set<String> seen_file_names = new HashSet<String>(); // true import names
|
||||
// maybe the name is importable via some existing 'import foo' statement, and only needs a qualifier.
|
||||
// walk up collecting all such statements and analyzing
|
||||
@@ -279,10 +279,10 @@ public class PyUnresolvedReferencesInspection extends LocalInspectionTool {
|
||||
}
|
||||
// look in other imported modules for this whole name
|
||||
if (! ref_in_import) {
|
||||
List<LocalQuickFix> import_fixes = proposeImportFixes(node, ref_text);
|
||||
Collection<LocalQuickFix> import_fixes = proposeImportFixes(node, ref_text);
|
||||
if (import_fixes.size() > 0) {
|
||||
actions.addAll(import_fixes);
|
||||
Object first_action = import_fixes.get(0);
|
||||
Object first_action = import_fixes.iterator().next();
|
||||
if (first_action instanceof HintAction) {
|
||||
hint_action = ((HintAction)first_action);
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ class A:
|
||||
|
||||
class B(A):
|
||||
def foo(self):
|
||||
return self.<warning descr="Unresolved attribute reference 'x' for class 'B'">x</warning>
|
||||
return self.<caret><warning descr="Unresolved attribute reference 'x' for class 'B'">x</warning>
|
||||
|
||||
@@ -4,4 +4,4 @@ class A:
|
||||
|
||||
|
||||
a = A()
|
||||
a.<warning descr="Unresolved attribute reference 'y' for class 'A'">y</warning>+1
|
||||
a.<caret><warning descr="Unresolved attribute reference 'y' for class 'A'">y</warning>+1
|
||||
|
||||
@@ -3,4 +3,4 @@ class A:
|
||||
self.x = 1
|
||||
|
||||
def foo(self):
|
||||
a = self.<warning descr="Unresolved attribute reference 'y' for class 'A'">y</warning>
|
||||
a = self.<caret><warning descr="Unresolved attribute reference 'y' for class 'A'">y</warning>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"Doc comment stays intact"
|
||||
<warning descr="Unresolved reference 'AddImportDoc'">AddImportDoc</warning>.foo
|
||||
<caret><warning descr="Unresolved reference 'AddImportDoc'">AddImportDoc</warning>.foo
|
||||
|
||||
foo = 1
|
||||
|
||||
@@ -4,4 +4,4 @@ class A:
|
||||
|
||||
|
||||
a = A()
|
||||
a.<warning descr="Unresolved attribute reference 'y' for class 'A'">y</warning>()
|
||||
a.<caret><warning descr="Unresolved attribute reference 'y' for class 'A'">y</warning>()
|
||||
|
||||
@@ -3,4 +3,4 @@ class A:
|
||||
self.x = 1
|
||||
|
||||
def foo(self, a):
|
||||
self.<warning descr="Unresolved attribute reference 'y' for class 'A'">y</warning>(1, a)
|
||||
self.<caret><warning descr="Unresolved attribute reference 'y' for class 'A'">y</warning>(1, a)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
class A:
|
||||
def foo<error descr="Method must have a first parameter, usually called 'self'">()</error>: # Add 'self'
|
||||
def foo<caret><error descr="Method must have a first parameter, usually called 'self'">()</error>: # Add 'self'
|
||||
pass
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
from AddToImportFromFoo import bar
|
||||
|
||||
<warning descr="Unresolved reference 'foo'">foo</warning> # must get imported
|
||||
<caret><warning descr="Unresolved reference 'foo'">foo</warning> # must get imported
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
import QualifyByImportFoo
|
||||
|
||||
<warning descr="Unresolved reference 'foo'">foo</warning> # must be qualified
|
||||
<caret><warning descr="Unresolved reference 'foo'">foo</warning> # must be qualified
|
||||
|
||||
@@ -1 +1 @@
|
||||
a = 4; b = 5<warning descr="Trailing semicolon in the statement">;</warning>
|
||||
a = 4; b = 5<caret><warning descr="Trailing semicolon in the statement">;</warning>
|
||||
@@ -1,3 +1,3 @@
|
||||
class A:
|
||||
def foo(slf): # Rename to 'self'
|
||||
def foo(<caret>slf): # Rename to 'self'
|
||||
pass
|
||||
|
||||
@@ -1,30 +1,26 @@
|
||||
package com.jetbrains.python;
|
||||
|
||||
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase;
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.openapi.editor.markup.GutterIconRenderer;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.projectRoots.SdkModificator;
|
||||
import com.intellij.openapi.projectRoots.impl.ProjectJdkImpl;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.jetbrains.python.fixtures.PyLightFixtureTestCase;
|
||||
import com.jetbrains.python.inspections.PyMethodParametersInspection;
|
||||
import com.jetbrains.python.inspections.PyTrailingSemicolonInspection;
|
||||
import com.jetbrains.python.inspections.PyUnresolvedReferencesInspection;
|
||||
import gnu.trove.TIntObjectHashMap;
|
||||
import com.jetbrains.python.sdk.PythonSdkType;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Test actions that various inspections add.
|
||||
* User: dcheryasov
|
||||
* Date: Nov 29, 2008 12:47:08 AM
|
||||
*/
|
||||
public class PyQuickFixTest extends DaemonAnalyzerTestCase {
|
||||
public class PyQuickFixTest extends PyLightFixtureTestCase {
|
||||
|
||||
public void testAddImport() throws Exception {
|
||||
doInspectionTest("AddImport.py", PyUnresolvedReferencesInspection.class, PyBundle.message("ACT.NAME.add.import"), true, true);
|
||||
@@ -106,20 +102,6 @@ public class PyQuickFixTest extends DaemonAnalyzerTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
protected VirtualFile[] loadFiles(String[] names) {
|
||||
VirtualFile[] ret = new VirtualFile[names.length];
|
||||
String prefix = getTestDataPath();
|
||||
for (int i=0; i < names.length; i += 1) {
|
||||
ret[i] = getVirtualFile(prefix+names[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected void doTest(VirtualFile[] vFile, boolean checkWarnings, boolean checkInfos) throws Exception {
|
||||
configureByFiles(null, vFile);
|
||||
doDoTest(checkWarnings, checkInfos);
|
||||
}
|
||||
|
||||
protected @NonNls String getTestDataPath() {
|
||||
return PythonTestUtil.getTestDataPath() + "/inspections/";
|
||||
}
|
||||
@@ -148,68 +130,32 @@ public class PyQuickFixTest extends DaemonAnalyzerTestCase {
|
||||
boolean applyFix,
|
||||
boolean available
|
||||
) throws Exception {
|
||||
final LocalInspectionTool tool = (LocalInspectionTool)inspectionClass.newInstance();
|
||||
enableInspectionTool(tool);
|
||||
configureByFiles(null, testFiles);
|
||||
Collection<HighlightInfo> infos = doDoTest(true, false);
|
||||
|
||||
doQuickFixTest(infos, quickFixName, applyFix, available, testFiles[0]);
|
||||
disableInspectionTool(tool.getShortName());
|
||||
}
|
||||
|
||||
protected void doQuickFixTest(Collection<HighlightInfo> infos,
|
||||
String quickFixName,
|
||||
boolean applyFix,
|
||||
boolean shouldBeAvailable,
|
||||
String s) throws Exception {
|
||||
final List<IntentionAction> availableActions = new ArrayList<IntentionAction>(1);
|
||||
|
||||
TIntObjectHashMap<HighlightInfo> map = new TIntObjectHashMap<HighlightInfo>(infos.size());
|
||||
for (HighlightInfo info : infos) {
|
||||
final GutterIconRenderer renderer = info.getGutterIconRenderer();
|
||||
if (renderer == null) {
|
||||
assertFalse("There should be one intention for highlight info", map.containsKey(info.startOffset));
|
||||
map.put(info.startOffset, info);
|
||||
|
||||
if (info.quickFixActionRanges != null) {
|
||||
for (Pair<HighlightInfo.IntentionActionDescriptor, TextRange> pair : info.quickFixActionRanges) {
|
||||
IntentionAction action = pair.first.getAction();
|
||||
availableActions.add(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final IntentionAction intentionAction = LightQuickFixTestCase.findActionWithText(availableActions, quickFixName);
|
||||
if (shouldBeAvailable) {
|
||||
myFixture.enableInspections(inspectionClass);
|
||||
myFixture.configureByFiles(testFiles);
|
||||
myFixture.checkHighlighting(true, false, false);
|
||||
final IntentionAction intentionAction = myFixture.findSingleIntention(quickFixName);
|
||||
if (available) {
|
||||
assertNotNull(intentionAction);
|
||||
if (applyFix) {
|
||||
myFixture.launchAction(intentionAction);
|
||||
|
||||
myFixture.checkResultByFile(graftBeforeExt(testFiles [0], "_after"));
|
||||
}
|
||||
}
|
||||
else {
|
||||
assertNull(intentionAction);
|
||||
}
|
||||
|
||||
if (applyFix && shouldBeAvailable) {
|
||||
intentionAction.invoke(myProject, myEditor, myFile);
|
||||
|
||||
checkResultByFile(graftBeforeExt(s, "_after"));
|
||||
}
|
||||
}
|
||||
|
||||
// Turns "name.ext" to "name_insertion.ext"
|
||||
private @NonNls String graftBeforeExt(String name, String insertion) {
|
||||
|
||||
@NonNls
|
||||
private static String graftBeforeExt(String name, String insertion) {
|
||||
int dotpos = name.indexOf('.');
|
||||
if (dotpos < 0) dotpos = name.length();
|
||||
return name.substring(0, dotpos) + insertion + name.substring(dotpos, name.length());
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
protected Sdk getProjectJDK() {
|
||||
return createMockJdk(PathManager.getHomePath() + "/plugins/python/testData/mockPythonJDK");
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
private static Sdk createMockJdk(String jdkHome) {
|
||||
File jdkHomeFile = new File(jdkHome);
|
||||
if (!jdkHomeFile.exists()) return null;
|
||||
@@ -226,5 +172,18 @@ public class PyQuickFixTest extends DaemonAnalyzerTestCase {
|
||||
jdk.setVersionString("2.5");
|
||||
return jdk;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
protected static class PyWithSdkProjectDescriptor extends PyLightProjectDescriptor {
|
||||
@Override
|
||||
public Sdk getSdk() {
|
||||
return createMockJdk(PathManager.getHomePath() + "/plugins/python/testData/mockPythonJDK");
|
||||
}
|
||||
}
|
||||
|
||||
private static final LightProjectDescriptor ourProjectDescriptor = new PyWithSdkProjectDescriptor();
|
||||
|
||||
@Override
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return ourProjectDescriptor;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user