1 | n/a | import pickle |
---|
2 | n/a | import unittest |
---|
3 | n/a | from test import support |
---|
4 | n/a | |
---|
5 | n/a | turtle = support.import_module('turtle') |
---|
6 | n/a | Vec2D = turtle.Vec2D |
---|
7 | n/a | |
---|
8 | n/a | test_config = """\ |
---|
9 | n/a | width = 0.75 |
---|
10 | n/a | height = 0.8 |
---|
11 | n/a | canvwidth = 500 |
---|
12 | n/a | canvheight = 200 |
---|
13 | n/a | leftright = 100 |
---|
14 | n/a | topbottom = 100 |
---|
15 | n/a | mode = world |
---|
16 | n/a | colormode = 255 |
---|
17 | n/a | delay = 100 |
---|
18 | n/a | undobuffersize = 10000 |
---|
19 | n/a | shape = circle |
---|
20 | n/a | pencolor = red |
---|
21 | n/a | fillcolor = blue |
---|
22 | n/a | resizemode = auto |
---|
23 | n/a | visible = None |
---|
24 | n/a | language = english |
---|
25 | n/a | exampleturtle = turtle |
---|
26 | n/a | examplescreen = screen |
---|
27 | n/a | title = Python Turtle Graphics |
---|
28 | n/a | using_IDLE = '' |
---|
29 | n/a | """ |
---|
30 | n/a | |
---|
31 | n/a | test_config_two = """\ |
---|
32 | n/a | # Comments! |
---|
33 | n/a | # Testing comments! |
---|
34 | n/a | pencolor = red |
---|
35 | n/a | fillcolor = blue |
---|
36 | n/a | visible = False |
---|
37 | n/a | language = english |
---|
38 | n/a | # Some more |
---|
39 | n/a | # comments |
---|
40 | n/a | using_IDLE = False |
---|
41 | n/a | """ |
---|
42 | n/a | |
---|
43 | n/a | invalid_test_config = """ |
---|
44 | n/a | pencolor = red |
---|
45 | n/a | fillcolor: blue |
---|
46 | n/a | visible = False |
---|
47 | n/a | """ |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | class TurtleConfigTest(unittest.TestCase): |
---|
51 | n/a | |
---|
52 | n/a | def get_cfg_file(self, cfg_str): |
---|
53 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
54 | n/a | with open(support.TESTFN, 'w') as f: |
---|
55 | n/a | f.write(cfg_str) |
---|
56 | n/a | return support.TESTFN |
---|
57 | n/a | |
---|
58 | n/a | def test_config_dict(self): |
---|
59 | n/a | |
---|
60 | n/a | cfg_name = self.get_cfg_file(test_config) |
---|
61 | n/a | parsed_cfg = turtle.config_dict(cfg_name) |
---|
62 | n/a | |
---|
63 | n/a | expected = { |
---|
64 | n/a | 'width' : 0.75, |
---|
65 | n/a | 'height' : 0.8, |
---|
66 | n/a | 'canvwidth' : 500, |
---|
67 | n/a | 'canvheight': 200, |
---|
68 | n/a | 'leftright': 100, |
---|
69 | n/a | 'topbottom': 100, |
---|
70 | n/a | 'mode': 'world', |
---|
71 | n/a | 'colormode': 255, |
---|
72 | n/a | 'delay': 100, |
---|
73 | n/a | 'undobuffersize': 10000, |
---|
74 | n/a | 'shape': 'circle', |
---|
75 | n/a | 'pencolor' : 'red', |
---|
76 | n/a | 'fillcolor' : 'blue', |
---|
77 | n/a | 'resizemode' : 'auto', |
---|
78 | n/a | 'visible' : None, |
---|
79 | n/a | 'language': 'english', |
---|
80 | n/a | 'exampleturtle': 'turtle', |
---|
81 | n/a | 'examplescreen': 'screen', |
---|
82 | n/a | 'title': 'Python Turtle Graphics', |
---|
83 | n/a | 'using_IDLE': '', |
---|
84 | n/a | } |
---|
85 | n/a | |
---|
86 | n/a | self.assertEqual(parsed_cfg, expected) |
---|
87 | n/a | |
---|
88 | n/a | def test_partial_config_dict_with_commments(self): |
---|
89 | n/a | |
---|
90 | n/a | cfg_name = self.get_cfg_file(test_config_two) |
---|
91 | n/a | parsed_cfg = turtle.config_dict(cfg_name) |
---|
92 | n/a | |
---|
93 | n/a | expected = { |
---|
94 | n/a | 'pencolor': 'red', |
---|
95 | n/a | 'fillcolor': 'blue', |
---|
96 | n/a | 'visible': False, |
---|
97 | n/a | 'language': 'english', |
---|
98 | n/a | 'using_IDLE': False, |
---|
99 | n/a | } |
---|
100 | n/a | |
---|
101 | n/a | self.assertEqual(parsed_cfg, expected) |
---|
102 | n/a | |
---|
103 | n/a | def test_config_dict_invalid(self): |
---|
104 | n/a | |
---|
105 | n/a | cfg_name = self.get_cfg_file(invalid_test_config) |
---|
106 | n/a | |
---|
107 | n/a | with support.captured_stdout() as stdout: |
---|
108 | n/a | parsed_cfg = turtle.config_dict(cfg_name) |
---|
109 | n/a | |
---|
110 | n/a | err_msg = stdout.getvalue() |
---|
111 | n/a | |
---|
112 | n/a | self.assertIn('Bad line in config-file ', err_msg) |
---|
113 | n/a | self.assertIn('fillcolor: blue', err_msg) |
---|
114 | n/a | |
---|
115 | n/a | self.assertEqual(parsed_cfg, { |
---|
116 | n/a | 'pencolor': 'red', |
---|
117 | n/a | 'visible': False, |
---|
118 | n/a | }) |
---|
119 | n/a | |
---|
120 | n/a | |
---|
121 | n/a | class VectorComparisonMixin: |
---|
122 | n/a | |
---|
123 | n/a | def assertVectorsAlmostEqual(self, vec1, vec2): |
---|
124 | n/a | if len(vec1) != len(vec2): |
---|
125 | n/a | self.fail("Tuples are not of equal size") |
---|
126 | n/a | for idx, (i, j) in enumerate(zip(vec1, vec2)): |
---|
127 | n/a | self.assertAlmostEqual( |
---|
128 | n/a | i, j, msg='values at index {} do not match'.format(idx)) |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | class TestVec2D(VectorComparisonMixin, unittest.TestCase): |
---|
132 | n/a | |
---|
133 | n/a | def test_constructor(self): |
---|
134 | n/a | vec = Vec2D(0.5, 2) |
---|
135 | n/a | self.assertEqual(vec[0], 0.5) |
---|
136 | n/a | self.assertEqual(vec[1], 2) |
---|
137 | n/a | self.assertIsInstance(vec, Vec2D) |
---|
138 | n/a | |
---|
139 | n/a | self.assertRaises(TypeError, Vec2D) |
---|
140 | n/a | self.assertRaises(TypeError, Vec2D, 0) |
---|
141 | n/a | self.assertRaises(TypeError, Vec2D, (0, 1)) |
---|
142 | n/a | self.assertRaises(TypeError, Vec2D, vec) |
---|
143 | n/a | self.assertRaises(TypeError, Vec2D, 0, 1, 2) |
---|
144 | n/a | |
---|
145 | n/a | def test_repr(self): |
---|
146 | n/a | vec = Vec2D(0.567, 1.234) |
---|
147 | n/a | self.assertEqual(repr(vec), '(0.57,1.23)') |
---|
148 | n/a | |
---|
149 | n/a | def test_equality(self): |
---|
150 | n/a | vec1 = Vec2D(0, 1) |
---|
151 | n/a | vec2 = Vec2D(0.0, 1) |
---|
152 | n/a | vec3 = Vec2D(42, 1) |
---|
153 | n/a | self.assertEqual(vec1, vec2) |
---|
154 | n/a | self.assertEqual(vec1, tuple(vec1)) |
---|
155 | n/a | self.assertEqual(tuple(vec1), vec1) |
---|
156 | n/a | self.assertNotEqual(vec1, vec3) |
---|
157 | n/a | self.assertNotEqual(vec2, vec3) |
---|
158 | n/a | |
---|
159 | n/a | def test_pickling(self): |
---|
160 | n/a | vec = Vec2D(0.5, 2) |
---|
161 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
162 | n/a | with self.subTest(proto=proto): |
---|
163 | n/a | pickled = pickle.dumps(vec, protocol=proto) |
---|
164 | n/a | unpickled = pickle.loads(pickled) |
---|
165 | n/a | self.assertEqual(unpickled, vec) |
---|
166 | n/a | self.assertIsInstance(unpickled, Vec2D) |
---|
167 | n/a | |
---|
168 | n/a | def _assert_arithmetic_cases(self, test_cases, lambda_operator): |
---|
169 | n/a | for test_case in test_cases: |
---|
170 | n/a | with self.subTest(case=test_case): |
---|
171 | n/a | |
---|
172 | n/a | ((first, second), expected) = test_case |
---|
173 | n/a | |
---|
174 | n/a | op1 = Vec2D(*first) |
---|
175 | n/a | op2 = Vec2D(*second) |
---|
176 | n/a | |
---|
177 | n/a | result = lambda_operator(op1, op2) |
---|
178 | n/a | |
---|
179 | n/a | expected = Vec2D(*expected) |
---|
180 | n/a | |
---|
181 | n/a | self.assertVectorsAlmostEqual(result, expected) |
---|
182 | n/a | |
---|
183 | n/a | def test_vector_addition(self): |
---|
184 | n/a | |
---|
185 | n/a | test_cases = [ |
---|
186 | n/a | (((0, 0), (1, 1)), (1.0, 1.0)), |
---|
187 | n/a | (((-1, 0), (2, 2)), (1, 2)), |
---|
188 | n/a | (((1.5, 0), (1, 1)), (2.5, 1)), |
---|
189 | n/a | ] |
---|
190 | n/a | |
---|
191 | n/a | self._assert_arithmetic_cases(test_cases, lambda x, y: x + y) |
---|
192 | n/a | |
---|
193 | n/a | def test_vector_subtraction(self): |
---|
194 | n/a | |
---|
195 | n/a | test_cases = [ |
---|
196 | n/a | (((0, 0), (1, 1)), (-1, -1)), |
---|
197 | n/a | (((10.625, 0.125), (10, 0)), (0.625, 0.125)), |
---|
198 | n/a | ] |
---|
199 | n/a | |
---|
200 | n/a | self._assert_arithmetic_cases(test_cases, lambda x, y: x - y) |
---|
201 | n/a | |
---|
202 | n/a | def test_vector_multiply(self): |
---|
203 | n/a | |
---|
204 | n/a | vec1 = Vec2D(10, 10) |
---|
205 | n/a | vec2 = Vec2D(0.5, 3) |
---|
206 | n/a | answer = vec1 * vec2 |
---|
207 | n/a | expected = 35 |
---|
208 | n/a | self.assertAlmostEqual(answer, expected) |
---|
209 | n/a | |
---|
210 | n/a | vec = Vec2D(0.5, 3) |
---|
211 | n/a | answer = vec * 10 |
---|
212 | n/a | expected = Vec2D(5, 30) |
---|
213 | n/a | self.assertVectorsAlmostEqual(answer, expected) |
---|
214 | n/a | |
---|
215 | n/a | def test_vector_negative(self): |
---|
216 | n/a | vec = Vec2D(10, -10) |
---|
217 | n/a | expected = (-10, 10) |
---|
218 | n/a | self.assertVectorsAlmostEqual(-vec, expected) |
---|
219 | n/a | |
---|
220 | n/a | def test_distance(self): |
---|
221 | n/a | vec = Vec2D(6, 8) |
---|
222 | n/a | expected = 10 |
---|
223 | n/a | self.assertEqual(abs(vec), expected) |
---|
224 | n/a | |
---|
225 | n/a | vec = Vec2D(0, 0) |
---|
226 | n/a | expected = 0 |
---|
227 | n/a | self.assertEqual(abs(vec), expected) |
---|
228 | n/a | |
---|
229 | n/a | vec = Vec2D(2.5, 6) |
---|
230 | n/a | expected = 6.5 |
---|
231 | n/a | self.assertEqual(abs(vec), expected) |
---|
232 | n/a | |
---|
233 | n/a | def test_rotate(self): |
---|
234 | n/a | |
---|
235 | n/a | cases = [ |
---|
236 | n/a | (((0, 0), 0), (0, 0)), |
---|
237 | n/a | (((0, 1), 90), (-1, 0)), |
---|
238 | n/a | (((0, 1), -90), (1, 0)), |
---|
239 | n/a | (((1, 0), 180), (-1, 0)), |
---|
240 | n/a | (((1, 0), 360), (1, 0)), |
---|
241 | n/a | ] |
---|
242 | n/a | |
---|
243 | n/a | for case in cases: |
---|
244 | n/a | with self.subTest(case=case): |
---|
245 | n/a | (vec, rot), expected = case |
---|
246 | n/a | vec = Vec2D(*vec) |
---|
247 | n/a | got = vec.rotate(rot) |
---|
248 | n/a | self.assertVectorsAlmostEqual(got, expected) |
---|
249 | n/a | |
---|
250 | n/a | |
---|
251 | n/a | class TestTNavigator(VectorComparisonMixin, unittest.TestCase): |
---|
252 | n/a | |
---|
253 | n/a | def setUp(self): |
---|
254 | n/a | self.nav = turtle.TNavigator() |
---|
255 | n/a | |
---|
256 | n/a | def test_goto(self): |
---|
257 | n/a | self.nav.goto(100, -100) |
---|
258 | n/a | self.assertAlmostEqual(self.nav.xcor(), 100) |
---|
259 | n/a | self.assertAlmostEqual(self.nav.ycor(), -100) |
---|
260 | n/a | |
---|
261 | n/a | def test_pos(self): |
---|
262 | n/a | self.assertEqual(self.nav.pos(), self.nav._position) |
---|
263 | n/a | self.nav.goto(100, -100) |
---|
264 | n/a | self.assertEqual(self.nav.pos(), self.nav._position) |
---|
265 | n/a | |
---|
266 | n/a | def test_left(self): |
---|
267 | n/a | self.assertEqual(self.nav._orient, (1.0, 0)) |
---|
268 | n/a | self.nav.left(90) |
---|
269 | n/a | self.assertVectorsAlmostEqual(self.nav._orient, (0.0, 1.0)) |
---|
270 | n/a | |
---|
271 | n/a | def test_right(self): |
---|
272 | n/a | self.assertEqual(self.nav._orient, (1.0, 0)) |
---|
273 | n/a | self.nav.right(90) |
---|
274 | n/a | self.assertVectorsAlmostEqual(self.nav._orient, (0, -1.0)) |
---|
275 | n/a | |
---|
276 | n/a | def test_reset(self): |
---|
277 | n/a | self.nav.goto(100, -100) |
---|
278 | n/a | self.assertAlmostEqual(self.nav.xcor(), 100) |
---|
279 | n/a | self.assertAlmostEqual(self.nav.ycor(), -100) |
---|
280 | n/a | self.nav.reset() |
---|
281 | n/a | self.assertAlmostEqual(self.nav.xcor(), 0) |
---|
282 | n/a | self.assertAlmostEqual(self.nav.ycor(), 0) |
---|
283 | n/a | |
---|
284 | n/a | def test_forward(self): |
---|
285 | n/a | self.nav.forward(150) |
---|
286 | n/a | expected = Vec2D(150, 0) |
---|
287 | n/a | self.assertVectorsAlmostEqual(self.nav.position(), expected) |
---|
288 | n/a | |
---|
289 | n/a | self.nav.reset() |
---|
290 | n/a | self.nav.left(90) |
---|
291 | n/a | self.nav.forward(150) |
---|
292 | n/a | expected = Vec2D(0, 150) |
---|
293 | n/a | self.assertVectorsAlmostEqual(self.nav.position(), expected) |
---|
294 | n/a | |
---|
295 | n/a | self.assertRaises(TypeError, self.nav.forward, 'skldjfldsk') |
---|
296 | n/a | |
---|
297 | n/a | def test_backwards(self): |
---|
298 | n/a | self.nav.back(200) |
---|
299 | n/a | expected = Vec2D(-200, 0) |
---|
300 | n/a | self.assertVectorsAlmostEqual(self.nav.position(), expected) |
---|
301 | n/a | |
---|
302 | n/a | self.nav.reset() |
---|
303 | n/a | self.nav.right(90) |
---|
304 | n/a | self.nav.back(200) |
---|
305 | n/a | expected = Vec2D(0, 200) |
---|
306 | n/a | self.assertVectorsAlmostEqual(self.nav.position(), expected) |
---|
307 | n/a | |
---|
308 | n/a | def test_distance(self): |
---|
309 | n/a | self.nav.forward(100) |
---|
310 | n/a | expected = 100 |
---|
311 | n/a | self.assertAlmostEqual(self.nav.distance(Vec2D(0,0)), expected) |
---|
312 | n/a | |
---|
313 | n/a | def test_radians_and_degrees(self): |
---|
314 | n/a | self.nav.left(90) |
---|
315 | n/a | self.assertAlmostEqual(self.nav.heading(), 90) |
---|
316 | n/a | self.nav.radians() |
---|
317 | n/a | self.assertAlmostEqual(self.nav.heading(), 1.57079633) |
---|
318 | n/a | self.nav.degrees() |
---|
319 | n/a | self.assertAlmostEqual(self.nav.heading(), 90) |
---|
320 | n/a | |
---|
321 | n/a | def test_towards(self): |
---|
322 | n/a | |
---|
323 | n/a | coordinates = [ |
---|
324 | n/a | # coordinates, expected |
---|
325 | n/a | ((100, 0), 0.0), |
---|
326 | n/a | ((100, 100), 45.0), |
---|
327 | n/a | ((0, 100), 90.0), |
---|
328 | n/a | ((-100, 100), 135.0), |
---|
329 | n/a | ((-100, 0), 180.0), |
---|
330 | n/a | ((-100, -100), 225.0), |
---|
331 | n/a | ((0, -100), 270.0), |
---|
332 | n/a | ((100, -100), 315.0), |
---|
333 | n/a | ] |
---|
334 | n/a | |
---|
335 | n/a | for (x, y), expected in coordinates: |
---|
336 | n/a | self.assertEqual(self.nav.towards(x, y), expected) |
---|
337 | n/a | self.assertEqual(self.nav.towards((x, y)), expected) |
---|
338 | n/a | self.assertEqual(self.nav.towards(Vec2D(x, y)), expected) |
---|
339 | n/a | |
---|
340 | n/a | def test_heading(self): |
---|
341 | n/a | |
---|
342 | n/a | self.nav.left(90) |
---|
343 | n/a | self.assertAlmostEqual(self.nav.heading(), 90) |
---|
344 | n/a | self.nav.left(45) |
---|
345 | n/a | self.assertAlmostEqual(self.nav.heading(), 135) |
---|
346 | n/a | self.nav.right(1.6) |
---|
347 | n/a | self.assertAlmostEqual(self.nav.heading(), 133.4) |
---|
348 | n/a | self.assertRaises(TypeError, self.nav.right, 'sdkfjdsf') |
---|
349 | n/a | self.nav.reset() |
---|
350 | n/a | |
---|
351 | n/a | rotations = [10, 20, 170, 300] |
---|
352 | n/a | result = sum(rotations) % 360 |
---|
353 | n/a | for num in rotations: |
---|
354 | n/a | self.nav.left(num) |
---|
355 | n/a | self.assertEqual(self.nav.heading(), result) |
---|
356 | n/a | self.nav.reset() |
---|
357 | n/a | |
---|
358 | n/a | result = (360-sum(rotations)) % 360 |
---|
359 | n/a | for num in rotations: |
---|
360 | n/a | self.nav.right(num) |
---|
361 | n/a | self.assertEqual(self.nav.heading(), result) |
---|
362 | n/a | self.nav.reset() |
---|
363 | n/a | |
---|
364 | n/a | rotations = [10, 20, -170, 300, -210, 34.3, -50.2, -10, -29.98, 500] |
---|
365 | n/a | sum_so_far = 0 |
---|
366 | n/a | for num in rotations: |
---|
367 | n/a | if num < 0: |
---|
368 | n/a | self.nav.right(abs(num)) |
---|
369 | n/a | else: |
---|
370 | n/a | self.nav.left(num) |
---|
371 | n/a | sum_so_far += num |
---|
372 | n/a | self.assertAlmostEqual(self.nav.heading(), sum_so_far % 360) |
---|
373 | n/a | |
---|
374 | n/a | def test_setheading(self): |
---|
375 | n/a | self.nav.setheading(102.32) |
---|
376 | n/a | self.assertAlmostEqual(self.nav.heading(), 102.32) |
---|
377 | n/a | self.nav.setheading(-123.23) |
---|
378 | n/a | self.assertAlmostEqual(self.nav.heading(), (-123.23) % 360) |
---|
379 | n/a | self.nav.setheading(-1000.34) |
---|
380 | n/a | self.assertAlmostEqual(self.nav.heading(), (-1000.34) % 360) |
---|
381 | n/a | self.nav.setheading(300000) |
---|
382 | n/a | self.assertAlmostEqual(self.nav.heading(), 300000%360) |
---|
383 | n/a | |
---|
384 | n/a | def test_positions(self): |
---|
385 | n/a | self.nav.forward(100) |
---|
386 | n/a | self.nav.left(90) |
---|
387 | n/a | self.nav.forward(-200) |
---|
388 | n/a | self.assertVectorsAlmostEqual(self.nav.pos(), (100.0, -200.0)) |
---|
389 | n/a | |
---|
390 | n/a | def test_setx_and_sety(self): |
---|
391 | n/a | self.nav.setx(-1023.2334) |
---|
392 | n/a | self.nav.sety(193323.234) |
---|
393 | n/a | self.assertVectorsAlmostEqual(self.nav.pos(), (-1023.2334, 193323.234)) |
---|
394 | n/a | |
---|
395 | n/a | def test_home(self): |
---|
396 | n/a | self.nav.left(30) |
---|
397 | n/a | self.nav.forward(-100000) |
---|
398 | n/a | self.nav.home() |
---|
399 | n/a | self.assertVectorsAlmostEqual(self.nav.pos(), (0,0)) |
---|
400 | n/a | self.assertAlmostEqual(self.nav.heading(), 0) |
---|
401 | n/a | |
---|
402 | n/a | def test_distance_method(self): |
---|
403 | n/a | self.assertAlmostEqual(self.nav.distance(30, 40), 50) |
---|
404 | n/a | vec = Vec2D(0.22, .001) |
---|
405 | n/a | self.assertAlmostEqual(self.nav.distance(vec), 0.22000227271553355) |
---|
406 | n/a | another_turtle = turtle.TNavigator() |
---|
407 | n/a | another_turtle.left(90) |
---|
408 | n/a | another_turtle.forward(10000) |
---|
409 | n/a | self.assertAlmostEqual(self.nav.distance(another_turtle), 10000) |
---|
410 | n/a | |
---|
411 | n/a | |
---|
412 | n/a | class TestTPen(unittest.TestCase): |
---|
413 | n/a | |
---|
414 | n/a | def test_pendown_and_penup(self): |
---|
415 | n/a | |
---|
416 | n/a | tpen = turtle.TPen() |
---|
417 | n/a | |
---|
418 | n/a | self.assertTrue(tpen.isdown()) |
---|
419 | n/a | tpen.penup() |
---|
420 | n/a | self.assertFalse(tpen.isdown()) |
---|
421 | n/a | tpen.pendown() |
---|
422 | n/a | self.assertTrue(tpen.isdown()) |
---|
423 | n/a | |
---|
424 | n/a | def test_showturtle_hideturtle_and_isvisible(self): |
---|
425 | n/a | |
---|
426 | n/a | tpen = turtle.TPen() |
---|
427 | n/a | |
---|
428 | n/a | self.assertTrue(tpen.isvisible()) |
---|
429 | n/a | tpen.hideturtle() |
---|
430 | n/a | self.assertFalse(tpen.isvisible()) |
---|
431 | n/a | tpen.showturtle() |
---|
432 | n/a | self.assertTrue(tpen.isvisible()) |
---|
433 | n/a | |
---|
434 | n/a | |
---|
435 | n/a | if __name__ == '__main__': |
---|
436 | n/a | unittest.main() |
---|