Changeset 64683
- Timestamp:
- Feb 18, 2015, 8:32:40 PM (9 years ago)
- Location:
- grass/trunk/lib/python/gunittest
- Files:
-
- 2 edited
-
case.py (modified) (3 diffs)
-
testsuite/test_assertions.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
grass/trunk/lib/python/gunittest/case.py
r64662 r64683 143 143 when comparing strings with assertEqual(). 144 144 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 146 147 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. 150 155 151 156 .. warning:: 152 If you need to test the actual line endings, use the standard157 If you need to test the actual newline characters, use the standard 153 158 string comparison and functions such as ``find()``. 154 159 """ 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') 155 165 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) 159 167 160 168 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. 162 173 163 174 See :func:`check_text_ellipsis` for details of behavior. … … 167 178 self.assertTrue(isinstance(reference, basestring), ( 168 179 'reference argument is not a string')) 180 if os.linesep != '\n' and os.linesep in actual: 181 actual = actual.replace(os.linesep, '\n') 169 182 if not check_text_ellipsis(actual=actual, reference=reference): 170 183 # TODO: add support for multiline (first line general, others with details) … … 559 572 """ 560 573 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)) 563 580 self.fail(self._formatMessage(msg, standardMsg)) 564 581 -
grass/trunk/lib/python/gunittest/testsuite/test_assertions.py
r64662 r64683 17 17 class TestTextAssertions(grass.gunittest.TestCase): 18 18 # pylint: disable=R0904 19 20 std_newline = "aaa\nbbb\n" 21 platfrom_newline = "aaa{nl}bbb{nl}".format(nl=os.linesep) 22 19 23 def test_assertLooksLike(self): 20 24 self.assertLooksLike("Generated map is <elevation>", … … 30 34 self.assertLooksLike("a=123\nb=456\nc=789", 31 35 "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)) 32 40 33 41 def test_assertLooksLike_numbers(self): … … 45 53 "abc = 689....") 46 54 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) 52 60 53 61 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) 57 65 58 66 def test_assertMultiLineEqual_raises(self): … … 64 72 65 73 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""" 71 80 self.assertRaises(self.failureException, 72 81 self.assertEqual,
Note:
See TracChangeset
for help on using the changeset viewer.
