class AdminTestCase(unittest.TestCase):
    """
    Test case that is suitable for testing Insurgency parser specific features with the B3 admin plugin available
    """

    @classmethod
    def setUpClass(cls):
        from b3.fake import FakeConsole
        InsurgencyParser.__bases__ = (FakeConsole,)
        # Now parser inheritance hierarchy is :
        # InsurgencyParser -> FakeConsole -> Parser

    def setUp(self):
        self.status_response = None  # defaults to STATUS_RESPONSE module attribute
        self.conf = XmlConfigParser()
        self.conf.loadFromString("""<configuration></configuration>""")
        self.parser = InsurgencyParser(self.conf)
        self.parser.output = Mock()
        self.parser.output.write = Mock(wraps=sys.stdout.write)
        when(self.parser).is_sourcemod_installed().thenReturn(True)
        adminPlugin_conf = CfgConfigParser()
        adminPlugin_conf.load(ADMIN_CONFIG_FILE)
        adminPlugin = AdminPlugin(self.parser, adminPlugin_conf)
        adminPlugin.onLoadConfig()
        adminPlugin.onStartup()
        when(self.parser).getPlugin('admin').thenReturn(adminPlugin)
        when(self.parser).getAllAvailableMaps().thenReturn (['buhriz', 'district', 'sinjar', 'siege', 'uprising', 'ministry', 'revolt', 'heights', 'contact', 'peak', 'panj', 'market'])
        when(self.parser).getMap().thenReturn('buhriz')
        self.parser.startup()
        self.parser.patch_b3_admin_plugin() # seems that without this the test module doesn't patch the admin plugin

    def tearDown(self):
        if hasattr(self, "parser"):
            del self.parser.clients
            self.parser.working = False
    def setUp(self):
        self.status_response = None  # defaults to STATUS_RESPONSE module attribute
        self.conf = XmlConfigParser()
        self.conf.loadFromString("""<configuration></configuration>""")
        self.parser = InsurgencyParser(self.conf)
        self.parser.output = Mock()
        self.parser.output.write = Mock(wraps=self.output_write)
        when(self.parser).is_sourcemod_installed().thenReturn(True)
        when(self.parser).getMap().thenReturn('buhriz')

        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()
        self.parser.startup()
 def setUp(self):
     self.status_response = None  # defaults to STATUS_RESPONSE module attribute
     self.conf = XmlConfigParser()
     self.conf.loadFromString("""<configuration></configuration>""")
     self.parser = InsurgencyParser(self.conf)
     self.parser.output = Mock()
     self.parser.output.write = Mock(wraps=sys.stdout.write)
     when(self.parser).is_sourcemod_installed().thenReturn(True)
     adminPlugin_conf = CfgConfigParser()
     adminPlugin_conf.load(ADMIN_CONFIG_FILE)
     adminPlugin = AdminPlugin(self.parser, adminPlugin_conf)
     adminPlugin.onLoadConfig()
     adminPlugin.onStartup()
     when(self.parser).getPlugin('admin').thenReturn(adminPlugin)
     when(self.parser).getAllAvailableMaps().thenReturn([
         'buhriz', 'district', 'sinjar', 'siege', 'uprising', 'ministry',
         'revolt', 'heights', 'contact', 'peak', 'panj', 'market'
     ])
     when(self.parser).getMap().thenReturn('buhriz')
     self.parser.startup()
     self.parser.patch_b3_admin_plugin(
     )  # seems that without this the test module doesn't patch the admin plugin
