*** unittest.py.orig Thu Apr 17 10:08:03 2003 --- unittest.py Fri Apr 25 11:09:34 2003 *************** *** 146,152 **** class TestFailed(Exception): pass ! class TestCase: """A class whose instances are single test cases. --- 146,208 ---- class TestFailed(Exception): pass ! ! def shortdiff(x,y): ! '''shortdiff(x,y) ! ! Compare strings x and y and display differences. ! If the strings are too long, shorten them to fit ! in one line, while still keeping at least some difference. ! ''' ! import difflib ! LINELEN = 79 ! def limit(s): ! if len(s) > LINELEN: ! return s[:LINELEN-3] + '...' ! return s ! ! def firstdiff(s, t): ! span = 1000 ! for pos in range(0, max(len(s), len(t)), span): ! if s[pos:pos+span] != t[pos:pos+span]: ! for index in range(pos, pos+span): ! if s[index:index+1] != t[index:index+1]: ! return index ! ! left = LINELEN/4 ! index = firstdiff(x, y) ! if index > left + 7: ! x = x[:left] + '...' + x[index-4:index+LINELEN] ! y = y[:left] + '...' + y[index-4:index+LINELEN] ! else: ! x, y = x[:LINELEN+1], y[:LINELEN+1] ! left = 0 ! ! cruncher = difflib.SequenceMatcher(None) ! xtags = ytags = "" ! cruncher.set_seqs(x, y) ! editchars = { 'replace': ('^', '^'), ! 'delete': ('-', ''), ! 'insert': ('', '+'), ! 'equal': (' ',' ') } ! for tag, xi1, xi2, yj1, yj2 in cruncher.get_opcodes(): ! lx, ly = xi2 - xi1, yj2 - yj1 ! edits = editchars[tag] ! xtags += edits[0] * lx ! ytags += edits[1] * ly ! ! # Include ellipsis in edits line. ! if left: ! xtags = xtags[:left] + '...' + xtags[left+3:] ! ytags = ytags[:left] + '...' + ytags[left+3:] ! ! diffs = [ x, xtags, y, ytags ] ! if max([len(s) for s in diffs]) < LINELEN: ! return '\n'.join(diffs) ! ! diffs = [ limit(s) for s in diffs ] ! return '\n'.join(diffs) ! class TestCase: """A class whose instances are single test cases. *************** *** 317,324 **** operator. """ if first != second: raise self.failureException, \ ! (msg or '%s != %s' % (`first`, `second`)) def failIfEqual(self, first, second, msg=None): """Fail if the two objects are equal as determined by the '==' --- 373,383 ---- operator. """ if first != second: + reprfirst, reprsecond = repr(first), repr(second) + if not msg and len(reprfirst) + len(reprsecond) > 60: + msg = "failUnlessEqual\n" + shortdiff(reprfirst, reprsecond) raise self.failureException, \ ! (msg or '%s != %s' % (reprfirst, reprsecond)) def failIfEqual(self, first, second, msg=None): """Fail if the two objects are equal as determined by the '=='