1
from unittest import TestCase
9
LINES_1 = "a\nb\nc\nd\ne\n".splitlines(True)
10
LINES_2 = "a\nc\nd\ne\n".splitlines(True)
11
LINES_3 = "a\nb\nc\nd\n".splitlines(True)
16
def __init__(self, **kwargs):
17
self.__dict__ = kwargs
20
class TestMulti(TestCase):
22
def test_compare_no_parent(self):
23
diff = multiparent.MultiParent.from_lines(LINES_1)
24
self.assertEqual([multiparent.NewText(LINES_1)], diff.hunks)
26
def test_compare_one_parent(self):
27
diff = multiparent.MultiParent.from_lines(LINES_1, [LINES_2])
28
self.assertEqual([multiparent.ParentText(0, 0, 0, 1),
29
multiparent.NewText(['b\n']),
30
multiparent.ParentText(0, 1, 2, 3)],
33
def test_compare_two_parents(self):
34
diff = multiparent.MultiParent.from_lines(LINES_1, [LINES_2, LINES_3])
35
self.assertEqual([multiparent.ParentText(1, 0, 0, 4),
36
multiparent.ParentText(0, 3, 4, 1)],
39
def test_range_iterator(self):
40
diff = multiparent.MultiParent.from_lines(LINES_1, [LINES_2, LINES_3])
41
diff.hunks.append(multiparent.NewText(['q\n']))
42
self.assertEqual([(0, 4, 'parent', (1, 0, 4)),
43
(4, 5, 'parent', (0, 3, 4)),
44
(5, 6, 'new', ['q\n'])],
45
list(diff.range_iterator()))
48
diff = multiparent.MultiParent.from_lines(LINES_1)
49
diff2 = multiparent.MultiParent.from_lines(LINES_1)
50
self.assertEqual(diff, diff2)
51
diff3 = multiparent.MultiParent.from_lines(LINES_2)
52
self.assertFalse(diff == diff3)
53
self.assertFalse(diff == Mock(hunks=[multiparent.NewText(LINES_1)]))
54
self.assertEqual(multiparent.MultiParent(
55
[multiparent.NewText(LINES_1),
56
multiparent.ParentText(0, 1, 2, 3)]),
57
multiparent.MultiParent(
58
[multiparent.NewText(LINES_1),
59
multiparent.ParentText(0, 1, 2, 3)]))
61
def test_to_patch(self):
62
self.assertEqual(['i 1\n', 'a\n', '\n', 'c 0 1 2 3\n'],
63
list(multiparent.MultiParent([multiparent.NewText(['a\n']),
64
multiparent.ParentText(0, 1, 2, 3)]).to_patch()))
66
def test_from_patch(self):
67
self.assertEqual(multiparent.MultiParent(
68
[multiparent.NewText(['a\n']),
69
multiparent.ParentText(0, 1, 2, 3)]),
70
multiparent.MultiParent.from_patch(
71
['i 1\n', 'a\n', '\n', 'c 0 1 2 3\n']))
72
self.assertEqual(multiparent.MultiParent(
73
[multiparent.NewText(['a']),
74
multiparent.ParentText(0, 1, 2, 3)]),
75
multiparent.MultiParent.from_patch(
76
['i 1\n', 'a\n', 'c 0 1 2 3\n']))
78
def test_num_lines(self):
79
mp = multiparent.MultiParent([multiparent.NewText(['a\n'])])
80
self.assertEqual(1, mp.num_lines())
81
mp.hunks.append(multiparent.NewText(['b\n', 'c\n']))
82
self.assertEqual(3, mp.num_lines())
83
mp.hunks.append(multiparent.ParentText(0, 0, 3, 2))
84
self.assertEqual(5, mp.num_lines())
85
mp.hunks.append(multiparent.NewText(['f\n', 'g\n']))
86
self.assertEqual(7, mp.num_lines())
89
class TestNewText(TestCase):
92
self.assertEqual(multiparent.NewText([]), multiparent.NewText([]))
93
self.assertFalse(multiparent.NewText(['a']) ==
94
multiparent.NewText(['b']))
95
self.assertFalse(multiparent.NewText(['a']) == Mock(lines=['a']))
97
def test_to_patch(self):
98
self.assertEqual(['i 0\n', '\n'],
99
list(multiparent.NewText([]).to_patch()))
100
self.assertEqual(['i 1\n', 'a', '\n'],
101
list(multiparent.NewText(['a']).to_patch()))
102
self.assertEqual(['i 1\n', 'a\n', '\n'],
103
list(multiparent.NewText(['a\n']).to_patch()))
106
class TestParentText(TestCase):
109
self.assertEqual(multiparent.ParentText(1, 2, 3, 4),
110
multiparent.ParentText(1, 2, 3, 4))
111
self.assertFalse(multiparent.ParentText(1, 2, 3, 4) ==
112
multiparent.ParentText(2, 2, 3, 4))
113
self.assertFalse(multiparent.ParentText(1, 2, 3, 4) ==
114
Mock(parent=1, parent_pos=2, child_pos=3,
117
def test_to_patch(self):
118
self.assertEqual(['c 0 1 2 3\n'],
119
list(multiparent.ParentText(0, 1, 2, 3).to_patch()))
122
REV_A = ['a\n', 'b\n', 'c\n', 'd\n']
123
REV_B = ['a\n', 'c\n', 'd\n', 'e\n']
124
REV_C = ['a\n', 'b\n', 'e\n', 'f\n']
127
class TestVersionedFile(TestCase):
129
def add_version(self, vf, text, version_id, parent_ids):
130
vf.add_version([(t+'\n') for t in text], version_id, parent_ids)
133
vf = multiparent.MultiMemoryVersionedFile()
134
self.add_version(vf, 'abcd', 'rev-a', [])
135
self.add_version(vf, 'acde', 'rev-b', [])
136
self.add_version(vf, 'abef', 'rev-c', ['rev-a', 'rev-b'])
139
def test_add_version(self):
141
self.assertEqual(REV_A, vf._lines['rev-a'])
143
self.assertEqual(vf._lines, {})
145
def test_get_line_list(self):
148
self.assertEqual(REV_A, vf.get_line_list(['rev-a'])[0])
149
self.assertEqual([REV_B, REV_C], vf.get_line_list(['rev-b', 'rev-c']))
152
def reconstruct(vf, revision_id, start, end):
153
reconstructor = multiparent._Reconstructor(vf, vf._lines,
156
reconstructor._reconstruct(lines, revision_id, start, end)
160
def reconstruct_version(vf, revision_id):
161
reconstructor = multiparent._Reconstructor(vf, vf._lines,
164
reconstructor.reconstruct_version(lines, revision_id)
167
def test_reconstructor(self):
169
self.assertEqual(['a\n', 'b\n'], self.reconstruct(vf, 'rev-a', 0, 2))
170
self.assertEqual(['c\n', 'd\n'], self.reconstruct(vf, 'rev-a', 2, 4))
171
self.assertEqual(['e\n', 'f\n'], self.reconstruct(vf, 'rev-c', 2, 4))
172
self.assertEqual(['a\n', 'b\n', 'e\n', 'f\n'],
173
self.reconstruct(vf, 'rev-c', 0, 4))
174
self.assertEqual(['a\n', 'b\n', 'e\n', 'f\n'],
175
self.reconstruct_version(vf, 'rev-c'))
177
def test_reordered(self):
178
"""Check for a corner case that requires re-starting the cursor"""
179
vf = multiparent.MultiMemoryVersionedFile()
180
# rev-b must have at least two hunks, so split a and b with c.
181
self.add_version(vf, 'c', 'rev-a', [])
182
self.add_version(vf, 'acb', 'rev-b', ['rev-a'])
183
# rev-c and rev-d must each have a line from a different rev-b hunk
184
self.add_version(vf, 'b', 'rev-c', ['rev-b'])
185
self.add_version(vf, 'a', 'rev-d', ['rev-b'])
186
# The lines from rev-c and rev-d must appear in the opposite order
187
self.add_version(vf, 'ba', 'rev-e', ['rev-c', 'rev-d'])
189
lines = vf.get_line_list(['rev-e'])[0]
190
self.assertEqual(['b\n', 'a\n'], lines)
193
class TestMultiVersionedFile(tests.TestCaseInTempDir):
195
def test_save_load(self):
196
vf = multiparent.MultiVersionedFile('foop')
197
vf.add_version('a\nb\nc\nd'.splitlines(True), 'a', [])
198
vf.add_version('a\ne\nd\n'.splitlines(True), 'b', ['a'])
200
newvf = multiparent.MultiVersionedFile('foop')
202
self.assertEqual('a\nb\nc\nd', ''.join(newvf.get_line_list(['a'])[0]))
203
self.assertEqual('a\ne\nd\n', ''.join(newvf.get_line_list(['b'])[0]))
205
def test_filenames(self):
206
vf = multiparent.MultiVersionedFile('foop')
207
vf.add_version('a\nb\nc\nd'.splitlines(True), 'a', [])
208
self.failUnlessExists('foop.mpknit')
209
self.failIfExists('foop.mpidx')
211
self.failUnlessExists('foop.mpidx')
213
self.failIfExists('foop.mpknit')
214
self.failIfExists('foop.mpidx')