Mercurial > pyntnclick
annotate gamelib/tests/game_logic_utils.py @ 536:49b6a3d31ee7
PEP-8 tests.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 12:05:13 +0200 |
parents | 0ce08d5e2acb |
children | d6ded808cc33 |
rev | line source |
---|---|
60 | 1 import unittest |
2 | |
3 from gamelib import state | |
4 | |
5 | |
527
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
6 # Monkey-patch albow.resource.get_image to not do alpha-conversion, |
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
7 # which would require pygame display intialisation, which we don't |
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
8 # really want in the tests. |
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
9 import albow.resource |
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
10 |
536 | 11 |
527
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
12 def get_image_unoptimized(*names, **kw): |
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
13 kw.setdefault('optimize', False) |
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
14 return albow.resource._get_image(names, **kw) |
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
15 |
6ef7cc57b94b
Monkey-patch albow.resource.get_image() in tests to avoid having to set up pygame display stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
491
diff
changeset
|
16 albow.resource.get_image = get_image_unoptimized |
60 | 17 |
18 | |
19 class GameLogicTestCase(unittest.TestCase): | |
20 CURRENT_SCENE = None | |
21 | |
22 def setUp(self): | |
23 self.state = state.initial_state() | |
24 self.state.set_current_scene(self.CURRENT_SCENE) | |
25 | |
254 | 26 def tearDown(self): |
27 for item in self.state.items.values(): | |
28 if isinstance(item, state.CloneableItem): | |
29 type(item)._counter = 0 | |
30 | |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
31 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
|
32 gizmo = self.state.current_scene |
60 | 33 if thing is not None: |
34 gizmo = gizmo.things[thing] | |
35 gizmo.set_data(key, value) | |
36 | |
37 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
|
38 gizmo = self.state.current_scene |
452
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
39 if self.state.current_detail is not None: |
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
40 gizmo = self.state.current_detail |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
41 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
|
42 gizmo = self.state.scenes[scene] |
60 | 43 if thing is not None: |
44 gizmo = gizmo.things[thing] | |
45 self.assertEquals(value, gizmo.get_data(key)) | |
46 | |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
47 def assert_inventory_item(self, item, in_inventory=True): |
331
a1ffc919d0c1
Add is_in_inventory helper
Neil Muller <neil@dip.sun.ac.za>
parents:
254
diff
changeset
|
48 self.assertEquals(in_inventory, self.state.is_in_inventory(item)) |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
49 |
190
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
50 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
|
51 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
|
52 |
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
53 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
|
54 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
|
55 |
247
0bad554d0926
More tests, some cryo room fixes and docs in the build.
Jeremy Thurgood <firxen@gmail.com>
parents:
190
diff
changeset
|
56 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
|
57 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
|
58 |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
59 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
|
60 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
|
61 |
355
bfb6c682b4fb
Updated walkthrough test as far as completed environmental puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents:
331
diff
changeset
|
62 def assert_current_detail(self, scene): |
bfb6c682b4fb
Updated walkthrough test as far as completed environmental puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents:
331
diff
changeset
|
63 self.assertEquals(scene, self.state.current_detail.name) |
bfb6c682b4fb
Updated walkthrough test as far as completed environmental puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents:
331
diff
changeset
|
64 |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
65 def handle_result(self, result): |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
66 if result is None: |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
67 return None |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
68 if hasattr(result, 'process'): |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
69 if result.detail_view: |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
70 self.state.set_current_detail(result.detail_view) |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
71 return result |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
72 return [self.handle_result(r) for r in result] |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
73 |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
74 def interact_thing(self, thing, item=None): |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
75 item_obj = None |
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
76 if item is not None: |
254 | 77 self.assert_inventory_item(item) |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
78 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
|
79 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
|
80 result = thing_container.things[thing].interact(item_obj) |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
81 return self.handle_result(result) |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
82 |
254 | 83 def interact_item(self, target_item, item): |
84 self.assert_inventory_item(target_item) | |
85 item_obj = self.state.items[item] | |
86 target_obj = self.state.items[target_item] | |
491
9f488671c02e
No more state in Item interacts.
Jeremy Thurgood <firxen@gmail.com>
parents:
452
diff
changeset
|
87 result = target_obj.interact(item_obj) |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
88 return self.handle_result(result) |
254 | 89 |
452
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
90 def close_detail(self): |
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
91 self.state.set_current_detail(None) |