瀏覽代碼

gunittest: improve the implementation of newline tests using os.linesep

 * use the same also in assertLooksLike function
 * fix message handling in assertMultiLineEqual function
 * update tests accordingly
 * tell user MD5 sums when testing against a MD5 sum


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@64683 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 10 年之前
父節點
當前提交
633b1fa7a5
共有 2 個文件被更改,包括 49 次插入23 次删除
  1. 28 11
      lib/python/gunittest/case.py
  2. 21 12
      lib/python/gunittest/testsuite/test_assertions.py

+ 28 - 11
lib/python/gunittest/case.py

@@ -142,23 +142,34 @@ class TestCase(unittest.TestCase):
         will be included in the error message. This method is used by default
         will be included in the error message. This method is used by default
         when comparing strings with assertEqual().
         when comparing strings with assertEqual().
 
 
-        This method replaces ``\r\n`` by ``\n`` in both parameters. This is
+        This method replaces platform dependent newline characters
+        by ``\n`` (LF) in both parameters. This is
         different from the same method implemented in Python ``unittest``
         different from the same method implemented in Python ``unittest``
-        package which preserves the original line endings. This removes the
-        burden of getting the line endings right on each platfrom. You can
-        just use ``\n`` everywhere. However, note that ``\r`` is not supported.
+        package which preserves the original newline characters.
+
+        This function removes the burden of getting the newline characters
+        right on each platfrom. You can just use ``\n`` everywhere and this
+        function will ensure that it does not matter if for example,
+        a module generates (as expected) ``\r\n`` (CRLF) newline characters
+        on MS Windows.
 
 
         .. warning::
         .. warning::
-            If you need to test the actual line endings, use the standard
+            If you need to test the actual newline characters, use the standard
             string comparison and functions such as ``find()``.
             string comparison and functions such as ``find()``.
         """
         """
+        if os.linesep != '\n':
+            if os.linesep in first:
+                first = first.replace(os.linesep, '\n')
+            if os.linesep in second:
+                second = second.replace(os.linesep, '\n')
         return super(TestCase, self).assertMultiLineEqual(
         return super(TestCase, self).assertMultiLineEqual(
-            first=first.replace('\r\n', '\n'),
-            second=second.replace('\r\n', '\n'),
-            msg=None)
+            first=first, second=second, msg=msg)
 
 
     def assertLooksLike(self, actual, reference, msg=None):
     def assertLooksLike(self, actual, reference, msg=None):
-        """Test that ``actual`` text is the same as ``referece`` with ellipses.
+        r"""Test that ``actual`` text is the same as ``referece`` with ellipses.
+
+        If ``actual`` contains platform dependent newline characters,
+        these will replaced by ``\n`` which is expected to be in the test data.
 
 
         See :func:`check_text_ellipsis` for details of behavior.
         See :func:`check_text_ellipsis` for details of behavior.
         """
         """
@@ -166,6 +177,8 @@ class TestCase(unittest.TestCase):
                         'actual argument is not a string'))
                         'actual argument is not a string'))
         self.assertTrue(isinstance(reference, basestring), (
         self.assertTrue(isinstance(reference, basestring), (
                         'reference argument is not a string'))
                         'reference argument is not a string'))
