Skip to content

Commit 241b9c3

Browse files
committed
update
1 parent 05b4f53 commit 241b9c3

File tree

7 files changed

+181
-78
lines changed

7 files changed

+181
-78
lines changed

visualize/directed_graph.svg

Lines changed: 2 additions & 0 deletions
Loading

visualize/graphics.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ class Line(GraphicObject):
1919
def __init__(self, start, end, **kwargs):
2020
super().__init__("line", **kwargs)
2121
self.start = start
22-
self.end = end
22+
self.end = end
23+
24+
class Arrow(GraphicObject):
25+
def __init__(self, start, end, **kwargs):
26+
super().__init__("arrow", **kwargs)
27+
self.start = start
28+
self.end = end
29+
30+
class Text(GraphicObject):
31+
def __init__(self, text, position, **kwargs):
32+
super().__init__("text", **kwargs)
33+
self.text = text
34+
self.position = position

visualize/test.svg

Lines changed: 1 addition & 1 deletion
Loading

visualize/tests/test_graphics.py

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,116 @@
11
import unittest
22

3-
from graphics import Circle, Rectangle, Line
3+
from graphics import Circle, Rectangle, Line, Arrow, Text
4+
45

56
class TestCircle(unittest.TestCase):
67
def test_init(self):
7-
circle = Circle((0, 0), 3, color='red', filled=True)
8-
self.assertEqual(circle.type, 'circle')
8+
circle = Circle((0, 0), 3, color="red", filled=True)
9+
self.assertEqual(circle.type, "circle")
910
self.assertEqual(circle.center, (0, 0))
1011
self.assertEqual(circle.radius, 3)
11-
self.assertEqual(circle.kwargs, {'color': 'red', 'filled': True})
12+
self.assertEqual(circle.kwargs, {"color": "red", "filled": True})
1213

1314
def test_center_as_float(self):
14-
circle = Circle((0.5, 0.5), 3, color='red', filled=True)
15+
circle = Circle((0.5, 0.5), 3, color="red", filled=True)
1516
self.assertEqual(circle.center, (0.5, 0.5))
1617

1718
def test_negative_radius(self):
18-
circle = Circle((0, 0), -3, color='red', filled=True)
19+
circle = Circle((0, 0), -3, color="red", filled=True)
1920
self.assertEqual(circle.radius, -3)
2021

2122
def test_color_as_none(self):
2223
circle = Circle((0, 0), 3, color=None, filled=True)
23-
self.assertEqual(circle.kwargs, {'color': None, 'filled': True})
24+
self.assertEqual(circle.kwargs, {"color": None, "filled": True})
2425

2526
def test_additional_kwargs(self):
26-
circle = Circle((0, 0), 3, color='red', filled=True, linewidth=2)
27-
self.assertEqual(circle.kwargs, {'color': 'red', 'filled': True, 'linewidth': 2})
27+
circle = Circle((0, 0), 3, color="red", filled=True, linewidth=2)
28+
self.assertEqual(
29+
circle.kwargs, {"color": "red", "filled": True, "linewidth": 2}
30+
)
31+
2832

2933
class TestRectangle(unittest.TestCase):
3034
def test_init(self):
31-
rectangle = Rectangle((0, 0), (3, 4), color='blue', filled=False)
32-
self.assertEqual(rectangle.type, 'rectangle')
35+
rectangle = Rectangle((0, 0), (3, 4), color="blue", filled=False)
36+
self.assertEqual(rectangle.type, "rectangle")
3337
self.assertEqual(rectangle.top_left, (0, 0))
3438
self.assertEqual(rectangle.bottom_right, (3, 4))
35-
self.assertEqual(rectangle.kwargs, {'color': 'blue', 'filled': False})
39+
self.assertEqual(rectangle.kwargs, {"color": "blue", "filled": False})
3640

3741
def test_coordinates_as_float(self):
38-
rectangle = Rectangle((0.5, 0.5), (3.5, 4.5), color='blue', filled=False)
42+
rectangle = Rectangle((0.5, 0.5), (3.5, 4.5), color="blue", filled=False)
3943
self.assertEqual(rectangle.top_left, (0.5, 0.5))
4044
self.assertEqual(rectangle.bottom_right, (3.5, 4.5))
4145

4246
def test_negative_coordinates(self):
43-
rectangle = Rectangle((-1, -1), (-2, -2), color='blue', filled=False)
47+
rectangle = Rectangle((-1, -1), (-2, -2), color="blue", filled=False)
4448
self.assertEqual(rectangle.top_left, (-1, -1))
4549
self.assertEqual(rectangle.bottom_right, (-2, -2))
4650

