comparison tests/test-bdiff.py @ 30591:1b393a93a7df

tests: migrate test-bdiff.py to use unittest (part 1 of 4) This moves all the test() calls, which were easy and mechanical.
author Augie Fackler <augie@google.com>
date Thu, 15 Dec 2016 10:10:15 -0500
parents 96f2f50d923f
children 0d8cada9998d
comparison
equal deleted inserted replaced
30590:74eecb93c617 30591:1b393a93a7df
1 from __future__ import absolute_import, print_function 1 from __future__ import absolute_import, print_function
2 import struct 2 import struct
3 import unittest
4
5 import silenttestrunner
6
3 from mercurial import ( 7 from mercurial import (
4 bdiff, 8 bdiff,
5 mpatch, 9 mpatch,
6 ) 10 )
7 11
8 def test1(a, b): 12 class BdiffTests(unittest.TestCase):
9 d = bdiff.bdiff(a, b)
10 c = a
11 if d:
12 c = mpatch.patches(a, [d])
13 if c != b:
14 print("bad diff+patch result from\n %r to\n %r:" % (a, b))
15 print("bdiff: %r" % d)
16 print("patched: %r" % c[:200])
17 13
18 def test(a, b): 14 def assert_bdiff_applies(self, a, b):
19 print("test", repr(a), repr(b)) 15 d = bdiff.bdiff(a, b)
20 test1(a, b) 16 c = a
21 test1(b, a) 17 if d:
18 c = mpatch.patches(a, [d])
19 self.assertEqual(
20 c, b, ("bad diff+patch result from\n %r to\n "
21 "%r: \nbdiff: %r\npatched: %r" % (a, b, d, c[:200])))
22 22
23 test("a\nc\n\n\n\n", "a\nb\n\n\n") 23 def assert_bdiff(self, a, b):
24 test("a\nb\nc\n", "a\nc\n") 24 self.assert_bdiff_applies(a, b)
25 test("", "") 25 self.assert_bdiff_applies(b, a)
26 test("a\nb\nc", "a\nb\nc") 26
27 test("a\nb\nc\nd\n", "a\nd\n") 27 def test_bdiff_basic(self):
28 test("a\nb\nc\nd\n", "a\nc\ne\n") 28 cases = [
29 test("a\nb\nc\n", "a\nc\n") 29 ("a\nc\n\n\n\n", "a\nb\n\n\n"),
30 test("a\n", "c\na\nb\n") 30 ("a\nb\nc\n", "a\nc\n"),
31 test("a\n", "") 31 ("", ""),
32 test("a\n", "b\nc\n") 32 ("a\nb\nc", "a\nb\nc"),
33 test("a\n", "c\na\n") 33 ("a\nb\nc\nd\n", "a\nd\n"),
34 test("", "adjfkjdjksdhfksj") 34 ("a\nb\nc\nd\n", "a\nc\ne\n"),
35 test("", "ab") 35 ("a\nb\nc\n", "a\nc\n"),
36 test("", "abc") 36 ("a\n", "c\na\nb\n"),
37 test("a", "a") 37 ("a\n", ""),
38 test("ab", "ab") 38 ("a\n", "b\nc\n"),
39 test("abc", "abc") 39 ("a\n", "c\na\n"),
40 test("a\n", "a\n") 40 ("", "adjfkjdjksdhfksj"),
41 test("a\nb", "a\nb") 41 ("", "ab"),
42 ("", "abc"),
43 ("a", "a"),
44 ("ab", "ab"),
45 ("abc", "abc"),
46 ("a\n", "a\n"),
47 ("a\nb", "a\nb"),
48 ]
49 for a, b in cases:
50 self.assert_bdiff(a, b)
42 51
43 #issue1295 52 #issue1295
44 def showdiff(a, b): 53 def showdiff(a, b):
45 print('showdiff(\n %r,\n %r):' % (a, b)) 54 print('showdiff(\n %r,\n %r):' % (a, b))
46 bin = bdiff.bdiff(a, b) 55 bin = bdiff.bdiff(a, b)
90 showdiff('a\n', 'a\n' * 5) 99 showdiff('a\n', 'a\n' * 5)
91 print("Diff 3 to 1 lines - preference for removing trailing lines:") 100 print("Diff 3 to 1 lines - preference for removing trailing lines:")
92 showdiff('a\n' * 3, 'a\n') 101 showdiff('a\n' * 3, 'a\n')
93 print("Diff 5 to 1 lines - preference for removing trailing lines:") 102 print("Diff 5 to 1 lines - preference for removing trailing lines:")
94 showdiff('a\n' * 5, 'a\n') 103 showdiff('a\n' * 5, 'a\n')
104
105 if __name__ == '__main__':
106 silenttestrunner.main(__name__)