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