Exemplo n.º 1
0
    def test_double_deep_assignment(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": PythonObject({
                    "bam": 10
                })
            })
        })

        Baz = UniversalObjectType({
            "bam": IntegerType()
        })

        Bar = UniversalObjectType({
            "baz": Baz
        })

        get_manager(foo).add_composite_type(
            UniversalObjectType({
                "bar": Bar
            })
        )

        self.assertEquals(foo.bar.baz.bam, 10)

        foo.bar = PythonObject({ "baz": PythonObject({ "bam": 42 }) }, bind=Bar)

        self.assertEquals(foo.bar.baz.bam, 42)
Exemplo n.º 2
0
    def test_that_python_constraints_dont_spread_to_constrained_children(self):
        bar = PythonObject({
            "baz": 42
        })
        foo = PythonObject({
            "bar": bar
        })

        # The first, stronger, type prevents the NO_SETTER_ERROR_COMPOSITE_TYPE spreading from foo to bar
        get_manager(bar).add_composite_type(UniversalObjectType({ "baz": IntegerType() }))
        get_manager(foo).add_composite_type(NO_SETTER_ERROR_COMPOSITE_TYPE)

        self.assertIs(foo.bar, bar)

        self.assertEquals(len(get_manager(foo).attached_types), 1)
        self.assertEquals(len(get_manager(foo.bar).attached_types), 1)

        # ... but when bar is replaced with a new object without constraints, the PythonObjectType
        # spreads to the new object
        foo.bar = PythonObject({
            "baz": 123
        })

        self.assertIsNot(foo.bar, bar)

        self.assertEquals(len(get_manager(foo.bar).attached_types), 1)

        # Now that the new object has the PythonObjectType constraint, we can't bind a stronger
        # constraint
        with self.assertRaises(CompositeTypeIncompatibleWithTarget):
            get_manager(foo.bar).add_composite_type(UniversalObjectType({ "baz": IntegerType() }))
Exemplo n.º 3
0
    def test_modifying(self):
        At = UniversalObjectType({
            "foo": IntegerType()
        })

        Bt = UniversalObjectType({
            "bar": At
        })

        A = PythonObject({
            "foo": 5
        })
        B = PythonObject({
            "bar": A
        }, bind=Bt)

        self.assertEquals(len(get_manager(A).attached_types), 1)
        self.assertEquals(get_manager(A).attached_type_counts[id(At)], 1)

        B.bar = PythonObject({
            "foo": 42
        }, bind=At)
        
        self.assertEquals(len(get_manager(A).attached_types), 0)
        self.assertEquals(get_manager(A).attached_type_counts[id(At)], 0)
Exemplo n.º 4
0
    def test_changes_blocked_without_micro_ops(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo)

        with self.assertRaises(Exception):
            foo.bar = "hello"
Exemplo n.º 5
0
    def test_python_replacing_object_works(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(DEFAULT_COMPOSITE_TYPE)

        foo.bar = PythonObject({ "baz": 123 })

        self.assertEquals(foo.bar.baz, 123)
Exemplo n.º 6
0
    def test_python_like_type(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(RICH_TYPE)

        foo.bar.baz = 22

        foo.bar = "hello"
        self.assertEquals(foo.bar, "hello")
Exemplo n.º 7
0
    def test_very_broad_assignment(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(
            UniversalObjectType({ "bar": AnyType() })
        )

        foo.bar = "hello"
        self.assertEquals(foo.bar, "hello")
Exemplo n.º 8
0
    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"
Exemplo n.º 9
0
    def test_blocked_basic_assignment(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(
            UniversalObjectType({
                "bar": UniversalObjectType({
                    "baz": IntegerType()
                })
            })
        )

        with self.assertRaises(Exception):
            foo.bar = PythonObject({ "baz": "hello" })
Exemplo n.º 10
0
    def test_python_object_with_reference_can_be_replaced(self):
        bar = PythonObject({
            "baz": 42
        })
        foo = PythonObject({
            "bar": bar
        })

        get_manager(bar).add_composite_type(UniversalObjectType({ "baz": IntegerType() }))
        get_manager(foo).add_composite_type(DEFAULT_COMPOSITE_TYPE)

        foo.bar = PythonObject({
            "baz": 123
        })

        self.assertEqual(foo.bar.baz, 123)
        foo.bar.baz = "what"
        self.assertEqual(foo.bar.baz, "what")
Exemplo n.º 11
0
    def test_basic_assignment(self):
        Bar = UniversalObjectType({
            "baz": IntegerType()
        })
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        get_manager(foo).add_composite_type(
            UniversalObjectType({
                "bar": Bar
            })
        )

        foo.bar = PythonObject({ "baz": 42 }, bind=Bar)

        self.assertEquals(foo.bar.baz, 42)
Exemplo n.º 12
0
    def test_broad_assignment(self):
        foo = PythonObject({
            "bar": PythonObject({
                "baz": 42
            })
        })

        Bar = UniversalObjectType({
            "baz": AnyType()
        })

        get_manager(foo).add_composite_type(
            UniversalObjectType({
                "bar": Bar
            })
        )

        foo.bar = PythonObject({ "baz": "hello" }, bind=Bar)

        self.assertEquals(foo.bar.baz, "hello")