comparison pyntnclick/tests/mad_clicker.py @ 854:79b5c1be9a5e default tip

Remove pyntnclick, it's its own library, now
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 21 Jun 2014 22:06:09 +0200
parents f95830b58336
children
comparison
equal deleted inserted replaced
852:f95830b58336 854:79b5c1be9a5e
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, because we don't allow seqs to contain seqs
18 if not self.check_result_obj(obj):
19 for subobj in obj:
20 if not self.check_result_obj(subobj):
21 return False
22 return True
23
24 def _format_item(self, item):
25 return "%s (%s)" % (item.name, item)
26
27 def _format_thing(self, thing):
28 if not hasattr(thing, 'current_interact'):
29 return self._format_item(thing)
30 interact_name = None
31 if thing.current_interact and thing.interacts:
32 for name in thing.interacts:
33 if thing.interacts[name] == thing.current_interact:
34 interact_name = name
35 break
36 if interact_name:
37 return "%s:%s (%s %s)" % (thing.name, interact_name,
38 thing, thing.current_interact)
39 elif thing.current_interact:
40 return "%s:<no name found> (%s %s)" % (thing.name, thing,
41 thing.current_interact)
42 else:
43 return "%s:<no interact> (%s %s)" % (thing.name, thing)
44
45 def format_error(self, thing, item, exception):
46 if not item:
47 msg = ("Unexpected result for interact with no item for %s"
48 % self._format_thing(thing))
49 else:
50 msg = ("Unexpected result for interact with item %s with %s" %
51 (self._format_item(item), self._format_thing(thing)))
52 if exception:
53 return "Exception raised %s:\nTest failed: %s" % (exception, msg)
54 return msg
55
56 def do_thing(self, thing, item):
57 try:
58 if item:
59 # We're interacting with an item in the inventory
60 self.state.add_inventory_item(item.name)
61 self.assertEqual(self.check_result(thing.interact(item)), True,
62 self.format_error(thing, item, None))
63 except self.failureException:
64 raise
65 except Exception, details:
66 raise self.failureException(self.format_error(thing, item,
67 details))
68 self.clear_inventory()
69 self.clear_event_queue()
70
71 def do_item(self, item, item2):
72 try:
73 self.state.add_inventory_item(item.name)
74 if item2:
75 self.state.add_inventory_item(item2.name)
76 self.assertEqual(self.check_result(item.interact(item2)), True,
77 self.format_error(item, item2, None))
78 except self.failureException:
79 raise
80 except Exception, details:
81 raise self.failureException(self.format_error(item, item2,
82 details))
83 self.clear_inventory()
84 self.clear_event_queue()
85
86 def do_mad_clicker(self):
87 """Implement frantic clicking behaviour"""
88 for scene in self.state.scenes.values():
89 self.state.data.set_current_scene(scene.name)
90 for thing in scene.things.values():
91 for interact_name in thing.interacts:
92 thing._set_interact(interact_name)
93 self.do_thing(thing, None)
94 for item in self.state.items.values():
95 self.do_thing(thing, item)
96 for detail in self.state.detail_views.values():
97 for thing in detail.things.values():
98 for interact_name in thing.interacts:
99 thing._set_interact(interact_name)
100 self.do_thing(thing, None)
101 for item in self.state.items.values():
102 self.do_thing(thing, item)
103 for item in self.state.items.values():
104 self.do_item(item, None)
105 for item2 in self.state.items.values():
106 self.do_item(item, item2)