def test_diff_item_is_a_nested_diff(self): dict1 = {1: 'ab'} dict2 = {1: 'bc'} diff_obj = diff.diff(dict1, dict2) nested_diff = diff.diff('ab', 'bc', _depth=1) diff_item = diff.MappingDiffItem( diff.unchanged, 1, diff.changed, nested_diff) expected_diff_output = '{} {}'.format(diff.changed(' '), diff_item) self.assertEqual( diff_obj.context_blocks[0].__str__(), expected_diff_output)
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.diff(seq1, seq2) expected_banner = [ '@@ {}{},{} {}{},{} @@'.format( diff.remove('-'), diff.remove(s1_start), diff.remove(s1_end), diff.insert('+'), diff.insert(s2_start), diff.insert(s2_end)) ] expected_diff_items = [ '{} {}'.format(diff.remove('-'), diff.remove('1')), '{} {}'.format(diff.insert('+'), diff.insert('4')), '{} {}'.format(diff.unchanged(' '), diff.unchanged('2')), '{} {}'.format(diff.remove('-'), diff.remove('3')), '{} {}'.format(diff.insert('+'), diff.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_no_differences(self): diff_obj = diff.diff([1, 2, 3], [1, 2, 3]) diffs = [ diff.DiffItem(diff.unchanged, 1, (0, 1, 0, 1)), diff.DiffItem(diff.unchanged, 2, (1, 2, 1, 2)), diff.DiffItem(diff.unchanged, 3, (2, 3, 2, 3))] expected_diff = diff.Diff(list, diffs) self.assertEqual(diff_obj, expected_diff)
def test_can_diff_set_type(self): fs1 = frozenset([1, 2, 3]) fs2 = frozenset([2, 3, 4]) diff_obj = diff.diff(fs1, fs2) diffs = [ diff.DiffItem(diff.remove, 1), diff.DiffItem(diff.unchanged, 2), diff.DiffItem(diff.unchanged, 3), diff.DiffItem(diff.insert, 4)] expected_diff = diff.Diff(frozenset, diffs) expected_diff.context_blocks = [ expected_diff.ContextBlock(frozenset, diffs)] self.assertEqual(diff_obj, expected_diff)
def test_can_diff_sequence_type(self): ThreeDPoint = namedtuple('ThreeDPoint', ['x', 'y', 'z']) p1 = ThreeDPoint(0, 0, 0) p2 = ThreeDPoint(0, 0, 1) diff_obj = diff.diff(p1, p2) diffs = [ diff.DiffItem(diff.unchanged, 0, (0, 1, 0, 1)), diff.DiffItem(diff.unchanged, 0, (1, 2, 1, 2)), diff.DiffItem(diff.remove, 0, (2, 3, 2, 2)), diff.DiffItem(diff.insert, 1, (3, 3, 2, 3))] expected_diff = diff.Diff(type(p1), diffs) expected_diff.context_blocks = [ expected_diff.ContextBlock(type(p1), diffs[2:])] self.assertEqual(diff_obj, expected_diff)
def test_single_char_edge_case(self): # handle edge cases 1 # make sure this is handled at the correct level (within diff) d1 = 'a' d2 = 'b' diff_obj = diff.diff(d1, d2) diffs = [ diff.DiffItem(diff.remove, 'a', (0, 1, 0, 0)), diff.DiffItem(diff.insert, 'b', (1, 1, 0, 1)), ] expected_diff = diff.Diff(str, diffs) expected_diff.context_blocks = [ expected_diff.ContextBlock(str, diffs) ] self.assertEqual(diff_obj, expected_diff)
def test_context_limit_is_adjustable(self): map1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4} map2 = {'a': 2, 'b': 2, 'c': 3, 'e': 4} diff_obj = diff.diff(map1, map2, context_limit=1) diffs = [ diff.MappingDiffItem(diff.remove, 'd', diff.remove, 4), diff.MappingDiffItem(diff.unchanged, 'a', diff.remove, 1), diff.MappingDiffItem(diff.unchanged, 'a', diff.insert, 2), diff.MappingDiffItem(diff.unchanged, 'c', diff.unchanged, 3), diff.MappingDiffItem(diff.unchanged, 'b', diff.unchanged, 2), diff.MappingDiffItem(diff.insert, 'e', diff.insert, 4)] expected_diff = diff.Diff(dict, diffs, context_limit=1) expected_diff.context_blocks = [ expected_diff.ContextBlock(dict, diffs[0:3]), expected_diff.ContextBlock(dict, diffs[5:])] self.assertEqual(diff_obj, expected_diff)
def test_can_diff_mapping_type(self): d1 = {'d': 1, 'c': 2, 'b': 3} d2 = {'d': 1, 'c': 2, 'a': 3} # sort by values od1 = OrderedDict(sorted(d1.items(), key=lambda i: i[1])) od2 = OrderedDict(sorted(d2.items(), key=lambda i: i[1])) diff_obj = diff.diff(od1, od2) diffs = [ diff.MappingDiffItem(diff.remove, 'b', diff.remove, 3), diff.MappingDiffItem(diff.unchanged, 'd', diff.unchanged, 1), diff.MappingDiffItem(diff.unchanged, 'c', diff.unchanged, 2), diff.MappingDiffItem(diff.insert, 'a', diff.insert, 3)] expected_diff = diff.Diff(OrderedDict, diffs) expected_diff.context_blocks = [ expected_diff.ContextBlock(OrderedDict, diffs)] self.assertEqual(diff_obj, expected_diff)
def test_no_context_banner_for_non_sequence(self): set1 = {1, 2} set2 = {'a', 'b'} diff_obj = diff.diff(set1, set2) expected_diff_items = [ '{} {}'.format(diff.remove('-'), diff.remove('1')), '{} {}'.format(diff.remove('-'), diff.remove('2')), '{} {}'.format(diff.insert('+'), diff.insert('a')), '{} {}'.format(diff.insert('+'), diff.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_recursive_diff(self): struct1 = [1, {'a': {'a', 'b'}}] struct2 = [1, {'a': {'b'}}] diff_obj = diff.diff(struct1, struct2) depth_2_diffs = [ diff.DiffItem(diff.remove, 'a'), diff.DiffItem(diff.unchanged, 'b')] diff_depth_2 = diff.Diff(set, depth_2_diffs, depth=2) diff_depth_2.context_blocks = [ diff_depth_2.ContextBlock(set, [diff_depth_2.diffs[0]], depth=2)] depth_1_diffs = [ diff.MappingDiffItem( diff.unchanged, 'a', diff.changed, diff_depth_2)] diff_depth_1 = diff.Diff(dict, depth_1_diffs, depth=1) diff_depth_1.context_blocks = [ diff_depth_1.ContextBlock(dict, diff_depth_1.diffs, depth=1)] diffs = [ diff.DiffItem(diff.unchanged, 1, (0, 1, 0, 1)), diff.DiffItem(diff.changed, diff_depth_1, (1, 2, 1, 2))] expected_diff = diff.Diff(list, diffs) expected_diff.context_blocks = [ expected_diff.ContextBlock(list, [diffs[1]])] self.assertEqual(diff_obj, expected_diff)
try: v8.eval_source(m_code) except Exception as x: v8_out = "" if x.args[0] == "bTimeout": v8_err = b"Timeout" elif isinstance(x.args[0], bytes): v8_err = x.args[0] else: v8_err = x.args[0].encode() else: v8_out = v8.result_console() v8_out = ("\n".join(v8_out) + "\n").encode() v8_err = "".encode() signal.alarm(0) report = diff(sm_out, sm_err, v8_out, v8_err) if report is not None: log_file = save_dir / target_program.relative_to(target_dir) log_file = log_file.with_suffix('.js.log') log_file.parent.mkdir(0o775, True, True) with log_file.open("wb") as p: p.write(m_code.encode()) p.write(b"----------------------------\n") p.write(report[0]) p.write(b"\n") p.write(report[1]) p.write(b"\n") p.write(report[2]) p.write(b"\n") p.write(report[3])
def test_depth_is_adjustable(self): diff_obj = diff.diff([1, 2], [2, 3, 4], _depth=3) self.assertEqual(diff_obj.depth, 3)
def test_empty_diff(self): diff_obj = diff.diff((), ()) self.assertEqual(diff_obj, diff.Diff(tuple, []))