def test_to_bytes(self):
"""test to_bytes()"""
from passlib.utils import to_bytes
# check unicode inputs
self.assertEqual(to_bytes(u('abc')), b'abc')
self.assertEqual(to_bytes(u('\x00\xff')), b'\x00\xc3\xbf')
# check unicode w/ encodings
self.assertEqual(to_bytes(u('\x00\xff'), 'latin-1'), b'\x00\xff')
self.assertRaises(ValueError, to_bytes, u('\x00\xff'), 'ascii')
# check bytes inputs
self.assertEqual(to_bytes(b'abc'), b'abc')
self.assertEqual(to_bytes(b'\x00\xff'), b'\x00\xff')
self.assertEqual(to_bytes(b'\x00\xc3\xbf'), b'\x00\xc3\xbf')
# check byte inputs ignores enocding
self.assertEqual(to_bytes(b'\x00\xc3\xbf', "latin-1"),
b'\x00\xc3\xbf')
# check bytes transcoding
self.assertEqual(to_bytes(b'\x00\xc3\xbf', "latin-1", "", "utf-8"),
b'\x00\xff')
# check other
self.assertRaises(AssertionError, to_bytes, 'abc', None)
self.assertRaises(TypeError, to_bytes, None)
评论列表
文章目录