def test_optional_struct_creation(self) -> None: with self.assertRaises(TypeError): # pyre-ignore[19]: for test easy(1, [1, 1], "test", Integers(tiny=1)) easy(val=1, an_int=Integers(small=500)) with self.assertRaises(TypeError): # pyre-ignore[6]: for test easy(name=b"binary") # Only Required Fields don't accept None easy(val=5, an_int=None)
def test_integers_fromValue(self) -> None: tiny = 2**7 - 1 small = 2**15 - 1 medium = 2**31 - 1 large = 2**63 - 1 union = Integers.fromValue(tiny) self.assertEqual(union.type, Integers.Type.tiny) union = Integers.fromValue(small) self.assertEqual(union.type, Integers.Type.small) union = Integers.fromValue(medium) self.assertEqual(union.type, Integers.Type.medium) union = Integers.fromValue(large) self.assertEqual(union.type, Integers.Type.large)
def test_union_usage(self) -> None: value = hash("i64") x = Integers(large=value) self.assertIsInstance(x, Union) self.assertEqual(x.type, x.get_type()) self.assertEqual(x.type, Integers.Type.large) self.assertEqual(x.value, value) # Hashing Works s = {x} self.assertIn(x, s) # Repr is useful rx = repr(x) self.assertIn(str(value), rx) self.assertIn(x.type.name, rx)
def test_call_replace(self) -> None: x = easy(val=1, an_int=Integers(small=300), name="foo") y = x(name="bar") self.assertNotEqual(x.name, y.name) z = y(an_int=None, val=4) self.assertNotEqual(x.an_int, z.an_int) self.assertNotEqual(x.val, z.val) self.assertIsNone(z.an_int.value) self.assertEqual(y.val, x.val) self.assertEqual(y.an_int, x.an_int) x = easy() self.assertIsNotNone(x.val) self.assertIsNotNone(x.val_list) self.assertIsNone(x.name) self.assertIsNotNone(x.an_int)
def test_wrong_type(self) -> None: x = Integers(small=1) with self.assertRaises(AttributeError): x.large x.small
def test_multiple_values(self) -> None: with self.assertRaises(TypeError): Integers(small=1, large=2)
def test_deserialize_empty(self) -> None: x = deserialize(Integers, serialize_iobuf(Integers())) self.assertEqual(x.type, Integers.Type.EMPTY)
def test_hashability(self) -> None: hash(Integers())
def test_copy(self) -> None: x = easy(val=1, an_int=Integers(small=300), name="foo", val_list=[1, 2, 3, 4]) dif_list = copy.copy(x.val_list) self.assertEqual(x.val_list, dif_list) dif_int = copy.copy(x.an_int) self.assertEqual(x.an_int, dif_int)
def test_isset_Union(self) -> None: i = Integers(large=2) with self.assertRaises(TypeError): # pyre-ignore[6]: for test isset(i)["large"]
def test_init_with_invalid_field(self) -> None: with self.assertRaises(TypeError): # pyre-ignore[28]: for test easy(val=1, an_int=Integers(small=300), name="foo", val_lists=[1, 2, 3, 4])