"cm" just tells that it is "context manager". "class Class" is similar.
"err" can be more appropriate name, telling that it holds something
related to the error.
def test_throws_when_not_enough_data(self) -> None:
encoded = b"\x0B\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01da"
- with self.assertRaises(NotEnoughData) as cm:
+ with self.assertRaises(NotEnoughData) as err:
loads(encoded)
- self.assertEqual(cm.exception.n, 4)
+ self.assertEqual(err.exception.n, 4)
def test_throws_when_not_enough_data_for_length(self) -> None:
encoded = b"\x0B\x00\x00\x00\x00"
- with self.assertRaises(NotEnoughData) as cm:
+ with self.assertRaises(NotEnoughData) as err:
loads(encoded)
- self.assertEqual(cm.exception.n, 9)
+ self.assertEqual(err.exception.n, 9)
def test_throws_when_wrong_terminator_length(self) -> None:
encoded = b"\x0B\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01data\x8Aterminator"
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
loads(encoded)
- self.assertEqual(str(cm.exception), "wrong terminator len")
+ self.assertEqual(str(err.exception), "wrong terminator len")
def test_throws_when_wrong_terminator_tag(self) -> None:
encoded = b"\x0B\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01data\x04that was a wrong tag"
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
loads(encoded)
- self.assertEqual(str(cm.exception), "unexpected tag")
+ self.assertEqual(str(err.exception), "unexpected tag")
class TestFloat(TestCase):
def test_throws_when_dumps_float(self) -> None:
- with self.assertRaises(NotImplementedError) as cm:
+ with self.assertRaises(NotImplementedError) as err:
dumps(1.5)
- self.assertEqual(str(cm.exception), "no FLOAT* support")
+ self.assertEqual(str(err.exception), "no FLOAT* support")
def test_float_loads(self) -> None:
floats: List[bytes] = [
class TestUnknownType(TestCase):
def runTest(self) -> None:
- with self.assertRaises(NotImplementedError) as cm:
+ with self.assertRaises(NotImplementedError) as err:
class A:
pass
dumps(A())
- self.assertEqual(str(cm.exception), "unsupported type")
+ self.assertEqual(str(err.exception), "unsupported type")
class TestUnknownTag(TestCase):
def runTest(self) -> None:
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
loads(b"\x05")
- self.assertEqual(str(cm.exception), "unknown tag")
+ self.assertEqual(str(err.exception), "unknown tag")
class TestEmptyData(TestCase):
def test_int_decode_not_enough_data(self) -> None:
encoded: bytes = b"\x0C\x81"
- with self.assertRaises(NotEnoughData) as cm:
+ with self.assertRaises(NotEnoughData) as err:
loads(encoded)
- self.assertEqual(cm.exception.n, 2)
+ self.assertEqual(err.exception.n, 2)
def test_throws_when_unminimal_int(self) -> None:
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
encoded: bytes = b"\x0C\x81\x00\x7B"
loads(encoded)
- self.assertEqual(str(cm.exception), "non-minimal encoding")
+ self.assertEqual(str(err.exception), "non-minimal encoding")
def test_throws_when_non_bin_in_int(self) -> None:
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
encoded: bytes = b"\x0C\x01\x7B"
loads(encoded)
- self.assertEqual(str(cm.exception), "non-BIN in INT")
+ self.assertEqual(str(err.exception), "non-BIN in INT")
@given(lists(any_st, max_size=4))
def test_no_eoc(self, test_list: List) -> None:
encoded = dumps(test_list)[:-1]
- with self.assertRaises(NotEnoughData) as cm:
+ with self.assertRaises(NotEnoughData) as err:
loads(encoded)
- self.assertEqual(cm.exception.n, 1)
+ self.assertEqual(err.exception.n, 1)
def test_map_throws_when_decoding_unsorted_keys(self):
encoded = b"\x09\xc4key2\x0c\x81\x01\xc4key1\xc6value1\x00"
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
loads(encoded)
- self.assertEqual(str(cm.exception), "unsorted keys")
+ self.assertEqual(str(err.exception), "unsorted keys")
def test_map_throws_when_encoding_non_string_key(self):
- with self.assertRaises(ValueError) as cm:
+ with self.assertRaises(ValueError) as err:
dumps({1: "a"})
- self.assertEqual(str(cm.exception), "map keys can contain only strings")
+ self.assertEqual(str(err.exception), "map keys can contain only strings")
def test_map_throws_when_decoding_non_string_key(self):
encoded = b"\x09\x0c\x80\xc6value2\x00"
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
loads(encoded)
- self.assertEqual(str(cm.exception), "non-string key")
+ self.assertEqual(str(err.exception), "non-string key")
def test_map_throws_when_unexpected_eoc(self):
encoded = b"\x09\xc4key1\x00\x00"
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
decoded, tail = loads(encoded)
- self.assertEqual(str(cm.exception), "unexpected EOC")
+ self.assertEqual(str(err.exception), "unexpected EOC")
def test_map_throws_when_encoding_empty_str_as_key(self):
- with self.assertRaises(ValueError) as cm:
+ with self.assertRaises(ValueError) as err:
dumps({"": "a"})
- self.assertEqual(str(cm.exception), "map keys can not be empty")
+ self.assertEqual(str(err.exception), "map keys can not be empty")
def test_map_throws_when_decoding_empty_str_as_key(self):
encoded = b"\x09\xc0\x0c\x81\x01\xc4key1\xc6value1\x00"
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
decoded, tail = loads(encoded)
- self.assertEqual(str(cm.exception), "empty key")
+ self.assertEqual(str(err.exception), "empty key")
def test_throws_when_not_enough_data(self) -> None:
encoded = b"\x85he"
- with self.assertRaises(NotEnoughData) as cm:
+ with self.assertRaises(NotEnoughData) as err:
loads(encoded)
- self.assertEqual(cm.exception.n, 6)
+ self.assertEqual(err.exception.n, 6)
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 cm:
+ with self.assertRaises(NotEnoughData) as err:
loads(encoded)
- self.assertEqual(cm.exception.n, 3)
+ self.assertEqual(err.exception.n, 3)
def test_throws_when_null_byte_in_utf(self) -> None:
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
result, tail = loads(b"\xc5he\x00\x01\x02")
- self.assertEqual(str(cm.exception), "null byte in UTF-8")
+ 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 cm:
+ with self.assertRaises(DecodeError) as err:
encoded = dumps("hello")[:-1] + invalid_utf
result, tail = loads(encoded)
- self.assertEqual(str(cm.exception), "invalid UTF-8")
+ 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 cm:
+ with self.assertRaises(DecodeError) as err:
encoded = dumps("hello")[:-2] + invalid_utf
result, tail = loads(encoded)
- self.assertEqual(str(cm.exception), "invalid UTF-8")
+ self.assertEqual(str(err.exception), "invalid UTF-8")
self.assertSequenceEqual(tail, b"")
def test_throws_when_first_bit_is_in_use(self) -> None:
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
loads(b"\x18\x80\x00\x00\x00\x65\x19\x5f\x65")
- self.assertEqual(str(cm.exception), "reserved TAI64 value is in use")
+ self.assertEqual(str(err.exception), "reserved TAI64 value is in use")
def test_throws_when_too_many_nanosecs(self) -> None:
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
loads(b"\x19\x40\x00\x00\x00\x65\x19\x5f\x65" + (999999999 + 1).to_bytes(4, "big"))
- self.assertEqual(str(cm.exception), "too many nanoseconds")
+ self.assertEqual(str(err.exception), "too many nanoseconds")
def test_throws_when_too_many_attosecs(self) -> None:
- with self.assertRaises(DecodeError) as cm:
+ with self.assertRaises(DecodeError) as err:
loads(
b"\x1A\x40\x00\x00\x00\x65\x19\x5f\x65\x07\x5b\xca\x00\xA7\x5b\xca\x00"
)
- self.assertEqual(str(cm.exception), "too many attoseconds")
+ self.assertEqual(str(err.exception), "too many attoseconds")
def test_leapseconds_match_DJB(self) -> None:
"""Check that our pyac.Leapsecs is equally calculated to DJB's