Patchwork [1,of,5,pure-fix] tests: migrate test-bdiff.py to use unittest (part 1 of 4)

login
register
mail settings
Submitter Augie Fackler
Date Dec. 15, 2016, 4:32 p.m.
Message ID <0aae265cd9708228fc36.1481819553@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/17922/
State Accepted
Headers show

Comments

Augie Fackler - Dec. 15, 2016, 4:32 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1481814615 18000
#      Thu Dec 15 10:10:15 2016 -0500
# Node ID 0aae265cd9708228fc3615b994ef8cdb31060c74
# Parent  be0e7af80543cbd711fc91135d0c9f51dafba452
tests: migrate test-bdiff.py to use unittest (part 1 of 4)

This moves all the test() calls, which were easy and mechanical.
Augie Fackler - Dec. 15, 2016, 4:36 p.m.
On Thu, Dec 15, 2016 at 11:32 AM, Augie Fackler <raf@durin42.com> wrote:
> @@ -1,44 +1,52 @@
>  from __future__ import absolute_import, print_function
> +import silenttestrunner
>  import struct
> +import unittest
> +

I got some weirdly inconsistent import-checking behavior out of these
lines on one machine, but can't reproduce it now. Given that this
looks very similar to the equivalent code in test-manifest.py, I'm not
going to sweat it.

Patch

diff --git a/tests/test-bdiff.py b/tests/test-bdiff.py
--- a/tests/test-bdiff.py
+++ b/tests/test-bdiff.py
@@ -1,44 +1,52 @@ 
 from __future__ import absolute_import, print_function
+import silenttestrunner
 import struct
+import unittest
+
 from mercurial import (
     bdiff,
     mpatch,
 )
 
-def test1(a, b):
-    d = bdiff.bdiff(a, b)
-    c = a
-    if d:
-        c = mpatch.patches(a, [d])
-    if c != b:
-        print("bad diff+patch result from\n  %r to\n  %r:" % (a, b))
-        print("bdiff: %r" % d)
-        print("patched: %r" % c[:200])
-
-def test(a, b):
-    print("test", repr(a), repr(b))
-    test1(a, b)
-    test1(b, a)
+class BdiffTests(unittest.TestCase):
 
-test("a\nc\n\n\n\n", "a\nb\n\n\n")
-test("a\nb\nc\n", "a\nc\n")
-test("", "")
-test("a\nb\nc", "a\nb\nc")
-test("a\nb\nc\nd\n", "a\nd\n")
-test("a\nb\nc\nd\n", "a\nc\ne\n")
-test("a\nb\nc\n", "a\nc\n")
-test("a\n", "c\na\nb\n")
-test("a\n", "")
-test("a\n", "b\nc\n")
-test("a\n", "c\na\n")
-test("", "adjfkjdjksdhfksj")
-test("", "ab")
-test("", "abc")
-test("a", "a")
-test("ab", "ab")
-test("abc", "abc")
-test("a\n", "a\n")
-test("a\nb", "a\nb")
+    def assert_bdiff_applies(self, a, b):
+        d = bdiff.bdiff(a, b)
+        c = a
+        if d:
+            c = mpatch.patches(a, [d])
+        self.assertEqual(
+            c, b, ("bad diff+patch result from\n  %r to\n  "
+                   "%r: \nbdiff: %r\npatched: %r" % (a, b, d, c[:200])))
+
+    def assert_bdiff(self, a, b):
+        self.assert_bdiff_applies(a, b)
+        self.assert_bdiff_applies(b, a)
+
+    def test_bdiff_basic(self):
+        cases = [
+            ("a\nc\n\n\n\n", "a\nb\n\n\n"),
+            ("a\nb\nc\n", "a\nc\n"),
+            ("", ""),
+            ("a\nb\nc", "a\nb\nc"),
+            ("a\nb\nc\nd\n", "a\nd\n"),
+            ("a\nb\nc\nd\n", "a\nc\ne\n"),
+            ("a\nb\nc\n", "a\nc\n"),
+            ("a\n", "c\na\nb\n"),
+            ("a\n", ""),
+            ("a\n", "b\nc\n"),
+            ("a\n", "c\na\n"),
+            ("", "adjfkjdjksdhfksj"),
+            ("", "ab"),
+            ("", "abc"),
+            ("a", "a"),
+            ("ab", "ab"),
+            ("abc", "abc"),
+            ("a\n", "a\n"),
+            ("a\nb", "a\nb"),
+        ]
+        for a, b in cases:
+            self.assert_bdiff(a, b)
 
 #issue1295
 def showdiff(a, b):
@@ -92,3 +100,6 @@  print("Diff 3 to 1 lines - preference fo
 showdiff('a\n' * 3, 'a\n')
 print("Diff 5 to 1 lines - preference for removing trailing lines:")
 showdiff('a\n' * 5, 'a\n')
+
+if __name__ == '__main__':
+    silenttestrunner.main(__name__)
diff --git a/tests/test-bdiff.py.out b/tests/test-bdiff.py.out
--- a/tests/test-bdiff.py.out
+++ b/tests/test-bdiff.py.out
@@ -1,22 +1,3 @@ 
-test 'a\nc\n\n\n\n' 'a\nb\n\n\n'
-test 'a\nb\nc\n' 'a\nc\n'
-test '' ''
-test 'a\nb\nc' 'a\nb\nc'
-test 'a\nb\nc\nd\n' 'a\nd\n'
-test 'a\nb\nc\nd\n' 'a\nc\ne\n'
-test 'a\nb\nc\n' 'a\nc\n'
-test 'a\n' 'c\na\nb\n'
-test 'a\n' ''
-test 'a\n' 'b\nc\n'
-test 'a\n' 'c\na\n'
-test '' 'adjfkjdjksdhfksj'
-test '' 'ab'
-test '' 'abc'
-test 'a' 'a'
-test 'ab' 'ab'
-test 'abc' 'abc'
-test 'a\n' 'a\n'
-test 'a\nb' 'a\nb'
 showdiff(
   'x\n\nx\n\nx\n\nx\n\nz\n',
   'x\n\nx\n\ny\n\nx\n\nx\n\nz\n'):