Mercurial > pyntnclick
annotate gamelib/tests/game_logic_utils.py @ 729:7d777c9a1af1 pyntnclick
Remove the last suspended-sentence-specific bits from GameLogicTestCase
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Tue, 15 Jan 2013 22:55:08 +0200 |
parents | db0762f1de5a |
children |
rev | line source |
---|---|
60 | 1 import unittest |
2 | |
722
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
3 import pygame.display |
60 | 4 |
722
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
5 import pyntnclick.state |
536 | 6 |
722
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
7 # Disable alpha conversion which requires a screen |
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
8 import pyntnclick.resources |
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
9 pyntnclick.resources.Resources.CONVERT_ALPHA = False |
60 | 10 |
11 | |
12 class GameLogicTestCase(unittest.TestCase): | |
13 CURRENT_SCENE = None | |
729
7d777c9a1af1
Remove the last suspended-sentence-specific bits from GameLogicTestCase
Stefano Rivera <stefano@rivera.za.net>
parents:
728
diff
changeset
|
14 GAME_DESCRIPTION_CLASS = None |
60 | 15 |
16 def setUp(self): | |
722
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
17 # Events require us to initialize the display |
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
18 pygame.display.init() |
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
19 |
729
7d777c9a1af1
Remove the last suspended-sentence-specific bits from GameLogicTestCase
Stefano Rivera <stefano@rivera.za.net>
parents:
728
diff
changeset
|
20 self.game_description = self.GAME_DESCRIPTION_CLASS() |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
21 self.state = self.game_description.initial_state() |
722
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
22 self.state.current_scene = self.state.scenes[self.CURRENT_SCENE] |
60 | 23 |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
24 # We aren't handling events, monkey patch change_scene |
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
25 def change_scene(name): |
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
26 self.state.current_scene = self.state.scenes[name] |
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
27 self.state.change_scene = change_scene |
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
28 |
254 | 29 def tearDown(self): |
30 for item in self.state.items.values(): | |
722
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
31 if isinstance(item, pyntnclick.state.CloneableItem): |
254 | 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 | |
727
657b4da736bc
Fix rest of the test suite
Neil Muller <neil@dip.sun.ac.za>
parents:
724
diff
changeset
|
40 def assert_game_data(self, key, value, thing=None, scene=None, detail=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 |
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
42 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
|
43 gizmo = self.state.scenes[scene] |
727
657b4da736bc
Fix rest of the test suite
Neil Muller <neil@dip.sun.ac.za>
parents:
724
diff
changeset
|
44 if detail is not None: |
657b4da736bc
Fix rest of the test suite
Neil Muller <neil@dip.sun.ac.za>
parents:
724
diff
changeset
|
45 gizmo = self.state.detail_views[detail] |
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): |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
57 return |
190
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
58 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
|
59 |
247
0bad554d0926
More tests, some cryo room fixes and docs in the build.
Jeremy Thurgood <firxen@gmail.com>
parents:
190
diff
changeset
|
60 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
|
61 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
|
62 |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
63 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
|
64 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
|
65 |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
66 def handle_result(self, result): |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
67 if result is None: |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
68 return None |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
69 if hasattr(result, 'process'): |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
70 if result.detail_view: |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
71 self.state.show_detail(result.detail_view) |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
72 return result |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
73 return [self.handle_result(r) for r in result] |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
74 |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
75 def interact_thing(self, thing, item=None, detail=None): |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
76 item_obj = None |
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
77 if item is not None: |
254 | 78 self.assert_inventory_item(item) |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
79 item_obj = self.state.items[item] |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
80 thing_container = self.state.current_scene |
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
81 if detail is not None: |
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
82 thing_container = self.state.detail_views[detail] |
252
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) |