source: nagslang/tests/test_puzzle.py@ 201:3495a2025bc6

Last change on this file since 201:3495a2025bc6 was 201:3495a2025bc6, checked in by Stefano Rivera <stefano@…>, 8 years ago

Break puzzlers out of game_object.py

File size: 2.5 KB
RevLine 
[110]1from unittest import TestCase
2
3from nagslang.constants import COLLISION_TYPE_OTHER, SWITCH_PUSHERS
[201]4from nagslang import puzzle
[110]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
[201]32class FakePuzzler(puzzle.Puzzler):
[110]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
[186]46 def assert_collide_state(self, expected, shapes, collision_types):
[125]47 gobj = FakeGameObject(None, FakeSpace(*shapes))
[186]48 puzzler = self.mkpuzzler(
[201]49 gobj, puzzle.CollidePuzzler, *collision_types)
[125]50 self.assertEqual(expected, puzzler.get_state())
[110]51
[186]52 def test_collide_puzzler(self):
53 self.assert_collide_state(False, [], [])
54 self.assert_collide_state(False, [FakeShape()], SWITCH_PUSHERS)
[110]55
56 for collision_type in SWITCH_PUSHERS:
[186]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)
[110]61
62 def test_state_proxy_puzzler(self):
[201]63 glue = puzzle.PuzzleGlue()
64 puzzler = puzzle.StateProxyPuzzler('faker')
[110]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())
[145]72
73 def test_glue_add_component(self):
[201]74 glue = puzzle.PuzzleGlue()
[145]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.