test_string_format: use unicode_literals
authorZac Medico <zmedico@gentoo.org>
Fri, 18 Jan 2013 20:46:51 +0000 (12:46 -0800)
committerZac Medico <zmedico@gentoo.org>
Fri, 18 Jan 2013 20:46:51 +0000 (12:46 -0800)
pym/portage/tests/unicode/test_string_format.py

index fb6e8e02ed6622997820c29d320b5047aae762b8..6723883e59f22d4a910a9bdcefd1d2842bf03d15 100644 (file)
@@ -1,9 +1,11 @@
-# Copyright 2010 Gentoo Foundation
+# Copyright 2010-2013 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
+from __future__ import unicode_literals
+
 import sys
 
-from portage import _encodings, _unicode_decode
+from portage import _encodings, _unicode_encode
 from portage.exception import PortageException
 from portage.tests import TestCase
 from _emerge.DependencyArg import DependencyArg
@@ -20,27 +22,25 @@ class StringFormatTestCase(TestCase):
        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.
+       # We need unicode_literals in order to get some unicode test strings
+       # in a way that works in both python2 and python3.
 
        unicode_strings = (
-               b'\xE2\x80\x98',
-               b'\xE2\x80\x99',
+               '\u2018',
+               '\u2019',
        )
 
        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'])
+               for arg_unicode in self.unicode_strings:
+                       arg_bytes = _unicode_encode(arg_unicode, 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,)
+                       # Use unicode_literals for unicode format string so that
+                       # __unicode__() is called in Python 2.
+                       formatted_str = "%s" % (dependency_arg,)
                        self.assertEqual(formatted_str, arg_unicode)
 
                        if STR_IS_UNICODE:
@@ -52,20 +52,20 @@ class StringFormatTestCase(TestCase):
                        else:
 
                                # Test the __str__ method which returns encoded bytes in python2
-                               formatted_bytes = "%s" % (dependency_arg,)
+                               formatted_bytes = b"%s" % (dependency_arg,)
                                self.assertEqual(formatted_bytes, arg_bytes)
 
        def testPortageException(self):
 
                self.assertEqual(_encodings['content'], 'utf_8')
 
-               for arg_bytes in self.unicode_strings:
-                       arg_unicode = _unicode_decode(arg_bytes, encoding=_encodings['content'])
+               for arg_unicode in self.unicode_strings:
+                       arg_bytes = _unicode_encode(arg_unicode, encoding=_encodings['content'])
                        e = PortageException(arg_unicode)
 
-                       # Force unicode format string so that __unicode__() is
-                       # called in python2.
-                       formatted_str = _unicode_decode("%s") % (e,)
+                       # Use unicode_literals for unicode format string so that
+                       # __unicode__() is called in Python 2.
+                       formatted_str = "%s" % (e,)
                        self.assertEqual(formatted_str, arg_unicode)
 
                        if STR_IS_UNICODE:
@@ -77,7 +77,7 @@ class StringFormatTestCase(TestCase):
                        else:
 
                                # Test the __str__ method which returns encoded bytes in python2
-                               formatted_bytes = "%s" % (e,)
+                               formatted_bytes = b"%s" % (e,)
                                self.assertEqual(formatted_bytes, arg_bytes)
 
        def testUseFlagDisplay(self):
@@ -86,13 +86,12 @@ class StringFormatTestCase(TestCase):
 
                for enabled in (True, False):
                        for forced in (True, False):
-                               for arg_bytes in self.unicode_strings:
-                                       arg_unicode = _unicode_decode(arg_bytes, encoding=_encodings['content'])
+                               for arg_unicode in self.unicode_strings:
                                        e = UseFlagDisplay(arg_unicode, enabled, forced)
 
-                                       # Force unicode format string so that __unicode__() is
-                                       # called in python2.
-                                       formatted_str = _unicode_decode("%s") % (e,)
+                                       # Use unicode_literals for unicode format string so that
+                                       # __unicode__() is called in Python 2.
+                                       formatted_str = "%s" % (e,)
                                        self.assertEqual(isinstance(formatted_str, basestring), True)
 
                                        if STR_IS_UNICODE:
@@ -104,5 +103,5 @@ class StringFormatTestCase(TestCase):
                                        else:
 
                                                # Test the __str__ method which returns encoded bytes in python2
-                                               formatted_bytes = "%s" % (e,)
+                                               formatted_bytes = b"%s" % (e,)
                                                self.assertEqual(isinstance(formatted_bytes, bytes), True)