📄 pythoncompletionwithoutbuiltinstest.java
字号:
/*
* Created on Mar 8, 2005
*
* @author Fabio Zadrozny
*/
package org.python.pydev.editor.codecompletion;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.python.pydev.core.ICallback;
import org.python.pydev.core.IModule;
import org.python.pydev.core.TestDependent;
import org.python.pydev.core.docutils.ImportsSelection;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.docutils.StringUtils;
import org.python.pydev.core.docutils.PySelection.ActivationTokenAndQual;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.structure.CompletionRecursionException;
import org.python.pydev.editor.codecompletion.revisited.CodeCompletionTestsBase;
import org.python.pydev.editor.codecompletion.revisited.modules.CompiledModule;
/**
* This tests the 'whole' code completion, passing through all modules.
*
* @author Fabio Zadrozny
*/
public class PythonCompletionWithoutBuiltinsTest extends CodeCompletionTestsBase {
public static void main(String[] args) {
try {
//DEBUG_TESTS_BASE = true;
PythonCompletionWithoutBuiltinsTest test = new PythonCompletionWithoutBuiltinsTest();
test.setUp();
test.testAssertDeterminesClass();
test.tearDown();
System.out.println("Finished");
junit.textui.TestRunner.run(PythonCompletionWithoutBuiltinsTest.class);
} catch (Exception e) {
e.printStackTrace();
} catch(Error e){
e.printStackTrace();
}
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
CompiledModule.COMPILED_MODULES_ENABLED = false;
this.restorePythonPath(false);
codeCompletion = new PyCodeCompletion();
PyCodeCompletion.onCompletionRecursionException = new ICallback<Object, CompletionRecursionException>(){
public Object call(CompletionRecursionException e) {
throw new RuntimeException("Recursion error:"+Log.getExceptionStr(e));
}
};
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
CompiledModule.COMPILED_MODULES_ENABLED = true;
super.tearDown();
PyCodeCompletion.onCompletionRecursionException = null;
}
public void testCompleteImportCompletion() throws CoreException, BadLocationException{
requestCompl("from testl" , "testlib");
requestCompl("import testl" , "testlib");
requestCompl("from testlib import " , new String[]{"__init__", "unittest"});
requestCompl("from testlib import unittest, __in" , new String[]{"__init__"});
requestCompl("from testlib import unittest,__in" , new String[]{"__init__"});
requestCompl("from testlib import unittest ,__in" , new String[]{"__init__"});
requestCompl("from testlib import unittest , __in" , new String[]{"__init__"});
requestCompl("from testlib import unittest , " , new String[]{"__init__", "unittest"});
requestCompl("from testlib.unittest import ", getTestLibUnittestTokens());
requestCompl("from testlib.unittest.testcase.TestCase import assertImagesNotE", new String[]{"assertImagesNotEqual"});
requestCompl("from testlib.unittest.testcase.TestCase import assertBM", new String[]{"assertBMPsNotEqual","assertBMPsEqual"});
}
/**
* This test checks the code-completion for adaptation and factory methods, provided that the
* class expected is passed as one of the parameters.
*
* This is done in AssignAnalysis
*/
public void testProtocolsAdaptation() throws CoreException, BadLocationException{
String s =
"import protocols\n" +
"class InterfM1(protocols.Interface):\n" +
" def m1(self):\n" +
" pass\n" +
" \n" +
"class Bar(object):\n" +
" protocols.advise(instancesProvide=[InterfM1])\n" +
"if __name__ == '__main__':\n" +
" a = protocols.adapt(Bar(), InterfM1)\n" +
" a.";
requestCompl(s, s.length(), -1, new String[]{"m1()"});
}
/**
* Check if some assert for an instance is enough to get the type of some variable. This should
* be configurable so that the user can do something as assert IsInterfaceDeclared(obj, Class) or
* AssertImplements(obj, Class), with the assert or not, providing some way for the user to configure that.
*
* This is done in ILocalScope#getPossibleClassesForActivationToken
*/
public void testAssertDeterminesClass() throws CoreException, BadLocationException{
String s =
"def m1(a):\n" +
" import xmllib\n" +
" assert isinstance(a, xmllib.XMLParser)\n" +
" a.";
requestCompl(s, s.length(), -1, new String[]{"handle_data(data)"});
}
public void testAssertDeterminesClass2() throws CoreException, BadLocationException{
String s =
"def m1(a):\n" +
" import xmllib\n" +
" assert isinstance(a.bar, xmllib.XMLParser)\n" +
" a.bar.";
requestCompl(s, s.length(), -1, new String[]{"handle_data(data)"});
}
public void testAssertDeterminesClass3() throws CoreException, BadLocationException{
String s =
"class InterfM1:\n" +
" def m1(self):\n" +
" pass\n" +
"\n" +
"" +
"def m1(a):\n" +
" assert isinstance(a, InterfM1)\n" +
" a.";
requestCompl(s, s.length(), -1, new String[]{"m1()"});
}
public void testAssertDeterminesClass4() throws CoreException, BadLocationException{
String s =
"class InterfM1:\n" +
" def m1(self):\n" +
" pass\n" +
"\n" +
"class InterfM2:\n" +
" def m2(self):\n" +
" pass\n" +
"\n" +
"" +
"def m1(a):\n" +
" assert isinstance(a, (InterfM1, InterfM2))\n" +
" a.";
requestCompl(s, s.length(), -1, new String[]{"m1()", "m2()"});
}
public void testAssertDeterminesClass5() throws CoreException, BadLocationException{
String s =
"class InterfM1:\n" +
" def m1(self):\n" +
" pass\n" +
"\n" +
"" +
"def m1(a):\n" +
" assert InterfM1.implementedBy(a)\n" +
" a.";
requestCompl(s, s.length(), -1, new String[]{"m1()"});
}
public void testAssertDeterminesClass6() throws CoreException, BadLocationException{
String s =
"class InterfM1:\n" +
" def m1(self):\n" +
" pass\n" +
"\n" +
"" +
"def m1(a):\n" +
" assert InterfM1.implementedBy()\n" + //should give no error
" a.";
requestCompl(s, s.length(), -1, new String[]{});
}
public void testMultilineImportCompletion() throws CoreException, BadLocationException{
String s = "from testlib import (\n";
requestCompl(s, new String[]{"__init__", "unittest"});
}
/**
* @return
*/
public String[] getTestLibUnittestTokens() {
return new String[]{
"__init__"
, "anothertest"
, "AnotherTest"
, "GUITest"
, "guitestcase"
, "main"
, "relative"
, "t"
, "TestCase"
, "testcase"
, "TestCaseAlias"
};
}
public void testSelfReference() throws CoreException, BadLocationException{
String s;
s = "class C: \n" +
" def met1(self): \n" +
" pass \n" +
" \n" +
"class B: \n" +
" def met2(self): \n" +
" self.c = C()\n" +
" \n" +
" def met3(self): \n" +
" self.c.";
requestCompl(s, s.length(), -1, new String[] { "met1()"});
}
public void testProj2() throws CoreException, BadLocationException{
String s;
s = ""+
"import proj2root\n" +
"print proj2root.";
requestCompl(s, s.length(), -1, new String[] { "Proj2Root"}, nature2);
}
public void testProj2Global() throws CoreException, BadLocationException{
String s;
s = ""+
"import ";
requestCompl(s, s.length(), -1, new String[] { "proj2root", "testlib"}, nature2);
}
public void testClassAttrs() throws CoreException, BadLocationException{
String s;
s = ""+
"class A:\n" +
" aa, bb, cc = range(3)\n" + //the heuristic to find the attrs (class HeuristicFindAttrs) was not getting this
" dd = 1\n" +
" def m1(self):\n" +
" self.";
requestCompl(s, s.length(), -1, new String[] { "aa", "bb", "cc", "dd"});
}
public void testFromImport() throws CoreException, BadLocationException{
//TODO: see AbstractASTManager.resolveImport
String s;
s = ""+
"from testOtherImports.f3 import test\n" +
"tes";
ICompletionProposal[] p = requestCompl(s, s.length(), -1, new String[] { "test(a, b, c)"}, nature);
assertEquals(p[0].getAdditionalProposalInfo(), "This is a docstring");
}
public void testFromImportAs() throws CoreException, BadLocationException{
String s;
s = ""+
"from testOtherImports.f3 import test as AnotherTest\n" +
"t = AnotherTes";
ICompletionProposal[] p = requestCompl(s, s.length(), -1, new String[] { "AnotherTest(a, b, c)"}, nature);
assertEquals("This is a docstring", p[0].getAdditionalProposalInfo());
}
public void testFromImportAs2() throws CoreException, BadLocationException{
String s;
s = ""+
"from testOtherImports.f3 import Foo\n" +
"t = Fo";
ICompletionProposal[] p = requestCompl(s, s.length(), -1, new String[] { "Foo(a, b)"}, nature);
assertEquals("SomeOtherTest", p[0].getAdditionalProposalInfo());
}
public void testInnerImport() throws CoreException, BadLocationException{
String s;
s = "" +
"def m1():\n" +
" from testlib import unittest\n" +
" unittest.";
requestCompl(s, s.length(), -1, new String[]{
"AnotherTest"
, "GUITest"
, "main"
, "TestCase"
, "testcase"
, "TestCaseAlias"
//gotten because unittest is actually an __init__, so, gather others that are in the same level
, "anothertest"
, "guitestcase"
, "testcase"
});
}
public void testSelfReferenceWithTabs() throws CoreException, BadLocationException{
String s;
s = "class C:\n" +
" def met1(self):\n" +
" pass\n" +
" \n" +
"class B:\n" +
" def met2(self):\n" +
" self.c = C()\n" +
" \n" +
" def met3(self):\n" +
" self.c.";
s = s.replaceAll("\\ \\ \\ \\ ", "\t");
requestCompl(s, s.length(), -1, new String[] { "met1()"});
}
public void testClassCompl() throws CoreException, BadLocationException{
String s;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -