Skip to content

Commit

Permalink
unify and extend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Aug 11, 2024
1 parent e2d9bf7 commit 6456846
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,18 +1747,6 @@ def test_constructor_keyword_args(self):
self.assertEqual(str(b'foo', errors='strict'), 'foo') # not "b'foo'"
self.assertEqual(str(object=b'foo', errors='strict'), 'foo')

def test_constructor_errors(self):
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not bytes"):
str(b"x", b"ascii")
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not bytes"):
str(b"x", encoding=b"ascii")
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not bytes"):
str(b"x", "ascii", b"strict")
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not bytes"):
str(b"x", "ascii", errors=b"strict")
with self.assertRaisesRegex(TypeError, r"str expected at most 3 arguments, got 4"):
str("too", "many", "argu", "ments")

def test_constructor_defaults(self):
"""Check the constructor argument defaults."""
# The object argument defaults to '' or b''.
Expand Down Expand Up @@ -2667,19 +2655,44 @@ def test_str_invalid_call(self):
check = lambda *a, **kw: self.assertRaises(TypeError, str, *a, **kw)

# too many args
check(1, "", "", 1)
with self.assertRaisesRegex(TypeError, r"str expected at most 3 arguments, got 4"):
str("too", "many", "argu", "ments")
with self.assertRaisesRegex(TypeError, r"str expected at most 3 arguments, got 4"):
str(1, "", "", 1)

# no such kw arg
check(test=1)
with self.assertRaisesRegex(TypeError, r"str\(\) got an unexpected keyword argument 'test'"):
str(test=1)

# 'encoding' must be str
check(1, encoding=1)
check(1, 1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
str(1, 1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
str(1, encoding=1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not bytes"):
str(b"x", b"ascii")
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not bytes"):
str(b"x", encoding=b"ascii")

# 'errors' must be str
check(1, errors=1)
check(1, "", errors=1)
check(1, 1, 1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
str(1, 1, 1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not int"):
str(1, errors=1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not int"):
str(1, "", errors=1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not bytes"):
str(b"x", "ascii", b"strict")
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not bytes"):
str(b"x", "ascii", errors=b"strict")

# both positional and kwarg
with self.assertRaisesRegex(TypeError, r"argument for str\(\) given by name \('encoding'\) and position \(2\)"):
str(b"x", "utf-8", encoding="ascii")
with self.assertRaisesRegex(TypeError, r"str\(\) takes at most 3 arguments \(4 given\)"):
str(b"x", "utf-8", "ignore", encoding="ascii")
with self.assertRaisesRegex(TypeError, r"str\(\) takes at most 3 arguments \(4 given\)"):
str(b"x", "utf-8", "strict", errors="ignore")


class StringModuleTest(unittest.TestCase):
Expand Down

0 comments on commit 6456846

Please sign in to comment.