annotate gamelib/tests/game_logic_utils.py @ 252:dfc89bc64fdb

Start of walkthrough "unit test" and associated fixes and tweaks.
author Jeremy Thurgood <firxen@gmail.com>
date Fri, 27 Aug 2010 16:45:47 +0200
parents 0bad554d0926
children ca0c2875ad8f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
1 import unittest
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
3 import pygame
252
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
4 from pygame.locals import SWSURFACE
60
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
5
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
6 from gamelib import state
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
7 from gamelib.constants import SCREEN
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
8
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
9
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
10 # We need this stuff set up so we can load images and whatnot.
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
11 pygame.display.init()
190
30f2308c1efc Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents: 67
diff changeset
12 pygame.font.init()
60
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
13 pygame.display.set_mode(SCREEN, SWSURFACE)
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
14
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
15
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
16 class GameLogicTestCase(unittest.TestCase):
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
17 CURRENT_SCENE = None
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
18
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
19 def setUp(self):
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
20 self.state = state.initial_state()
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
21 self.state.set_current_scene(self.CURRENT_SCENE)
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
22
252
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
23 def set_game_data(self, key, value, thing=None):
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
24 gizmo = self.state.current_scene
60
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
25 if thing is not None:
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
26 gizmo = gizmo.things[thing]
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
27 gizmo.set_data(key, value)
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
28
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
29 def assert_game_data(self, key, value, thing=None, scene=None):
252
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
30 gizmo = self.state.current_scene
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
31 if scene is not None:
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
32 gizmo = self.state.scenes[scene]
60
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
33 if thing is not None:
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
34 gizmo = gizmo.things[thing]
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
35 self.assertEquals(value, gizmo.get_data(key))
90b32447b239 Some test refactorage.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
36
67
6b0f7364f3bf Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents: 60
diff changeset
37 def assert_inventory_item(self, item, in_inventory=True):
6b0f7364f3bf Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents: 60
diff changeset
38 self.assertEquals(in_inventory, self.state.items[item] in self.state.inventory)
6b0f7364f3bf Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents: 60
diff changeset
39
190
30f2308c1efc Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents: 67
diff changeset
40 def assert_scene_thing(self, thing, in_scene=True):
30f2308c1efc Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents: 67
diff changeset
41 self.assertEquals(in_scene, thing in self.state.current_scene.things)
30f2308c1efc Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents: 67
diff changeset
42
30f2308c1efc Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents: 67
diff changeset
43 def assert_detail_thing(self, thing, in_detail=True):
30f2308c1efc Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents: 67
diff changeset
44 self.assertEquals(in_detail, thing in self.state.current_detail.things)
30f2308c1efc Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents: 67
diff changeset
45
247
0bad554d0926 More tests, some cryo room fixes and docs in the build.
Jeremy Thurgood <firxen@gmail.com>
parents: 190
diff changeset
46 def assert_item_exists(self, item, exists=True):
0bad554d0926 More tests, some cryo room fixes and docs in the build.
Jeremy Thurgood <firxen@gmail.com>
parents: 190
diff changeset
47 self.assertEquals(exists, item in self.state.items)
0bad554d0926 More tests, some cryo room fixes and docs in the build.
Jeremy Thurgood <firxen@gmail.com>
parents: 190
diff changeset
48
252
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
49 def assert_current_scene(self, scene):
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
50 self.assertEquals(scene, self.state.current_scene.name)
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
51
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
52 def interact_thing(self, thing, item=None):
67
6b0f7364f3bf Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents: 60
diff changeset
53 item_obj = None
6b0f7364f3bf Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents: 60
diff changeset
54 if item is not None:
6b0f7364f3bf Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents: 60
diff changeset
55 item_obj = self.state.items[item]
252
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
56 thing_container = self.state.current_detail or self.state.current_scene
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
57 result = thing_container.things[thing].interact(item_obj)
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
58 if result and result.detail_view:
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
59 self.state.set_current_detail(result.detail_view)
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
60 return result
dfc89bc64fdb Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 247
diff changeset
61