Skip to content

Commit 2c47c14

Browse files
author
Johannes Gijsbers
committed
Do a semantic comparison of messages when the test fails.
1 parent b1771f7 commit 2c47c14

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

test/test_mailgw.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# but WITHOUT ANY WARRANTY; without even the implied warranty of
99
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1010
#
11-
# $Id: test_mailgw.py,v 1.59 2003-11-03 19:08:41 jlgijsbers Exp $
11+
# $Id: test_mailgw.py,v 1.60 2003-11-03 22:23:02 jlgijsbers Exp $
1212

1313
import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822
1414

@@ -28,32 +28,40 @@ def __init__(self, s):
2828
rfc822.Message.__init__(self, StringIO(s.strip()))
2929

3030
def __eq__(self, other):
31-
del self['date'], other['date']
32-
3331
return (self.dict == other.dict and
3432
self.fp.read() == other.fp.read())
3533

36-
# TODO: Do a semantic diff instead of a straight text diff when a test fails.
3734
class DiffHelper:
38-
def compareMessages(self, s2, s1):
35+
def compareMessages(self, new, old):
3936
"""Compare messages for semantic equivalence."""
40-
if not Message(s2) == Message(s1):
41-
self.compareStrings(s2, s1)
37+
new, old = Message(new), Message(old)
38+
del new['date'], old['date']
39+
40+
if not new == old:
41+
res = ['Generated message not correct (diff follows):']
42+
43+
for key in new.keys():
44+
if new[key] != old[key]:
45+
res.append(' %s: %s != %s' % (key, old[key], new[key]))
46+
47+
body_diff = self.compareStrings(new.fp.read(), old.fp.read())
48+
if body_diff:
49+
res.append('')
50+
res.extend(body_diff)
51+
52+
raise AssertionError, '\n'.join(res)
4253

4354
def compareStrings(self, s2, s1):
4455
'''Note the reversal of s2 and s1 - difflib.SequenceMatcher wants
4556
the first to be the "original" but in the calls in this file,
4657
the second arg is the original. Ho hum.
4758
'''
48-
# we have to special-case the Date: header here 'cos we can't test
49-
# for it properly
50-
l1=s1.strip().split('\n')
51-
l2=[x for x in s2.strip().split('\n') if not x.startswith('Date: ')]
59+
l1 = s1.strip().split('\n')
60+
l2 = s2.strip().split('\n')
5261
if l1 == l2:
5362
return
54-
5563
s = difflib.SequenceMatcher(None, l1, l2)
56-
res = ['Generated message not correct (diff follows):']
64+
res = []
5765
for value, s1s, s1e, s2s, s2e in s.get_opcodes():
5866
if value == 'equal':
5967
for i in range(s1s, s1e):
@@ -69,7 +77,7 @@ def compareStrings(self, s2, s1):
6977
res.append('- %s'%l1[i])
7078
res.append('+ %s'%l2[j])
7179

72-
raise AssertionError, '\n'.join(res)
80+
return res
7381

7482
class MailgwTestCase(unittest.TestCase, DiffHelper):
7583
count = 0

0 commit comments

Comments
 (0)