Example #1
0
 def test_init(self):
     analyzer = ClassAnalyzer()
     self.assertEqual(analyzer.base_nodes, [])
     self.assertEqual(analyzer.decorator_nodes, [])
     self.assertEqual(analyzer.method_nodes, [])
     self.assertEqual(analyzer.attribute_nodes, [])
     analyzer.generic_visit("node")
Example #2
0
 def test_visit_AsyncFunctionDef(self):
     analyzer = ClassAnalyzer()
     node = MagicMock()
     node.mock_add_spec(ast.FunctionDef)
     node.name = "FunctionDef"
     node.body = ["body_node"]
     self.assertIsNone(analyzer.visit_AsyncFunctionDef(node))
     self.assertEqual(analyzer.method_nodes, [node])
Example #3
0
 def test_visit_ClassDef(self, visit_mock):
     analyzer = ClassAnalyzer()
     node = MagicMock()
     node.decorator_list = ["decorator"]
     node.bases = ["base", "base2"]
     node.body = ["body1", "body2"]
     self.assertIsNone(analyzer.visit_ClassDef(node))
     self.assertEqual(analyzer.decorator_nodes, ["decorator"])
     self.assertEqual(analyzer.base_nodes, ["base", "base2"])
     visit_mock.assert_any_call("body1")
     visit_mock.assert_any_call("body2")
Example #4
0
    def test_visit_FunctionDef(self):
        analyzer = ClassAnalyzer()
        node = MagicMock()
        node.mock_add_spec(ast.FunctionDef)
        node.name = "FunctionDef"
        node.body = ["body_node"]
        self.assertIsNone(analyzer.visit_FunctionDef(node))
        self.assertEqual(analyzer.method_nodes, [node])

        node.name = "_private_method"
        self.assertIsNone(analyzer.visit_FunctionDef(node))
        self.assertEqual(len(analyzer.method_nodes), 1)

        node.name = "__magic_method__"
        self.assertIsNone(analyzer.visit_FunctionDef(node))
        self.assertEqual(len(analyzer.method_nodes), 1)
Example #5
0
    def _parse(self) -> None:
        analyzer = ClassAnalyzer()
        analyzer.visit(self.node)

        for method_node in analyzer.method_nodes:
            self.method_records.append(FunctionRecord(method_node, is_method=True))

        for base_node in analyzer.base_nodes:
            self.base_records.append(ExpressionRecord(base_node))

        for decorator_node in analyzer.decorator_nodes:
            self.decorator_records.append(ExpressionRecord(decorator_node))

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

        self.method_records.sort(key=lambda x: x.name)
Example #6
0
    def test_visit_Assign(self):
        analyzer = ClassAnalyzer()
        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(analyzer.attribute_nodes, [node])

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

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

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