1
+ # ----------------------------------------------------------------------------
2
+ # Python A-Turtle
3
+ # ----------------------------------------------------------------------------
4
+ # Copyright (c) Tiago Montes.
5
+ # See LICENSE for details.
6
+ # ----------------------------------------------------------------------------
7
+
8
+ import contextlib
9
+ import unittest
10
+ from unittest import mock
11
+
12
+ from aturtle import canvas
13
+
14
+ from . import fake_tkinter
15
+
16
+
17
+
18
+ SCREEN_WIDTH = 640
19
+ SCREEN_HEIGHT = 480
20
+
21
+
22
+ class TestInvertedTkYCanvas (unittest .TestCase ):
23
+
24
+ def setUp (self ):
25
+
26
+ self .tkinter = fake_tkinter .FakeTkinter (
27
+ screen_width = SCREEN_WIDTH ,
28
+ screen_height = SCREEN_HEIGHT ,
29
+ )
30
+ self .master = self .tkinter .Tk ()
31
+
32
+ self ._exit_stack = contextlib .ExitStack ()
33
+ self ._exit_stack .enter_context (
34
+ mock .patch ('aturtle.canvas.tkinter' , self .tkinter )
35
+ )
36
+
37
+
38
+ def tearDown (self ):
39
+
40
+ self ._exit_stack .close ()
41
+
42
+
43
+ def test_create_creates_tkinter_Canvas (self ):
44
+
45
+ _c = canvas .InvertedYTkCanvas (self .master , 'background' )
46
+
47
+ tkinter_canvas_call_args = self .tkinter .canvas_init_calls
48
+
49
+ self .assertEqual (len (tkinter_canvas_call_args ), 1 , 'tkinter.Canvas call count' )
50
+
51
+
52
+ def test_create_creates_tkinter_Canvas_with_given_master (self ):
53
+
54
+ _c = canvas .InvertedYTkCanvas (self .master , 'background' )
55
+
56
+ tkinter_canvas_call_args = self .tkinter .canvas_init_calls
57
+
58
+ # Passed a single positional argument: self.master
59
+ (single_arg ,), _kwargs = tkinter_canvas_call_args [0 ]
60
+ self .assertIs (single_arg , self .master )
61
+
62
+
63
+ def test_create_creates_tkinter_Canvas_with_given_background (self ):
64
+
65
+ _c = canvas .InvertedYTkCanvas (self .master , 'background' )
66
+
67
+ tkinter_canvas_call_args = self .tkinter .canvas_init_calls
68
+
69
+ _args , kwargs = tkinter_canvas_call_args [0 ]
70
+ self .assertEqual (kwargs ['background' ], 'background' )
71
+
72
+
73
+ def test_create_creates_tkinter_Canvas_with_zero_highlightthickness (self ):
74
+
75
+ _c = canvas .InvertedYTkCanvas (self .master , 'background' )
76
+
77
+ tkinter_canvas_call_args = self .tkinter .canvas_init_calls
78
+
79
+ _args , kwargs = tkinter_canvas_call_args [0 ]
80
+ self .assertEqual (kwargs ['highlightthickness' ], 0 )
81
+
82
+
83
+ def test_create_polygon_returns_integer_item_id (self ):
84
+
85
+ c = canvas .InvertedYTkCanvas (self .master , None )
86
+
87
+ item_id = c .create_polygon ([], fill = 'fill' , outline = 'outline' , width = 42 )
88
+
89
+ self .assertIsInstance (item_id , int )
90
+
91
+
92
+ def test_create_polygon_inverts_y_coordinates (self ):
93
+
94
+ c = canvas .InvertedYTkCanvas (self .master , None )
95
+
96
+ coords = [0 , 0 , 1 , 1 , 2 , - 2 ]
97
+ _item_id = c .create_polygon (coords , fill = 'fill' , outline = 'outline' , width = 42 )
98
+
99
+ args = c ._canvas .create_polygon_coords
100
+
101
+ self .assertEqual (args , [0 , 0 , 1 , - 1 , 2 , 2 ])
102
+
103
+
104
+ def test_create_polygon_passes_args_to_Canvas_create_polygon (self ):
105
+
106
+ c = canvas .InvertedYTkCanvas (self .master , None )
107
+
108
+ _item_id = c .create_polygon ([], fill = 'fill' , outline = 'outline' , width = 42 )
109
+
110
+ kwargs = c ._canvas .create_polygon_kwargs
111
+ self .assertEqual (kwargs , dict (fill = 'fill' , outline = 'outline' , width = 42 ))
112
+
113
+
114
+ def test_create_image_returns_integer_item_id (self ):
115
+
116
+ c = canvas .InvertedYTkCanvas (self .master , None )
117
+
118
+ item_id = c .create_image (0 , 0 , image = None , anchor = None )
119
+
120
+ self .assertIsInstance (item_id , int )
121
+
122
+
123
+ def test_create_image_inverts_y_coordinate (self ):
124
+
125
+ c = canvas .InvertedYTkCanvas (self .master , None )
126
+
127
+ _item_id = c .create_image (42 , 24 , image = None , anchor = None )
128
+
129
+ c ._canvas .create_image .assert_called_with (
130
+ 42 , - 24 , image = mock .ANY , anchor = mock .ANY
131
+ )
132
+
133
+
134
+ def test_create_image_passes_args_to_Canvas_create_image (self ):
135
+
136
+ c = canvas .InvertedYTkCanvas (self .master , None )
137
+
138
+ _item_id = c .create_image (42 , 24 , image = 'image' , anchor = 'anchor' )
139
+
140
+ c ._canvas .create_image .assert_called_with (
141
+ mock .ANY , mock .ANY , image = 'image' , anchor = 'anchor'
142
+ )
143
+
144
+
145
+ def test_create_line_returns_integer_item_id (self ):
146
+
147
+ c = canvas .InvertedYTkCanvas (self .master , None )
148
+
149
+ item_id = c .create_line ([], fill = 'fill' , width = 42 , capstyle = 'capstyle' )
150
+
151
+ self .assertIsInstance (item_id , int )
152
+
153
+
154
+ def test_create_line_inverts_y_coordinates (self ):
155
+
156
+ c = canvas .InvertedYTkCanvas (self .master , None )
157
+
158
+ coords = [0 , 0 , 1 , 1 , 2 , - 2 ]
159
+ _item_id = c .create_line (coords , fill = 'fill' , width = 42 , capstyle = 'capstyle' )
160
+
161
+ c ._canvas .create_line .assert_called_with (
162
+ [0 , 0 , 1 , - 1 , 2 , 2 ], fill = mock .ANY , width = mock .ANY , capstyle = mock .ANY ,
163
+ )
164
+
165
+
166
+ def test_create_line_passes_args_to_Canvas_create_line (self ):
167
+
168
+ c = canvas .InvertedYTkCanvas (self .master , None )
169
+
170
+ _item_id = c .create_line ([], fill = 'fill' , width = 42 , capstyle = 'capstyle' )
171
+
172
+ c ._canvas .create_line .assert_called_with (
173
+ mock .ANY , fill = 'fill' , width = 42 , capstyle = 'capstyle' ,
174
+ )
175
+
176
+
177
+ def test_move_calls_canvas_move_with_same_item_id (self ):
178
+
179
+ c = canvas .InvertedYTkCanvas (self .master , None )
180
+
181
+ c .move (42 , 0 , 0 )
182
+
183
+ c ._canvas .move .assert_called_with (42 , mock .ANY , mock .ANY )
184
+
185
+
186
+ def test_move_calls_canvas_move_with_inverted_y (self ):
187
+
188
+ c = canvas .InvertedYTkCanvas (self .master , None )
189
+
190
+ c .move (None , 42 , 24 )
191
+
192
+ c ._canvas .move .assert_called_with (mock .ANY , 42 , - 24 )
193
+
194
+
195
+ def test_coords_calls_canvas_coords_with_same_item_id (self ):
196
+
197
+ c = canvas .InvertedYTkCanvas (self .master , None )
198
+
199
+ c .coords (42 , [])
200
+
201
+ c ._canvas .coords .assert_called_with (42 , mock .ANY )
202
+
203
+
204
+ def test_coords_calls_canvas_coords_with_inverted_y_coords (self ):
205
+
206
+ c = canvas .InvertedYTkCanvas (self .master , None )
207
+
208
+ c .coords (None , [1 , 2 , 3 , 4 , 5 , 6 ])
209
+
210
+ c ._canvas .coords .assert_called_with (
211
+ mock .ANY , [1 , - 2 , 3 , - 4 , 5 , - 6 ]
212
+ )
213
+
214
+
215
+ def test_attribute_access_returns_tkinter_Canvas_attribute (self ):
216
+
217
+ c = canvas .InvertedYTkCanvas (self .master , None )
218
+
219
+ names = (
220
+ 'pack' ,
221
+ 'config' ,
222
+ 'xview_scroll' ,
223
+ 'yview_scroll' ,
224
+ 'update' ,
225
+ 'delete' ,
226
+ 'itemconfig' ,
227
+ 'tag_lower' ,
228
+ 'tag_raise' ,
229
+ )
230
+
231
+ for name in names :
232
+ with self .subTest (attr_name = name ):
233
+ result = getattr (c , name )
234
+ underlying = getattr (c ._canvas , name )
235
+ self .assertIs (result , underlying )
0 commit comments