class TestBool(TestCase):
- def test_bool_encode_true(self):
+ def test_encode_true(self):
encoded = dumps(True)
self.assertEqual(encoded, b"\x03")
- def test_bool_encode_false(self):
+ def test_encode_false(self):
encoded = dumps(False)
self.assertEqual(encoded, b"\x02")
@given(junk_st)
- def test_bool_decode_true(self, junk):
+ def test_decode_true(self, junk):
encoded = b"\x03" + junk
decoded, tail = loads(encoded)
self.assertIs(decoded, True)
self.assertSequenceEqual(tail, junk)
@given(junk_st)
- def test_bool_decode_false(self, junk):
+ def test_decode_false(self, junk):
encoded = b"\x02" + junk
decoded, tail = loads(encoded)
self.assertIs(decoded, False)
class TestInt(TestCase):
- def test_int_positive(self) -> None:
+ def test_positive(self) -> None:
ints: list[int] = [1, 123, 1 << 64, 1 << 130]
expected: list[bytes] = [
b"\x0C\x81\x01",
self.assertEqual(decoded, integer)
self.assertSequenceEqual(tail, b"")
- def test_int_negative(self) -> None:
+ def test_negative(self) -> None:
ints: list[int] = [-1, -123, -(1 << 64), -(1 << 130)]
expected: list[bytes] = [
b"\x0D\x80",
self.assertEqual(decoded, integer)
self.assertSequenceEqual(tail, b"")
- def test_int_zero(self) -> None:
+ def test_zero(self) -> None:
encoded: bytes = dumps(0)
self.assertSequenceEqual(encoded, b"\x0C\x80")
decoded, tail = loads(encoded)
self.assertEqual(decoded, 0)
self.assertSequenceEqual(tail, b"")
- def test_int_decode_not_enough_data(self) -> None:
+ def test_decode_not_enough_data(self) -> None:
encoded: bytes = b"\x0C\x81"
with self.assertRaises(NotEnoughData) as err:
loads(encoded)
class TestList(TestCase):
- def test_list_encode_empty(self) -> None:
+ def test_encode_empty(self) -> None:
encoded = dumps([])
self.assertSequenceEqual(encoded, b"\x08\x00")
@given(lists(any_st, max_size=4))
- def test_list_encode_non_empty(self, test_list: List) -> None:
+ def test_encode_non_empty(self, test_list: List) -> None:
encoded = dumps(test_list)
self.assertSequenceEqual(
encoded, b"\x08" + b"".join(dumps(i) for i in test_list) + b"\x00"
)
- def test_list_decode_empty(self) -> None:
+ def test_decode_empty(self) -> None:
encoded = b"\x08\x00"
decoded, tail = loads(encoded)
self.assertEqual(decoded, [])
self.assertSequenceEqual(tail, b"")
@given(lists(any_st, max_size=4), junk_st)
- def test_list_decode_non_empty(self, test_list: List, junk: bytes) -> None:
+ def test_decode_non_empty(self, test_list: List, junk: bytes) -> None:
encoded = b"\x08" + b"".join(dumps(i) for i in test_list) + b"\x00" + junk
decoded, tail = loads(encoded)
self.assertEqual(decoded, test_list)
class TestMap(TestCase):
@given(dictionaries(keys=mapkey_st, values=any_st, max_size=4))
- def test_map_encode(self, test_map):
+ def test_encode(self, test_map):
encoded = dumps(test_map)
expected = (
b"\x09" +
self.assertSequenceEqual(encoded, expected)
@given(dictionaries(keys=mapkey_st, values=any_st, max_size=4), junk_st)
- def test_map_decode(self, test_map, junk):
+ def test_decode(self, test_map, junk):
encoded = (
b"\x09" +
b"".join(
self.assertSequenceEqual(tail, junk)
@given(junk_st)
- def test_map_empty(self, junk):
+ def test_empty(self, junk):
test_map = {}
encoded = dumps(test_map) + junk
expected = b"\x09\x00" + junk
self.assertSetEqual(decoded, keys)
self.assertSequenceEqual(tail, junk)
- def test_map_throws_when_decoding_unsorted_keys(self):
+ def test_throws_when_decoding_unsorted_keys(self):
encoded = b"\x09\xc4key2\x0c\x81\x01\xc4key1\xc6value1\x00"
with self.assertRaises(DecodeError) as err:
loads(encoded)
self.assertEqual(str(err.exception), "unsorted keys")
- def test_map_throws_when_encoding_non_string_key(self):
+ def test_throws_when_encoding_non_string_key(self):
with self.assertRaises(ValueError) as err:
dumps({1: "a"})
self.assertEqual(str(err.exception), "map keys can contain only strings")
- def test_map_throws_when_decoding_non_string_key(self):
+ def test_throws_when_decoding_non_string_key(self):
encoded = b"\x09\x0c\x80\xc6value2\x00"
with self.assertRaises(DecodeError) as err:
loads(encoded)
self.assertEqual(str(err.exception), "non-string key")
- def test_map_throws_when_unexpected_eoc(self):
+ def test_throws_when_unexpected_eoc(self):
encoded = b"\x09\xc4key1\x00\x00"
with self.assertRaises(DecodeError) as err:
decoded, tail = loads(encoded)
self.assertEqual(str(err.exception), "unexpected EOC")
- def test_map_throws_when_encoding_empty_str_as_key(self):
+ def test_throws_when_encoding_empty_str_as_key(self):
with self.assertRaises(ValueError) as err:
dumps({"": "a"})
self.assertEqual(str(err.exception), "map keys can not be empty")
- def test_map_throws_when_decoding_empty_str_as_key(self):
+ def test_throws_when_decoding_empty_str_as_key(self):
encoded = b"\x09\xc0\x0c\x81\x01\xc4key1\xc6value1\x00"
with self.assertRaises(DecodeError) as err:
decoded, tail = loads(encoded)
class TestUUID(TestCase):
- def test_uuid_encode(self) -> None:
+ def test_encode(self) -> None:
uuid_str: str = "12345678-1234-5678-1234-567812345678"
uuid_obj: UUID = UUID(uuid_str)
encoded: bytes = dumps(uuid_obj)
self.assertSequenceEqual(encoded, b"\x04\x124Vx\x124Vx\x124Vx\x124Vx")
@given(junk_st)
- def test_uuid_decode(self, junk: bytes) -> None:
+ def test_decode(self, junk: bytes) -> None:
uuid_str: str = "12345678-1234-5678-1234-567812345678"
encoded: bytes = b"\x04\x124Vx\x124Vx\x124Vx\x124Vx"
decoded: UUID
self.assertEqual(decoded, UUID(uuid_str))
self.assertSequenceEqual(tail, junk)
- def test_uuid_not_enough_data(self) -> None:
+ def test_not_enough_data(self) -> None:
encoded: bytes = b"\x04\x124Vx\x124Vx\x124Vx\x124V"
with self.assertRaises(NotEnoughData):
loads(encoded)