mirror of
https://github.com/django/django.git
synced 2025-07-19 03:05:32 +00:00
Refs #27753 -- Favored force/smart_str() over force/smart_text().
This commit is contained in:
parent
24b82cd201
commit
3bb6a4390c
21 changed files with 84 additions and 101 deletions
|
@ -6,7 +6,7 @@ from urllib.parse import quote_plus
|
|||
from django.test import SimpleTestCase
|
||||
from django.utils.encoding import (
|
||||
DjangoUnicodeDecodeError, escape_uri_path, filepath_to_uri, force_bytes,
|
||||
force_text, get_system_encoding, iri_to_uri, smart_bytes, smart_text,
|
||||
force_str, get_system_encoding, iri_to_uri, smart_bytes, smart_str,
|
||||
uri_to_iri,
|
||||
)
|
||||
from django.utils.functional import SimpleLazyObject
|
||||
|
@ -14,7 +14,7 @@ from django.utils.translation import gettext_lazy
|
|||
|
||||
|
||||
class TestEncodingUtils(SimpleTestCase):
|
||||
def test_force_text_exception(self):
|
||||
def test_force_str_exception(self):
|
||||
"""
|
||||
Broken __str__ actually raises an error.
|
||||
"""
|
||||
|
@ -24,19 +24,19 @@ class TestEncodingUtils(SimpleTestCase):
|
|||
|
||||
# str(s) raises a TypeError if the result is not a text type.
|
||||
with self.assertRaises(TypeError):
|
||||
force_text(MyString())
|
||||
force_str(MyString())
|
||||
|
||||
def test_force_text_lazy(self):
|
||||
def test_force_str_lazy(self):
|
||||
s = SimpleLazyObject(lambda: 'x')
|
||||
self.assertIs(type(force_text(s)), str)
|
||||
self.assertIs(type(force_str(s)), str)
|
||||
|
||||
def test_force_text_DjangoUnicodeDecodeError(self):
|
||||
def test_force_str_DjangoUnicodeDecodeError(self):
|
||||
msg = (
|
||||
"'utf-8' codec can't decode byte 0xff in position 0: invalid "
|
||||
"start byte. You passed in b'\\xff' (<class 'bytes'>)"
|
||||
)
|
||||
with self.assertRaisesMessage(DjangoUnicodeDecodeError, msg):
|
||||
force_text(b'\xff')
|
||||
force_str(b'\xff')
|
||||
|
||||
def test_force_bytes_exception(self):
|
||||
"""
|
||||
|
@ -75,16 +75,16 @@ class TestEncodingUtils(SimpleTestCase):
|
|||
self.assertEqual(smart_bytes(1), b'1')
|
||||
self.assertEqual(smart_bytes('foo'), b'foo')
|
||||
|
||||
def test_smart_text(self):
|
||||
def test_smart_str(self):
|
||||
class Test:
|
||||
def __str__(self):
|
||||
return 'ŠĐĆŽćžšđ'
|
||||
|
||||
lazy_func = gettext_lazy('x')
|
||||
self.assertIs(smart_text(lazy_func), lazy_func)
|
||||
self.assertEqual(smart_text(Test()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111')
|
||||
self.assertEqual(smart_text(1), '1')
|
||||
self.assertEqual(smart_text('foo'), 'foo')
|
||||
self.assertIs(smart_str(lazy_func), lazy_func)
|
||||
self.assertEqual(smart_str(Test()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111')
|
||||
self.assertEqual(smart_str(1), '1')
|
||||
self.assertEqual(smart_str('foo'), 'foo')
|
||||
|
||||
def test_get_default_encoding(self):
|
||||
with mock.patch('locale.getdefaultlocale', side_effect=Exception):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue