Beispiel #1
0
    def test_context_excludes_bordering_unchanged(self):
        lines = [("line %d" % i) for i in range(10)]
        changed_lines = lines[:4] + ['one line', 'another line'] + lines[6:]

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)

        self.assertEqual(list(context_diff(diff)),
                         [(2, 2, list(diff_clone)[2:8])])
Beispiel #2
0
    def test_user_defined_context(self):
        lines = [("line %d" % i) for i in range(12)]
        changed_lines = lines[:5] + ['one line', 'another line'] + lines[7:]

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)

        self.assertEqual(list(context_diff(diff, context=4)),
                         [(1, 1, list(diff_clone)[1:11])])
Beispiel #3
0
    def test_context_excludes_final_unchanged(self):
        lines = [("line %d" % i) for i in range(5)]
        changed_lines = ['one line', 'another line'] + lines[2:]

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)

        self.assertEqual(list(context_diff(diff)),
                         [(0, 0, list(diff_clone)[:-1])])
Beispiel #4
0
    def test_context_excludes_initial_unchanged(self):
        lines = [("line %d" % i) for i in range(5)]
        changed_lines = lines[:3] + ['one line', 'another line']

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)

        self.assertEqual(list(context_diff(diff)),
                         [(1, 1, list(diff_clone)[1:])])
Beispiel #5
0
    def test_bordering_contexts_are_merged(self):
        lines = [("line %d" % i) for i in range(12)]
        changed_lines = (lines[:3] + ['one line'] + lines[4:8]
                         + ['another one'] + lines[9:])

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)

        self.assertEqual(list(context_diff(diff)),
                         [(1, 1, list(diff_clone)[1:11])])
Beispiel #6
0
    def test_deletion_context(self):
        lines = [("line %d" % i) for i in range(15)]
        changed_lines = (lines[:3] + lines[5:10]
                         + ['third line', 'forth line'] + lines[12:])

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)
        diff_clone = list(diff_clone)

        self.assertEqual(list(context_diff(diff)),
                         [(1, 1, diff_clone[1:7]),
                          (8, 6, diff_clone[8:14])])
Beispiel #7
0
    def test_insertion_context(self):
        lines = [("line %d" % i) for i in range(13)]
        changed_lines = (lines[:3] + ['one line', 'another line'] + lines[3:8]
                         + ['third line', 'forth line'] + lines[10:])

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)
        diff_clone = list(diff_clone)

        self.assertEqual(list(context_diff(diff)),
                         [(1, 1, diff_clone[1:7]),
                          (6, 8, diff_clone[8:14])])
Beispiel #8
0
    def test_multiple_contexts(self):
        lines = [("line %d" % i) for i in range(20)]
        changed_lines = (lines[:4] + ['one line', 'another line'] + lines[6:14]
                         + ['third line', 'forth line'] + lines[16:])

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)
        diff_clone = list(diff_clone)

        self.assertEqual(list(context_diff(diff)),
                         [(2, 2, diff_clone[2:8]),
                          (12, 12, diff_clone[12:18])])
Beispiel #9
0
    def test_full_change_merged_contexts(self):
        lines = [("line %d" % i) for i in range(30)]
        changed_lines = (lines[:3] + ['one line', 'another line'] + lines[5:9]
                         + ['third line', 'forth line'] + lines[9:13]
                         + ['one more line', 'yet another'] + lines[15:19]
                         + lines[21:25]
                         + ['nearly last', 'possibly last line'] + lines[27:])

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)
        diff_clone = list(diff_clone)

        self.assertEqual(list(context_diff(diff)),
                         [(1, 1, diff_clone[1:31])])
Beispiel #10
0
    def test_full_change_contexts(self):
        lines = [("line %d" % i) for i in range(34)]
        changed_lines = (lines[:3] + ['one line', 'another line'] + lines[5:10]
                         + ['third line', 'forth line'] + lines[10:15]
                         + ['one more line', 'yet another'] + lines[17:22]
                         + lines[24:29]
                         + ['nearly last', 'possibly last line'] + lines[31:])

        diff = side_by_side_diff('\n'.join(lines), '\n'.join(changed_lines))
        diff, diff_clone = itertools.tee(diff)
        diff_clone = list(diff_clone)

        self.assertEqual(list(context_diff(diff)),
                         [(1, 1, diff_clone[1:7]),
                          (8, 8, diff_clone[8:14]),
                          (13, 15, diff_clone[15:21]),
                          (20, 22, diff_clone[22:28]),
                          (27, 27, diff_clone[29:35])])
Beispiel #11
0
    def render(self, context):
        try:
            old_value = self.old_value.resolve(context)
        except VariableDoesNotExist:
            raise TemplateSyntaxError('"cache" tag got an unknown variable: %r'
                                      % self.old_value)
        try:
            new_value = self.new_value.resolve(context)
        except VariableDoesNotExist:
            raise TemplateSyntaxError('"cache" tag got an unknown variable: %r'
                                      % self.old_value)

        old_text = force_unicode(old_value) if old_value else ''
        new_text = force_unicode(new_value) if new_value else ''
        diff = side_by_side_diff(old_text, new_text)
        contextual_diff = context_diff(diff, context=self.context_width)

        context.push()
        diff_str = render_to_string('wikify/contextual_diff_tr.html',
                                    {'context_diff': contextual_diff},
                                    context)
        context.pop()
        return diff_str
Beispiel #12
0
 def test_multi_line_change_is_included(self):
     diff = side_by_side_diff("old text\nline\nanother line",
                              "new text\nline\nsome line")
     diff, diff_clone = itertools.tee(diff)
     self.assertEqual(list(context_diff(diff)),
                      [(0, 0, list(diff_clone))])
Beispiel #13
0
 def test_small_change_is_included(self):
     diff = side_by_side_diff("old text",
                              "new text")
     diff, diff_clone = itertools.tee(diff)
     self.assertEqual(list(context_diff(diff)),
                      [(0, 0, list(diff_clone))])