Skip to content

Commit 438a4db

Browse files
committed
polyfill for assertRegexpMatches in Python 2.6
1 parent f231769 commit 438a4db

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

pusher_tests/test_request.py

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import print_function, absolute_import, division
44

55
import unittest
6+
import re
67

78
from pusher import Pusher
89
from pusher.http import Request
@@ -49,6 +50,20 @@ def test_post_signature_generation(self):
4950

5051
json_dumps_mock.assert_called_once_with({u"foo": u"bar"})
5152

53+
# Copied wholesale from https://github.com/python/cpython/blob/2d305e1c46abfcd609bf8b2dff8d2065e6af8ab2/Lib/unittest/case.py#L1279-L1289
54+
# This can be removed when we no longer support Python 2.6
55+
def assertRegexpMatches(self, text, expected_regex, msg=None):
56+
"""Fail the test unless the text matches the regular expression."""
57+
if isinstance(expected_regex, (str, bytes)):
58+
assert expected_regex, "expected_regex must not be empty."
59+
expected_regex = re.compile(expected_regex)
60+
if not expected_regex.search(text):
61+
standardMsg = "Regex didn't match: %r not found in %r" % (
62+
expected_regex.pattern, text)
63+
# _formatMessage ensures the longMessage option is respected
64+
msg = self._formatMessage(msg, standardMsg)
65+
raise self.failureException(msg)
66+
5267
def test_x_pusher_library_header(self):
5368
conf = Pusher.from_url(u'http://key:secret@somehost/apps/4')
5469
req = Request(conf, u'GET', u'/some/obscure/api', {u'foo': u'bar'})

0 commit comments

Comments
 (0)