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