Add tests for DependencyArg __str__ and __unicode__ methods.
authorZac Medico <zmedico@gentoo.org>
Thu, 16 Sep 2010 02:41:14 +0000 (19:41 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 16 Sep 2010 02:41:14 +0000 (19:41 -0700)
pym/portage/tests/unicode/__init__.py [new file with mode: 0644]
pym/portage/tests/unicode/__test__ [new file with mode: 0644]
pym/portage/tests/unicode/test_string_format.py [new file with mode: 0644]

diff --git a/pym/portage/tests/unicode/__init__.py b/pym/portage/tests/unicode/__init__.py
new file mode 100644 (file)
index 0000000..21a391a
--- /dev/null
@@ -0,0 +1,2 @@
+# Copyright 2010 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
diff --git a/pym/portage/tests/unicode/__test__ b/pym/portage/tests/unicode/__test__
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/pym/portage/tests/unicode/test_string_format.py b/pym/portage/tests/unicode/test_string_format.py
new file mode 100644 (file)
index 0000000..95ef9cb
--- /dev/null
@@ -0,0 +1,51 @@
+# Copyright 2010 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+import sys
+
+from portage import _encodings, _unicode_decode
+from portage.tests import TestCase
+from _emerge.DependencyArg import DependencyArg
+
+STR_IS_UNICODE = sys.hexversion >= 0x3000000
+
+class StringFormatTestCase(TestCase):
+       """
+       Test that string formatting works correctly in the current interpretter,
+       which may be either python2 or python3.
+       """
+
+       # In order to get some unicode test strings in a way that works in
+       # both python2 and python3, write them here as byte strings and
+       # decode them before use. This assumes _encodings['content'] is
+       # utf_8.
+
+       unicode_strings = (
+               b'\xE2\x80\x98',
+               b'\xE2\x80\x99',
+       )
+
+       def testDependencyArg(self):
+
+               self.assertEqual(_encodings['content'], 'utf_8')
+
+               for arg_bytes in self.unicode_strings:
+                       arg_unicode = _unicode_decode(arg_bytes, encoding=_encodings['content'])
+                       dependency_arg = DependencyArg(arg=arg_unicode)
+
+                       # Force unicode format string so that __unicode__() is
+                       # called in python2.
+                       formatted_str = _unicode_decode("%s") % (dependency_arg,)
+                       self.assertEqual(formatted_str, arg_unicode)
+
+                       if STR_IS_UNICODE:
+
+                               # Test the __str__ method which returns unicode in python3
+                               formatted_str = "%s" % (dependency_arg,)
+                               self.assertEqual(formatted_str, arg_unicode)
+
+                       else:
+
+                               # Test the __str__ method which returns encoded bytes in python2
+                               formatted_bytes = "%s" % (dependency_arg,)
+                               self.assertEqual(formatted_bytes, arg_bytes)