source: nagslang/tests/test_puzzle.py@ 685:7ad528f7d17c

Last change on this file since 685:7ad528f7d17c was 685:7ad528f7d17c, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Flesh out mock objects for puzzle tests

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