Changeset 64683


Ignore:
Timestamp:
Feb 18, 2015, 8:32:40 PM (9 years ago)
Author:
wenzeslaus
Message:

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
Location:
grass/trunk/lib/python/gunittest
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • grass/trunk/lib/python/gunittest/case.py

    r64662 r64683  
    143143        when comparing strings with assertEqual().
    144144
    145         This method replaces ``\r\n`` by ``\n`` in both parameters. This is
     145        This method replaces platform dependent newline characters
     146        by ``\n`` (LF) in both parameters. This is
    146147        different from the same method implemented in Python ``unittest``
    147         package which preserves the original line endings. This removes the
    148         burden of getting the line endings right on each platfrom. You can
    149         just use ``\n`` everywhere. However, note that ``\r`` is not supported.
     148        package which preserves the original newline characters.
     149
     150        This function removes the burden of getting the newline characters
     151        right on each platfrom. You can just use ``\n`` everywhere and this
     152        function will ensure that it does not matter if for example,
     153        a module generates (as expected) ``\r\n`` (CRLF) newline characters
     154        on MS Windows.
    150155
    151156        .. warning::
    152             If you need to test the actual line endings, use the standard
     157            If you need to test the actual newline characters, use the standard
    153158            string comparison and functions such as ``find()``.
    154159        """
     160        if os.linesep != '\n':
     161            if os.linesep in first:
     162                first = first.replace(os.linesep, '\n')
     163            if os.linesep in second:
     164                second = second.replace(os.linesep, '\n')
    155165        return super(TestCase, self).assertMultiLineEqual(
    156             first=first.replace('\r\n', '\n'),
    157             second=second.replace('\r\n', '\n'),
    158             msg=None)
     166            first=first, second=second, msg=msg)
    159167
    160168    def assertLooksLike(self, actual, reference, msg=None):
    161         """Test that ``actual`` text is the same as ``referece`` with ellipses.
     169        r"""Test that ``actual`` text is the same as ``referece`` with ellipses.
     170
     171        If ``actual`` contains platform dependent newline characters,
     172        these will replaced by ``\n`` which is expected to be in the test data.
    162173
    163174        See :func:`check_text_ellipsis` for details of behavior.
     
    167178        self.assertTrue(isinstance(reference, basestring), (
    168179                        'reference argument is not a string'))
     180        if os.linesep != '\n' and os.linesep in actual:
     181            actual = actual.replace(os.linesep, '\n')
    169182        if not check_text_ellipsis(actual=actual, reference=reference):
    170183            # TODO: add support for multiline (first line general, others with details)
     
    559572        """
    560573        self.assertFileExists(filename, msg=msg)
    561         if not file_md5(filename) == md5:
    562             standardMsg = 'File %s does not have the right MD5 sum' % filename
     574        actual = file_md5(filename)
     575        if not actual == md5:
     576            standardMsg = ('File <{name}> does not have the right MD5 sum.\n'
     577                           'Expected is <{expected}>,'
     578                           ' actual is <{actual}>'.format(
     579                               name=filename, expected=md5, actual=actual))
    563580            self.fail(self._formatMessage(msg, standardMsg))
    564581
  • grass/trunk/lib/python/gunittest/testsuite/test_assertions.py

    r64662 r64683  
    1717class TestTextAssertions(grass.gunittest.TestCase):
    1818    # pylint: disable=R0904
     19
     20    std_newline = "aaa\nbbb\n"
     21    platfrom_newline = "aaa{nl}bbb{nl}".format(nl=os.linesep)
     22
    1923    def test_assertLooksLike(self):
    2024        self.assertLooksLike("Generated map is <elevation>",
     
    3034        self.assertLooksLike("a=123\nb=456\nc=789",
    3135                             "a=...\nb=...\nc=...")
     36
     37    def test_assertLooksLike_multiline_platform_dependent(self):
     38        self.assertLooksLike("a=123\nb=456\nc=789",
     39                             "a=...{nl}b=...{nl}c=...".format(nl=os.linesep))
    3240
    3341    def test_assertLooksLike_numbers(self):
     
    4553                          "abc = 689....")
    4654
    47     def do_all_combidnations(self, first, second):
    48         self.assertMultiLineEqual(first, first)
    49         self.assertMultiLineEqual(first, second)
    50         self.assertMultiLineEqual(second, first)
    51         self.assertMultiLineEqual(second, second)
     55    def do_all_combidnations(self, first, second, function):
     56        function(first, first)
     57        function(first, second)
     58        function(second, first)
     59        function(second, second)
    5260
    5361    def test_assertMultiLineEqual(self):
    54         unix_end = "aaa\nbbb\n"
    55         mswindows_end = "aaa\r\nbbb\r\n"
    56         self.do_all_combidnations(unix_end, mswindows_end)
     62        r"""Test different combinations of ``\n`` and os.linesep"""
     63        self.do_all_combidnations(self.std_newline, self.platfrom_newline,
     64                                  function=self.assertMultiLineEqual)
    5765
    5866    def test_assertMultiLineEqual_raises(self):
     
    6472
    6573    def test_assertEqual(self):
    66         """Test for strings (uses overwritten assertMultiLineEqual())"""
    67         unix_end = "aaa\nbbb\n"
    68         mswindows_end = "aaa\r\nbbb\r\n"
    69         self.do_all_combidnations(unix_end, mswindows_end)
    70 
     74        """Test for of newlines for strings (uses overwritten assertMultiLineEqual())"""
     75        self.do_all_combidnations(self.std_newline, self.platfrom_newline,
     76                                  function=self.assertEqual)
     77
     78    def test_assertEqual_raises(self):
     79        """Test mixed line endings"""
    7180        self.assertRaises(self.failureException,
    7281                          self.assertEqual,
Note: See TracChangeset for help on using the changeset viewer.