[GRASS-SVN] r64683 - in grass/trunk/lib/python/gunittest: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Feb 18 20:32:40 PST 2015


Author: wenzeslaus
Date: 2015-02-18 20:32:40 -0800 (Wed, 18 Feb 2015)
New Revision: 64683

Modified:
   grass/trunk/lib/python/gunittest/case.py
   grass/trunk/lib/python/gunittest/testsuite/test_assertions.py
Log:
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


Modified: grass/trunk/lib/python/gunittest/case.py
===================================================================
--- grass/trunk/lib/python/gunittest/case.py	2015-02-18 22:21:18 UTC (rev 64682)
+++ grass/trunk/lib/python/gunittest/case.py	2015-02-19 04:32:40 UTC (rev 64683)
@@ -142,30 +142,43 @@
         will be included in the error message. This method is used by default
         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``
-        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::
-            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()``.
         """
+        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(
-            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):
-        """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.
         """
         self.assertTrue(isinstance(actual, basestring), (
                         'actual argument is not a string'))
         self.assertTrue(isinstance(reference, basestring), (
                         '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):
             # TODO: add support for multiline (first line general, others with details)
             standardMsg = '"%s" does not correspond with "%s"' % (actual,
@@ -558,8 +571,12 @@
             hasher.hexdigest()
         """
         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))
 
     def assertFilesEqualMd5(self, filename, reference, msg=None):

Modified: grass/trunk/lib/python/gunittest/testsuite/test_assertions.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/test_assertions.py	2015-02-18 22:21:18 UTC (rev 64682)
+++ grass/trunk/lib/python/gunittest/testsuite/test_assertions.py	2015-02-19 04:32:40 UTC (rev 64683)
@@ -16,6 +16,10 @@
 
 class TestTextAssertions(grass.gunittest.TestCase):
     # pylint: disable=R0904
+
+    std_newline = "aaa\nbbb\n"
+    platfrom_newline = "aaa{nl}bbb{nl}".format(nl=os.linesep)
+
     def test_assertLooksLike(self):
         self.assertLooksLike("Generated map is <elevation>",
                              "Generated map is <...>")
@@ -30,6 +34,10 @@
         self.assertLooksLike("a=123\nb=456\nc=789",
                              "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):
         self.assertLooksLike("abc = 125521",
                              "abc = 125...")
@@ -44,16 +52,16 @@
                           "abc = 689.159589",
                           "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):
-        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):
         """Test mixed line endings"""
@@ -63,11 +71,12 @@
                           "aaa\nbbb\n")
 
     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.assertEqual,
                           "aaa\n\rbbb\r",



More information about the grass-commit mailing list