Debian Codegraph @

test/test_screen.py

raw symbols SCIP
1 import sys
2 import unittest
3 import pygame
4 import pygame.image
5 import numpy as np
6 from pathlib import Path
7
8 from pgzero.screen import Screen
9 from pgzero.loaders import set_root, images
10
11 pygame.init()
12 surf = pygame.display.set_mode((200, 100))
13
14
15 class ScreenTest(unittest.TestCase):
16 @classmethod
17 def setUpClass(cls):
18 """Initialise the display and set loaders to target the current dir."""
19 pygame.init()
20 cls.surf = pygame.display.set_mode((200, 100))
21 set_root(__file__)
22
23 @classmethod
24 def tearDownClass(cls):
25 """Shut down the display."""
26 pygame.display.quit()
27
28 def setUp(self):
29 self.screen = Screen(self.surf)
30 self.screen.clear()
31
32 def assertImagesAlmostEqual(self, computed, expected):
33 """Check that 2 images are approximately equal."""
34 comp_surf = pygame.surfarray.array3d(computed)
35 exp_surf = pygame.surfarray.array3d(expected)
36
37 if np.allclose(comp_surf, exp_surf, atol=7):
38 return
39
40 name = sys._getframe(1).f_code.co_name
41 tmpdir = Path(__file__).parent / 'failed-image'
42 tmpdir.mkdir(exist_ok=True)
43 pygame.image.save(
44 computed,
45 str(tmpdir / '{}-computed.png'.format(name))
46 )
47 pygame.image.save(
48 expected,
49 str(tmpdir / '{}-expected.png'.format(name))
50 )
51
52 raise AssertionError(
53 "Images differ; saved comparison images to {}".format(tmpdir)
54 )
55
56 def test_blit_surf(self):
57 """We can blit a surface to the screen."""
58 self.screen.blit(images.alien, (0, 0))
59 self.assertImagesAlmostEqual(surf, images.expected_alien_blit)
60
61 def test_blit_name(self):
62 """screen.blit() accepts an image name instead of a Surface."""
63 self.screen.blit('alien', (0, 0))
64 self.assertImagesAlmostEqual(surf, images.expected_alien_blit)
65
66
67 if __name__ == '__main__':
68 unittest.main()