source: nagslang/tests/test_game_object.py@ 185:dfacd08b8566

Last change on this file since 185:dfacd08b8566 was 145:0c49627920eb, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Load game objects from level.

File size: 2.4 KB
RevLine 
[110]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
[124]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
[110]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):
[124]41 def mkpuzzler(self, gobj, cls, *args, **kw):
42 puzzler = cls(*args, **kw)
43 puzzler.set_game_object(gobj)
44 return puzzler
45
[125]46 def assert_floor_switch(self, expected, shapes):
47 gobj = FakeGameObject(None, FakeSpace(*shapes))
[124]48 puzzler = self.mkpuzzler(gobj, game_object.FloorSwitchPuzzler)
[125]49 self.assertEqual(expected, puzzler.get_state())
[110]50
[125]51 def test_floor_switch_puzzler(self):
52 self.assert_floor_switch(False, [])
53 self.assert_floor_switch(False, [FakeShape()])
[110]54
55 for collision_type in SWITCH_PUSHERS:
[125]56 self.assert_floor_switch(True, [FakeShape(collision_type)])
57 self.assert_floor_switch(
58 True, [FakeShape(), FakeShape(collision_type)])
[110]59
60 def test_state_proxy_puzzler(self):
61 glue = game_object.PuzzleGlue()
62 puzzler = game_object.StateProxyPuzzler('faker')
63 glue.add_component('puzzler', puzzler)
64 faker = FakePuzzler('foo')
65 glue.add_component('faker', faker)
66
67 self.assertEqual('foo', puzzler.get_state())
68 faker.fake_state = 'bar'
69 self.assertEqual('bar', puzzler.get_state())
[145]70
71 def test_glue_add_component(self):
72 glue = game_object.PuzzleGlue()
73 puzzler = FakePuzzler('foo')
74 gobj = FakeGameObject(None, None)
75 gobj.puzzler = FakePuzzler('bar')
76
77 self.assertEqual({}, glue._components)
78 glue.add_component('foo', puzzler)
79 self.assertEqual({'foo': puzzler}, glue._components)
80 glue.add_component('bar', gobj)
81 self.assertEqual(
82 {'foo': puzzler, 'bar': gobj.puzzler}, glue._components)
Note: See TracBrowser for help on using the repository browser.