Skip to content

Commit 12935f9

Browse files
committed
test: add keyword str rejection tests for all entry points
Test that data='hello' (keyword) raises the same 'Strings must be encoded before hashing' error as positional str for one-shot functions, constructors, and update().
1 parent fba326c commit 12935f9

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

tests/test_hashlib_compat.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,36 @@ def test_str_rejected(self):
2323
for fn in (getattr(xxhash, f'{algo}_digest'),
2424
getattr(xxhash, f'{algo}_intdigest'),
2525
getattr(xxhash, f'{algo}_hexdigest')):
26+
# positional str
2627
with self.assertRaisesRegex(TypeError,
2728
'Strings must be encoded before hashing'):
2829
fn('hello')
30+
# keyword str
31+
with self.assertRaisesRegex(TypeError,
32+
'Strings must be encoded before hashing'):
33+
fn(data='hello')
2934

3035
def test_str_rejected_constructor(self):
3136
for algo in ('xxh32', 'xxh64', 'xxh3_64', 'xxh3_128'):
3237
cls = getattr(xxhash, algo)
38+
# positional str
3339
with self.assertRaisesRegex(TypeError,
3440
'Strings must be encoded before hashing'):
3541
cls('hello')
42+
# keyword str
43+
with self.assertRaisesRegex(TypeError,
44+
'Strings must be encoded before hashing'):
45+
cls(data='hello')
3646

3747
def test_str_rejected_update(self):
3848
for algo in ('xxh32', 'xxh64', 'xxh3_64', 'xxh3_128'):
3949
obj = getattr(xxhash, algo)()
4050
with self.assertRaisesRegex(TypeError,
4151
'Strings must be encoded before hashing'):
4252
obj.update('hello')
53+
# also test that bytes work after
54+
obj.update(b'hello')
55+
self.assertIsInstance(obj.intdigest(), int)
4356

4457
# ── data keyword ───────────────────────────────────────────────
4558

0 commit comments

Comments
 (0)