summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Droettboom <mdboom@gmail.com>2022-09-07 11:51:50 (GMT)
committerGitHub <noreply@github.com>2022-09-07 11:51:50 (GMT)
commitdde15f5879c3576db42ee4366fb684747c31459f (patch)
tree8e359135935205addfd4dc7c09c7d6c661582215
parent2fd7246e97c8cc09b4e3f22933693f9d68f08163 (diff)
downloadcpython-dde15f5879c3576db42ee4366fb684747c31459f.zip
cpython-dde15f5879c3576db42ee4366fb684747c31459f.tar.gz
cpython-dde15f5879c3576db42ee4366fb684747c31459f.tar.bz2
gh-94808: Improve coverage of _PyBytes_FormatEx (GH-95895)
There were two specific areas not covered: - %(name) syntax - %*s syntax Automerge-Triggered-By: GH:iritkatriel
-rw-r--r--Lib/test/test_bytes.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 521e391..53ba1ad 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -715,6 +715,24 @@ class BaseBytesTest:
self.assertEqual(b, b'hello,\x00world!')
self.assertIs(type(b), self.type2test)
+ def check(fmt, vals, result):
+ b = self.type2test(fmt)
+ b = b % vals
+ self.assertEqual(b, result)
+ self.assertIs(type(b), self.type2test)
+
+ # A set of tests adapted from test_unicode:UnicodeTest.test_formatting
+ check(b'...%(foo)b...', {b'foo':b"abc"}, b'...abc...')
+ check(b'...%(f(o)o)b...', {b'f(o)o':b"abc", b'foo':b'bar'}, b'...abc...')
+ check(b'...%(foo)b...', {b'foo':b"abc",b'def':123}, b'...abc...')
+ check(b'%*b', (5, b'abc',), b' abc')
+ check(b'%*b', (-5, b'abc',), b'abc ')
+ check(b'%*.*b', (5, 2, b'abc',), b' ab')
+ check(b'%*.*b', (5, 3, b'abc',), b' abc')
+ check(b'%i %*.*b', (10, 5, 3, b'abc',), b'10 abc')
+ check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc')
+ check(b'%c', b'a', b'a')
+
def test_imod(self):
b = self.type2test(b'hello, %b!')
orig = b