def test_dir_with_two_nonempty_py(self): directory = "/home/kemri/Projects/pyrser/test_files/test_dir4" dirnode = DirNode(directory, "test_dir4") filenode = FileNode(directory + "/test1.py", "test1.py") filenode.scope = [1, 3] fncnode = FncNode(directory + "/test1.py", "test") fncnode.parent = filenode fncnode.scope = [1, 3] filenode.add_child(fncnode) filenode.parent = dirnode dirnode.add_child(filenode) filenode2 = FileNode(directory + "/test2.py", "test2.py") filenode2.scope = [1, 3] fncnode2 = FncNode(directory + "/test2.py", "test") fncnode2.parent = filenode2 fncnode2.scope = [1, 3] filenode2.add_child(fncnode2) filenode2.parent = dirnode dirnode.add_child(filenode2) gold = dirnode output = pyrser(directory) self.assertEqual(output, gold)
def test_dir_within_dir(self): directory = "/home/kemri/Projects/pyrser/test_files/test_dir5" dirnode = DirNode(directory, "test_dir5") nested_dir = DirNode(directory + "/test_dir3", "test_dir3") nested_dir.parent = dirnode filenode = FileNode(directory + "/test_dir3" + "/test1.py", "test1.py") filenode.scope = [1, 3] fncnode = FncNode(directory + "/test_dir3" + "/test1.py", "test") fncnode.parent = filenode fncnode.scope = [1, 3] filenode.add_child(fncnode) filenode.parent = nested_dir nested_dir.add_child(filenode) dirnode.add_child(nested_dir) gold = dirnode output = pyrser(directory) self.assertEqual(output, gold)
def dir_parser(path: str) -> Node: name = fh.get_file_name_from_path(path) dirnode = DirNode(path, name) for f in fh.list_dir(path): if fh.nonempty_pyfile(f): f_name = fh.get_file_name_from_path(f) lines = fh.reader(f) length = len(lines) - 1 output, _ = file_parser( FileNode, f, f_name, lines, length) # TODO: eliminate useless second return value output.parent = dirnode dirnode.add_child(output) if fh.is_dir(f): child_dir = dir_parser(f) child_dir.parent = dirnode dirnode.add_child(child_dir) return dirnode