comparison tests/test-simplemerge.py @ 48584:ce8c82a5cd65

simplemerge: convert `merge_lines()` away from generator We always consume all the lines and put them in a list anyway. By making the function not a generator, we can later make it return an additional value (to indicate if there were conflicts). Differential Revision: https://phab.mercurial-scm.org/D11973
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 07 Jan 2022 18:42:31 -0800
parents 374bf34c9ffd
children c6649c53073f
comparison
equal deleted inserted replaced
48583:c91418480cb0 48584:ce8c82a5cd65
177 # to match without zz 177 # to match without zz
178 self.assertEqual(list(m3.find_sync_regions()), [(0, 0, 2, 2, 0, 0)]) 178 self.assertEqual(list(m3.find_sync_regions()), [(0, 0, 2, 2, 0, 0)])
179 179
180 self.assertEqual(list(m3.merge_regions()), [(b'a', 0, 2)]) 180 self.assertEqual(list(m3.merge_regions()), [(b'a', 0, 2)])
181 181
182 self.assertEqual(list(m3.merge_lines()), [b'aaa', b'bbb']) 182 self.assertEqual(m3.merge_lines(), ([b'aaa', b'bbb'], False))
183 183
184 def test_no_conflicts(self): 184 def test_no_conflicts(self):
185 """No conflicts because only one side changed""" 185 """No conflicts because only one side changed"""
186 m3 = Merge3( 186 m3 = Merge3(
187 [b'aaa', b'bbb'], [b'aaa', b'111', b'bbb'], [b'aaa', b'bbb'] 187 [b'aaa', b'bbb'], [b'aaa', b'111', b'bbb'], [b'aaa', b'bbb']
202 [b'aaa\n', b'bbb\n'], 202 [b'aaa\n', b'bbb\n'],
203 [b'aaa\n', b'bbb\n', b'222\n'], 203 [b'aaa\n', b'bbb\n', b'222\n'],
204 [b'aaa\n', b'bbb\n'], 204 [b'aaa\n', b'bbb\n'],
205 ) 205 )
206 206
207 self.assertEqual(b''.join(m3.merge_lines()), b'aaa\nbbb\n222\n') 207 self.assertEqual(b''.join(m3.merge_lines()[0]), b'aaa\nbbb\n222\n')
208 208
209 def test_append_b(self): 209 def test_append_b(self):
210 m3 = Merge3( 210 m3 = Merge3(
211 [b'aaa\n', b'bbb\n'], 211 [b'aaa\n', b'bbb\n'],
212 [b'aaa\n', b'bbb\n'], 212 [b'aaa\n', b'bbb\n'],
213 [b'aaa\n', b'bbb\n', b'222\n'], 213 [b'aaa\n', b'bbb\n', b'222\n'],
214 ) 214 )
215 215
216 self.assertEqual(b''.join(m3.merge_lines()), b'aaa\nbbb\n222\n') 216 self.assertEqual(b''.join(m3.merge_lines()[0]), b'aaa\nbbb\n222\n')
217 217
218 def test_append_agreement(self): 218 def test_append_agreement(self):
219 m3 = Merge3( 219 m3 = Merge3(
220 [b'aaa\n', b'bbb\n'], 220 [b'aaa\n', b'bbb\n'],
221 [b'aaa\n', b'bbb\n', b'222\n'], 221 [b'aaa\n', b'bbb\n', b'222\n'],
222 [b'aaa\n', b'bbb\n', b'222\n'], 222 [b'aaa\n', b'bbb\n', b'222\n'],
223 ) 223 )
224 224
225 self.assertEqual(b''.join(m3.merge_lines()), b'aaa\nbbb\n222\n') 225 self.assertEqual(b''.join(m3.merge_lines()[0]), b'aaa\nbbb\n222\n')
226 226
227 def test_append_clash(self): 227 def test_append_clash(self):
228 m3 = Merge3( 228 m3 = Merge3(
229 [b'aaa\n', b'bbb\n'], 229 [b'aaa\n', b'bbb\n'],
230 [b'aaa\n', b'bbb\n', b'222\n'], 230 [b'aaa\n', b'bbb\n', b'222\n'],
231 [b'aaa\n', b'bbb\n', b'333\n'], 231 [b'aaa\n', b'bbb\n', b'333\n'],
232 ) 232 )
233 233
234 ml = m3.merge_lines( 234 ml, conflicts = m3.merge_lines(
235 name_a=b'a', 235 name_a=b'a',
236 name_b=b'b', 236 name_b=b'b',
237 start_marker=b'<<', 237 start_marker=b'<<',
238 mid_marker=b'--', 238 mid_marker=b'--',
239 end_marker=b'>>', 239 end_marker=b'>>',
248 [b'aaa\n', b'bbb\n'], 248 [b'aaa\n', b'bbb\n'],
249 [b'aaa\n', b'222\n', b'bbb\n'], 249 [b'aaa\n', b'222\n', b'bbb\n'],
250 [b'aaa\n', b'222\n', b'bbb\n'], 250 [b'aaa\n', b'222\n', b'bbb\n'],
251 ) 251 )
252 252
253 ml = m3.merge_lines( 253 ml, conflicts = m3.merge_lines(
254 name_a=b'a', 254 name_a=b'a',
255 name_b=b'b', 255 name_b=b'b',
256 start_marker=b'<<', 256 start_marker=b'<<',
257 mid_marker=b'--', 257 mid_marker=b'--',
258 end_marker=b'>>', 258 end_marker=b'>>',
288 (b'conflict', ([], [b'111\n'], [b'222\n'])), 288 (b'conflict', ([], [b'111\n'], [b'222\n'])),
289 (b'unchanged', [b'bbb\n']), 289 (b'unchanged', [b'bbb\n']),
290 ], 290 ],
291 ) 291 )
292 292
293 ml = m3.merge_lines( 293 ml, conflicts = m3.merge_lines(
294 name_a=b'a', 294 name_a=b'a',
295 name_b=b'b', 295 name_b=b'b',
296 start_marker=b'<<', 296 start_marker=b'<<',
297 mid_marker=b'--', 297 mid_marker=b'--',
298 end_marker=b'>>', 298 end_marker=b'>>',
336 ) 336 )
337 337
338 def test_merge_poem(self): 338 def test_merge_poem(self):
339 """Test case from diff3 manual""" 339 """Test case from diff3 manual"""
340 m3 = Merge3(TZU, LAO, TAO) 340 m3 = Merge3(TZU, LAO, TAO)
341 ml = list(m3.merge_lines(b'LAO', b'TAO')) 341 ml, conflicts = m3.merge_lines(b'LAO', b'TAO')
342 self.log(b'merge result:') 342 self.log(b'merge result:')
343 self.log(b''.join(ml)) 343 self.log(b''.join(ml))
344 self.assertEqual(ml, MERGED_RESULT) 344 self.assertEqual(ml, MERGED_RESULT)
345 345
346 def test_binary(self): 346 def test_binary(self):
354 m3 = Merge3( 354 m3 = Merge3(
355 base_text.splitlines(True), 355 base_text.splitlines(True),
356 other_text.splitlines(True), 356 other_text.splitlines(True),
357 this_text.splitlines(True), 357 this_text.splitlines(True),
358 ) 358 )
359 m_lines = m3.merge_lines(b'OTHER', b'THIS') 359 m_lines, conflicts = m3.merge_lines(b'OTHER', b'THIS')
360 self.assertEqual( 360 self.assertEqual(
361 b'<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n' 361 b'<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
362 b'>>>>>>> THIS\r\n'.splitlines(True), 362 b'>>>>>>> THIS\r\n'.splitlines(True),
363 list(m_lines), 363 m_lines,
364 ) 364 )
365 365
366 def test_mac_text(self): 366 def test_mac_text(self):
367 base_text = b'a\r' 367 base_text = b'a\r'
368 this_text = b'b\r' 368 this_text = b'b\r'
370 m3 = Merge3( 370 m3 = Merge3(
371 base_text.splitlines(True), 371 base_text.splitlines(True),
372 other_text.splitlines(True), 372 other_text.splitlines(True),
373 this_text.splitlines(True), 373 this_text.splitlines(True),
374 ) 374 )
375 m_lines = m3.merge_lines(b'OTHER', b'THIS') 375 m_lines, conflicts = m3.merge_lines(b'OTHER', b'THIS')
376 self.assertEqual( 376 self.assertEqual(
377 b'<<<<<<< OTHER\rc\r=======\rb\r' 377 b'<<<<<<< OTHER\rc\r=======\rb\r'
378 b'>>>>>>> THIS\r'.splitlines(True), 378 b'>>>>>>> THIS\r'.splitlines(True),
379 list(m_lines), 379 m_lines,
380 ) 380 )
381 381
382 382
383 if __name__ == '__main__': 383 if __name__ == '__main__':
384 import silenttestrunner 384 import silenttestrunner