'Asserts that the expected number of messages have been attached.
The expected number of messages can be specified per message type.
Usage would look like ``self.assertMessageCount(success=1)``.'

def assertMessageCount(self, response=None, **kwargs):

    temp_req = self.client.request(**{'wsgi.input': None})
    temp_req.COOKIES = self.client.cookies
    storage = default_storage(temp_req)
    messages = []
    if (response is None):
        if ('messages' in self.client.cookies):
            message_cookie = self.client.cookies['messages'].value
            messages = storage._decode(message_cookie)
    elif (hasattr(response, 'context') and ('messages' in response.context)):
        messages = response.context['messages']
    elif (hasattr(response, '_request') and hasattr(response._request, '_messages')):
        messages = response._request._messages._queued_messages
    if ((not any(kwargs.values())) and (not messages)):
        return
    if (any(kwargs.values()) and (not messages)):
        error_msg = 'Messages    were    expected,    but    none    were    set.'
        assert (0 == sum(kwargs.values())), error_msg
    for (msg_type, count) in kwargs.items():
        msgs = [force_text(m.message) for m in messages if (msg_type in m.tags)]
        assert (len(msgs) == count), ('%s    messages    not    as    expected:    %s' % (msg_type.title(), ',    '.join(msgs)))
