Mercurial > pyntnclick
annotate gamelib/tests/game_logic_utils.py @ 528:0ce08d5e2acb
pyflakes-induced cleanup.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Fri, 10 Sep 2010 18:47:17 +0200 |
parents | 6ef7cc57b94b |
children | 49b6a3d31ee7 |
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 |
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
|
11 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
|
12 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
|
13 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
|
14 |
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 albow.resource.get_image = get_image_unoptimized |
60 | 16 |
17 | |
18 class GameLogicTestCase(unittest.TestCase): | |
19 CURRENT_SCENE = None | |
20 | |
21 def setUp(self): | |
22 self.state = state.initial_state() | |
23 self.state.set_current_scene(self.CURRENT_SCENE) | |
24 | |
254 | 25 def tearDown(self): |
26 for item in self.state.items.values(): | |
27 if isinstance(item, state.CloneableItem): | |
28 type(item)._counter = 0 | |
29 | |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
30 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
|
31 gizmo = self.state.current_scene |
60 | 32 if thing is not None: |
33 gizmo = gizmo.things[thing] | |
34 gizmo.set_data(key, value) | |
35 | |
36 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
|
37 gizmo = self.state.current_scene |
452
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
38 if self.state.current_detail is not None: |
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
39 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
|
40 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
|
41 gizmo = self.state.scenes[scene] |
60 | 42 if thing is not None: |
43 gizmo = gizmo.things[thing] | |
44 self.assertEquals(value, gizmo.get_data(key)) | |
45 | |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
46 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
|
47 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
|
48 |
190
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
49 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
|
50 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
|
51 |
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
52 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
|
53 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
|
54 |
247
0bad554d0926
More tests, some cryo room fixes and docs in the build.
Jeremy Thurgood <firxen@gmail.com>
parents:
190
diff
changeset
|
55 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
|
56 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
|
57 |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
58 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
|
59 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
|
60 |
355
bfb6c682b4fb
Updated walkthrough test as far as completed environmental puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents:
331
diff
changeset
|
61 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
|
62 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
|
63 |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
64 def handle_result(self, result): |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
65 if result is None: |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
66 return None |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
67 if hasattr(result, 'process'): |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
68 if result.detail_view: |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
69 self.state.set_current_detail(result.detail_view) |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
70 return result |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
71 return [self.handle_result(r) for r in result] |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
72 |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
73 def interact_thing(self, thing, item=None): |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
74 item_obj = None |
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
75 if item is not None: |
254 | 76 self.assert_inventory_item(item) |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
77 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
|
78 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
|
79 result = thing_container.things[thing].interact(item_obj) |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
80 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
|
81 |
254 | 82 def interact_item(self, target_item, item): |
83 self.assert_inventory_item(target_item) | |
84 item_obj = self.state.items[item] | |
85 target_obj = self.state.items[target_item] | |
491
9f488671c02e
No more state in Item interacts.
Jeremy Thurgood <firxen@gmail.com>
parents:
452
diff
changeset
|
86 result = target_obj.interact(item_obj) |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
87 return self.handle_result(result) |
254 | 88 |
452
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
89 def close_detail(self): |
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
90 self.state.set_current_detail(None) |
fdf3a6dd476b
Walkthrough test /almost/ finished.
Jeremy Thurgood <firxen@gmail.com>
parents:
365
diff
changeset
|
91 |