FossilRepo

fossilrepo / tests / test_split_diff.py
Source Blame History 164 lines
c588255… ragelink 1 """Tests for the split-diff view helper (_compute_split_lines)."""
c588255… ragelink 2
c588255… ragelink 3 from fossil.views import _compute_split_lines
c588255… ragelink 4
c588255… ragelink 5
c588255… ragelink 6 def _make_line(text, line_type, old_num="", new_num=""):
c588255… ragelink 7 """Build a diff-line dict matching the shape produced by checkin_detail."""
c588255… ragelink 8 if line_type in ("add", "del", "context") and text:
c588255… ragelink 9 prefix = text[0]
c588255… ragelink 10 code = text[1:]
c588255… ragelink 11 else:
c588255… ragelink 12 prefix = ""
c588255… ragelink 13 code = text
c588255… ragelink 14 return {
c588255… ragelink 15 "text": text,
c588255… ragelink 16 "type": line_type,
c588255… ragelink 17 "old_num": old_num,
c588255… ragelink 18 "new_num": new_num,
c588255… ragelink 19 "prefix": prefix,
c588255… ragelink 20 "code": code,
c588255… ragelink 21 }
c588255… ragelink 22
c588255… ragelink 23
c588255… ragelink 24 class TestComputeSplitLines:
c588255… ragelink 25 """Unit tests for _compute_split_lines."""
c588255… ragelink 26
c588255… ragelink 27 def test_context_lines_appear_on_both_sides(self):
c588255… ragelink 28 lines = [
c588255… ragelink 29 _make_line(" hello", "context", old_num=1, new_num=1),
c588255… ragelink 30 _make_line(" world", "context", old_num=2, new_num=2),
c588255… ragelink 31 ]
c588255… ragelink 32 left, right = _compute_split_lines(lines)
c588255… ragelink 33 assert len(left) == 2
c588255… ragelink 34 assert len(right) == 2
c588255… ragelink 35 assert left[0]["text"] == " hello"
c588255… ragelink 36 assert right[0]["text"] == " hello"
c588255… ragelink 37 assert left[1]["text"] == " world"
c588255… ragelink 38 assert right[1]["text"] == " world"
c588255… ragelink 39
c588255… ragelink 40 def test_deletion_only_on_left(self):
c588255… ragelink 41 lines = [
c588255… ragelink 42 _make_line("-removed", "del", old_num=5),
c588255… ragelink 43 ]
c588255… ragelink 44 left, right = _compute_split_lines(lines)
c588255… ragelink 45 assert len(left) == 1
c588255… ragelink 46 assert len(right) == 1
c588255… ragelink 47 assert left[0]["type"] == "del"
c588255… ragelink 48 assert left[0]["text"] == "-removed"
c588255… ragelink 49 assert right[0]["type"] == "empty"
c588255… ragelink 50 assert right[0]["text"] == ""
c588255… ragelink 51
c588255… ragelink 52 def test_addition_only_on_right(self):
c588255… ragelink 53 lines = [
c588255… ragelink 54 _make_line("+added", "add", new_num=10),
c588255… ragelink 55 ]
c588255… ragelink 56 left, right = _compute_split_lines(lines)
c588255… ragelink 57 assert len(left) == 1
c588255… ragelink 58 assert len(right) == 1
c588255… ragelink 59 assert left[0]["type"] == "empty"
c588255… ragelink 60 assert right[0]["type"] == "add"
c588255… ragelink 61 assert right[0]["text"] == "+added"
c588255… ragelink 62
c588255… ragelink 63 def test_paired_del_add_block(self):
c588255… ragelink 64 """Adjacent del+add lines should be paired row-by-row."""
c588255… ragelink 65 lines = [
c588255… ragelink 66 _make_line("-old_a", "del", old_num=1),
c588255… ragelink 67 _make_line("-old_b", "del", old_num=2),
c588255… ragelink 68 _make_line("+new_a", "add", new_num=1),
c588255… ragelink 69 _make_line("+new_b", "add", new_num=2),
c588255… ragelink 70 ]
c588255… ragelink 71 left, right = _compute_split_lines(lines)
c588255… ragelink 72 assert len(left) == 2
c588255… ragelink 73 assert len(right) == 2
c588255… ragelink 74 assert left[0]["type"] == "del"
c588255… ragelink 75 assert right[0]["type"] == "add"
c588255… ragelink 76 assert left[1]["type"] == "del"
c588255… ragelink 77 assert right[1]["type"] == "add"
c588255… ragelink 78
c588255… ragelink 79 def test_unequal_del_add_pads_with_empty(self):
c588255… ragelink 80 """When there are more dels than adds, right side gets empty placeholders."""
c588255… ragelink 81 lines = [
c588255… ragelink 82 _make_line("-old_a", "del", old_num=1),
c588255… ragelink 83 _make_line("-old_b", "del", old_num=2),
c588255… ragelink 84 _make_line("-old_c", "del", old_num=3),
c588255… ragelink 85 _make_line("+new_a", "add", new_num=1),
c588255… ragelink 86 ]
c588255… ragelink 87 left, right = _compute_split_lines(lines)
c588255… ragelink 88 assert len(left) == 3
c588255… ragelink 89 assert len(right) == 3
c588255… ragelink 90 assert left[0]["type"] == "del"
c588255… ragelink 91 assert right[0]["type"] == "add"
c588255… ragelink 92 assert left[1]["type"] == "del"
c588255… ragelink 93 assert right[1]["type"] == "empty"
c588255… ragelink 94 assert left[2]["type"] == "del"
c588255… ragelink 95 assert right[2]["type"] == "empty"
c588255… ragelink 96
c588255… ragelink 97 def test_more_adds_than_dels_pads_left(self):
c588255… ragelink 98 """When there are more adds than dels, left side gets empty placeholders."""
c588255… ragelink 99 lines = [
c588255… ragelink 100 _make_line("-old", "del", old_num=1),
c588255… ragelink 101 _make_line("+new_a", "add", new_num=1),
c588255… ragelink 102 _make_line("+new_b", "add", new_num=2),
c588255… ragelink 103 ]
c588255… ragelink 104 left, right = _compute_split_lines(lines)
c588255… ragelink 105 assert len(left) == 2
c588255… ragelink 106 assert len(right) == 2
c588255… ragelink 107 assert left[0]["type"] == "del"
c588255… ragelink 108 assert right[0]["type"] == "add"
c588255… ragelink 109 assert left[1]["type"] == "empty"
c588255… ragelink 110 assert right[1]["type"] == "add"
c588255… ragelink 111
c588255… ragelink 112 def test_hunk_and_header_lines_on_both_sides(self):
c588255… ragelink 113 lines = [
c588255… ragelink 114 _make_line("--- a/file.py", "header"),
c588255… ragelink 115 _make_line("+++ b/file.py", "header"),
c588255… ragelink 116 _make_line("@@ -1,3 +1,3 @@", "hunk"),
c588255… ragelink 117 _make_line(" ctx", "context", old_num=1, new_num=1),
c588255… ragelink 118 ]
c588255… ragelink 119 left, right = _compute_split_lines(lines)
c588255… ragelink 120 assert len(left) == 4
c588255… ragelink 121 assert len(right) == 4
c588255… ragelink 122 assert left[0]["type"] == "header"
c588255… ragelink 123 assert right[0]["type"] == "header"
c588255… ragelink 124 assert left[2]["type"] == "hunk"
c588255… ragelink 125 assert right[2]["type"] == "hunk"
c588255… ragelink 126 assert left[3]["type"] == "context"
c588255… ragelink 127 assert right[3]["type"] == "context"
c588255… ragelink 128
c588255… ragelink 129 def test_mixed_sequence(self):
c588255… ragelink 130 """Full realistic sequence: header, hunk, context, del, add, context."""
c588255… ragelink 131 lines = [
c588255… ragelink 132 _make_line("--- a/f.py", "header"),
c588255… ragelink 133 _make_line("+++ b/f.py", "header"),
c588255… ragelink 134 _make_line("@@ -1,4 +1,4 @@", "hunk"),
c588255… ragelink 135 _make_line(" line1", "context", old_num=1, new_num=1),
c588255… ragelink 136 _make_line("-old2", "del", old_num=2),
c588255… ragelink 137 _make_line("+new2", "add", new_num=2),
c588255… ragelink 138 _make_line(" line3", "context", old_num=3, new_num=3),
c588255… ragelink 139 ]
c588255… ragelink 140 left, right = _compute_split_lines(lines)
c588255… ragelink 141 # header(2) + hunk(1) + context(1) + paired del/add(1) + context(1) = 6
c588255… ragelink 142 assert len(left) == 6
c588255… ragelink 143 assert len(right) == 6
c588255… ragelink 144 # Check paired del/add at index 4
c588255… ragelink 145 assert left[4]["type"] == "del"
c588255… ragelink 146 assert right[4]["type"] == "add"
c588255… ragelink 147
c588255… ragelink 148 def test_empty_input(self):
c588255… ragelink 149 left, right = _compute_split_lines([])
c588255… ragelink 150 assert left == []
c588255… ragelink 151 assert right == []
c588255… ragelink 152
c588255… ragelink 153 def test_orphan_add_without_preceding_del(self):
c588255… ragelink 154 """An add line not preceded by a del should still work."""
c588255… ragelink 155 lines = [
c588255… ragelink 156 _make_line(" ctx", "context", old_num=1, new_num=1),
c588255… ragelink 157 _make_line("+new", "add", new_num=2),
c588255… ragelink 158 _make_line(" ctx2", "context", old_num=2, new_num=3),
c588255… ragelink 159 ]
c588255… ragelink 160 left, right = _compute_split_lines(lines)
c588255… ragelink 161 assert len(left) == 3
c588255… ragelink 162 assert len(right) == 3
c588255… ragelink 163 assert left[1]["type"] == "empty"
c588255… ragelink 164 assert right[1]["type"] == "add"

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button