def test_rev_const_assigned_to_narrow_type(self): rev_const_type = CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()), ("set", "foo"): SetterMicroOpType("foo", AnyType()) }, name="test") normal_broad_type = CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()), ("set", "foo"): SetterMicroOpType("foo", StringType()) }, name="test") self.assertTrue(normal_broad_type.is_copyable_from(rev_const_type, DUMMY_REASONER))
def test_add_micro_op_object(self): class Foo(PythonObject): pass obj = Foo({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()) }, name="test"))
def test_failed_setup_broad_writing_property(self): with self.assertRaises(CompositeTypeIsInconsistent): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()), ("set", "foo"): SetterMicroOpType("foo", AnyType()) }, name="test"))
def test_rev_const_merge_types_in_list(self): rev_const_type = CompositeType({ ("get", 0): GetterMicroOpType(0, StringType()), ("set", 0): SetterMicroOpType(0, StringType()), ("get", 1): GetterMicroOpType(1, IntegerType()), ("set", 1): SetterMicroOpType(1, IntegerType()), ("get", 2): GetterMicroOpType(2, AnyType()), ("set", 2): SetterMicroOpType(2, AnyType()), ("insert-start", ): InsertStartMicroOpType(StringType(), False) }, name="test") normal_broad_type = CompositeType({ ("get", 0): GetterMicroOpType(0, StringType()), ("set", 0): SetterMicroOpType(0, StringType()), ("get", 1): GetterMicroOpType(1, IntegerType()), ("set", 1): SetterMicroOpType(1, IntegerType()), ("get", 2): GetterMicroOpType(2, AnyType()), ("set", 2): SetterMicroOpType(2, AnyType()), }, name="test") self.assertFalse(rev_const_type.is_self_consistent(DUMMY_REASONER)) rev_const_type = prepare_lhs_type(rev_const_type, None) self.assertTrue(rev_const_type.is_self_consistent(DUMMY_REASONER)) self.assertTrue(normal_broad_type.is_copyable_from(rev_const_type, DUMMY_REASONER))
def test_delete_property(self): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("delete-wildcard", ): DeletterWildcardMicroOpType(StringType(), True) }, name="test")) del obj.foo self.assertFalse(hasattr(obj, "foo"))
def test_rev_const_can_not_be_added_to_object(self): rev_const_type = CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()), ("set", "foo"): SetterMicroOpType("foo", AnyType()) }, name="test") obj = PythonObject({ "foo": "hello" }) with self.assertRaises(Exception): get_manager(obj).add_composite_type(rev_const_type)
def test_composite_object_assignment(self): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()), ("set", "foo"): SetterMicroOpType("foo", StringType()) }, name="test")) obj.foo = "what"
def test_composite_object_dereference(self): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()), ("set", "foo"): SetterMicroOpType("foo", StringType()) }, name="test")) self.assertEquals(obj.foo, "hello")
def test_const_property(self): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()) }, name="test")) self.assertEquals(obj.foo, "hello") with self.assertRaises(Exception): obj.foo = "what"
def test_composite_object_invalid_assignment(self): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()), ("set", "foo"): SetterMicroOpType("foo", StringType()) }, name="test")) with self.assertRaises(TypeError): obj.foo = 5
def test_python_like_object(self): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", AnyType()), ("set", "foo"): SetterMicroOpType("foo", AnyType()) }, name="test")) self.assertEquals(obj.foo, "hello") obj.foo = "what" self.assertEquals(obj.foo, "what")
def build_universal_type(data): micro_ops = OrderedDict({}) for micro_op in data._get("micro_ops")._to_list(): if micro_op._contains("index"): tag = (micro_op._get("type"), micro_op._get("index")) else: tag = (micro_op._get("type"), ) Factory = MICRO_OP_FACTORIES[micro_op._get("type")] micro_ops[tag] = Factory(*micro_op._get("params")._to_list()) return CompositeType(micro_ops, name="App")
def merge_composite_types(types, name=None): from lockdown.type_system.composites import CompositeType types_with_opcodes = [t for t in types if t.get_micro_op_types()] from lockdown.type_system.universal_type import EMPTY_COMPOSITE_TYPE if len(types_with_opcodes) == 0: return EMPTY_COMPOSITE_TYPE if len(types_with_opcodes) == 1: return types_with_opcodes[0].clone(name=name) result = {} for type in types_with_opcodes: for tag, micro_op_type in type.get_micro_op_types().items(): if tag in result: result[tag] = result[tag].merge(micro_op_type) else: result[tag] = micro_op_type return CompositeType(result, name=name)
def test_rev_const_flatten_tuple(self): rev_const_type = CompositeType({ ("get", 0): GetterMicroOpType(0, StringType()), ("set", 0): SetterMicroOpType(0, AnyType()) }, name="test") normal_broad_type = CompositeType({ ("get", 0): GetterMicroOpType(0, StringType()), ("set", 0): SetterMicroOpType(0, StringType()) }, name="test") rev_const_type = prepare_lhs_type(rev_const_type, None) self.assertTrue(isinstance(rev_const_type.get_micro_op_type(("set", 0)).value_type, StringType)) self.assertTrue(normal_broad_type.is_copyable_from(rev_const_type, DUMMY_REASONER))
def test_rev_const_wildcard(self): rev_const_type = CompositeType({ ("get-wildcard", ): GetterWildcardMicroOpType(StringType(), StringType(), True), ("set-wildcard", ): SetterWildcardMicroOpType(StringType(), AnyType(), False, False) }, name="test") normal_broad_type = CompositeType({ ("get", "foo"): GetterWildcardMicroOpType(StringType(), StringType(), True), ("set", "foo"): SetterWildcardMicroOpType(StringType(), StringType(), False, False) }, name="test") rev_const_type = prepare_lhs_type(rev_const_type, None) self.assertTrue(isinstance(rev_const_type.get_micro_op_type(("set-wildcard",)).value_type, StringType)) self.assertTrue(normal_broad_type.is_copyable_from(rev_const_type, DUMMY_REASONER))
def test_setup_broad_reading_property(self): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", AnyType()), ("set", "foo"): SetterMicroOpType("foo", StringType()) }, name="test"))
def test_setup_narrow_write_property(self): obj = PythonObject({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", UnitType("hello")), ("set", "foo"): SetterMicroOpType("foo", UnitType("hello")) }, name="test"))
def test_add_micro_op_dictionary(self): obj = PythonDict({ "foo": "hello" }) get_manager(obj).add_composite_type(CompositeType({ ("get", "foo"): GetterMicroOpType("foo", StringType()) }, name="test"))