Mercurial > public > mercurial-scm > hg
comparison tests/test-simplemerge.py @ 4363:2e3c54fb79a3
actually port simplemerge to hg
- use bdiff instead of patiencediff; this is a larger change, since
bdiff works on 2 multi-line strings, while patiencediff works on 2
lists;
- rename the main class from Merge3 to Merge3Text and add a Merge3
class that derives from Merge3Text. This new Merge3 class has
the same interface from the original class, so that the tests
still work;
- Merge3 uses util.binary to detect binary data and raises
util.Abort instead of a specific exception;
- don't use the @decorator syntax, to keep python2.3 compatibility;
- the test uses unittest, which likes to print how long it took to
run. This obviously doesn't play too well with hg's test suite,
so we override time.time to fool unittest;
- one test has a different (but still valid) output because of the
different diff algorithm used;
- the TestCase class used by bzr has some extras to help debugging.
test-merge3.py used 2 of them:
- log method to log some data
- assertEqualDiff method to ease viewing diffs of diffs
We add a dummy log method and use regular assertEquals instead of
assertEqualDiff.
- make simplemerge executable and add "#!/usr/bin/env python" header
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Mon, 16 Apr 2007 20:17:39 -0300 |
parents | 465b9ea02868 |
children | 96d8a56d4ef9 |
comparison
equal
deleted
inserted
replaced
4362:465b9ea02868 | 4363:2e3c54fb79a3 |
---|---|
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with this program; if not, write to the Free Software | 14 # along with this program; if not, write to the Free Software |
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 | 16 |
17 | 17 import os |
18 from bzrlib.tests import TestCaseInTempDir, TestCase | 18 import unittest |
19 from bzrlib.merge3 import Merge3 | 19 from unittest import TestCase |
20 from bzrlib.errors import CantReprocessAndShowBase, BinaryFile | 20 import imp |
21 import shutil | |
22 from mercurial import util | |
23 | |
24 # copy simplemerge to the cwd to avoid creating a .pyc file in the source tree | |
25 shutil.copyfile(os.path.join(os.environ['TESTDIR'], os.path.pardir, | |
26 'contrib', 'simplemerge'), | |
27 'simplemerge.py') | |
28 simplemerge = imp.load_source('simplemerge', 'simplemerge.py') | |
29 Merge3 = simplemerge.Merge3 | |
30 CantReprocessAndShowBase = simplemerge.CantReprocessAndShowBase | |
21 | 31 |
22 def split_lines(t): | 32 def split_lines(t): |
23 from cStringIO import StringIO | 33 from cStringIO import StringIO |
24 return StringIO(t).readlines() | 34 return StringIO(t).readlines() |
25 | 35 |
90 | 100 |
91 >>>>>>> TAO | 101 >>>>>>> TAO |
92 """) | 102 """) |
93 | 103 |
94 class TestMerge3(TestCase): | 104 class TestMerge3(TestCase): |
105 def log(self, msg): | |
106 pass | |
95 | 107 |
96 def test_no_changes(self): | 108 def test_no_changes(self): |
97 """No conflicts because nothing changed""" | 109 """No conflicts because nothing changed""" |
98 m3 = Merge3(['aaa', 'bbb'], | 110 m3 = Merge3(['aaa', 'bbb'], |
99 ['aaa', 'bbb'], | 111 ['aaa', 'bbb'], |
308 this_text = ("a\n"*10+"b\n" * 10).splitlines(True) | 320 this_text = ("a\n"*10+"b\n" * 10).splitlines(True) |
309 other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True) | 321 other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True) |
310 m3 = Merge3(base_text, other_text, this_text) | 322 m3 = Merge3(base_text, other_text, this_text) |
311 m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True) | 323 m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True) |
312 merged_text = "".join(list(m_lines)) | 324 merged_text = "".join(list(m_lines)) |
313 optimal_text = ("a\n" * 10 + "<<<<<<< OTHER\nc\n" | 325 optimal_text = ("a\n" * 10 + "<<<<<<< OTHER\nc\n=======\n" |
314 + 8* "b\n" + "c\n=======\n" | 326 + ">>>>>>> THIS\n" |
315 + 10*"b\n" + ">>>>>>> THIS\n") | 327 + 8* "b\n" + "<<<<<<< OTHER\nc\n=======\n" |
316 self.assertEqualDiff(optimal_text, merged_text) | 328 + 2* "b\n" + ">>>>>>> THIS\n") |
329 self.assertEquals(optimal_text, merged_text) | |
317 | 330 |
318 def test_minimal_conflicts_unique(self): | 331 def test_minimal_conflicts_unique(self): |
319 def add_newline(s): | 332 def add_newline(s): |
320 """Add a newline to each entry in the string""" | 333 """Add a newline to each entry in the string""" |
321 return [(x+'\n') for x in s] | 334 return [(x+'\n') for x in s] |
329 optimal_text = ''.join(add_newline("abcdefghijklm") | 342 optimal_text = ''.join(add_newline("abcdefghijklm") |
330 + ["<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"] | 343 + ["<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"] |
331 + add_newline('OPQRSTUVWXY') | 344 + add_newline('OPQRSTUVWXY') |
332 + ["<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"] | 345 + ["<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"] |
333 ) | 346 ) |
334 self.assertEqualDiff(optimal_text, merged_text) | 347 self.assertEquals(optimal_text, merged_text) |
335 | 348 |
336 def test_minimal_conflicts_nonunique(self): | 349 def test_minimal_conflicts_nonunique(self): |
337 def add_newline(s): | 350 def add_newline(s): |
338 """Add a newline to each entry in the string""" | 351 """Add a newline to each entry in the string""" |
339 return [(x+'\n') for x in s] | 352 return [(x+'\n') for x in s] |
347 optimal_text = ''.join(add_newline("abacddefgghijk") | 360 optimal_text = ''.join(add_newline("abacddefgghijk") |
348 + ["<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"] | 361 + ["<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"] |
349 + add_newline('lmontfpr') | 362 + add_newline('lmontfpr') |
350 + ["<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"] | 363 + ["<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"] |
351 ) | 364 ) |
352 self.assertEqualDiff(optimal_text, merged_text) | 365 self.assertEquals(optimal_text, merged_text) |
353 | 366 |
354 def test_reprocess_and_base(self): | 367 def test_reprocess_and_base(self): |
355 """Reprocessing and showing base breaks correctly""" | 368 """Reprocessing and showing base breaks correctly""" |
356 base_text = ("a\n" * 20).splitlines(True) | 369 base_text = ("a\n" * 20).splitlines(True) |
357 this_text = ("a\n"*10+"b\n" * 10).splitlines(True) | 370 this_text = ("a\n"*10+"b\n" * 10).splitlines(True) |
360 m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True, | 373 m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True, |
361 base_marker='|||||||') | 374 base_marker='|||||||') |
362 self.assertRaises(CantReprocessAndShowBase, list, m_lines) | 375 self.assertRaises(CantReprocessAndShowBase, list, m_lines) |
363 | 376 |
364 def test_binary(self): | 377 def test_binary(self): |
365 self.assertRaises(BinaryFile, Merge3, ['\x00'], ['a'], ['b']) | 378 self.assertRaises(util.Abort, Merge3, ['\x00'], ['a'], ['b']) |
366 | 379 |
367 def test_dos_text(self): | 380 def test_dos_text(self): |
368 base_text = 'a\r\n' | 381 base_text = 'a\r\n' |
369 this_text = 'b\r\n' | 382 this_text = 'b\r\n' |
370 other_text = 'c\r\n' | 383 other_text = 'c\r\n' |
381 m3 = Merge3(base_text.splitlines(True), other_text.splitlines(True), | 394 m3 = Merge3(base_text.splitlines(True), other_text.splitlines(True), |
382 this_text.splitlines(True)) | 395 this_text.splitlines(True)) |
383 m_lines = m3.merge_lines('OTHER', 'THIS') | 396 m_lines = m3.merge_lines('OTHER', 'THIS') |
384 self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r' | 397 self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r' |
385 '>>>>>>> THIS\r'.splitlines(True), list(m_lines)) | 398 '>>>>>>> THIS\r'.splitlines(True), list(m_lines)) |
399 | |
400 if __name__ == '__main__': | |
401 # hide the timer | |
402 import time | |
403 orig = time.time | |
404 try: | |
405 time.time = lambda: 0 | |
406 unittest.main() | |
407 finally: | |
408 time.time = orig | |
409 |