| 1 | import unittest |
| 2 | |
| 3 | import pygame |
| 4 | |
| 5 | from pgzero.actor import calculate_anchor, Actor |
| 6 | from pgzero.loaders import set_root |
| 7 | |
| 8 | |
| 9 | TEST_MODULE = "pgzero.actor" |
| 10 | TEST_DISP_W, TEST_DISP_H = (200, 100) |
| 11 | |
| 12 | |
| 13 | pygame.init() |
| 14 | pygame.display.set_mode((TEST_DISP_W, TEST_DISP_H)) |
| 15 | |
| 16 | |
| 17 | class ModuleTest(unittest.TestCase): |
| 18 | def test_calculate_anchor_with_float(self): |
| 19 | self.assertEqual( |
| 20 | calculate_anchor(1.23, "x", 12345), |
| 21 | 1.23 |
| 22 | ) |
| 23 | |
| 24 | def test_calculate_anchor_centre(self): |
| 25 | self.assertEqual( |
| 26 | calculate_anchor("center", "x", 100), |
| 27 | 50 |
| 28 | ) |
| 29 | |
| 30 | def test_calculate_anchor_bottom(self): |
| 31 | self.assertEqual( |
| 32 | calculate_anchor("bottom", "y", 100), |
| 33 | 100 |
| 34 | ) |
| 35 | |
| 36 | |
| 37 | class ActorTest(unittest.TestCase): |
| 38 | @classmethod |
| 39 | def setUpClass(self): |
| 40 | set_root(__file__) |
| 41 | |
| 42 | def test_sensible_init_defaults(self): |
| 43 | a = Actor("alien") |
| 44 | |
| 45 | self.assertEqual(a.image, "alien") |
| 46 | self.assertEqual(a.topleft, (0, 0)) |
| 47 | |
| 48 | def test_setting_absolute_initial_pos(self): |
| 49 | a = Actor("alien", pos=(100, 200), anchor=("right", "bottom")) |
| 50 | |
| 51 | self.assertEqual( |
| 52 | a.topleft, |
| 53 | (100 - a.width, 200 - a.height), |
| 54 | ) |
| 55 | |
| 56 | def test_setting_relative_initial_pos_topleft(self): |
| 57 | a = Actor("alien", topleft=(500, 500)) |
| 58 | self.assertEqual(a.topleft, (500, 500)) |
| 59 | |
| 60 | def test_setting_relative_initial_pos_center(self): |
| 61 | a = Actor("alien", center=(500, 500)) |
| 62 | self.assertEqual(a.center, (500, 500)) |
| 63 | |
| 64 | def test_setting_relative_initial_pos_bottomright(self): |
| 65 | a = Actor("alien", bottomright=(500, 500)) |
| 66 | self.assertEqual(a.bottomright, (500, 500)) |
| 67 | |
| 68 | def test_setting_absolute_pos_and_relative_raises_typeerror(self): |
| 69 | with self.assertRaises(TypeError): |
| 70 | Actor("alien", pos=(0, 0), bottomright=(500, 500)) |
| 71 | |
| 72 | def test_setting_multiple_relative_pos_raises_typeerror(self): |
| 73 | with self.assertRaises(TypeError): |
| 74 | Actor("alien", topleft=(500, 500), bottomright=(600, 600)) |
| 75 | |
| 76 | def test_unexpected_kwargs(self): |
| 77 | with self.assertRaises(TypeError) as cm: |
| 78 | Actor("alien", toplift=(0, 0)) |
| 79 | |
| 80 | self.assertEqual( |
| 81 | cm.exception.args[0], |
| 82 | "Unexpected keyword argument 'toplift' (did you mean 'topleft'?)", |
| 83 | ) |
| 84 | |
| 85 | def test_set_pos_relative_to_anchor(self): |
| 86 | a = Actor("alien", anchor=(10, 10)) |
| 87 | a.pos = (100, 100) |
| 88 | self.assertEqual(a.topleft, (90, 90)) |
| 89 | |
| 90 | def test_right_angle(self): |
| 91 | a = Actor("alien") |
| 92 | self.assertEqual(a.image, "alien") |
| 93 | self.assertEqual(a.topleft, (0, 0)) |
| 94 | self.assertEqual(a.pos, (33.0, 46.0)) |
| 95 | self.assertEqual(a.width, 66) |
| 96 | self.assertEqual(a.height, 92) |
| 97 | a.angle += 90.0 |
| 98 | self.assertEqual(a.angle, 90.0) |
| 99 | self.assertEqual(a.topleft, (-13, 13)) |
| 100 | self.assertEqual(a.pos, (33.0, 46.0)) |
| 101 | self.assertEqual(a.width, 92) |
| 102 | self.assertEqual(a.height, 66) |
| 103 | |
| 104 | def test_rotation(self): |
| 105 | """The pos of the actor must not drift with continued small rotation.""" |
| 106 | a = Actor('alien', pos=(100.0, 100.0)) |
| 107 | for _ in range(360): |
| 108 | a.angle += 1.0 |
| 109 | self.assertEqual(a.pos, (100.0, 100.0)) |