Created
October 29, 2024 18:20
-
-
Save rogigs/8acc0a9f85d2f586e265ebe907fb0432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from io import StringIO | |
import sys | |
from tree import TreeNode | |
def test_should_create_object_tree_node(): | |
# Expected | |
node = TreeNode("root") | |
# Assert | |
assert node.name == "root" | |
assert node._children == [] | |
assert node._url is None | |
def test_should_create_object_tree_node_and_change_private_attributes_url_and_children(): | |
# Expected | |
node = TreeNode("root") | |
#Act | |
node.add_child(TreeNode("child_1")) | |
node.add_url("http://www.example.com") | |
# Assert | |
assert node.name == "root" | |
assert all(isinstance(child, TreeNode) for child in node._children) | |
assert node._url == "http://www.example.com" | |
def test_should_add_child_return_an_exception_when_param_is_not_an_instance_tree_node(): | |
# Expected | |
node = TreeNode("root") | |
# Assert | |
with pytest.raises(ValueError, match="child_node is not a TreeNode"): | |
node.add_child("child_1") | |
def test_display(): | |
# Expected | |
root = TreeNode("root") | |
child1 = TreeNode("child_1") | |
child2 = TreeNode("child_2") | |
child3 = TreeNode("child_3") | |
#Act | |
root.add_child(child1) | |
root.add_child(child2) | |
child1.add_child(child3) | |
child2._url = "http://www.example.com/test" #TODO(fix):if url ends with / then no one value to link | |
captured_output = StringIO() | |
sys.stdout = captured_output | |
root.display() # Chamando o método display | |
sys.stdout = sys.__stdout__ # Resetando a saída padrão | |
# Assert | |
expected_output = "- root\n - child_1\n - child_3\n - [test](http://www.example.com/test)\n" | |
assert captured_output.getvalue() == expected_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeNode: | |
def __init__(self, name): | |
self.name = name | |
self._children = [] | |
self._url = None | |
def add_child(self, child_node): | |
if not isinstance(child_node, TreeNode): | |
raise ValueError("child_node is not a TreeNode") | |
self._children.append(child_node) | |
def add_url(self, url): | |
self._url = url | |
def display(self, level=0): | |
indent = " " * level * 2 | |
if self._url: | |
link_name = self._url.split('/')[-1] | |
print(f"{indent}- [{link_name}]({self._url})") | |
else: | |
print(f"{indent}- {self.name}") | |
for child in self._children: | |
child.display(level + 1) | |
def generate_markdown_by_paths(paths): | |
if paths is None: | |
raise ValueError("No path specified") | |
paths.sort() | |
root_nodes = {} | |
for path in paths: | |
path, url = path.split('/http', 1) | |
url = 'http' + url | |
nodes = path.split('/')[1:] | |
father = nodes[0] | |
children = nodes[1:] | |
if father not in root_nodes: | |
root_nodes[father] = TreeNode(father) | |
current_node = root_nodes[father] | |
for i, child in enumerate(children): | |
child_node = next((c for c in current_node._children if c.name == child), None) | |
if not child_node: | |
child_node = TreeNode(child) | |
current_node.add_child(child_node) | |
if i == len(children) - 1: | |
child_node.add_url(url) | |
current_node = child_node | |
for node in root_nodes.values(): | |
node.display() | |
generate_markdown_by_paths([ | |
"/file/imagens/google/http://www.google.com/search", | |
"/assets/imagens/stackoverflow/https://stackoverflow.com/questions", | |
"/file/repos/github/http://github.com/openai/chatgpt", | |
"/content/artigos/medium/https://medium.com/@username/sample-article", | |
"/media/tracks/spotify/https://open.spotify.com/track/song-title", | |
"/news/noticias/bbc/https://www.bbc.com/news/world", | |
"/tutorials/online/devto/https://dev.to/someauthor/how-to-code", | |
"/file/social/twitter/http://twitter.com/someuser/status/update", | |
"/apps/store/apple/http://apps.apple.com/app/details", | |
"/assets/imagens/google/https://www.google.com/imghp", | |
"/content/tutorials/youtube/https://www.youtube.com/user/tutorials", | |
"/file/news/bbc/http://www.bbc.com/sports", | |
"/apps/tools/github/https://github.com/cli/cli" | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment