Skip to content

Commit 34c9d18

Browse files
committed
Fake tkinter Canvas.create_polygon now a Mock object.
1 parent 8e1f88d commit 34c9d18

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

tests/fake_tkinter.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,13 @@ def __init__(self):
2121
self.coords = mock.Mock()
2222
self.update = mock.Mock()
2323
self.delete = mock.Mock()
24-
self.create_polygon_coords = None
25-
self.create_polygon_kwargs = None
24+
self.create_polygon = mock.Mock(return_value=42)
2625
self.create_image = mock.Mock(return_value=24)
2726
self.create_line = mock.Mock(return_value=42)
2827
self.itemconfig = mock.Mock()
2928
self.tag_lower = mock.Mock()
3029
self.tag_raise = mock.Mock()
3130

32-
def create_polygon(self, coords, **kwargs):
33-
self.create_polygon_coords = coords
34-
self.create_polygon_kwargs = kwargs
35-
# Return some numeric id.
36-
return 42
37-
3831

3932

4033
class FakeWindow:

tests/test_canvas.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,12 @@ def test_create_polygon_inverts_y_coordinates(self):
9696
coords = [0, 0, 1, 1, 2, -2]
9797
_item_id = c.create_polygon(coords, fill='fill', outline='outline', width=42)
9898

99-
args = c._canvas.create_polygon_coords
100-
101-
self.assertEqual(args, [0, 0, 1, -1, 2, 2])
99+
c._canvas.create_polygon.assert_called_once_with(
100+
[0, 0, 1, -1, 2, 2],
101+
fill=mock.ANY,
102+
outline=mock.ANY,
103+
width=mock.ANY,
104+
)
102105

103106

104107
def test_create_polygon_passes_args_to_Canvas_create_polygon(self):
@@ -107,8 +110,12 @@ def test_create_polygon_passes_args_to_Canvas_create_polygon(self):
107110

108111
_item_id = c.create_polygon([], fill='fill', outline='outline', width=42)
109112

110-
kwargs = c._canvas.create_polygon_kwargs
111-
self.assertEqual(kwargs, dict(fill='fill', outline='outline', width=42))
113+
c._canvas.create_polygon.assert_called_once_with(
114+
mock.ANY,
115+
fill='fill',
116+
outline='outline',
117+
width=42,
118+
)
112119

113120

114121
def test_create_image_returns_integer_item_id(self):

tests/test_vector_sprites.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# See LICENSE for details.
66
# ----------------------------------------------------------------------------
77

8+
from unittest import mock
9+
810
from aturtle import sprites, shapes
911

1012
from . import base
@@ -53,24 +55,36 @@ def test_shape_fill_color_passed_to_create_polygon_fill_kwarg(self):
5355

5456
_sprite = sprites.VectorSprite(self.canvas, UnitSquare(fill_color='#009fff'))
5557

56-
create_polygon_kwargs = self.canvas.create_polygon_kwargs
57-
self.assertEqual(create_polygon_kwargs['fill'], '#009fff')
58+
self.canvas.create_polygon.assert_called_once_with(
59+
mock.ANY,
60+
fill='#009fff',
61+
outline=mock.ANY,
62+
width=mock.ANY,
63+
)
5864

5965

6066
def test_shape_line_color_passed_to_create_polygon_outline_kwarg(self):
6167

6268
_sprite = sprites.VectorSprite(self.canvas, UnitSquare(line_color='black'))
6369

64-
create_polygon_kwargs = self.canvas.create_polygon_kwargs
65-
self.assertEqual(create_polygon_kwargs['outline'], 'black')
70+
self.canvas.create_polygon.assert_called_once_with(
71+
mock.ANY,
72+
fill=mock.ANY,
73+
outline='black',
74+
width=mock.ANY,
75+
)
6676

6777

6878
def test_shape_line_width_passed_to_create_polygon_width_kwarg(self):
6979

7080
_sprite = sprites.VectorSprite(self.canvas, UnitSquare(line_width=2))
7181

72-
create_polygon_kwargs = self.canvas.create_polygon_kwargs
73-
self.assertEqual(create_polygon_kwargs['width'], 2)
82+
self.canvas.create_polygon.assert_called_once_with(
83+
mock.ANY,
84+
fill=mock.ANY,
85+
outline=mock.ANY,
86+
width=2,
87+
)
7488

7589

7690
def test_shape_coords(self):

0 commit comments

Comments
 (0)