def test_string_is_term_width(self): a = '' b = 'a' * (term.width - 1) d = diff(a, b) expected_str = [ unchanged('{!s}('.format(type(a))), '@@ {}{},{} {}{},{} @@'.format( remove('-'), remove('0'), remove('0'), insert('+'), insert('0'), insert('{}'.format(term.width - 1))), ' ' + ('{}'.format(insert('+')) * (term.width - 1)), ' ' + ('{}'.format(insert('a')) * (term.width - 1)), unchanged(')') ] self.assertEqual(d.__str__(), '\n'.join(expected_str))
def test_no_context_banner_for_non_sequence(self): set1 = {1, 2} set2 = {'a', 'b'} diff_obj = diff(set1, set2) expected_diff_items = [ '{} {}'.format(remove('-'), remove('1')), '{} {}'.format(remove('-'), remove('2')), '{} {}'.format(insert('+'), insert('a')), '{} {}'.format(insert('+'), insert('b')) ] # allow the expected output to be unordered actual_string = diff_obj.context_blocks[0].__str__() actual_items = actual_string.split('\n') if sys.version_info.major >= 3: self.assertCountEqual(expected_diff_items, actual_items) else: self.assertItemsEqual(expected_diff_items, actual_items)
def test_context_banner_is_correct_for_sequences(self): ''' Context banners should contain the information you need the two original sequences such that you only get the items contained within the displayed context block. ''' seq1 = [0, 1, 2, 3, 0] seq2 = [0, 4, 2, 5, 0] # the useful context for this diff is the slice 1:4 in both sequences s1_start = s2_start = '1' s1_end = s2_end = '4' diff_obj = diff(seq1, seq2) expected_banner = [ '@@ {}{},{} {}{},{} @@'.format( remove('-'), remove(s1_start), remove(s1_end), insert('+'), insert(s2_start), insert(s2_end)) ] expected_diff_items = [ '{} {}'.format(remove('-'), remove('1')), '{} {}'.format(insert('+'), insert('4')), '{} {}'.format(unchanged(' '), unchanged('2')), '{} {}'.format(remove('-'), remove('3')), '{} {}'.format(insert('+'), insert('5')) ] expected_diff_output = '\n'.join(expected_banner + expected_diff_items) # expected_diff_output is unicode type, convert to str for comparison self.assertEqual( diff_obj.context_blocks[0].__str__(), str(expected_diff_output))
def test_mapping_diff_item_str(self): key = 'a' val = [1, 2, 3] di = MappingDiffItem( unchanged, key, insert, val) expected_str = ( unchanged('{!s}: '.format(key)) + insert('{!s}'.format(val))) self.assertEqual(di.__str__(), expected_str)
def test_only_context_blocks_are_displayed(self): a = [1, 0, 0, 0, 0, 1] b = [2, 0, 0, 0, 0, 2] diff_obj = diff(a, b) expected_diff_output = [ unchanged('{!s}('.format(type(a))), '@@ {}{},{} {}{},{} @@'.format( remove('-'), remove('0'), remove('1'), insert('+'), insert('0'), insert('1')), '{} {}'.format(remove('-'), remove('1')), '{} {}'.format(insert('+'), insert('2')), '@@ {}{},{} {}{},{} @@'.format( remove('-'), remove('5'), remove('6'), insert('+'), insert('5'), insert('6')), '{} {}'.format(remove('-'), remove('1')), '{} {}'.format(insert('+'), insert('2')), unchanged(')') ] self.assertEqual(diff_obj.__str__(), '\n'.join(expected_diff_output))
def test_strings_display_on_single_line(self): a = 'this' b = 'that' d = diff(a, b) expected_str = [ unchanged('{!s}('.format(type(a))), '@@ {}{},{} {}{},{} @@'.format( remove('-'), remove('2'), remove('4'), insert('+'), insert('2'), insert('4')), ' {}{}{}{}'.format( remove('-'), remove('-'), insert('+'), insert('+')), ' {}{}{}{}'.format( remove('i'), remove('s'), insert('a'), insert('t')), unchanged(')') ] self.assertEqual(d.__str__(), '\n'.join(expected_str))