📄 findscopevisitortest.java
字号:
/*
* Created on Jul 10, 2006
* @author Fabio
*/
package org.python.pydev.editor.codecompletion.revisited.visitors;
import java.util.List;
import org.python.pydev.core.ILocalScope;
import org.python.pydev.parser.PyParserTestBase;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.plugin.PydevPlugin;
public class FindScopeVisitorTest extends PyParserTestBase {
public static void main(String[] args) {
try{
FindScopeVisitorTest test = new FindScopeVisitorTest();
test.setUp();
test.testFindAssertInLocalScope2();
test.tearDown();
junit.textui.TestRunner.run(FindScopeVisitorTest.class);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* This method tries to find a local scope given some code, the current line and col
*
* @param s the code
* @param line starts at 1
* @param col starts at 1
* @return the local scope found
*/
private ILocalScope findLocalScope(String s, int line, int col) {
SimpleNode ast = parseLegalDocStr(s);
FindScopeVisitor scopeVisitor = new FindScopeVisitor(line, col);
if (ast != null){
try {
ast.accept(scopeVisitor);
} catch (Exception e) {
PydevPlugin.log(e);
}
}
ILocalScope localScope = scopeVisitor.scope;
return localScope;
}
public void testFindLocalScope() throws Exception {
String s = "" +
"#file mod3.py \n" + //line = 1 (in ast)
"class SomeA(object):\n" +
" def fun(self):\n" +
" pass\n" +
" \n" +
"class C1(object):\n" +
" a = SomeA() #yes, these are class-defined\n" +
" \n" +
" def someFunct(self):\n" +
" pass\n" +
" \n" +
"\n" +
"";
ILocalScope localScope = findLocalScope(s, 8, 3);
assertTrue(localScope.getClassDef() != null);
}
public void testFindAssertInLocalScope() throws Exception {
String s =
"def m1(a):\n" +
" assert isinstance(a, str)\n" +
" ";
ILocalScope localScope = findLocalScope(s, 2, 1);
List<String> found = localScope.getPossibleClassesForActivationToken("a");
assertEquals(1, found.size());
assertEquals("str", found.get(0));
}
public void testFindAssertInLocalScope2() throws Exception {
String s =
"def m1(a):\n" +
" assert isinstance(a, (list, tuple))\n" +
" ";
ILocalScope localScope = findLocalScope(s, 2, 1);
List<String> found = localScope.getPossibleClassesForActivationToken("a");
assertEquals(2, found.size());
assertEquals("list", found.get(0));
assertEquals("tuple", found.get(1));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -