def test_serialize(node): """Serialized returns the expected dict""" obj = ParsedModule(node, DEFAULT_PATH) serialized = obj.serialize() expected = { "name": DEFAULT_NAME, "docstring": ast.get_docstring(node), } assert isinstance(serialized, dict) assert serialized == expected
def parse_module(self, node): """ Parses an ast node creating a ParsedModule object. Adds the module to the references :param node: ast node :returns ParsedModule """ self.module = ParsedModule(node, self.path) self.parsed_references.append(node)
def test_init(node, path_prefix, path, name): """__init__ sets all the expected properties""" module_path = path_prefix + path obj = ParsedModule(node, module_path) assert obj.node == node assert obj.name == name assert obj.path == module_path assert obj.docstring == ast.get_docstring(node)
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 test_eq(node): """__eq__ should be defined""" obj1 = ParsedModule(node, DEFAULT_PATH) obj2 = ParsedModule(node, DEFAULT_PATH) assert id(obj1) != id(obj2) assert obj1 == obj2
def test_str(node): """__str__ should be defined""" obj = ParsedModule(node, DEFAULT_PATH) obj_str = str_should_be_defined(obj) assert obj_str == f"Module at: '{DEFAULT_PATH}'"
def test_repr(node): """__repr__ should be defined""" obj = ParsedModule(node, DEFAULT_PATH) obj_repr = repr_should_be_defined(obj) assert obj_repr == f"<Module at: '{DEFAULT_PATH}'>"
class ModuleStructure(CodeStructure): """ Code structure of a Python module """ def __init__(self, module_path): """ Stores information about the structure of a module :param module_path: str - Filepath of the python module """ self.name = get_python_module_name_from_path(module_path) self.path = module_path self.module = None self.classes = [] self.functions = [] self.parsed_references = [] def parse_module(self, node): """ Parses an ast node creating a ParsedModule object. Adds the module to the references :param node: ast node :returns ParsedModule """ self.module = ParsedModule(node, self.path) self.parsed_references.append(node) def parse_class(self, node): """ Parses an ast node creating a ParsedClass object. Adds the class and its methods to the references :param node: ast node :returns ParsedModule """ if not node in self.parsed_references: parsed_class = ParsedClass(node) self.classes.append(parsed_class) self.parsed_references.append(node) self.parsed_references += [ func.node for func in parsed_class.functions ] def parse_function(self, node): """ Parses an ast node creating a ParsedFunction object. Adds the function to the references :param node: ast node :returns ParsedFunction """ if not node in self.parsed_references: parsed_function = ParsedFunction(node) self.functions.append(parsed_function) self.parsed_references.append(node) def serialize(self): """ Serializes the library information in a dict :returns: dict """ return { "module": self.module.serialize() if self.module else None, "classes": [parsed_class.serialize() for parsed_class in self.classes], "functions": [ parsed_function.serialize() for parsed_function in self.functions ], } def __str__(self): """ String representation of the instance :returns: str """ return "Module '{name}' structure".format(name=self.name, )
def test_parse_module(node): """Checks the module is parsed and added to the references""" structure = ModuleStructure(DEFAULT_PATH) structure.parse_module(node) assert structure.module == ParsedModule(node, DEFAULT_PATH) assert structure.parsed_references == [node]