Source

pygame / test / rwobject_test.py

Diff from to

test/rwobject_test.py

 else:
     from test.test_utils import test_not_implemented, unittest
 import pygame
-rwobject = sys.modules['pygame.rwobject']
-encode_file_path = rwobject.encode_file_path
+from pygame import encode_string, encode_file_path
 from pygame.compat import bytes_, as_bytes, as_unicode
 
 
-class RWopsEncodeFilePathTest(unittest.TestCase):
+class RWopsEncodeStringTest(unittest.TestCase):
+    global getrefcount
+
     def test_obj_None(self):
-        self.assert_(encode_file_path(None) is None)
+        self.assert_(encode_string(None) is None)
     
     def test_returns_bytes(self):
-        path = as_unicode("Hello")
-        self.assert_(isinstance(encode_file_path(path), bytes_))
+        u = as_unicode(r"Hello")
+        self.assert_(isinstance(encode_string(u), bytes_))
     
     def test_obj_bytes(self):
-        path = as_bytes("encyclop\xE6dia")
-        result = encode_file_path(path, encoding='ascii', errors='strict')
-        self.assert_(result == path)
+        b = as_bytes("encyclop\xE6dia")
+        self.assert_(encode_string(b, 'ascii', 'strict') is b)
         
     def test_encode_unicode(self):
-        path = as_unicode(r"\u00DEe Olde Komputer Shoppe")
-        epath = path.encode('utf-8')
-        self.assert_(encode_file_path(path, encoding='utf-8') == epath)
+        u = as_unicode(r"\u00DEe Olde Komp\u00FCter Shoppe")
+        b = u.encode('utf-8')
+        self.assertEqual(encode_string(u, 'utf-8'), b)
         
     def test_error_fowarding(self):
-        self.assertRaises(SyntaxError, encode_file_path)
+        self.assertRaises(SyntaxError, encode_string)
         
-    def test_etype(self):
-        self.assertRaises(OverflowError, encode_file_path,
-                          as_unicode(r"\u00DE"), OverflowError,
-                          'ascii', 'strict')
-
     def test_errors(self):
         s = r"abc\u0109defg\u011Dh\u0125ij\u0135klmnoprs\u015Dtu\u016Dvz"
-        path = as_unicode(s)
-        epath = path.encode('ascii', 'ignore')
-        result = encode_file_path(path, encoding='ascii', errors='ignore')
-        self.assert_(result == epath)
+        u = as_unicode(s)
+        b = u.encode('ascii', 'ignore')
+        self.assertEqual(encode_string(u, 'ascii', 'ignore'), b)
 
-    def test_default_etype(self):
-        self.assertRaises(UnicodeError, encode_file_path,
-                          as_unicode(r"\u00DE"),
-                          encoding='ascii', errors='strict')
+    def test_encoding_error(self):
+        u = as_unicode(r"a\x80b")
+        self.assert_(encode_string(u, 'ascii', 'strict') is None)
+
+    def test_check_defaults(self):
+        u = as_unicode(r"a\u01F7b")
+        b = u.encode("unicode_escape", "backslashreplace") 
+        self.assert_(encode_string(u) == b)
+
+    def test_etype(self):
+        u = as_unicode(r"a\x80b")
+        self.assertRaises(SyntaxError, encode_string,
+                          u, 'ascii', 'strict', SyntaxError)
+
+    def test_string_with_null_bytes(self):
+        b = as_bytes("a\x00b\x00c")
+        self.assert_(encode_string(b, etype=SyntaxError) is b)
+        u = b.decode()
+        self.assert_(encode_string(u, 'ascii', 'strict') == b)
+
+    try:
+        from sys import getrefcount as _g
+        getrefcount = _g                   # This nonsense is for Python 3.x
+    except ImportError:
+        pass
+    else:
+        def test_refcount(self):
+            bpath = as_bytes(" This is a string that is not cached.")[1:]
+            upath = bpath.decode('ascii')
+            before = getrefcount(bpath)
+            bpath = encode_string(bpath)
+            self.assertEqual(getrefcount(bpath), before)
+            bpath = encode_string(upath)
+            self.assertEqual(getrefcount(bpath), before)
+            
+    def test_smp(self):
+        utf_8 = as_bytes("a\xF0\x93\x82\xA7b")
+        u = as_unicode(r"a\U000130A7b")
+        b = encode_string(u, 'utf-8', 'strict', AssertionError)
+        self.assertEqual(b, utf_8)
+        #  For Python 3.1, surrogate pair handling depends on whether the
+        #  interpreter was built with UCS-2 or USC-4 unicode strings.
+        ##u = as_unicode(r"a\uD80C\uDCA7b")
+        ##b = encode_string(u, 'utf-8', 'strict', AssertionError)
+        ##self.assertEqual(b, utf_8)
+
+class RWopsEncodeFilePathTest(unittest.TestCase):
+    # Most tests can be skipped since RWopsEncodeFilePath wraps
+    # RWopsEncodeString
+    def test_encoding(self):
+        u = as_unicode(r"Hello")
+        self.assert_(isinstance(encode_file_path(u), bytes_))
+    
+    def test_error_fowarding(self):
+        self.assertRaises(SyntaxError, encode_file_path)
 
     def test_path_with_null_bytes(self):
-        path = as_bytes("a\x00b\x00c")
-        self.assertRaises(TypeError, encode_file_path, path)
+        b = as_bytes("a\x00b\x00c")
+        self.assert_(encode_file_path(b) is None)
 
+    def test_etype(self):
+        b = as_bytes("a\x00b\x00c")
+        self.assertRaises(TypeError, encode_file_path, b, TypeError)
+                                   
 if __name__ == '__main__':
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.