diff options
author | Bastian Venthur <venthur@debian.org> | 2021-09-29 20:38:21 +0200 |
---|---|---|
committer | Bastian Venthur <venthur@debian.org> | 2021-09-29 20:38:21 +0200 |
commit | 280a3b8737038f45d4242e26896036525dc192ba (patch) | |
tree | 2f47e0470615acfbf88ae8132cb26dccdc23f18d | |
parent | 63e1e42c4b6978530042637824a40887d9e377e3 (diff) | |
download | feedgenerator-280a3b8737038f45d4242e26896036525dc192ba.tar.gz |
modernize test_stringio.py
-rw-r--r-- | tests_feedgenerator/test_stringio.py | 105 |
1 files changed, 51 insertions, 54 deletions
diff --git a/tests_feedgenerator/test_stringio.py b/tests_feedgenerator/test_stringio.py index 3791035..89e784f 100644 --- a/tests_feedgenerator/test_stringio.py +++ b/tests_feedgenerator/test_stringio.py @@ -1,5 +1,3 @@ -import unittest - from io import StringIO ENCODING = 'utf-8' @@ -9,56 +7,55 @@ S0_BYTES = 'fe fi foe fam'.encode(ENCODING) #print("###", StringIO, "###") -class TestStringIO(unittest.TestCase): - - def test_001_text(self): - # If we throw unicode into the StringIO buffer, we'll - # get unicode out of it. - self.assertEqual(type(S0), str) - buf = StringIO() - print(S0, file=buf, end="") - s1 = buf.getvalue() - self.assertEqual(type(S0), type(s1)) - self.assertEqual(S0, s1) - self.assertEqual(type(s1), str) - - def test_002_bytes(self): - buf = StringIO() - print(S0_BYTES, file=buf, end="") - s1 = buf.getvalue() - - # In Python 3 StringIO *ALWAYS* returns str (=text=unicode) ! - # Even if we originally write bytes into the buffer, the value - # we get out of it has type str! - # Input is bytes - self.assertEqual(type(S0_BYTES), bytes) - # Output is NOT bytes... - self.assertNotEqual(type(S0_BYTES), type(s1)) - self.assertNotEqual(type(s1), bytes) - # ...but str! - self.assertEqual(type(s1), str) - # So the contents are not equal! - self.assertNotEqual(S0_BYTES, s1) - # StringIO coerced bytes into str: - # b'xyz' ---> "b'xyz'" - self.assertEqual(str(S0_BYTES), s1) - # See, the type info is literally present in the output str! - self.assertEqual("b'" + str(S0_BYTES, encoding=ENCODING) + "'", s1) - # Coercion is NOT decoding! - self.assertNotEqual(S0_BYTES.decode(ENCODING), s1) - self.assertNotEqual(str(S0_BYTES, encoding=ENCODING), s1) - # These are the same - self.assertEqual(S0_BYTES.decode(ENCODING), - str(S0_BYTES, encoding=ENCODING)) - # Additional note: - # If we do not specify an encoding when we create a StringIO - # buffer, Python 3 automatically uses the locale's preferred - # encoding: locale.getpreferredencoding() - # Cf. http://docs.python.org/release/3.0.1/library/io.html#io.TextIOWrapper - # In my case this is the same encoding as the encoding of this source file, - # namely UTF-8. If on your system both encodings are different, you may - # encounter other results than the above. - # - # In Python 3.2 the signature of StringIO() has changed. It is no more - # possible to specify an encoding here. +def test_001_text(): + # If we throw unicode into the StringIO buffer, we'll + # get unicode out of it. + assert isinstance(S0, str) + buf = StringIO() + print(S0, file=buf, end="") + s1 = buf.getvalue() + assert isinstance(S0, type(s1)) + assert S0 == s1 + assert isinstance(s1, str) + + +def test_002_bytes(): + buf = StringIO() + print(S0_BYTES, file=buf, end="") + s1 = buf.getvalue() + + # In Python 3 StringIO *ALWAYS* returns str (=text=unicode) ! + # Even if we originally write bytes into the buffer, the value + # we get out of it has type str! + + # Input is bytes + assert isinstance(S0_BYTES, bytes) + # Output is NOT bytes... + assert not isinstance(S0_BYTES, type(s1)) + assert not isinstance(s1, bytes) + # ...but str! + assert isinstance(s1, str) + # So the contents are not equal! + assert S0_BYTES != s1 + # StringIO coerced bytes into str: + # b'xyz' ---> "b'xyz'" + assert str(S0_BYTES) == s1 + # See, the type info is literally present in the output str! + assert "b'" + str(S0_BYTES, encoding=ENCODING) + "'" == s1 + # Coercion is NOT decoding! + assert S0_BYTES.decode(ENCODING) != s1 + assert str(S0_BYTES, encoding=ENCODING) != s1 + # These are the same + assert S0_BYTES.decode(ENCODING) == str(S0_BYTES, encoding=ENCODING) + # Additional note: + # If we do not specify an encoding when we create a StringIO + # buffer, Python 3 automatically uses the locale's preferred + # encoding: locale.getpreferredencoding() + # Cf. http://docs.python.org/release/3.0.1/library/io.html#io.TextIOWrapper + # In my case this is the same encoding as the encoding of this source file, + # namely UTF-8. If on your system both encodings are different, you may + # encounter other results than the above. + # + # In Python 3.2 the signature of StringIO() has changed. It is no more + # possible to specify an encoding here. |