comparison tests/conftest.py @ 365:a9929e0b8f66

tests: Changes to output report and hack for comparing outputs. * Still some problems with newlines and YAML so might as well strip both the actual and expected outputs. * More fancy output for comparing strings.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 03 May 2015 18:40:05 -0700
parents 1f22d4b10fef
children 2d5f2289885a
comparison
equal deleted inserted replaced
364:81480d0219ba 365:a9929e0b8f66
95 "Can't access output file %s: %s" % (key, e)]) 95 "Can't access output file %s: %s" % (key, e)])
96 96
97 expected = expected_partial_files[key] 97 expected = expected_partial_files[key]
98 # HACK because for some reason PyYAML adds a new line for those 98 # HACK because for some reason PyYAML adds a new line for those
99 # and I have no idea why. 99 # and I have no idea why.
100 actual = actual.rstrip('\n')
100 expected = expected.rstrip('\n') 101 expected = expected.rstrip('\n')
101 cmpres = _compare_str(expected, actual, key) 102 cmpres = _compare_str(expected, actual, key)
102 if cmpres: 103 if cmpres:
103 raise ExpectedBakeOutputError(cmpres) 104 raise ExpectedBakeOutputError(cmpres)
104 105
186 def _compare_str(left, right, path): 187 def _compare_str(left, right, path):
187 if left == right: 188 if left == right:
188 return None 189 return None
189 for i in range(min(len(left), len(right))): 190 for i in range(min(len(left), len(right))):
190 if left[i] != right[i]: 191 if left[i] != right[i]:
191 start = max(0, i - 5) 192 start = max(0, i - 15)
192 lend = min(len(left), i + 5) 193 marker_offset = min(15, (i - start)) + 3
193 rend = min(len(right), i + 5) 194
194 return ["Items '%s' differ at index %d:" % (path, i), 195 lend = min(len(left), i + 15)
195 left[start:lend], 196 rend = min(len(right), i + 15)
196 (' ' * start + '^'), 197
197 right[start:rend], 198 return ["Items '%s' differ at index %d:" % (path, i), '',
198 (' ' * start + '^')] 199 "Left:", left, '',
200 "Right:", right, '',
201 "Difference:",
202 repr(left[start:lend]),
203 (' ' * marker_offset + '^'),
204 repr(right[start:rend]),
205 (' ' * marker_offset + '^')]
199 if len(left) > len(right): 206 if len(left) > len(right):
200 return ["Left is longer.", 207 return ["Left is longer.",
201 "Left '%s': " % path, left, 208 "Left '%s': " % path, left,
202 "Right '%s': " % path, right, 209 "Right '%s': " % path, right,
203 "Extra items: %r" % left[len(right):]] 210 "Extra items: %r" % left[len(right):]]