* pyupgrade
* various new codemods
* manual removal of Py2/3 compat imports
* manual adjustment of docs, examples, tests
* to_str is now the new to_unicode, deprecated
redirection to to_bytes has been removed
* to_native_str has been redirected to point to
to_str
* to_unicode has been redirected to point to
to_str
* atomic_write refactored to use
NamedTemporaryDirectory
* various other fixes/refactorings all over the
place
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
|
|
__copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
|
|
|
import unittest
|
|
|
|
import octoprint.util
|
|
|
|
|
|
class StringHelperTest(unittest.TestCase):
|
|
def test_to_str_unicode(self):
|
|
result = octoprint.util.to_str("test")
|
|
self.assertEqual(result, "test")
|
|
self.assertIsInstance(result, str)
|
|
|
|
def test_to_str_bytes(self):
|
|
result = octoprint.util.to_str(b"test")
|
|
self.assertEqual(result, "test")
|
|
self.assertIsInstance(result, str)
|
|
|
|
def test_to_str_bytes_utf8(self):
|
|
data = "äöüß"
|
|
result = octoprint.util.to_str(data.encode("utf-8"), encoding="utf-8")
|
|
self.assertEqual(result, data)
|
|
self.assertIsInstance(result, str)
|
|
|
|
def test_to_str_bytes_utf8_vs_ascii(self):
|
|
self.assertRaises(
|
|
UnicodeDecodeError,
|
|
octoprint.util.to_str,
|
|
"äöüß".encode(),
|
|
encoding="ascii",
|
|
)
|
|
|
|
def test_to_str_bytes_utf8_vs_ascii_replace(self):
|
|
data = "äöüß"
|
|
result = octoprint.util.to_str(
|
|
data.encode("utf-8"), encoding="ascii", errors="replace"
|
|
)
|
|
self.assertEqual(result, data.encode("utf-8").decode("ascii", errors="replace"))
|
|
self.assertIsInstance(result, str)
|
|
|
|
def test_to_bytes_bytes(self):
|
|
result = octoprint.util.to_bytes(b"test")
|
|
self.assertEqual(result, b"test")
|
|
self.assertIsInstance(result, bytes)
|
|
|
|
def test_to_bytes_str(self):
|
|
result = octoprint.util.to_bytes("test")
|
|
self.assertEqual(result, b"test")
|
|
self.assertIsInstance(result, bytes)
|
|
|
|
def test_to_bytes_str_utf8(self):
|
|
data = "äöüß"
|
|
result = octoprint.util.to_bytes(data, encoding="utf-8")
|
|
self.assertEqual(result, data.encode("utf-8"))
|
|
self.assertIsInstance(result, bytes)
|
|
|
|
def test_to_bytes_str_utf8_vs_ascii(self):
|
|
self.assertRaises(
|
|
UnicodeEncodeError, octoprint.util.to_bytes, "äöüß", encoding="ascii"
|
|
)
|
|
|
|
def test_to_bytes_str_utf8_vs_ascii_replace(self):
|
|
data = "äöüß"
|
|
result = octoprint.util.to_bytes(data, encoding="ascii", errors="replace")
|
|
self.assertEqual(result, data.encode("ascii", errors="replace"))
|
|
self.assertIsInstance(result, bytes)
|
|
|
|
def test_to_unicode(self):
|
|
result = octoprint.util.to_unicode(b"test")
|
|
self.assertEqual(result, "test")
|
|
self.assertIsInstance(result, str)
|
|
|
|
def test_to_native_str(self):
|
|
result = octoprint.util.to_native_str(b"test")
|
|
self.assertEqual(result, "test")
|
|
self.assertIsInstance(result, str)
|