+        if os.linesep != '\n' and os.linesep in actual:
+            actual = actual.replace(os.linesep, '\n')
         if not check_text_ellipsis(actual=actual, reference=reference):
         if not check_text_ellipsis(actual=actual, reference=reference):
             # TODO: add support for multiline (first line general, others with details)
             # TODO: add support for multiline (first line general, others with details)
             standardMsg = '"%s" does not correspond with "%s"' % (actual,
             standardMsg = '"%s" does not correspond with "%s"' % (actual,
@@ -558,8 +571,12 @@ class TestCase(unittest.TestCase):
             hasher.hexdigest()
             hasher.hexdigest()
         """
         """
         self.assertFileExists(filename, msg=msg)
         self.assertFileExists(filename, msg=msg)
-        if not file_md5(filename) == md5:
-            standardMsg = 'File %s does not have the right MD5 sum' % filename
+        actual = file_md5(filename)
+        if not actual == md5:
+            standardMsg = ('File <{name}> does not have the right MD5 sum.\n'
+                           'Expected is <{expected}>,'
+                           ' actual is <{actual}>'.format(
+                               name=filename, expected=md5, actual=actual))
             self.fail(self._formatMessage(msg, standardMsg))
             self.fail(self._formatMessage(msg, standardMsg))
 
 
     def assertFilesEqualMd5(self, filename, reference, msg=None):
     def assertFilesEqualMd5(self, filename, reference, msg=None):

+ 21 - 12
lib/python/gunittest/testsuite/test_assertions.py

@@ -16,6 +16,10 @@ from grass.gunittest.gmodules import SimpleModule
 
 
 class TestTextAssertions(grass.gunittest.TestCase):
 class TestTextAssertions(grass.gunittest.TestCase):
     # pylint: disable=R0904
     # pylint: disable=R0904
+
+    std_newline = "aaa\nbbb\n"
+    platfrom_newline = "aaa{nl}bbb{nl}".format(nl=os.linesep)
+
     def test_assertLooksLike(self):
     def test_assertLooksLike(self):
         self.assertLooksLike("Generated map is <elevation>",
         self.assertLooksLike("Generated map is <elevation>",
                              "Generated map is <...>")
                              "Generated map is <...>")
@@ -30,6 +34,10 @@ class TestTextAssertions(grass.gunittest.TestCase):
         self.assertLooksLike("a=123\nb=456\nc=789",
         self.assertLooksLike("a=123\nb=456\nc=789",
                              "a=...\nb=...\nc=...")
                              "a=...\nb=...\nc=...")
 
 
+    def test_assertLooksLike_multiline_platform_dependent(self):
+        self.assertLooksLike("a=123\nb=456\nc=789",
+                             "a=...{nl}b=...{nl}c=...".format(nl=os.linesep))
+
     def test_assertLooksLike_numbers(self):
     def test_assertLooksLike_numbers(self):
         self.assertLooksLike("abc = 125521",
         self.assertLooksLike("abc = 125521",
                              "abc = 125...")
                              "abc = 125...")
@@ -44,16 +52,16 @@ class TestTextAssertions(grass.gunittest.TestCase):
                           "abc = 689.159589",
                           "abc = 689.159589",
                           "abc = 689....")
                           "abc = 689....")
 
 
-    def do_all_combidnations(self, first, second):
-        self.assertMultiLineEqual(first, first)
-        self.assertMultiLineEqual(first, second)
-        self.assertMultiLineEqual(second, first)
-        self.assertMultiLineEqual(second, second)
+    def do_all_combidnations(self, first, second, function):
+        function(first, first)
+        function(first, second)
+        function(second, first)
+        function(second, second)
 
 
     def test_assertMultiLineEqual(self):
     def test_assertMultiLineEqual(self):
-        unix_end = "aaa\nbbb\n"
-        mswindows_end = "aaa\r\nbbb\r\n"
-        self.do_all_combidnations(unix_end, mswindows_end)
+        r"""Test different combinations of ``\n`` and os.linesep"""
+        self.do_all_combidnations(self.std_newline, self.platfrom_newline,
+                                  function=self.assertMultiLineEqual)
 
 
     def test_assertMultiLineEqual_raises(self):
     def test_assertMultiLineEqual_raises(self):
         """Test mixed line endings"""
         """Test mixed line endings"""
@@ -63,11 +71,12 @@ class TestTextAssertions(grass.gunittest.TestCase):
                           "aaa\nbbb\n")
                           "aaa\nbbb\n")
 
 
     def test_assertEqual(self):
     def test_assertEqual(self):
-        """Test for strings (uses overwritten assertMultiLineEqual())"""
-        unix_end = "aaa\nbbb\n"
-        mswindows_end = "aaa\r\nbbb\r\n"
-        self.do_all_combidnations(unix_end, mswindows_end)
+        """Test for of newlines for strings (uses overwritten assertMultiLineEqual())"""
+        self.do_all_combidnations(self.std_newline, self.platfrom_newline,
+                                  function=self.assertEqual)
 
 
+    def test_assertEqual_raises(self):
+        """Test mixed line endings"""
         self.assertRaises(self.failureException,
         self.assertRaises(self.failureException,
                           self.assertEqual,
                           self.assertEqual,
                           "aaa\n\rbbb\r",
                           "aaa\n\rbbb\r",