def test_parse_module(walk_mock, n_classes, n_functions): """Parses a module and creates a ModuleStructure""" module_node = create_module_node() class_nodes = [create_class_node() for _ in range(n_classes)] function_nodes = [create_function_node() for _ in range(n_functions)] nodes = [module_node] + class_nodes + function_nodes + [ create_node(), ] walk_mock.return_value = nodes with TempDirectory(file_suffixes=["module"]) as refs: library_structure = LibraryStructure(refs[0]) module_structure = library_structure.parse_module(refs[1][0]) assert module_structure.module == ParsedModule( module_node, library_structure.library_path) assert module_structure.classes == [ ParsedClass(node) for node in class_nodes ] assert module_structure.functions == [ ParsedFunction(node) for node in function_nodes ]
def create_module_node(**kwargs): """ Creates a module node """ return create_node(node_type=ast.Module, **kwargs)
def create_function_node(**kwargs): """ Creates a function node """ return create_node(node_type=ast.FunctionDef, **kwargs)
def create_class_node(**kwargs): """ Creates a class node """ return create_node(node_type=ast.ClassDef, **kwargs)
return create_node(node_type=ast.Module, **kwargs) def get_node_functions(node): """Returns the nodes that are functions in node.body""" return [ child_node for child_node in node.body if isinstance(child_node, ast.FunctionDef) ] # Typical function nodes FUNCTION_NODES = ( create_function_node(), create_function_node(name=""), create_function_node(name="FUNCTION"), create_function_node(body=(create_node(),)), create_function_node(body=(create_node(), create_node())), create_function_node(name="FUNCTION_WITH_BODY", body=(create_node(),)), ) CLASS_NODES = ( create_class_node(), create_class_node(name=""), create_class_node(name="CLASS"), create_class_node(body=(create_node(),)), create_class_node(body=FUNCTION_NODES[:1]), create_class_node(body=(create_node(),) + FUNCTION_NODES[:1]), create_class_node(body=FUNCTION_NODES[:3]), create_class_node(body=(create_node(),) + FUNCTION_NODES[:3]), create_class_node(body=FUNCTION_NODES[:]), create_class_node(body=(create_node(),) + FUNCTION_NODES[:]),