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_nested(self): self.assertTrue( UniversalObjectType({ "foo": OneOfType([ IntegerType(), StringType() ]) }).is_copyable_from(UniversalObjectType({ "foo": OneOfType([ IntegerType(), StringType() ]) }), DUMMY_REASONER) ) # Blocked because the receiver could set obj.foo = "hello", breaking the sender self.assertFalse( UniversalObjectType({ "foo": OneOfType([ IntegerType(), StringType() ]) }).is_copyable_from(UniversalObjectType({ "foo": IntegerType() }), DUMMY_REASONER) ) self.assertTrue( UniversalObjectType({ "foo": Const(OneOfType([ IntegerType(), StringType() ])) }).is_copyable_from(UniversalObjectType({ "foo": IntegerType() }), DUMMY_REASONER) )
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_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_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_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_default_dict(self): def default_factory(target, key): return "{}-123".format(key) foo = PythonObject({ "bar": "forty-two" }, default_factory=default_factory) get_manager(foo).add_composite_type(UniversalDefaultDictType(StringType(), StringType(), name="test")) self.assertEquals(foo.bar, "forty-two") self.assertEquals(foo.bam, "bam-123")
def test_java_like_object(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") obj.foo = "what" self.assertEquals(obj.foo, "what") with self.assertRaises(Exception): obj.bar = "hello"
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_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_to_match_with_one_of_type_combo(self): func = function_lit( one_of_type([string_type(), int_type(), bool_type()]), return_op( match_op(dereference("argument"), [ prepared_function(int_type(), literal_op("int is not a string")), prepared_function(bool_type(), literal_op("bool is not a string")), prepared_function(inferred_type(), dereference("argument")) ]))) _, result = bootstrap_function(func, argument=2) self.assertEquals(result.caught_break_mode, "return") self.assertEquals(result.value, "int is not a string") _, result = bootstrap_function(func, argument=True) self.assertEquals(result.caught_break_mode, "return") self.assertEquals(result.value, "bool is not a string") _, result = bootstrap_function(func, argument="hello world") self.assertEquals(result.caught_break_mode, "return") self.assertEquals(result.value, "hello world") prepared_func = prepare(func, PythonObject({}), FrameManager()) self.assertEquals(len(prepared_func.break_types), 1) self.assertTrue("return" in prepared_func.break_types) for return_break_type in prepared_func.break_types["return"]: self.assertTrue(StringType().is_copyable_from( return_break_type["out"], DUMMY_REASONER))
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_basic_ignored(self): foo = UniversalObjectType({ "bar": StringType() }) foo = prepare_lhs_type(foo, UniversalObjectType({ "bar": IntegerType() })) self.assertIsInstance(foo.get_micro_op_type(("get", "bar")).value_type, StringType)
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_runtime(self): obj = PythonObject({ "foo": 5 }) get_manager(obj).add_composite_type( UniversalObjectType({ "foo": OneOfType([ IntegerType(), StringType() ]) }) )
def test_dangling_error(self): foo = UniversalObjectType({ "bar": InferredType() }) with self.assertRaises(DanglingInferredType): foo = prepare_lhs_type(foo, UniversalObjectType({ "bam": StringType() })) check_dangling_inferred_types(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_types_on_object_merged(self): foo = PythonObject({ "foo": 5, "bar": "hello" }) get_manager(foo).add_composite_type( UniversalObjectType({ "foo": IntegerType() }) ) get_manager(foo).add_composite_type( UniversalObjectType({ "bar": StringType() }) ) object_type = get_manager(foo).get_effective_composite_type() UniversalObjectType({ "foo": IntegerType(), "bar": StringType() }).is_copyable_from(object_type, DUMMY_REASONER)
def test_basic_class(self): T = UniversalObjectType({ "foo": IntegerType() }) S = UniversalObjectType({ "foo": IntegerType(), "bar": StringType() }) self.assertTrue(T.is_copyable_from(S, DUMMY_REASONER)) self.assertFalse(S.is_copyable_from(T, DUMMY_REASONER))
def test_merge_pairwise_unrelated_types(self): self.assertTrue(isinstance(merge_types([ StringType(), IntegerType() ], "super"), OneOfType)) self.assertTrue(len(merge_types([ StringType(), IntegerType() ], "super").types) == 2) self.assertTrue(isinstance(merge_types([ StringType(), IntegerType() ], "sub"), OneOfType)) self.assertTrue(len(merge_types([ StringType(), IntegerType() ], "sub").types) == 2) self.assertTrue(isinstance(merge_types([ StringType(), IntegerType() ], "exact"), OneOfType)) self.assertTrue(len(merge_types([ StringType(), IntegerType() ], "exact").types) == 2)
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))
# return PythonDict({ # "type": "Universal", # "micro_ops": PythonList(micro_ops) # }) TYPES = { "Any": lambda data: AnyType(), "Object": build_object_type, "List": build_list_type, "Universal": build_universal_type, "Function": build_closed_function_type, "OneOf": build_one_of_type, "Integer": lambda data: IntegerType(), "Boolean": lambda data: BooleanType(), "NoValue": lambda data: NoValueType(), "String": lambda data: StringType(), "Unit": build_unit_type, "Inferred": lambda data: InferredType() } def enrich_type(data): from lockdown.type_system.managers import get_manager if not isinstance(data, Universal): raise PreparationException("Unknown type data {}, {}".format( data, type(data))) if not data._contains("type"): raise PreparationException("Missing type in data {}".format(data)) if data._get("type") not in TYPES: raise PreparationException("Unknown type {}".format(data.type))
def test_basic(self): self.assertTrue(OneOfType([IntegerType(), StringType()]).is_copyable_from(IntegerType(), DUMMY_REASONER)) self.assertTrue(OneOfType([IntegerType(), StringType()]).is_copyable_from(StringType(), DUMMY_REASONER)) self.assertFalse(StringType().is_copyable_from(OneOfType([IntegerType(), StringType()]), DUMMY_REASONER))
def test_extreme_type1_contains_conflicts(self): foo = UniversalLupleType([ IntegerType() ], StringType()) self.assertFalse(foo.is_self_consistent(DUMMY_REASONER))
def test_merge_irrelevant_types(self): self.assertTrue(isinstance(merge_types([ StringType(), StringType(), IntegerType() ], "super"), OneOfType)) self.assertTrue(len(merge_types([ StringType(), StringType(), IntegerType() ], "super").types) == 2) self.assertTrue(isinstance(merge_types([ StringType(), StringType(), IntegerType() ], "sub"), OneOfType)) self.assertTrue(len(merge_types([ StringType(), StringType(), IntegerType() ], "sub").types) == 2) self.assertTrue(isinstance(merge_types([ StringType(), StringType(), IntegerType() ], "exact"), OneOfType)) self.assertTrue(len(merge_types([ StringType(), IntegerType() ], "exact").types) == 2)
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_default_dict_is_consistent_type(self): type = UniversalDefaultDictType(StringType(), StringType(), name="test") self.assertTrue(type.is_self_consistent(DUMMY_REASONER))
def test_incorrect_type_blocked(self): foo = PythonList([ 4, 6, 8 ]) with self.assertRaises(Exception): get_manager(foo).add_composite_type(UniversalListType(StringType()))
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"))