From: Sergey Matveev Date: Sat, 30 Nov 2024 18:01:35 +0000 (+0300) Subject: Split strings testing X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7881bd4b4e472a09fbd9a9507b30adf2ea9beec6fe02fcbedfd26abd5e26443d;p=keks.git Split strings testing Binary and Unicode strings are two distinct data types. However, of course, sharing big amount of code. --- diff --git a/pyac/tests/test_str.py b/pyac/tests/test_str.py index ae1d8fc..2b80855 100644 --- a/pyac/tests/test_str.py +++ b/pyac/tests/test_str.py @@ -25,16 +25,16 @@ invalid_utf8_3byte_s = integers(min_value=0, max_value=(1 << 10) - 1).map( ) -class TestString(TestCase): +class TestStr(TestCase): @given(text(max_size=60)) - def test_encode_utf8(self, test_str: str) -> None: + def test_encode(self, test_str: str) -> None: if(len(test_str.encode("utf-8")) > 60): return encoded = dumps(test_str) tag = (TagStr | TagUTF8 | len(test_str.encode("utf-8"))).to_bytes(1, "big") self.assertSequenceEqual(encoded, tag + test_str.encode("utf-8")) - def test_long_utf8(self) -> None: + def test_long(self) -> None: long_strings = ["a" * 62, "a" * 318, "a" * 65853] for long_string in long_strings: encoded = dumps(long_string) @@ -42,7 +42,35 @@ class TestString(TestCase): self.assertSequenceEqual(decoded, long_string) self.assertSequenceEqual(tail, b"") - def test_encode_non_utf8(self) -> None: + def test_throws_when_null_byte_in_utf(self) -> None: + with self.assertRaises(DecodeError) as err: + result, tail = loads(b"\xc5he\x00\x01\x02") + self.assertEqual(str(err.exception), "null byte in UTF-8") + + @given(invalid_utf8_2byte_s) + def test_throws_when_invalid_utf_2bytes(self, invalid_utf: bytes) -> None: + with self.assertRaises(DecodeError) as err: + encoded = dumps("hello")[:-1] + invalid_utf + result, tail = loads(encoded) + self.assertEqual(str(err.exception), "invalid UTF-8") + + @given(invalid_utf8_3byte_s) + def test_throws_when_invalid_utf_3bytes(self, invalid_utf: bytes) -> None: + with self.assertRaises(DecodeError) as err: + encoded = dumps("hello")[:-2] + invalid_utf + result, tail = loads(encoded) + self.assertEqual(str(err.exception), "invalid UTF-8") + + def test_throws_when_not_enough_data_for_length(self) -> None: + long_string = "a" * 318 + encoded = dumps(long_string)[:2] + with self.assertRaises(NotEnoughData) as err: + loads(encoded) + self.assertEqual(err.exception.n, 3) + + +class TestBin(TestCase): + def test_encode(self) -> None: bs = b"\x00\x01\x02" encoded = dumps(bs) self.assertSequenceEqual(encoded, b"\x83\x00\x01\x02") @@ -50,7 +78,7 @@ class TestString(TestCase): self.assertSequenceEqual(decoded, b"\x00\x01\x02") self.assertSequenceEqual(tail, b"") - def test_long_non_utf8(self) -> None: + def test_long(self) -> None: long_bss = [b"\x01" * 62, b"\x01" * 318, b"\x01" * 65853] for long_bs in long_bss: encoded = dumps(long_bs) @@ -65,27 +93,8 @@ class TestString(TestCase): self.assertEqual(err.exception.n, 6) def test_throws_when_not_enough_data_for_length(self) -> None: - long_string = "a" * 318 + long_string = b"a" * 318 encoded = dumps(long_string)[:2] with self.assertRaises(NotEnoughData) as err: loads(encoded) self.assertEqual(err.exception.n, 3) - - def test_throws_when_null_byte_in_utf(self) -> None: - with self.assertRaises(DecodeError) as err: - result, tail = loads(b"\xc5he\x00\x01\x02") - self.assertEqual(str(err.exception), "null byte in UTF-8") - - @given(invalid_utf8_2byte_s) - def test_throws_when_invalid_utf_2bytes(self, invalid_utf: bytes) -> None: - with self.assertRaises(DecodeError) as err: - encoded = dumps("hello")[:-1] + invalid_utf - result, tail = loads(encoded) - self.assertEqual(str(err.exception), "invalid UTF-8") - - @given(invalid_utf8_3byte_s) - def test_throws_when_invalid_utf_3bytes(self, invalid_utf: bytes) -> None: - with self.assertRaises(DecodeError) as err: - encoded = dumps("hello")[:-2] + invalid_utf - result, tail = loads(encoded) - self.assertEqual(str(err.exception), "invalid UTF-8")