-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_scene.py
69 lines (52 loc) · 1.85 KB
/
test_scene.py
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
import pygame
pygame.init()
from scene import Scene
from moveableobject import MoveableObject
from loadstaticres import shipimg
import pytest
class MockScreen:
pass
class MockGameObj(MoveableObject):
def __init__(self, x, y, img):
super().__init__(x, y, 0, img)
self.testlist = []
def on_collide(self, event):
if event.kwargs.get("who") == self:
self.testlist.append(str(event) + " " + str(event.source))
def on_tick(self, event):
super().on_tick(event)
self.testlist.append(event.source)
@pytest.fixture
def PopulatedScene():
s = Scene(MockScreen())
g1 = MockGameObj(0, 0, shipimg)
g2 = MockGameObj(g1.width - 1, g1.height - 1, shipimg)
s.attach(g1)
s.attach(g2)
return s
def test_attach():
s = Scene(MockScreen())
g1 = MockGameObj(0, 0, shipimg)
g2 = MockGameObj(g1.width - 1, g1.height - 1, shipimg)
s.attach(g1)
s.attach(g2)
assert len(s.children) == 2
assert s.receive_signals in g1.callbacks["remove"]
def test_remove(PopulatedScene):
item = PopulatedScene.children[0]
assert len(PopulatedScene.children) == 2
PopulatedScene.remove(item)
assert len(PopulatedScene.children) == 1
assert PopulatedScene.receive_signals not in item.callbacks["remove"]
def test_check_collisions(PopulatedScene):
# attach an object with no collision
coordx = shipimg.get_width() * 2 + 1
coordy = shipimg.get_height() * 2 + 1
g = MockGameObj(coordx, coordy, shipimg)
PopulatedScene.attach(g)
PopulatedScene.check_collisions()
assert len(PopulatedScene.children[0].testlist) == 1
assert "collision" in PopulatedScene.children[0].testlist[0]
assert len(PopulatedScene.children[1].testlist) == 1
assert "collision" in PopulatedScene.children[1].testlist[0]
assert len(PopulatedScene.children[2].testlist) == 0