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