4751
def test_color_as_none(self):
4852
rectangle = Rectangle((0, 0), (3, 4), color=None, filled=False)
49-
self.assertEqual(rectangle.kwargs, {'color': None, 'filled': False})
53+
self.assertEqual(rectangle.kwargs, {"color": None, "filled": False})
5054

5155
def test_additional_kwargs(self):
52-
rectangle = Rectangle((0, 0), (3, 4), color='blue', filled=False, linewidth=2)
53-
self.assertEqual(rectangle.kwargs, {'color': 'blue', 'filled': False, 'linewidth': 2})
56+
rectangle = Rectangle((0, 0), (3, 4), color="blue", filled=False, linewidth=2)
57+
self.assertEqual(
58+
rectangle.kwargs, {"color": "blue", "filled": False, "linewidth": 2}
59+
)
60+
5461

5562
class TestLine(unittest.TestCase):
5663
def test_init(self):
57-
line = Line((0, 0), (3, 4), color='green', dashed=True)
58-
self.assertEqual(line.type, 'line')
64+
line = Line((0, 0), (3, 4), color="green", dashed=True)
65+
self.assertEqual(line.type, "line")
5966
self.assertEqual(line.start, (0, 0))
6067
self.assertEqual(line.end, (3, 4))
61-
self.assertEqual(line.kwargs, {'color': 'green', 'dashed': True})
68+
self.assertEqual(line.kwargs, {"color": "green", "dashed": True})
6269

6370
def test_coordinates_as_float(self):
64-
line = Line((0.5, 0.5), (3.5, 4.5), color='green', dashed=True)
71+
line = Line((0.5, 0.5), (3.5, 4.5), color="green", dashed=True)
6572
self.assertEqual(line.start, (0.5, 0.5))
6673
self.assertEqual(line.end, (3.5, 4.5))
6774

6875
def test_negative_coordinates(self):
69-
line = Line((-1, -1), (-2, -2), color='green', dashed=True)
76+
line = Line((-1, -1), (-2, -2), color="green", dashed=True)
7077
self.assertEqual(line.start, (-1, -1))
7178
self.assertEqual(line.end, (-2, -2))
7279

7380
def test_color_as_none(self):
7481
line = Line((0, 0), (3, 4), color=None, dashed=True)
75-
self.assertEqual(line.kwargs, {'color': None, 'dashed': True})
82+
self.assertEqual(line.kwargs, {"color": None, "dashed": True})
7683

7784
def test_additional_kwargs(self):
78-
line = Line((0, 0), (3, 4), color='green', dashed=True, linewidth=2)
79-
self.assertEqual(line.kwargs, {'color': 'green', 'dashed': True, 'linewidth': 2})
80-
81-
if __name__ == '__main__':
82-
unittest.main()
85+
line = Line((0, 0), (3, 4), color="green", dashed=True, linewidth=2)
86+
self.assertEqual(
87+
line.kwargs, {"color": "green", "dashed": True, "linewidth": 2}
88+
)
89+
90+
91+
class TestArrowObject(unittest.TestCase):
92+
def test_arrow_object(self):
93+
# Create an ArrowObject instance
94+
start = (0, 0)
95+
end = (100, 100)
96+
arrow = Arrow(start, end, color="red", head_width=10, head_length=20)
97+
98+
# Test that the ArrowObject was created with the correct attributes
99+
self.assertEqual(arrow.type, "arrow")
100+
self.assertEqual(arrow.start, start)
101+
self.assertEqual(arrow.end, end)
102+
self.assertEqual(
103+
arrow.kwargs, {"color": "red", "head_width": 10, "head_length": 20}
104+
)
105+
106+
107+
class TestTextObject(unittest.TestCase):
108+
def test_text_object(self):
109+
text = Text(text="Hello", position=(100, 100))
110+
self.assertEqual(text.type, "text")
111+
self.assertEqual(text.position, (100, 100))
112+
self.assertEqual(text.text, "Hello")
113+
114+
115+
if __name__ == "__main__":
116+
unittest.main()

visualize/tests/test_visualizer.py

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,93 @@
11
import unittest
2-
from graphics import Circle, Rectangle, Line
2+
from graphics import Circle, Line, Arrow
33
from graph import Graph
4-
from graphics import GraphicObject
54
from visualizer import Visualizer, SvgVisualizer
65
import svgwrite
76
import os
87

98

109
class TestVisualizer(unittest.TestCase):
11-
def test_visualize(self):
12-
# Create a graph with two nodes and one edge
10+
def test_visualize_undirected(self):
11+
# Create an undirected graph with 3 nodes and 2 edges
1312
graph = Graph()
1413
graph.add_edge(1, 2)
15-
16-
# Create an instance of Visualizer
14+
graph.add_edge(2, 3)
15+
# Ensure the correct GraphicObject instances are returned
1716
visualizer = Visualizer()
18-
19-
# Call the visualize method and store the result in a variable
2017
graphic_objects = visualizer.visualize(graph)
21-
22-
self.assertEqual(len(graphic_objects), 4)
23-
24-
# Check that the first element is a Circle
18+
self.assertEqual(len(graphic_objects), 5)
2519
self.assertIsInstance(graphic_objects[0], Circle)
26-
27-
# Check that the second element is a Line
28-
self.assertIsInstance(graphic_objects[3], Line)
20+
self.assertIsInstance(graphic_objects[1], Circle)
21+
self.assertIsInstance(graphic_objects[4], Line)
22+
23+
def test_visualize_directed(self):
24+
# Create a directed graph with 3 nodes and 2 edges
25+
graph = Graph(directed=True)
26+
graph.add_edge(1, 2)
27+
graph.add_edge(2, 3)
28+
# Ensure the correct GraphicObject instances are returned
29+
visualizer = SvgVisualizer()
30+
graphic_objects = visualizer.visualize(graph)
31+
self.assertEqual(len(graphic_objects), 5)
32+
self.assertIsInstance(graphic_objects[0], Circle)
33+
self.assertIsInstance(graphic_objects[1], Circle)
34+
self.assertIsInstance(graphic_objects[4], Arrow)
2935

3036

3137
class TestSvgVisualizer(unittest.TestCase):
3238
def setUp(self):
3339
self.skip_tear_down = False
34-
self.filepath = ""
40+
self.directed_filepath = "directed_graph.svg"
41+
self.undirected_filepath = "undirected_graph.svg"
42+
undirected_graph = Graph()
43+
undirected_graph.add_edge(1, 2)
44+
undirected_graph.add_edge(2, 3)
45+
undirected_graph.add_edge(3, 4)
46+
self.undirected_graph = undirected_graph
47+
48+
directed_graph = Graph(directed=True)
49+
directed_graph.add_edge(1, 2)
50+
directed_graph.add_edge(2, 3)
51+
directed_graph.add_edge(3, 4)
52+
self.directed_graph = directed_graph
53+
54+
3555

3656
def tearDown(self):
3757
if self.skip_tear_down:
3858
return
39-
# Delete the file created by the test
40-
try:
41-
os.remove(self.filepath)
42-
except FileNotFoundError:
43-
pass
59+
# Delete the files created by the test
60+
for filepath in [self.directed_filepath, self.undirected_filepath]:
61+
try:
62+
os.remove(filepath)
63+
except FileNotFoundError:
64+
pass
4465

4566
def test_draw(self):
46-
# Create a Graph object with a few nodes and edges
47-
graph = Graph()
48-
graph.add_edge(1, 2)
49-
graph.add_edge(2, 3)
50-
graph.add_edge(3, 4)
51-
5267
# Create an instance of SvgVisualizer
5368
visualizer = SvgVisualizer()
5469

5570
# Call the draw method
56-
objects = visualizer.visualize(graph)
71+
objects = visualizer.visualize(self.directed_graph)
5772
drawing = visualizer.draw(objects)
5873

5974
# Check that the returned value is an svgwrite.Drawing object
6075
self.assertIsInstance(drawing, svgwrite.Drawing)
6176

6277
def test_write(self):
6378
# Create a Graph object with a few nodes and edges
64-
self.filepath = "test.svg"
65-
graph = Graph()
66-
graph.add_edge(1, 2)
67-
graph.add_edge(2, 3)
68-
graph.add_edge(3, 4)
79+
6980

7081
# Create an instance of SvgVisualizer
7182
visualizer = SvgVisualizer()
72-
graphic_objects = visualizer.visualize(graph)
7383

74-
# Call the write method
75-
visualizer.write(self.filepath, graphic_objects)
84+
graphic_objects = visualizer.visualize(self.directed_graph)
85+
visualizer.write(self.directed_filepath, graphic_objects)
86+
self.assertTrue(os.path.exists(self.directed_filepath))
7687

77-
# Check that the file was created
78-
self.assertTrue(os.path.exists(self.filepath))
88+
graphic_objects = visualizer.visualize(self.undirected_graph)
89+
visualizer.write(self.undirected_filepath, graphic_objects)
90+
self.assertTrue(os.path.exists(self.undirected_filepath))
7991

8092
self.skip_tear_down = True
8193

visualize/undirected_graph.svg

Lines changed: 2 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)