def setUp(self): self.conf = XmlConfigParser() self.conf.loadFromString("""<configuration></configuration>""") self.parser = ChivParser(self.conf) self.parser._client = Mock() self.evt_queue = [] def queue_event(evt): self.evt_queue.append(evt) self.queueEvent_patcher = patch.object(self.parser, "queueEvent", wraps=queue_event) self.queueEvent_mock = self.queueEvent_patcher.start()
class ChivTestCase(unittest.TestCase): """ Test case that is suitable for testing Chivalry parser specific features """ @classmethod def setUpClass(cls): from b3.fake import FakeConsole ChivParser.__bases__ = (FakeConsole, ) # Now parser inheritance hierarchy is : # ChivParser -> FakeConsole -> Parser def setUp(self): self.conf = XmlConfigParser() self.conf.loadFromString("""<configuration></configuration>""") self.parser = ChivParser(self.conf) self.parser._client = Mock() self.evt_queue = [] def queue_event(evt): self.evt_queue.append(evt) self.queueEvent_patcher = patch.object(self.parser, "queueEvent", wraps=queue_event) self.queueEvent_mock = self.queueEvent_patcher.start() def tearDown(self): self.queueEvent_patcher.stop() if hasattr(self, "parser"): del self.parser.clients self.parser.working = False def clear_events(self): """ clear the event queue, so when assert_has_event is called, it will look only at the newly caught events. """ self.evt_queue = [] def assert_has_event(self, event_type, data=WHATEVER, client=WHATEVER, target=WHATEVER): """ assert that self.evt_queue contains at least one event for the given type that has the given characteristics. """ assert isinstance(event_type, basestring) expected_event = self.parser.getEvent(event_type, data, client, target) if not len(self.evt_queue): self.fail("expecting %s. Got no event instead" % expected_event) elif len(self.evt_queue) == 1: actual_event = self.evt_queue[0] self.assertEqual(expected_event.type, actual_event.type) if data != WHATEVER: self.assertEqual(expected_event.data, actual_event.data) if client != WHATEVER: self.assertTrue( client_equal(expected_event.client, actual_event.client)) if target != WHATEVER: self.assertTrue( client_equal(expected_event.target, actual_event.target)) else: for evt in self.evt_queue: if expected_event.type == evt.type \ and (expected_event.data == evt.data or data == WHATEVER)\ and (client_equal(expected_event.client, evt.client) or client == WHATEVER)\ and (client_equal(expected_event.target, evt.target) or target == WHATEVER): return self.fail("expecting event %s. Got instead: %s" % (expected_event, map(str, self.evt_queue))) def assert_has_not_event(self, event_type, data=None, client=None, target=None): """ assert that self.evt_queue does not contain at least one event for the given type that has the given characteristics. """ assert isinstance(event_type, basestring) unexpected_event = self.parser.getEvent(event_type, data, client, target) if not len(self.evt_queue): return else: def event_match(evt): return (unexpected_event.type == evt.type and (data is None or data == evt.data) and (client is None or client_equal(client, evt.client)) and (target is None or client_equal(target, evt.target))) if any(map(event_match, self.evt_queue)): self.fail("not expecting event %s" % (filter(event_match, self.evt_queue)))
class ChivTestCase(unittest.TestCase): """ Test case that is suitable for testing Chivalry parser specific features """ @classmethod def setUpClass(cls): from b3.fake import FakeConsole ChivParser.__bases__ = (FakeConsole,) # Now parser inheritance hierarchy is : # ChivParser -> FakeConsole -> Parser def setUp(self): self.conf = XmlConfigParser() self.conf.loadFromString("""<configuration></configuration>""") self.parser = ChivParser(self.conf) self.parser._client = Mock() self.evt_queue = [] def queue_event(evt): self.evt_queue.append(evt) self.queueEvent_patcher = patch.object(self.parser, "queueEvent", wraps=queue_event) self.queueEvent_mock = self.queueEvent_patcher.start() def tearDown(self): self.queueEvent_patcher.stop() if hasattr(self, "parser"): del self.parser.clients self.parser.working = False def clear_events(self): """ clear the event queue, so when assert_has_event is called, it will look only at the newly caught events. """ self.evt_queue = [] def assert_has_event(self, event_type, data=WHATEVER, client=WHATEVER, target=WHATEVER): """ assert that self.evt_queue contains at least one event for the given type that has the given characteristics. """ assert isinstance(event_type, basestring) expected_event = self.parser.getEvent(event_type, data, client, target) if not len(self.evt_queue): self.fail("expecting %s. Got no event instead" % expected_event) elif len(self.evt_queue) == 1: actual_event = self.evt_queue[0] self.assertEqual(expected_event.type, actual_event.type) if data != WHATEVER: self.assertEqual(expected_event.data, actual_event.data) if client != WHATEVER: self.assertTrue(client_equal(expected_event.client, actual_event.client)) if target != WHATEVER: self.assertTrue(client_equal(expected_event.target, actual_event.target)) else: for evt in self.evt_queue: if expected_event.type == evt.type \ and (expected_event.data == evt.data or data == WHATEVER)\ and (client_equal(expected_event.client, evt.client) or client == WHATEVER)\ and (client_equal(expected_event.target, evt.target) or target == WHATEVER): return self.fail("expecting event %s. Got instead: %s" % (expected_event, map(str, self.evt_queue))) def assert_has_not_event(self, event_type, data=None, client=None, target=None): """ assert that self.evt_queue does not contain at least one event for the given type that has the given characteristics. """ assert isinstance(event_type, basestring) unexpected_event = self.parser.getEvent(event_type, data, client, target) if not len(self.evt_queue): return else: def event_match(evt): return ( unexpected_event.type == evt.type and (data is None or data == evt.data) and (client is None or client_equal(client, evt.client)) and (target is None or client_equal(target, evt.target)) ) if any(map(event_match, self.evt_queue)): self.fail("not expecting event %s" % (filter(event_match, self.evt_queue)))