浏览代码

gunittest: update syntax to be python3 ready

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@68356 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 9 年之前
父节点
当前提交
f1c9d4d6ff

+ 5 - 5
lib/python/gunittest/case.py

@@ -183,9 +183,9 @@ class TestCase(unittest.TestCase):
 
         See :func:`check_text_ellipsis` for details of behavior.
         """
-        self.assertTrue(isinstance(actual, basestring), (
+        self.assertTrue(isinstance(actual, str), (
                         'actual argument is not a string'))
-        self.assertTrue(isinstance(reference, basestring), (
+        self.assertTrue(isinstance(reference, str), (
                         'reference argument is not a string'))
         if os.linesep != '\n' and os.linesep in actual:
             actual = actual.replace(os.linesep, '\n')
@@ -220,7 +220,7 @@ class TestCase(unittest.TestCase):
         The output of the module should be key-value pairs (shell script style)
         which is typically obtained using ``-g`` flag.
         """
-        if isinstance(reference, basestring):
+        if isinstance(reference, str):
             reference = text_to_keyvalue(reference, sep=sep, skip_empty=True)
         module = _module_from_parameters(module, **parameters)
         self.runModule(module, expecting_stdout=True)
@@ -1194,13 +1194,13 @@ class TestCase(unittest.TestCase):
 # some test and documentation add to assertModuleKeyValue
 def _module_from_parameters(module, **kwargs):
     if kwargs:
-        if not isinstance(module, basestring):
+        if not isinstance(module, str):
             raise ValueError('module can be only string or PyGRASS Module')
         if isinstance(module, Module):
             raise ValueError('module can be only string if other'
                              ' parameters are given')
             # allow passing all parameters in one dictionary called parameters
-        if kwargs.keys() == ['parameters']:
+        if list(kwargs.keys()) == ['parameters']:
             kwargs = kwargs['parameters']
         module = SimpleModule(module, **kwargs)
     return module

+ 2 - 2
lib/python/gunittest/checkers.py

@@ -82,14 +82,14 @@ def unify_units(dic):
     dic = dict(dic)
     for l in lookup:
         import types
-        if not isinstance(dic['unit'], types.StringTypes):
+        if not isinstance(dic['unit'], str):
             for n in range(len(dic['unit'])):
                 if dic['unit'][n] in l:
                     dic['unit'][n] = l[0]
         else:
             if dic['unit'] in l:
                 dic['unit'] = l[0]
-        if not isinstance(dic['units'], types.StringTypes):
+        if not isinstance(dic['units'], str):
             for n in range(len(dic['units'])):
                 if dic['units'][n] in l:
                     dic['units'][n] = l[0]

+ 2 - 2
lib/python/gunittest/multireport.py

@@ -400,7 +400,7 @@ def main():
             all_results.append(result)
             del result
         except KeyError as e:
-            print 'File %s does not have right values (%s)' % (report, e.message) 
+            print('File %s does not have right values (%s)' % (report, e.message)) 
 
     locations_main_page = open(os.path.join(output, 'index.html'), 'w')
     locations_main_page.write(
@@ -420,7 +420,7 @@ def main():
     plot_style = PlotStyle(linestyle='-', linewidth=4.0,
                            success_color='g', fail_color='r', total_color='b')
 
-    for location_type, results in results_in_locations.iteritems():
+    for location_type, results in results_in_locations.items():
         results = sorted(results, key=operator.attrgetter('timestamp'))
         # TODO: document: location type must be a valid dir name
         directory = os.path.join(output, location_type)

+ 1 - 2
lib/python/gunittest/reporters.py

@@ -26,7 +26,6 @@ if sys.version_info[0] == 2:
     from StringIO import StringIO
 else:
     from io import StringIO
-    basestring = str
 
 
 # TODO: change text_to_keyvalue to same sep as here
@@ -38,7 +37,7 @@ def keyvalue_to_text(keyvalue, sep='=', vsep='\n', isep=',',
     items = []
     for key, value in keyvalue.items():
         # TODO: use isep for iterables other than strings
-        if (not isinstance(value, basestring)
+        if (not isinstance(value, str)
                 and isinstance(value, collections.Iterable)):
             # TODO: this does not work for list of non-strings
             value = isep.join(value)

+ 1 - 1
lib/python/gunittest/testsuite/data/samplecode/fake_code.py

@@ -1 +1 @@
-print "This is file (%s) should not run." % __file__
+print("This is file (%s) should not run." % __file__)

+ 1 - 1
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/fake_code.py

@@ -1 +1 @@
-print "This is file (%s) should not run." % __file__
+print("This is file (%s) should not run." % __file__)

+ 1 - 1
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/fake_code.py

@@ -1 +1 @@
-print "This is file (%s) should not run." % __file__
+print("This is file (%s) should not run." % __file__)

+ 1 - 1
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/fake_code.py

@@ -1 +1 @@
-print "This is file (%s) should not run." % __file__
+print("This is file (%s) should not run." % __file__)

+ 1 - 1
lib/python/gunittest/testsuite/data/samplecode/submodule_test_fail/fake_code.py

@@ -1 +1 @@
-print "This is file (%s) should not run." % __file__
+print("This is file (%s) should not run." % __file__)

+ 5 - 5
lib/python/gunittest/testsuite/data/samplecode/testsuite/test_python_unittest.py

@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-
+from __furute__ import print_function
 from unittest import TestCase, main
 
 
@@ -7,10 +7,10 @@ class TestUnittestSuccessVerboseSetUp(TestCase):
     # pylint: disable=R0904
 
     def setUp(self):
-        print "print from setUp"
+        print("print from setUp")
 
     def tearDown(self):
-        print "print from tearDown"
+        print("print from tearDown")
 
     def test_something(self):
         self.assertTrue(True, msg="This should not fail")
@@ -24,11 +24,11 @@ class TestUnittestSuccessVerboseClassSetUp(TestCase):
 
     @classmethod
     def setUpClass(cls):
-        print "print from setUpClass"
+        print("print from setUpClass")
 
     @classmethod
     def tearDownClass(cls):
-        print "print from tearDownClass"
+        print("print from tearDownClass")
 
     def test_something(self):
         self.assertTrue(True, msg="This should not fail")

+ 5 - 4
lib/python/gunittest/testsuite/data/samplecode/testsuite/test_success.py

@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+from __future__ import print_function
 
 from grass.gunittest.case import TestCase
 from grass.gunittest.main import test
@@ -8,10 +9,10 @@ class TestSuccessVerboseSetUp(TestCase):
     # pylint: disable=R0904
 
     def setUp(self):
-        print "print from setUp"
+        print("print from setUp")
 
     def tearDown(self):
-        print "print from tearDown"
+        print("print from tearDown")
 
     def test_something(self):
         self.assertTrue(True)
@@ -22,11 +23,11 @@ class TestSuccessVerboseClassSetUp(TestCase):
 
     @classmethod
     def setUpClass(cls):
-        print "print from setUpClass"
+        print("print from setUpClass")
 
     @classmethod
     def tearDownClass(cls):
-        print "print from tearDownClass"
+        print("print from tearDownClass")
 
     def test_something(self):
         self.assertTrue(True)