1
from unittest import TestCase
3
from bzrlib import tests
8
LINES_1 = "a\nb\nc\nd\ne\n".splitlines(True)
9
LINES_2 = "a\nc\nd\ne\n".splitlines(True)
10
LINES_3 = "a\nb\nc\nd\n".splitlines(True)
15
def __init__(self, **kwargs):
16
self.__dict__ = kwargs
19
class TestMulti(TestCase):
21
def test_compare_no_parent(self):
22
diff = multiparent.MultiParent.from_lines(LINES_1)
23
self.assertEqual([multiparent.NewText(LINES_1)], diff.hunks)
25
def test_compare_one_parent(self):
26
diff = multiparent.MultiParent.from_lines(LINES_1, [LINES_2])
27
self.assertEqual([multiparent.ParentText(0, 0, 0, 1),
28
multiparent.NewText(['b\n']),
29
multiparent.ParentText(0, 1, 2, 3)],
32
def test_compare_two_parents(self):
33
diff = multiparent.MultiParent.from_lines(LINES_1, [LINES_2, LINES_3])
34
self.assertEqual([multiparent.ParentText(1, 0, 0, 4),
35
multiparent.ParentText(0, 3, 4, 1)],
38
def test_range_iterator(self):
39
diff = multiparent.MultiParent.from_lines(LINES_1, [LINES_2, LINES_3])
40
diff.hunks.append(multiparent.NewText(['q\n']))
41
self.assertEqual([(0, 4, 'parent', (1, 0, 4)),
42
(4, 5, 'parent', (0, 3, 4)),
43
(5, 6, 'new', ['q\n'])],
44
list(diff.range_iterator()))
47
diff = multiparent.MultiParent.from_lines(LINES_1)
48
diff2 = multiparent.MultiParent.from_lines(LINES_1)
49
self.assertEqual(diff, diff2)
50
diff3 = multiparent.MultiParent.from_lines(LINES_2)
51
self.assertFalse(diff == diff3)
52
self.assertFalse(diff == Mock(hunks=[multiparent.NewText(LINES_1)]))
53
self.assertEqual(multiparent.MultiParent(
54
[multiparent.NewText(LINES_1),
55
multiparent.ParentText(0, 1, 2, 3)]),
56
multiparent.MultiParent(
57
[multiparent.NewText(LINES_1),
58
multiparent.ParentText(0, 1, 2, 3)]))
60
def test_to_patch(self):
61
self.assertEqual(['i 1\n', 'a\n', '\n', 'c 0 1 2 3\n'],
62
list(multiparent.MultiParent([multiparent.NewText(['a\n']),
63
multiparent.ParentText(0, 1, 2, 3)]).to_patch()))
65
def test_from_patch(self):
66
self.assertEqual(multiparent.MultiParent(
67
[multiparent.NewText(['a\n']),
68
multiparent.ParentText(0, 1, 2, 3)]),
69
multiparent.MultiParent.from_patch(
70
['i 1\n', 'a\n', '\n', 'c 0 1 2 3\n']))
71
self.assertEqual(multiparent.MultiParent(
72
[multiparent.NewText(['a']),
73
multiparent.ParentText(0, 1, 2, 3)]),
74
multiparent.MultiParent.from_patch(
75
['i 1\n', 'a\n', 'c 0 1 2 3\n']))
77
def test_num_lines(self):
78
mp = multiparent.MultiParent([multiparent.NewText(['a\n'])])
79
self.assertEqual(1, mp.num_lines())
80
mp.hunks.append(multiparent.NewText(['b\n', 'c\n']))
81
self.assertEqual(3, mp.num_lines())
82
mp.hunks.append(multiparent.ParentText(0, 0, 3, 2))
83
self.assertEqual(5, mp.num_lines())
84
mp.hunks.append(multiparent.NewText(['f\n', 'g\n']))
85
self.assertEqual(7, mp.num_lines())
88
class TestNewText(TestCase):
91
self.assertEqual(multiparent.NewText([]), multiparent.NewText([]))
92
self.assertFalse(multiparent.NewText(['a']) ==
93
multiparent.NewText(['b']))
94
self.assertFalse(multiparent.NewText(['a']) == Mock(lines=['a']))
96
def test_to_patch(self):
97
self.assertEqual(['i 0\n', '\n'],
98
list(multiparent.NewText([]).to_patch()))
99
self.assertEqual(['i 1\n', 'a', '\n'],
100
list(multiparent.NewText(['a']).to_patch()))
101
self.assertEqual(['i 1\n', 'a\n', '\n'],
102
list(multiparent.NewText(['a\n']).to_patch()))
105
class TestParentText(TestCase):
108
self.assertEqual(multiparent.ParentText(1, 2, 3, 4),
109
multiparent.ParentText(1, 2, 3, 4))
110
self.assertFalse(multiparent.ParentText(1, 2, 3, 4) ==
111
multiparent.ParentText(2, 2, 3, 4))
112
self.assertFalse(multiparent.ParentText(1, 2, 3, 4) ==
113
Mock(parent=1, parent_pos=2, child_pos=3,
116
def test_to_patch(self):
117
self.assertEqual(['c 0 1 2 3\n'],
118
list(multiparent.ParentText(0, 1, 2, 3).to_patch()))
121
REV_A = ['a\n', 'b\n', 'c\n', 'd\n']
122
REV_B = ['a\n', 'c\n', 'd\n', 'e\n']
123
REV_C = ['a\n', 'b\n', 'e\n', 'f\n']
126
class TestVersionedFile(TestCase):
128
def add_version(self, vf, text, version_id, parent_ids):
129
vf.add_version([(t+'\n') for t in text], version_id, parent_ids)
132
vf = multiparent.MultiMemoryVersionedFile()
133
self.add_version(vf, 'abcd', 'rev-a', [])
134
self.add_version(vf, 'acde', 'rev-b', [])
135
self.add_version(vf, 'abef', 'rev-c', ['rev-a', 'rev-b'])
138
def test_add_version(self):
140
self.assertEqual(REV_A, vf._lines['rev-a'])
142
self.assertEqual(vf._lines, {})
144
def test_get_line_list(self):
147
self.assertEqual(REV_A, vf.get_line_list(['rev-a'])[0])
148
self.assertEqual([REV_B, REV_C], vf.get_line_list(['rev-b', 'rev-c']))
151
def reconstruct(vf, revision_id, start, end):
152
reconstructor = multiparent._Reconstructor(vf, vf._lines,
155
reconstructor._reconstruct(lines, revision_id, start, end)
159
def reconstruct_version(vf, revision_id):
160
reconstructor = multiparent._Reconstructor(vf, vf._lines,
163
reconstructor.reconstruct_version(lines, revision_id)
166
def test_reconstructor(self):
168
self.assertEqual(['a\n', 'b\n'], self.reconstruct(vf, 'rev-a', 0, 2))
169
self.assertEqual(['c\n', 'd\n'], self.reconstruct(vf, 'rev-a', 2, 4))
170
self.assertEqual(['e\n', 'f\n'], self.reconstruct(vf, 'rev-c', 2, 4))
171
self.assertEqual(['a\n', 'b\n', 'e\n', 'f\n'],
172
self.reconstruct(vf, 'rev-c', 0, 4))
173
self.assertEqual(['a\n', 'b\n', 'e\n', 'f\n'],
174
self.reconstruct_version(vf, 'rev-c'))
176
def test_reordered(self):
177
"""Check for a corner case that requires re-starting the cursor"""
178
vf = multiparent.MultiMemoryVersionedFile()
179
# rev-b must have at least two hunks, so split a and b with c.
180
self.add_version(vf, 'c', 'rev-a', [])
181
self.add_version(vf, 'acb', 'rev-b', ['rev-a'])
182
# rev-c and rev-d must each have a line from a different rev-b hunk
183
self.add_version(vf, 'b', 'rev-c', ['rev-b'])
184
self.add_version(vf, 'a', 'rev-d', ['rev-b'])
185
# The lines from rev-c and rev-d must appear in the opposite order
186
self.add_version(vf, 'ba', 'rev-e', ['rev-c', 'rev-d'])
188
lines = vf.get_line_list(['rev-e'])[0]
189
self.assertEqual(['b\n', 'a\n'], lines)
192
class TestMultiVersionedFile(tests.TestCaseInTempDir):
194
def test_save_load(self):
195
vf = multiparent.MultiVersionedFile('foop')
196
vf.add_version('a\nb\nc\nd'.splitlines(True), 'a', [])
197
vf.add_version('a\ne\nd\n'.splitlines(True), 'b', ['a'])
199
newvf = multiparent.MultiVersionedFile('foop')
201
self.assertEqual('a\nb\nc\nd', ''.join(newvf.get_line_list(['a'])[0]))
202
self.assertEqual('a\ne\nd\n', ''.join(newvf.get_line_list(['b'])[0]))
204
def test_filenames(self):
205
vf = multiparent.MultiVersionedFile('foop')
206
vf.add_version('a\nb\nc\nd'.splitlines(True), 'a', [])
207
self.failUnlessExists('foop.mpknit')
208
self.failIfExists('foop.mpidx')
210
self.failUnlessExists('foop.mpidx')
212
self.failIfExists('foop.mpknit')
213
self.failIfExists('foop.mpidx')