Mercurial > boomslang
annotate pyntnclick/tests/game_logic_utils.py @ 816:eed75a1d50c4 pyntnclick
Better Item handling.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 27 Jan 2013 22:19:39 +0200 |
parents | bdaffaa8b6bf |
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 |
743
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
4 import pygame.event |
60 | 5 |
734
c1d548a76a8c
disable CONVERT_ALPHA in setUp, not at import
Stefano Rivera <stefano@rivera.za.net>
parents:
731
diff
changeset
|
6 import pyntnclick.resources |
722
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
7 import pyntnclick.state |
536 | 8 |
60 | 9 |
10 class GameLogicTestCase(unittest.TestCase): | |
11 CURRENT_SCENE = None | |
729
7d777c9a1af1
Remove the last suspended-sentence-specific bits from GameLogicTestCase
Stefano Rivera <stefano@rivera.za.net>
parents:
728
diff
changeset
|
12 GAME_DESCRIPTION_CLASS = None |
60 | 13 |
14 def setUp(self): | |
722
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
15 # Events require us to initialize the display |
b2e362eacfe9
Update game initialization in gamelib tests
Stefano Rivera <stefano@rivera.za.net>
parents:
692
diff
changeset
|
16 pygame.display.init() |
734
c1d548a76a8c
disable CONVERT_ALPHA in setUp, not at import
Stefano Rivera <stefano@rivera.za.net>
parents:
731
diff
changeset
|
17 # Disable alpha conversion which requires a screen |
c1d548a76a8c
disable CONVERT_ALPHA in setUp, not at import
Stefano Rivera <stefano@rivera.za.net>
parents:
731
diff
changeset
|
18 pyntnclick.resources.Resources.CONVERT_ALPHA = False |
722
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() |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
22 self.scene_stack = [] |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
23 |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
24 # We aren't handling events, monkey patch change_scene and show_detail |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
25 def change_scene(name): |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
26 self.state.data.set_current_scene(name) |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
27 self.scene_stack = [self.state.get_current_scene()] |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
28 self.state.change_scene = change_scene |
60 | 29 |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
30 def show_detail(name): |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
31 self.scene_stack.append(self.state.detail_views[name]) |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
32 self.state.show_detail = show_detail |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
33 |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
34 self.state.change_scene(self.CURRENT_SCENE) |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
35 |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
36 def close_detail(self): |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
37 self.scene_stack.pop() |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
38 self.assertTrue(len(self.scene_stack) > 0) |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
39 |
743
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
40 def clear_event_queue(self): |
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
41 # Since we aren't handling events, we may overflow the pygame |
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
42 # event buffer if we're generating a lot of events |
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
43 pygame.event.clear() |
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
44 |
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
45 def clear_inventory(self): |
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
46 # Remove all items from the inventory, ensuring tool is set to None |
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
47 self.state.set_tool(None) |
750
ef4bda7d623d
Better state and inventory management.
Jeremy Thurgood <firxen@gmail.com>
parents:
743
diff
changeset
|
48 self.state.inventory()[:] = [] |
743
432cd9d51d80
Import the 'mad clicker' test stuff
Neil Muller <neil@dip.sun.ac.za>
parents:
734
diff
changeset
|
49 |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
50 def set_game_data(self, key, value, thing=None): |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
51 gizmo = self.state.get_current_scene() |
60 | 52 if thing is not None: |
53 gizmo = gizmo.things[thing] | |
54 gizmo.set_data(key, value) | |
55 | |
731 | 56 def assert_game_data(self, key, value, thing=None, scene=None, |
57 detail=None): | |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
58 gizmo = self.state.get_current_scene() |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
59 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
|
60 gizmo = self.state.scenes[scene] |
727
657b4da736bc
Fix rest of the test suite
Neil Muller <neil@dip.sun.ac.za>
parents:
724
diff
changeset
|
61 if detail is not None: |
657b4da736bc
Fix rest of the test suite
Neil Muller <neil@dip.sun.ac.za>
parents:
724
diff
changeset
|
62 gizmo = self.state.detail_views[detail] |
60 | 63 if thing is not None: |
64 gizmo = gizmo.things[thing] | |
65 self.assertEquals(value, gizmo.get_data(key)) | |
66 | |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
67 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
|
68 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
|
69 |
190
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
70 def assert_scene_thing(self, thing, in_scene=True): |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
71 self.assertEquals( |
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
72 in_scene, thing in self.state.get_current_scene().things) |
190
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
73 |
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
74 def assert_detail_thing(self, thing, in_detail=True): |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
75 self.assertEquals(in_detail, thing in self.scene_stack[-1].things) |
190
30f2308c1efc
Fix tests and add a (currently unhooked) laser welder.
Jeremy Thurgood <firxen@gmail.com>
parents:
67
diff
changeset
|
76 |
247
0bad554d0926
More tests, some cryo room fixes and docs in the build.
Jeremy Thurgood <firxen@gmail.com>
parents:
190
diff
changeset
|
77 def assert_item_exists(self, item, exists=True): |
816 | 78 try: |
79 self.state.get_item(item) | |
80 self.assertTrue(exists) | |
81 except: | |
82 self.assertFalse(exists) | |
247
0bad554d0926
More tests, some cryo room fixes and docs in the build.
Jeremy Thurgood <firxen@gmail.com>
parents:
190
diff
changeset
|
83 |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
84 def assert_current_scene(self, scene): |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
85 self.assertEquals(scene, self.state.get_current_scene().name) |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
86 |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
87 def handle_result(self, result): |
787
f46c3197e887
Clear the event queue regularly in GameLogicTestCase
Stefano Rivera <stefano@rivera.za.net>
parents:
750
diff
changeset
|
88 self.clear_event_queue() |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
89 if result is None: |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
90 return None |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
91 if hasattr(result, 'process'): |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
92 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
|
93 self.state.show_detail(result.detail_view) |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
94 return result |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
95 return [self.handle_result(r) for r in result] |
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
96 |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
97 def interact_thing(self, thing, item=None, detail=None): |
67
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
98 item_obj = None |
6b0f7364f3bf
Inventory-related game state.
Jeremy Thurgood <firxen@gmail.com>
parents:
60
diff
changeset
|
99 if item is not None: |
254 | 100 self.assert_inventory_item(item) |
816 | 101 item_obj = self.state.get_item(item) |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
102 thing_container = self.scene_stack[-1] |
724
11daadcb06d6
We can now walk through until we hit the first JimLabel
Stefano Rivera <stefano@rivera.za.net>
parents:
722
diff
changeset
|
103 if detail is not None: |
792
bdaffaa8b6bf
Loading and saving! (Plus a bunch of other stuff to make it possible.)
Jeremy Thurgood <firxen@gmail.com>
parents:
787
diff
changeset
|
104 self.assertEqual(detail, thing_container.name) |
252
dfc89bc64fdb
Start of walkthrough "unit test" and associated fixes and tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
105 result = thing_container.things[thing].interact(item_obj) |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
106 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
|
107 |
254 | 108 def interact_item(self, target_item, item): |
109 self.assert_inventory_item(target_item) | |
816 | 110 item_obj = self.state.get_item(item) |
111 target_obj = self.state.get_item(target_item) | |
491
9f488671c02e
No more state in Item interacts.
Jeremy Thurgood <firxen@gmail.com>
parents:
452
diff
changeset
|
112 result = target_obj.interact(item_obj) |
365
45f209fbec2e
Better result handling for tests.
Jeremy Thurgood <firxen@gmail.com>
parents:
355
diff
changeset
|
113 return self.handle_result(result) |