| 8 | |
| 9 | |
| 10 | class Test_PythonParser(unittest.TestCase): |
| 11 | def setUp(self) -> None: |
| 12 | with open('tests/test_parser/test_sample/py_test_sample.py', 'r') as file: |
| 13 | self.code_sample = file.read() |
| 14 | |
| 15 | tree = parse_code(self.code_sample, 'python') |
| 16 | self.root_node = tree.root_node |
| 17 | return super().setUp() |
| 18 | |
| 19 | def test_get_function_list(self): |
| 20 | root = self.root_node |
| 21 | |
| 22 | function_list = PythonParser.get_function_list(root) |
| 23 | |
| 24 | self.assertEqual(len(function_list), 3) |
| 25 | |
| 26 | def test_get_class_list(self): |
| 27 | root = self.root_node |
| 28 | |
| 29 | class_list = PythonParser.get_class_list(root) |
| 30 | self.assertEqual(len(class_list), 1) |
| 31 | |
| 32 | def test_get_docstring(self): |
| 33 | code_sample = ''' |
| 34 | def test_sample(): |
| 35 | """This is a docstring""" |
| 36 | return |
| 37 | ''' |
| 38 | root = parse_code(code_sample, 'python').root_node |
| 39 | |
| 40 | function = PythonParser.get_function_list(root)[0] |
| 41 | docstring = PythonParser.get_docstring(function) |
| 42 | self.assertEqual(docstring, "This is a docstring") |
| 43 | |
| 44 | def test_get_function_metadata(self): |
| 45 | code_sample = ''' |
| 46 | def test_sample(arg1: str = "string", arg2 = "another_string"): |
| 47 | return NotImplement() |
| 48 | ''' |
| 49 | root = parse_code(code_sample, 'python').root_node |
| 50 | |
| 51 | function = list(PythonParser.get_function_list(root))[0] |
| 52 | metadata = PythonParser.get_function_metadata(function) |
| 53 | |
| 54 | for key in ['identifier', 'parameters', 'return_type']: |
| 55 | self.assertTrue(key in metadata.keys()) |
| 56 | self.assertEqual(metadata['parameters'], {'arg1': 'str', 'arg2': None}) |
| 57 | self.assertEqual(metadata['identifier'], 'test_sample') |
| 58 | |
| 59 | def test_get_class_metadata(self): |
| 60 | code_sample = ''' |
| 61 | class ABC(): |
| 62 | pass |
| 63 | |
| 64 | class Sample(ABC): |
| 65 | def __init__(self): |
| 66 | pass |
| 67 |
nothing calls this directly
no outgoing calls
no test coverage detected