class AdminTestCase(unittest.TestCase):
    """
    Test case that is suitable for testing Insurgency parser specific features with the B3 admin plugin available
    """
    @classmethod
    def setUpClass(cls):
        from b3.fake import FakeConsole
        InsurgencyParser.__bases__ = (FakeConsole, )
        # Now parser inheritance hierarchy is :
        # InsurgencyParser -> FakeConsole -> Parser

    def setUp(self):
        self.status_response = None  # defaults to STATUS_RESPONSE module attribute
        self.conf = XmlConfigParser()
        self.conf.loadFromString("""<configuration></configuration>""")
        self.parser = InsurgencyParser(self.conf)
        self.parser.output = Mock()
        self.parser.output.write = Mock(wraps=sys.stdout.write)
        when(self.parser).is_sourcemod_installed().thenReturn(True)
        adminPlugin_conf = CfgConfigParser()
        adminPlugin_conf.load(ADMIN_CONFIG_FILE)
        adminPlugin = AdminPlugin(self.parser, adminPlugin_conf)
        adminPlugin.onLoadConfig()
        adminPlugin.onStartup()
        when(self.parser).getPlugin('admin').thenReturn(adminPlugin)
        when(self.parser).getAllAvailableMaps().thenReturn([
            'buhriz', 'district', 'sinjar', 'siege', 'uprising', 'ministry',
            'revolt', 'heights', 'contact', 'peak', 'panj', 'market'
        ])
        when(self.parser).getMap().thenReturn('buhriz')
        self.parser.startup()
        self.parser.patch_b3_admin_plugin(
        )  # seems that without this the test module doesn't patch the admin plugin

    def tearDown(self):
        if hasattr(self, "parser"):
            del self.parser.clients
            self.parser.working = False
    def setUp(self):
        self.status_response = None  # defaults to STATUS_RESPONSE module attribute
        self.conf = XmlConfigParser()
        self.conf.loadFromString("""<configuration></configuration>""")
        self.parser = InsurgencyParser(self.conf)
        self.parser.output = Mock()
        self.parser.output.write = Mock(wraps=self.output_write)
        when(self.parser).is_sourcemod_installed().thenReturn(True)
        when(self.parser).getMap().thenReturn('buhriz')

        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()
        self.parser.startup()
 def setUp(self):
     self.status_response = None  # defaults to STATUS_RESPONSE module attribute
     self.conf = XmlConfigParser()
     self.conf.loadFromString("""<configuration></configuration>""")
     self.parser = InsurgencyParser(self.conf)
     self.parser.output = Mock()
     self.parser.output.write = Mock(wraps=sys.stdout.write)
     when(self.parser).is_sourcemod_installed().thenReturn(True)
     adminPlugin_conf = CfgConfigParser()
     adminPlugin_conf.load(ADMIN_CONFIG_FILE)
     adminPlugin = AdminPlugin(self.parser, adminPlugin_conf)
     adminPlugin.onLoadConfig()
     adminPlugin.onStartup()
     when(self.parser).getPlugin('admin').thenReturn(adminPlugin)
     when(self.parser).getAllAvailableMaps().thenReturn (['buhriz', 'district', 'sinjar', 'siege', 'uprising', 'ministry', 'revolt', 'heights', 'contact', 'peak', 'panj', 'market'])
     when(self.parser).getMap().thenReturn('buhriz')
     self.parser.startup()
     self.parser.patch_b3_admin_plugin() # seems that without this the test module doesn't patch the admin plugin
class InsurgencyTestCase(unittest.TestCase):
    """
    Test case that is suitable for testing Insurgency parser specific features
    """
    @classmethod
    def setUpClass(cls):
        from b3.fake import FakeConsole
        InsurgencyParser.__bases__ = (FakeConsole, )
        # Now parser inheritance hierarchy is :
        # InsurgencyParser -> FakeConsole -> Parser

    def setUp(self):
        self.status_response = None  # defaults to STATUS_RESPONSE module attribute
        self.conf = XmlConfigParser()
        self.conf.loadFromString("""<configuration></configuration>""")
        self.parser = InsurgencyParser(self.conf)
        self.parser.output = Mock()
        self.parser.output.write = Mock(wraps=self.output_write)
        when(self.parser).is_sourcemod_installed().thenReturn(True)
        when(self.parser).getMap().thenReturn('buhriz')

        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()
        self.parser.startup()

    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, str)
        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, list(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, str)
        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" %
                          (list(filter(event_match, self.evt_queue))))

    def output_write(self, *args, **kwargs):
        """
        Used to override parser self.output.write method so we can control the
        response given to the 'status' rcon command
        """
        if len(args) and args[0] == "status":
            if self.status_response is not None:
                return self.status_response
            else:
                return STATUS_RESPONSE
class InsurgencyTestCase(unittest.TestCase):
    """
    Test case that is suitable for testing Insurgency parser specific features
    """

    @classmethod
    def setUpClass(cls):
        from b3.fake import FakeConsole
        InsurgencyParser.__bases__ = (FakeConsole,)
        # Now parser inheritance hierarchy is :
        # InsurgencyParser -> FakeConsole -> Parser

    def setUp(self):
        self.status_response = None  # defaults to STATUS_RESPONSE module attribute
        self.conf = XmlConfigParser()
        self.conf.loadFromString("""<configuration></configuration>""")
        self.parser = InsurgencyParser(self.conf)
        self.parser.output = Mock()
        self.parser.output.write = Mock(wraps=self.output_write)
        when(self.parser).is_sourcemod_installed().thenReturn(True)
        when(self.parser).getMap().thenReturn('buhriz')

        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()
        self.parser.startup()

    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)))

    def output_write(self, *args, **kwargs):
        """
        Used to override parser self.output.write method so we can control the
        response given to the 'status' rcon command
        """
        if len(args) and args[0] == "status":
            if self.status_response is not None:
                return self.status_response
            else:
                return STATUS_RESPONSE