-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_space_invaders.py
More file actions
197 lines (159 loc) · 7.06 KB
/
test_space_invaders.py
File metadata and controls
197 lines (159 loc) · 7.06 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import unittest
import pygame
from pygame.sprite import Group
from gun import Gun
from shift import Shift
from statistics import Statistics
from scores import Scores
from controls import create_invaders, update_screen, update_bullets, pause
from unittest.mock import patch
from unittest.mock import MagicMock
class TestGun(unittest.TestCase):
def setUp(self):
pygame.init()
self.screen = pygame.display.set_mode((800, 600))
self.gun = Gun(self.screen)
def tearDown(self):
pygame.quit()
def test_initial_position(self):
"""Test that gun is positioned in the center bottom of the screen initially"""
self.assertEqual(self.gun.rect.centerx, self.screen.get_rect().centerx)
self.assertEqual(self.gun.rect.bottom, self.screen.get_rect().bottom)
def test_move_right(self):
"""Test that gun moves right when move_right is True"""
initial_center = self.gun.center
self.gun.move_right = True
self.gun.update()
self.assertGreater(self.gun.center, initial_center)
def test_move_left(self):
"""Test that gun moves left when move_left is True"""
initial_center = self.gun.center
self.gun.move_left = True
self.gun.update()
self.assertLess(self.gun.center, initial_center)
def test_no_movement(self):
"""Test that gun does not move if no keys are pressed"""
initial_center = self.gun.center
self.gun.update()
self.assertEqual(self.gun.center, initial_center)
class TestStatistics(unittest.TestCase):
def setUp(self):
self.stats = Statistics()
def test_initial_lives(self):
self.assertEqual(self.stats.lives, 3)
def test_score_initialization(self):
self.assertEqual(self.stats.score, 0)
def test_top_score_read(self):
with open("top_score.txt", "w") as f:
f.write("100")
stats = Statistics()
self.assertEqual(stats.top_score, 100)
class TestUpdateBullets(unittest.TestCase):
def setUp(self):
pygame.init()
self.screen = pygame.display.set_mode((800, 600))
self.gun = Gun(self.screen)
self.shooter_bullets = MagicMock()
self.bullets = MagicMock()
self.invaders = Group()
self.walls = Group()
self.shift = MagicMock()
self.level_info = "easy"
self.statistics = Statistics()
self.score = Scores(self.screen, self.statistics, "")
self.mistery = MagicMock()
self.machine_guns = MagicMock()
self.invincibilities = MagicMock()
self.freezes = MagicMock()
def test_bullet_removal(self):
bullet = pygame.sprite.Sprite()
bullet.rect = pygame.Rect(0, 0, 5, 5)
bullet.rect.bottom = -10
self.bullets.add(bullet)
update_bullets(self.screen, self.statistics, self.score, self.bullets,
self.invaders, self.shooter_bullets, self.walls,
self.shift, self.level_info, self.mistery, self.machine_guns,
self.gun, self.invincibilities, self.freezes)
self.assertNotIn(bullet, self.bullets)
class TestInvaders(unittest.TestCase):
def test_create_invaders(self):
screen = MagicMock()
invaders = MagicMock()
shooter_bullets = MagicMock()
shift = MagicMock()
level_info = "easy"
create_invaders(screen, invaders, shooter_bullets, shift, level_info)
self.assertTrue(invaders.add.called)
level_info = "middle"
create_invaders(screen, invaders, shooter_bullets, shift, level_info)
self.assertTrue(invaders.add.called)
level_info = "hard"
create_invaders(screen, invaders, shooter_bullets, shift, level_info)
self.assertTrue(invaders.add.called)
class TestPause(unittest.TestCase):
def setUp(self):
pygame.init()
pygame.font.init()
self.screen = pygame.display.set_mode((800, 600))
self.statistics = Statistics()
self.gun = Gun(self.screen)
self.bullets = pygame.sprite.Group()
self.shooter_bullets = pygame.sprite.Group()
self.invaders = pygame.sprite.Group()
self.walls = pygame.sprite.Group()
self.shift = Shift()
self.level_info = "easy"
self.score = Scores(self.screen, self.statistics, "Player1")
self.mistery = MagicMock()
def tearDown(self):
pygame.quit()
@patch('pygame.key.get_pressed', return_value=[0] * 323)
@patch('pygame.event.get', return_value=[])
def test_pause_escape(self, mock_event, mock_get_pressed):
mock_get_pressed.return_value[pygame.K_ESCAPE] = 1
with patch('pygame.display.update'), patch('time.sleep', return_value=None):
pause(self.screen, self.score, self.statistics, self.gun, self.bullets,
self.shooter_bullets, self.invaders, self.walls, self.shift, self.level_info, self.mistery)
self.assertFalse(self.statistics.is_pressed_return_to_menu)
@patch('pygame.key.get_pressed', return_value=[0] * 323)
@patch('pygame.event.get', return_value=[])
def test_pause_return_to_menu(self, mock_event, mock_get_pressed):
mock_get_pressed.return_value[pygame.K_m] = 1
with patch('pygame.display.update'), patch('time.sleep', return_value=None):
pause(self.screen, self.score, self.statistics, self.gun, self.bullets,
self.shooter_bullets, self.invaders, self.walls, self.shift, self.level_info, self.mistery)
self.assertTrue(self.statistics.is_pressed_return_to_menu)
class TestUpdateScreen(unittest.TestCase):
@patch('pygame.display.flip')
def test_update_screen(self, mock_flip):
screen = MagicMock()
score = MagicMock()
gun = MagicMock()
invaders = MagicMock()
color = (0, 0, 0)
bullets = MagicMock()
shooter_bullets = MagicMock()
walls = MagicMock()
clock = MagicMock()
mistery = MagicMock()
statistics = MagicMock()
machine_gun = MagicMock()
invincibility = MagicMock()
freeze = MagicMock()
machine_guns = [machine_gun]
invincibilities = [invincibility]
freezes = [freeze]
update_screen(screen, score, gun, invaders, color, bullets, shooter_bullets, walls, clock, mistery, statistics,
machine_guns, invincibilities, freezes)
screen.fill.assert_called_once_with(color)
score.print.assert_called_once()
score.print_superpower.assert_called_once_with(gun)
gun.print.assert_called_once()
mistery.update.assert_called_once()
mock_flip.assert_called_once()
clock.tick.assert_called_once_with(60)
bullets.sprites.assert_called_once()
shooter_bullets.sprites.assert_called_once()
machine_gun.update.assert_called_once()
invincibility.update.assert_called_once()
freeze.update.assert_called_once()