Mercurial > pyntnclick
comparison pyntnclick/tests/mad_clicker.py @ 743:432cd9d51d80 pyntnclick
Import the 'mad clicker' test stuff
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Wed, 16 Jan 2013 13:57:00 +0200 |
parents | |
children | 4d032d09d599 |
comparison
equal
deleted
inserted
replaced
742:0d5ff96527da | 743:432cd9d51d80 |
---|---|
1 from pyntnclick.tests.game_logic_utils import GameLogicTestCase | |
2 | |
3 | |
4 class MadClickerTestCase(GameLogicTestCase): | |
5 "Provide a 'mad clicker' test to expose potential undefined behaviour" | |
6 | |
7 def check_result_obj(self, obj): | |
8 """Check that the obj is the sort of result obj/seq we expect""" | |
9 if obj is None: | |
10 return True | |
11 if hasattr(obj, 'process'): | |
12 return True | |
13 return False | |
14 | |
15 def check_result(self, obj): | |
16 """Check that the obj is the sort of result obj/seq we expect""" | |
17 # We do it this way, becuase we don't allow seqs to contain seqs | |
18 if obj and not hasattr(obj, 'process'): | |
19 for subobj in obj: | |
20 if not self.check_result_obj(subobj): | |
21 return False | |
22 return True | |
23 else: | |
24 return self.check_result_obj(obj) | |
25 | |
26 def _format_item(self, item): | |
27 return "%s (%s)" % (item.name, item) | |
28 | |
29 def _format_thing(self, thing): | |
30 if not hasattr(thing, 'current_interact'): | |
31 return self._format_item(thing) | |
32 interact_name = None | |
33 if thing.current_interact and thing.interacts: | |
34 for name in thing.interacts: | |
35 if thing.interacts[name] == thing.current_interact: | |
36 interact_name = name | |
37 break | |
38 if interact_name: | |
39 return "%s:%s (%s %s)" % (thing.name, interact_name, | |
40 thing, thing.current_interact) | |
41 elif thing.current_interact: | |
42 return "%s:<no name found> (%s %s)" % (thing.name, thing, | |
43 thing.current_interact) | |
44 else: | |
45 return "%s:<no interact> (%s %s)" % (thing.name, thing) | |
46 | |
47 def format_error(self, thing, item, exception): | |
48 if not item: | |
49 msg = ("Unexpected result for interact with no item for %s" | |
50 % self._format_thing(thing)) | |
51 else: | |
52 msg = ("Unexpected result for interact with item %s with %s" % | |
53 (self._format_item(item), self._format_thing(thing))) | |
54 if exception: | |
55 return "Exception raised %s:\nTest failed: %s" % (exception, msg) | |
56 return msg | |
57 | |
58 def do_thing(self, thing, item): | |
59 try: | |
60 if item: | |
61 # We're interacting with an item in the inventory | |
62 self.state.add_inventory_item(item.name) | |
63 self.assertEqual(self.check_result(thing.interact(item)), True, | |
64 self.format_error(thing, item, None)) | |
65 except self.failureException: | |
66 raise | |
67 except Exception, details: | |
68 raise self.failureException(self.format_error(thing, item, | |
69 details)) | |
70 self.clear_inventory() | |
71 self.clear_event_queue() | |
72 | |
73 def do_item(self, item, item2): | |
74 try: | |
75 self.state.add_inventory_item(item.name) | |
76 if item2: | |
77 self.state.add_inventory_item(item2.name) | |
78 self.assertEqual(self.check_result(item.interact(item2)), True, | |
79 self.format_error(item, item2, None)) | |
80 except self.failureException: | |
81 raise | |
82 except Exception, details: | |
83 raise self.failureException(self.format_error(item, item2, | |
84 details)) | |
85 self.clear_inventory() | |
86 self.clear_event_queue() | |
87 | |
88 def do_mad_clicker(self): | |
89 """Implement frantic clicking behaviour""" | |
90 for scene in self.state.scenes.values(): | |
91 self.state.current_scene = scene | |
92 for thing in scene.things.values(): | |
93 for interact_name in thing.interacts: | |
94 thing.set_interact(interact_name) | |
95 self.do_thing(thing, None) | |
96 for item in self.state.items.values(): | |
97 self.do_thing(thing, item) | |
98 for detail in self.state.detail_views.values(): | |
99 for thing in detail.things.values(): | |
100 for interact_name in thing.interacts: | |
101 thing.set_interact(interact_name) | |
102 self.do_thing(thing, None) | |
103 for item in self.state.items.values(): | |
104 self.do_thing(thing, item) | |
105 for item in self.state.items.values(): | |
106 self.do_item(item, None) | |
107 for item2 in self.state.items.values(): | |
108 self.do_item(item, item2) |