)
-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)
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")
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)
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")