[GRASS-SVN] r68179 - grass/trunk/lib/python/script/testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Mar 28 23:24:03 PDT 2016
Author: zarch
Date: 2016-03-28 23:24:03 -0700 (Mon, 28 Mar 2016)
New Revision: 68179
Added:
grass/trunk/lib/python/script/testsuite/test_utils.py
Log:
script.utils: add test to highlight bug in encode/decode functions
Added: grass/trunk/lib/python/script/testsuite/test_utils.py
===================================================================
--- grass/trunk/lib/python/script/testsuite/test_utils.py (rev 0)
+++ grass/trunk/lib/python/script/testsuite/test_utils.py 2016-03-29 06:24:03 UTC (rev 68179)
@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+import os
+
+from grass.gunittest.case import TestCase
+from grass.gunittest.main import test
+
+from grass.script import utils
+
+
+def change_environ(**envs):
+ NOT_FOUND = 'Not found!'
+ original_envs = {k: os.environ.get(k, NOT_FOUND) for k in envs.keys()}
+
+ def work_with_env(func):
+ def wrap_func(*args, **kwargs):
+ # modify the environment variables
+ for k, v in envs.items():
+ os.environ[k] = v
+
+ # execute
+ func(*args, **kwargs)
+
+ # restore the environment variables
+ for k, v in envs.items():
+ oval = original_envs[k]
+ if oval == NOT_FOUND:
+ os.environ.pop(k)
+ else:
+ os.environ[k] = oval
+ return wrap_func
+ return work_with_env
+
+
+class TestEncode(TestCase):
+ """Tests function `encode` that convert value to bytes."""
+
+ def test_bytes(self):
+ self.assertEqual(b'text', utils.encode(b'text'))
+
+ def test_unicode(self):
+ self.assertEqual(b'text', utils.encode(u'text'))
+
+ @change_environ(LC_ALL='C')
+ def test_bytes_LC_ALL_C(self):
+ self.assertEqual(b'text', utils.encode(b'text'))
+
+ @change_environ(LC_ALL='C')
+ def test_unicode_LC_ALL_C(self):
+ self.assertEqual(b'text', utils.encode(u'text'))
+
+
+class TestDecode(TestCase):
+ """Tests function `encode` that convert value to unicode."""
+
+ def test_bytes(self):
+ self.assertEqual(u'text', utils.decode(b'text'))
+
+ def test_unicode(self):
+ self.assertEqual(u'text', utils.decode(u'text'))
+
+ @change_environ(LC_ALL='C')
+ def test_bytes_LC_ALL_C(self):
+ self.assertEqual(u'text', utils.decode(b'text'))
+
+ @change_environ(LC_ALL='C')
+ def test_unicode_LC_ALL_C(self):
+ self.assertEqual(u'text', utils.decode(u'text'))
+
+
+
+
+if __name__ == '__main__':
+ test()
\ No newline at end of file
Property changes on: grass/trunk/lib/python/script/testsuite/test_utils.py
___________________________________________________________________
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
More information about the grass-commit
mailing list