Example #1
0
def test_read_functions(node):
    """Reads the functions and generates ParsedFunctions"""
    obj = ParsedClass(node)
    expected_parsed_functions = [
        ParsedFunction(function_node)
        for function_node in get_node_functions(node)
    ]

    assert obj.read_functions(node.body) == expected_parsed_functions
    assert obj.functions == expected_parsed_functions
Example #2
0
def test_serialize(node):
    """Serialized returns the expected dict"""
    obj = ParsedClass(node)
    serialized = obj.serialize()
    expected = {
        "name": node.name,
        "docstring": ast.get_docstring(node),
        "methods": [func.serialize() for func in obj.functions],
    }
    assert isinstance(serialized, dict)
    assert serialized == expected
Example #3
0
def test_init(node):
    """__init__ sets all the expected properties"""
    obj = ParsedClass(node)
    assert obj.node == node
    assert obj.docstring == ast.get_docstring(node)
    assert obj.name == node.name
    assert len(obj.functions) == len(get_node_functions(node))
def test_parse_class(node):
    """Checks the class and its methods are parsed and added to the references"""
    structure = ModuleStructure(DEFAULT_PATH)
    structure.parse_class(node)
    expected_references = [node] + get_node_functions(node)
    assert structure.classes == [ParsedClass(node)]
    assert structure.functions == []
    assert structure.parsed_references == expected_references
Example #5
0
    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
            ]
Example #6
0
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
        ]
Example #7
0
def test_eq(node):
    """__eq__ should be defined"""
    obj1 = ParsedClass(node)
    obj2 = ParsedClass(node)
    assert id(obj1) != id(obj2)
    assert obj1 == obj2
Example #8
0
def test_str(node):
    """__str__ should be defined"""
    obj = ParsedClass(node)
    obj_str = str_should_be_defined(obj)
    assert obj_str == f"Class: '{node.name}'"
Example #9
0
def test_repr(node):
    """__repr__ should be defined"""
    obj = ParsedClass(node)
    obj_repr = repr_should_be_defined(obj)
    assert obj_repr == f"<Class: '{node.name}'>"