101
100
multiparent.ParentText(0, 1, 2, 3)]))
103
102
def test_to_patch(self):
104
self.assertEqual(['i 1\n', 'a\n', '\n', 'c 0 1 2 3\n'],
105
list(multiparent.MultiParent([multiparent.NewText(['a\n']),
106
multiparent.ParentText(0, 1, 2, 3)]).to_patch()))
103
self.assertEqual([b'i 1\n', b'a\n', b'\n', b'c 0 1 2 3\n'],
104
list(multiparent.MultiParent([multiparent.NewText([b'a\n']),
105
multiparent.ParentText(0, 1, 2, 3)]).to_patch()))
108
107
def test_from_patch(self):
109
108
self.assertEqual(multiparent.MultiParent(
110
[multiparent.NewText(['a\n']),
109
[multiparent.NewText([b'a\n']),
111
110
multiparent.ParentText(0, 1, 2, 3)]),
112
multiparent.MultiParent.from_patch('i 1\na\n\nc 0 1 2 3'))
111
multiparent.MultiParent.from_patch(b'i 1\na\n\nc 0 1 2 3'))
113
112
self.assertEqual(multiparent.MultiParent(
114
[multiparent.NewText(['a']),
113
[multiparent.NewText([b'a']),
115
114
multiparent.ParentText(0, 1, 2, 3)]),
116
multiparent.MultiParent.from_patch('i 1\na\nc 0 1 2 3\n'))
115
multiparent.MultiParent.from_patch(b'i 1\na\nc 0 1 2 3\n'))
118
117
def test_binary_content(self):
120
119
multiparent.MultiParent.from_lines(LF_SPLIT_LINES).to_patch())
121
multiparent.MultiParent.from_patch(''.join(patch))
120
multiparent.MultiParent.from_patch(b''.join(patch))
123
122
def test_make_patch_from_binary(self):
124
patch = multiparent.MultiParent.from_texts(''.join(LF_SPLIT_LINES))
123
patch = multiparent.MultiParent.from_texts(b''.join(LF_SPLIT_LINES))
125
124
expected = multiparent.MultiParent([
126
125
multiparent.NewText(LF_SPLIT_LINES)])
127
126
self.assertEqual(expected, patch)
129
128
def test_num_lines(self):
130
mp = multiparent.MultiParent([multiparent.NewText(['a\n'])])
129
mp = multiparent.MultiParent([multiparent.NewText([b'a\n'])])
131
130
self.assertEqual(1, mp.num_lines())
132
mp.hunks.append(multiparent.NewText(['b\n', 'c\n']))
131
mp.hunks.append(multiparent.NewText([b'b\n', b'c\n']))
133
132
self.assertEqual(3, mp.num_lines())
134
133
mp.hunks.append(multiparent.ParentText(0, 0, 3, 2))
135
134
self.assertEqual(5, mp.num_lines())
136
mp.hunks.append(multiparent.NewText(['f\n', 'g\n']))
135
mp.hunks.append(multiparent.NewText([b'f\n', b'g\n']))
137
136
self.assertEqual(7, mp.num_lines())
139
138
def test_to_lines(self):
140
mpdiff = multiparent.MultiParent.from_texts('a\nb\nc\n', ('b\nc\n',))
141
lines = mpdiff.to_lines(('b\ne\n',))
142
self.assertEqual(['a\n', 'b\n', 'e\n'], lines)
139
mpdiff = multiparent.MultiParent.from_texts(b'a\nb\nc\n', (b'b\nc\n',))
140
lines = mpdiff.to_lines((b'b\ne\n',))
141
self.assertEqual([b'a\n', b'b\n', b'e\n'], lines)
145
144
class TestNewText(TestCase):
147
146
def test_eq(self):
148
147
self.assertEqual(multiparent.NewText([]), multiparent.NewText([]))
149
self.assertFalse(multiparent.NewText(['a']) ==
150
multiparent.NewText(['b']))
151
self.assertFalse(multiparent.NewText(['a']) == Mock(lines=['a']))
148
self.assertFalse(multiparent.NewText([b'a'])
149
== multiparent.NewText([b'b']))
150
self.assertFalse(multiparent.NewText([b'a']) == Mock(lines=[b'a']))
153
152
def test_to_patch(self):
154
self.assertEqual(['i 0\n', '\n'],
153
self.assertEqual([b'i 0\n', b'\n'],
155
154
list(multiparent.NewText([]).to_patch()))
156
self.assertEqual(['i 1\n', 'a', '\n'],
157
list(multiparent.NewText(['a']).to_patch()))
158
self.assertEqual(['i 1\n', 'a\n', '\n'],
159
list(multiparent.NewText(['a\n']).to_patch()))
155
self.assertEqual([b'i 1\n', b'a', b'\n'],
156
list(multiparent.NewText([b'a']).to_patch()))
157
self.assertEqual([b'i 1\n', b'a\n', b'\n'],
158
list(multiparent.NewText([b'a\n']).to_patch()))
162
161
class TestParentText(TestCase):
164
163
def test_eq(self):
165
164
self.assertEqual(multiparent.ParentText(1, 2, 3, 4),
166
165
multiparent.ParentText(1, 2, 3, 4))
167
self.assertFalse(multiparent.ParentText(1, 2, 3, 4) ==
168
multiparent.ParentText(2, 2, 3, 4))
169
self.assertFalse(multiparent.ParentText(1, 2, 3, 4) ==
170
Mock(parent=1, parent_pos=2, child_pos=3,
166
self.assertFalse(multiparent.ParentText(1, 2, 3, 4)
167
== multiparent.ParentText(2, 2, 3, 4))
168
self.assertFalse(multiparent.ParentText(1, 2, 3, 4)
169
== Mock(parent=1, parent_pos=2, child_pos=3,
173
172
def test_to_patch(self):
174
self.assertEqual(['c 0 1 2 3\n'],
173
self.assertEqual([b'c 0 1 2 3\n'],
175
174
list(multiparent.ParentText(0, 1, 2, 3).to_patch()))
178
REV_A = ['a\n', 'b\n', 'c\n', 'd\n']
179
REV_B = ['a\n', 'c\n', 'd\n', 'e\n']
180
REV_C = ['a\n', 'b\n', 'e\n', 'f\n']
177
REV_A = [b'a\n', b'b\n', b'c\n', b'd\n']
178
REV_B = [b'a\n', b'c\n', b'd\n', b'e\n']
179
REV_C = [b'a\n', b'b\n', b'e\n', b'f\n']
183
182
class TestVersionedFile(TestCase):
185
184
def add_version(self, vf, text, version_id, parent_ids):
186
vf.add_version([(t+'\n') for t in text], version_id, parent_ids)
186
[(bytes([t]) + b'\n') for t in bytearray(text)],
187
version_id, parent_ids)
188
189
def make_vf(self):
189
190
vf = multiparent.MultiMemoryVersionedFile()
190
self.add_version(vf, 'abcd', 'rev-a', [])
191
self.add_version(vf, 'acde', 'rev-b', [])
192
self.add_version(vf, 'abef', 'rev-c', ['rev-a', 'rev-b'])
191
self.add_version(vf, b'abcd', b'rev-a', [])
192
self.add_version(vf, b'acde', b'rev-b', [])
193
self.add_version(vf, b'abef', b'rev-c', [b'rev-a', b'rev-b'])
195
196
def test_add_version(self):
196
197
vf = self.make_vf()
197
self.assertEqual(REV_A, vf._lines['rev-a'])
198
self.assertEqual(REV_A, vf._lines[b'rev-a'])
199
200
self.assertEqual(vf._lines, {})
201
202
def test_get_line_list(self):
202
203
vf = self.make_vf()
204
self.assertEqual(REV_A, vf.get_line_list(['rev-a'])[0])
205
self.assertEqual([REV_B, REV_C], vf.get_line_list(['rev-b', 'rev-c']))
205
self.assertEqual(REV_A, vf.get_line_list([b'rev-a'])[0])
207
[REV_B, REV_C], vf.get_line_list([b'rev-b', b'rev-c']))
207
209
def test_reconstruct_empty(self):
208
210
vf = multiparent.MultiMemoryVersionedFile()
209
vf.add_version([], 'a', [])
210
self.assertEqual([], self.reconstruct_version(vf, 'a'))
211
vf.add_version([], b'a', [])
212
self.assertEqual([], self.reconstruct_version(vf, b'a'))
213
215
def reconstruct(vf, revision_id, start, end):
228
230
def test_reconstructor(self):
229
231
vf = self.make_vf()
230
self.assertEqual(['a\n', 'b\n'], self.reconstruct(vf, 'rev-a', 0, 2))
231
self.assertEqual(['c\n', 'd\n'], self.reconstruct(vf, 'rev-a', 2, 4))
232
self.assertEqual(['e\n', 'f\n'], self.reconstruct(vf, 'rev-c', 2, 4))
233
self.assertEqual(['a\n', 'b\n', 'e\n', 'f\n'],
234
self.reconstruct(vf, 'rev-c', 0, 4))
235
self.assertEqual(['a\n', 'b\n', 'e\n', 'f\n'],
236
self.reconstruct_version(vf, 'rev-c'))
232
self.assertEqual([b'a\n', b'b\n'],
233
self.reconstruct(vf, b'rev-a', 0, 2))
234
self.assertEqual([b'c\n', b'd\n'],
235
self.reconstruct(vf, b'rev-a', 2, 4))
236
self.assertEqual([b'e\n', b'f\n'],
237
self.reconstruct(vf, b'rev-c', 2, 4))
238
self.assertEqual([b'a\n', b'b\n', b'e\n', b'f\n'],
239
self.reconstruct(vf, b'rev-c', 0, 4))
240
self.assertEqual([b'a\n', b'b\n', b'e\n', b'f\n'],
241
self.reconstruct_version(vf, b'rev-c'))
238
243
def test_reordered(self):
239
244
"""Check for a corner case that requires re-starting the cursor"""
240
245
vf = multiparent.MultiMemoryVersionedFile()
241
246
# rev-b must have at least two hunks, so split a and b with c.
242
self.add_version(vf, 'c', 'rev-a', [])
243
self.add_version(vf, 'acb', 'rev-b', ['rev-a'])
247
self.add_version(vf, b'c', b'rev-a', [])
248
self.add_version(vf, b'acb', b'rev-b', [b'rev-a'])
244
249
# rev-c and rev-d must each have a line from a different rev-b hunk
245
self.add_version(vf, 'b', 'rev-c', ['rev-b'])
246
self.add_version(vf, 'a', 'rev-d', ['rev-b'])
250
self.add_version(vf, b'b', b'rev-c', [b'rev-b'])
251
self.add_version(vf, b'a', b'rev-d', [b'rev-b'])
247
252
# The lines from rev-c and rev-d must appear in the opposite order
248
self.add_version(vf, 'ba', 'rev-e', ['rev-c', 'rev-d'])
253
self.add_version(vf, b'ba', b'rev-e', [b'rev-c', b'rev-d'])
250
lines = vf.get_line_list(['rev-e'])[0]
251
self.assertEqual(['b\n', 'a\n'], lines)
255
lines = vf.get_line_list([b'rev-e'])[0]
256
self.assertEqual([b'b\n', b'a\n'], lines)
254
259
class TestMultiVersionedFile(tests.TestCaseInTempDir):
256
261
def test_save_load(self):
257
262
vf = multiparent.MultiVersionedFile('foop')
258
vf.add_version('a\nb\nc\nd'.splitlines(True), 'a', [])
259
vf.add_version('a\ne\nd\n'.splitlines(True), 'b', ['a'])
263
vf.add_version(b'a\nb\nc\nd'.splitlines(True), b'a', [])
264
vf.add_version(b'a\ne\nd\n'.splitlines(True), b'b', [b'a'])
261
266
newvf = multiparent.MultiVersionedFile('foop')
263
self.assertEqual('a\nb\nc\nd', ''.join(newvf.get_line_list(['a'])[0]))
264
self.assertEqual('a\ne\nd\n', ''.join(newvf.get_line_list(['b'])[0]))
268
self.assertEqual(b'a\nb\nc\nd', b''.join(
269
newvf.get_line_list([b'a'])[0]))
270
self.assertEqual(b'a\ne\nd\n', b''.join(
271
newvf.get_line_list([b'b'])[0]))
266
273
def test_filenames(self):
267
274
vf = multiparent.MultiVersionedFile('foop')
268
vf.add_version('a\nb\nc\nd'.splitlines(True), 'a', [])
269
self.failUnlessExists('foop.mpknit')
270
self.failIfExists('foop.mpidx')
275
vf.add_version(b'a\nb\nc\nd'.splitlines(True), b'a', [])
276
self.assertPathExists('foop.mpknit')
277
self.assertPathDoesNotExist('foop.mpidx')
272
self.failUnlessExists('foop.mpidx')
279
self.assertPathExists('foop.mpidx')
274
self.failIfExists('foop.mpknit')
275
self.failIfExists('foop.mpidx')
281
self.assertPathDoesNotExist('foop.mpknit')
282
self.assertPathDoesNotExist('foop.mpidx')