D-Bus Testing: ensure that we have assertRegexpMatches()

unittest.assertRegexpMatches() is very useful for command line
output testing. But it is not yet available in Ubuntu Lucid,
the current base line for SyncEvolution releases. Therefore
provide a fallback implementation.
This commit is contained in:
Patrick Ohly 2012-04-16 10:54:52 +02:00
parent aa562e5ef9
commit 5389b8eeed
1 changed files with 28 additions and 0 deletions

View File

@ -850,6 +850,21 @@ class DBusUtil(Timeout):
diff = ''.join(difflib.Differ().compare(expected, res))
self.fail('differences between expected and actual text\n\n' + diff)
def assertRegexpMatchesCustom(self, text, regex, msg=None):
if isinstance(regex, str):
regex = re.compile(regex)
if not regex.search(text):
if msg != None:
self.fail(msg)
else:
self.fail('text does not match regex\n\nText:\n%s\n\nRegex:\n%s' % \
(text, regex.pattern))
# reimplement Python 2.7 assertions only in older Python
if True or not 'assertRegexpMatches' in dir(self):
assertRegexpMatches = assertRegexpMatchesCustom
class TestDBusServer(unittest.TestCase, DBusUtil):
"""Tests for the read-only Server API."""
@ -3597,6 +3612,19 @@ class TestCmdline(unittest.TestCase, DBusUtil):
self.assertEqualDiff([ 'foo\n', 'bar' ], 'foo\nbar')
self.assertEqualDiff([ 'foo\n', 'bar' ], [ 'foo\n', 'bar' ])
# test our own regex match
self.assertRegexpMatchesCustom('foo\nbar\nend', 'bar')
self.assertRegexpMatchesCustom('foo\nbar\nend', 'b.r')
self.assertRegexpMatchesCustom('foo\nbar\nend', re.compile('^b.r$', re.MULTILINE))
try:
self.assertRegexpMatchesCustom('foo\nbar\nend', 'xxx')
except AssertionError, ex:
expected = '''text does not match regex\n\nText:\nfoo\nbar\nend\n\nRegex:\nxxx'''
self.assertTrue(str(ex).endswith(expected), 'actual exception differs\n' + str(ex))
else:
self.fail('''DBusUtil.assertRegexpMatchesCustom() did not fail''')
self.assertRegexpMatches('foo\nbar\nend', 'bar')
lines = "a\nb\nc\n"
lastline = "c\n"
res = lastLine(lines)