def gets_the_names_of_base_classes(self): src = dedent(""" class root: def theMethod(): pass class a(root): def theMethod(): pass class b(root): pass class TheClass(a,b): def theMethod(): pass rootinstance = root() rootinstance.theMethod() """) module = fastparser(src) theclass = module.getChild("TheClass") assert_equal(['a','b'],theclass.getBaseClassNames()) theclass = module.getChild("a") assert_equal(['root'],theclass.getBaseClassNames()) theclass = module.getChild("root") assert_equal([],theclass.getBaseClassNames())
def finds_variable_attributes_assigned_to_in_the_function(self): src = dedent(""" def theFunction(): a.keyword = foo a.augassign += 'yeah' """) fn = fastparser(src).getChild('theFunction') matches = [a for a in fn.getAssignmentAttributesMatchingKeyword("keyword")] assert_equal(1,len(matches)) a, line, col = matches[0] assert_equal(3,line) assert_equal(6,col)
def can_generate_AssName_nodes_with_source_coordinates(self): src = dedent(""" def theFunction(): foo,keyword = 3 """) module = fastparser(src) fn = module.getChild('theFunction') nodes = [t for t in fn._generateASTNodesMatchingKeyword("keyword","AssName")] assert isinstance(nodes[0],compiler.ast.AssName) assert_equal("keyword",nodes[0].name) assert_equal(3,nodes[0].linenumber) assert_equal(8,nodes[0].column) assert_equal("keyword",nodes[0].text)
def can_generate_compiler_ast_nodes_matching_keywords(self): src = dedent(""" def theFunction(): fncall(somearg, 'someotherarg', keyword) """) module = fastparser(src) fn = module.getChild('theFunction') nodes = [t for t in fn._generateASTNodesMatchingKeyword("keyword",["CallFunc"])] assert isinstance(nodes[0],compiler.ast.CallFunc) assert_equal("keyword",nodes[0].args[2].name) nodes = [t for t in fn._generateASTNodesMatchingKeyword("keyword",["Name"])] assert_equal("keyword",nodes[0].name)
def can_still_get_the_end_line_when_the_function_has_empty_lines_in_it(self): src = dedent(""" class TheClass: def theFunction(): a = foo() print 'a' # end of class """) module = fastparser(src) fn = module.getChild('TheClass').getChild('theFunction') assert_equal(8,fn.getEndLine())
def test_returnsEndLineWithSimpleFunction(self): src = trimLines(""" class TheClass: def theMethod(): pass def foo(): b = TheClass() return b a = foo() a.theMethod() """) root = fastparser(src) fn = getTypeOf(root,"foo") self.assertEqual(fn.getEndLine(),7)
def can_get_the_line_number_corresponding_to_the_end_of_the_function(self): src = dedent(""" class TheClass: def theMethod(): pass def foo(): b = TheClass() return b a = foo() a.theMethod() """) m = fastparser(src) fn = m.getChild('foo') assert_equal(8,fn.getEndLine())
def finds_imports_made_in_the_function(self): src = dedent(""" import baz def theFunction(): import a.b.bah print a.b.bah.mything() """) module = fastparser(src) fn = module.getChild('theFunction') imports = fn.getImports() assert_equal(1,len(imports)) module,name,alias = imports[0] assert_equal("a.b.bah",module) assert_equal(None,name) assert_equal(None,alias)
def finds_variables_created_in_the_function(self): src = dedent(""" def theFunction(fnarg): # fn arg is a new variable from mymodule import yadda as newalias # alias is a new var keyword = foo # basic name c,foobah = bah() # assignment tuple augass += 3 # created, since a = a + 3 (the a is new in the scope) """) fn = fastparser(src).getChild('theFunction') matches = [node for node in fn.getVariablesAssignedInScope()] assert_equal(6,len(matches)) # 6 (count em!) variables created in the scope matches = [node for node in fn.getVariablesAssignedInScope(keyword="keyword")] assert_equal(1,len(matches)) name, line, col, node = matches[0] assert_equal("keyword",name) assert_equal(4,line) assert_equal(4,col) matches = [node for node in fn.getVariablesAssignedInScope(keyword="foobah")] assert_equal(1,len(matches)) name, line, col, node = matches[0] assert_equal("foobah",name) assert_equal(5,line) assert_equal(6,col) matches = [node for node in fn.getVariablesAssignedInScope(keyword="augass")] assert_equal(1,len(matches)) name, line, col, node = matches[0] assert_equal("augass",name) assert_equal(6,line) assert_equal(4,col) matches = [node for node in fn.getVariablesAssignedInScope(keyword="newalias")] assert_equal(1,len(matches)) name, line, col, node = matches[0] assert_equal("newalias",name) assert_equal(3,line) assert_equal(34,col) matches = [node for node in fn.getVariablesAssignedInScope(keyword="fnarg")] assert_equal(1,len(matches)) name, line, col, node = matches[0] assert_equal("fnarg",name) assert_equal(2,line) assert_equal(16,col)
def can_generate_compiler_ast_nodes_with_source_coordinates(self): src = dedent(""" def theFunction(): fncall(somearg, 'someotherarg', keyword) """) module = fastparser(src) fn = module.getChild('theFunction') nodes = [t for t in fn._generateASTNodesMatchingKeyword("keyword",["CallFunc"])] assert isinstance(nodes[0],compiler.ast.CallFunc) assert_equal(3,nodes[0].linenumber) assert_equal(4,nodes[0].column) nodes = [t for t in fn._generateASTNodesMatchingKeyword("keyword",["Name"])] assert isinstance(nodes[0],compiler.ast.Name) assert_equal("keyword",nodes[0].name) assert_equal(4,nodes[0].linenumber) assert_equal(27,nodes[0].column)
def can_generate_compiler_ast_nodes_matching_keywords(self): src = dedent(""" def theFunction(): fncall(somearg, 'someotherarg', keyword) """) module = fastparser(src) fn = module.getChild('theFunction') nodes = [ t for t in fn._generateASTNodesMatchingKeyword( "keyword", ["CallFunc"]) ] assert isinstance(nodes[0], compiler.ast.CallFunc) assert_equal("keyword", nodes[0].args[2].name) nodes = [ t for t in fn._generateASTNodesMatchingKeyword("keyword", ["Name"]) ] assert_equal("keyword", nodes[0].name)
def gets_function_arguments(self): src = dedent(""" def theFunction(foo,bah,baz=a.b.c,**kwargs): pass """) fn = fastparser(src).getChild('theFunction') args = [a for a in fn.getArguments()] arg, line, col, defaultExpr = args[0] assert_equal("foo", arg) assert_equal(2, line) assert_equal(16, col) arg, line, col, defaultExpr = args[2] assert_equal("baz", arg) assert_equal(2, line) assert_equal(24, col) assert isinstance(defaultExpr, compiler.ast.Getattr) arg, line, col, defaultExpr = args[3] assert_equal("**kwargs", arg)
def gets_function_arguments(self): src = dedent(""" def theFunction(foo,bah,baz=a.b.c,**kwargs): pass """) fn = fastparser(src).getChild('theFunction') args = [a for a in fn.getArguments()] arg,line,col,defaultExpr = args[0] assert_equal("foo",arg) assert_equal(2,line) assert_equal(16,col) arg,line,col,defaultExpr = args[2] assert_equal("baz",arg) assert_equal(2,line) assert_equal(24,col) assert isinstance(defaultExpr,compiler.ast.Getattr) arg,line,col,defaultExpr = args[3] assert_equal("**kwargs",arg)
def can_generate_compiler_ast_nodes_with_source_coordinates(self): src = dedent(""" def theFunction(): fncall(somearg, 'someotherarg', keyword) """) module = fastparser(src) fn = module.getChild('theFunction') nodes = [ t for t in fn._generateASTNodesMatchingKeyword( "keyword", ["CallFunc"]) ] assert isinstance(nodes[0], compiler.ast.CallFunc) assert_equal(3, nodes[0].linenumber) assert_equal(4, nodes[0].column) nodes = [ t for t in fn._generateASTNodesMatchingKeyword("keyword", ["Name"]) ] assert isinstance(nodes[0], compiler.ast.Name) assert_equal("keyword", nodes[0].name) assert_equal(4, nodes[0].linenumber) assert_equal(27, nodes[0].column)
def test_worksWithFunctionsThatHaveEmptyLinesInThem(self): src = fnWithEmptyLineInIt root = fastparser(src) fn = getTypeOf(root, "TheClass.theFunction") self.assertEqual(fn.getEndLine(), 8)
def test_worksWithFunctionsThatHaveEmptyLinesInThem(self): src = fnWithEmptyLineInIt root = fastparser(src) fn = getTypeOf(root,"TheClass.theFunction") self.assertEqual(fn.getEndLine(),8)