Example #1
0
    def test_visit_AsyncFunctionDef(self):
        analyzer = ModuleAnalyzer()
        node = MagicMock()
        node.name = "my_func"
        self.assertIsNone(analyzer.visit_AsyncFunctionDef(node))
        self.assertEqual(len(analyzer.function_nodes), 1)
        self.assertEqual(analyzer.function_nodes[0], node)

        node.name = "_private_func"
        self.assertIsNone(analyzer.visit_AsyncFunctionDef(node))
        self.assertEqual(len(analyzer.function_nodes), 1)
Example #2
0
    def test_visit_ClassDef(self):
        analyzer = ModuleAnalyzer()
        node = MagicMock()
        node.name = "MyClass"
        self.assertIsNone(analyzer.visit_ClassDef(node))
        self.assertEqual(len(analyzer.class_nodes), 1)
        self.assertEqual(analyzer.class_nodes[0], node)

        node.name = "_PrivateClass"
        self.assertIsNone(analyzer.visit_ClassDef(node))
        self.assertEqual(len(analyzer.class_nodes), 1)
Example #3
0
 def test_init(self):
     analyzer = ModuleAnalyzer()
     self.assertEqual(analyzer.all_names, [])
     self.assertEqual(analyzer.import_nodes, [])
     self.assertEqual(analyzer.function_nodes, [])
     self.assertEqual(analyzer.attribute_nodes, [])
     self.assertEqual(analyzer.class_nodes, [])
Example #4
0
    def build_children(self):
        # type: () -> None
        """
		Collect full information about Module child records.

		Used only when doc for this ModuleRecord is building.
		"""
        analyzer = ModuleAnalyzer()
        analyzer.visit(self.node)

        self.all_names = analyzer.all_names

        for class_node in analyzer.class_nodes:
            self.class_records.append(ClassRecord(class_node))

        for function_node in analyzer.function_nodes:
            self.function_records.append(
                FunctionRecord(function_node, is_method=False))

        for attribute_node in analyzer.attribute_nodes:
            self.attribute_records.append(AttributeRecord(attribute_node))

        for import_node in analyzer.import_nodes:
            for alias in import_node.names:
                self.import_records.append(ImportRecord(import_node, alias))

        self.class_records.sort(key=lambda x: x.name)
        self.function_records.sort(key=lambda x: x.name)

        main_class_lookup_name = self.name.replace("_", "")
        for class_record in self.class_records:
            class_record.parse()
            # find real title
            if class_record.name.lower() == main_class_lookup_name:
                self.title = class_record.name

        self._set_import_strings()
Example #5
0
    def test_visit_Assign(self):
        analyzer = ModuleAnalyzer()
        node = MagicMock()
        node.mock_add_spec(ast.Assign)
        node.value = "value"
        target = MagicMock()
        target.mock_add_spec(ast.Name)
        target.id = "attr"
        node.targets = [target]
        self.assertIsNone(analyzer.visit_Assign(node))
        self.assertEqual(len(analyzer.attribute_nodes), 1)
        self.assertEqual(analyzer.attribute_nodes[0], node)

        node.targets = [target, target]
        self.assertIsNone(analyzer.visit_Assign(node))
        self.assertEqual(len(analyzer.attribute_nodes), 1)

        node.targets = ["not_name_target"]
        self.assertIsNone(analyzer.visit_Assign(node))
        self.assertEqual(len(analyzer.attribute_nodes), 1)

        target.id = "_private_attr"
        node.targets = [target]
        self.assertIsNone(analyzer.visit_Assign(node))
        self.assertEqual(len(analyzer.attribute_nodes), 1)

        target.id = "__all__"
        node.targets = [target]
        name_1 = MagicMock()
        name_1.mock_add_spec(ast.Str)
        name_1.s = "MyClass"
        name_2 = MagicMock()
        name_2.mock_add_spec(ast.Str)
        name_2.s = b"my_func"
        value = MagicMock()
        value.mock_add_spec(ast.List)
        value.elts = [name_1, name_2, "not_name"]
        node.value = value
        self.assertIsNone(analyzer.visit_Assign(node))
        self.assertEqual(len(analyzer.attribute_nodes), 1)
        self.assertEqual(analyzer.all_names, ["MyClass", "my_func"])
Example #6
0
 def test_visit_ImportFrom(self):
     analyzer = ModuleAnalyzer()
     node = "import_from_node"
     self.assertIsNone(analyzer.visit_ImportFrom(node))
     self.assertEqual(len(analyzer.import_nodes), 1)
     self.assertEqual(analyzer.import_nodes[0], "import_from_node")