@@ -285,20 +285,30 @@ def _compare_eq_iterable(left, right, verbose=0):
285
285
286
286
def _compare_eq_sequence (left , right , verbose = 0 ):
287
287
explanation = []
288
- for i in range (min (len (left ), len (right ))):
288
+ len_left = len (left )
289
+ len_right = len (right )
290
+ for i in range (min (len_left , len_right )):
289
291
if left [i ] != right [i ]:
290
292
explanation += [u"At index %s diff: %r != %r" % (i , left [i ], right [i ])]
291
293
break
292
- if len (left ) > len (right ):
293
- explanation += [
294
- u"Left contains more items, first extra item: %s"
295
- % saferepr (left [len (right )])
296
- ]
297
- elif len (left ) < len (right ):
298
- explanation += [
299
- u"Right contains more items, first extra item: %s"
300
- % saferepr (right [len (left )])
301
- ]
294
+ len_diff = len_left - len_right
295
+
296
+ if len_diff :
297
+ if len_diff > 0 :
298
+ dir_with_more = "Left"
299
+ extra = saferepr (left [len_right ])
300
+ else :
301
+ len_diff = 0 - len_diff
302
+ dir_with_more = "Right"
303
+ extra = saferepr (right [len_left ])
304
+
305
+ if len_diff == 1 :
306
+ explanation += [u"%s contains one more item: %s" % (dir_with_more , extra )]
307
+ else :
308
+ explanation += [
309
+ u"%s contains %d more items, first extra item: %s"
310
+ % (dir_with_more , len_diff , extra )
311
+ ]
302
312
return explanation
303
313
304
314
@@ -319,7 +329,9 @@ def _compare_eq_set(left, right, verbose=0):
319
329
320
330
def _compare_eq_dict (left , right , verbose = 0 ):
321
331
explanation = []
322
- common = set (left ).intersection (set (right ))
332
+ set_left = set (left )
333
+ set_right = set (right )
334
+ common = set_left .intersection (set_right )
323
335
same = {k : left [k ] for k in common if left [k ] == right [k ]}
324
336
if same and verbose < 2 :
325
337
explanation += [u"Omitting %s identical items, use -vv to show" % len (same )]
@@ -331,15 +343,23 @@ def _compare_eq_dict(left, right, verbose=0):
331
343
explanation += [u"Differing items:" ]
332
344
for k in diff :
333
345
explanation += [saferepr ({k : left [k ]}) + " != " + saferepr ({k : right [k ]})]
334
- extra_left = set (left ) - set (right )
335
- if extra_left :
336
- explanation .append (u"Left contains more items:" )
346
+ extra_left = set_left - set_right
347
+ len_extra_left = len (extra_left )
348
+ if len_extra_left :
349
+ explanation .append (
350
+ u"Left contains %d more item%s:"
351
+ % (len_extra_left , "" if len_extra_left == 1 else "s" )
352
+ )
337
353
explanation .extend (
338
354
pprint .pformat ({k : left [k ] for k in extra_left }).splitlines ()
339
355
)
340
- extra_right = set (right ) - set (left )
341
- if extra_right :
342
- explanation .append (u"Right contains more items:" )
356
+ extra_right = set_right - set_left
357
+ len_extra_right = len (extra_right )
358
+ if len_extra_right :
359
+ explanation .append (
360
+ u"Right contains %d more item%s:"
361
+ % (len_extra_right , "" if len_extra_right == 1 else "s" )
362
+ )
343
363
explanation .extend (
344
364
pprint .pformat ({k : right [k ] for k in extra_right }).splitlines ()
345
365
)
0 commit comments