source: nagslang/tests/test_game_object.py @ 186:d63c19003aec

Last change on this file since 186:d63c19003aec was 186:d63c19003aec, checked in by Jeremy Thurgood <firxen@…>, 7 years ago

Some refactoring and fixing, start of better collision handling.

File size: 2.5 KB
Line 
1from unittest import TestCase
2
3from nagslang.constants import COLLISION_TYPE_OTHER, SWITCH_PUSHERS
4from nagslang import game_object
5
6
7class FakeShape(object):
8    def __init__(self, collision_type=COLLISION_TYPE_OTHER):
9        self.collision_type = collision_type
10
11
12class FakeSpace(object):
13    def __init__(self, *shapes):
14        self._shapes = shapes
15
16    def shape_query(self, shape):
17        return self._shapes
18
19
20class FakeGameObject(object):
21    def __init__(self, shape, space):
22        self._shape = shape
23        self._space = space
24
25    def get_shape(self):
26        return self._shape
27
28    def get_space(self):
29        return self._space
30
31
32class FakePuzzler(game_object.Puzzler):
33    def __init__(self, fake_state):
34        self.fake_state = fake_state
35
36    def get_state(self):
37        return self.fake_state
38
39
40class TestPuzzles(TestCase):
41    def mkpuzzler(self, gobj, cls, *args, **kw):
42        puzzler = cls(*args, **kw)
43        puzzler.set_game_object(gobj)
44        return puzzler
45
46    def assert_collide_state(self, expected, shapes, collision_types):
47        gobj = FakeGameObject(None, FakeSpace(*shapes))
48        puzzler = self.mkpuzzler(
49            gobj, game_object.CollidePuzzler, *collision_types)
50        self.assertEqual(expected, puzzler.get_state())
51
52    def test_collide_puzzler(self):
53        self.assert_collide_state(False, [], [])
54        self.assert_collide_state(False, [FakeShape()], SWITCH_PUSHERS)
55
56        for collision_type in SWITCH_PUSHERS:
57            self.assert_collide_state(
58                True, [FakeShape(collision_type)], SWITCH_PUSHERS)
59            self.assert_collide_state(
60                True, [FakeShape(), FakeShape(collision_type)], SWITCH_PUSHERS)
61
62    def test_state_proxy_puzzler(self):
63        glue = game_object.PuzzleGlue()
64        puzzler = game_object.StateProxyPuzzler('faker')
65        glue.add_component('puzzler', puzzler)
66        faker = FakePuzzler('foo')
67        glue.add_component('faker', faker)
68
69        self.assertEqual('foo', puzzler.get_state())
70        faker.fake_state = 'bar'
71        self.assertEqual('bar', puzzler.get_state())
72
73    def test_glue_add_component(self):
74        glue = game_object.PuzzleGlue()
75        puzzler = FakePuzzler('foo')
76        gobj = FakeGameObject(None, None)
77        gobj.puzzler = FakePuzzler('bar')
78
79        self.assertEqual({}, glue._components)
80        glue.add_component('foo', puzzler)
81        self.assertEqual({'foo': puzzler}, glue._components)
82        glue.add_component('bar', gobj)
83        self.assertEqual(
84            {'foo': puzzler, 'bar': gobj.puzzler}, glue._components)
Note: See TracBrowser for help on using the repository browser.