Example #1
0
    def test_comp_context(self):
        Register.add(SubComponent)
        obj = MyComponent()
        obj.root = obj
        template = """<comp><li><a><SubComponent></SubComponent></a></li></comp>"""
        obj.instructions = self.tp.parse(template)

        obj.mount()
        comp_li = obj.children[0]
        comp_a = comp_li.children[0]
        comp_sub = comp_a.children[0]
        self.assertEqual(RefMap.get(comp_li.context['self']), comp_li)
        self.assertEqual(RefMap.get(comp_li.context['parent']), obj)
        self.assertEqual(RefMap.get(comp_li.context['root']), obj)

        self.assertEqual(comp_li.root, obj)
        self.assertEqual(comp_li.parent, obj)

        self.assertEqual(RefMap.get(comp_a.context['self']), comp_a)
        self.assertEqual(RefMap.get(comp_a.context['parent']), comp_li)
        self.assertEqual(RefMap.get(comp_a.context['root']), obj)

        self.assertEqual(comp_a.root, obj)
        self.assertEqual(comp_a.parent, comp_li)

        self.assertEqual(RefMap.get(comp_sub.context['parent']), comp_a)
        self.assertEqual(RefMap.get(comp_sub.context['root']), comp_sub)

        comp_sub2 = SubComponent()
        obj.add(comp_sub2)
        self.assertEqual(RefMap.get(comp_sub2.context['parent']), obj)
        self.assertEqual(RefMap.get(comp_sub2.context['root']), comp_sub2)
Example #2
0
    def test_comp_context(self):
        Register.add(SubComponent)
        obj = MyComponent()
        obj.root = obj
        template = """<comp><li><a><SubComponent></SubComponent></a></li></comp>"""
        obj.instructions = self.tp.parse(template)

        obj.mount()
        comp_li = obj.children[0]
        comp_a = comp_li.children[0]
        comp_sub = comp_a.children[0]
        self.assertEqual(RefMap.get(comp_li.context["self"]), comp_li)
        self.assertEqual(RefMap.get(comp_li.context["parent"]), obj)
        self.assertEqual(RefMap.get(comp_li.context["root"]), obj)

        self.assertEqual(comp_li.root, obj)
        self.assertEqual(comp_li.parent, obj)

        self.assertEqual(RefMap.get(comp_a.context["self"]), comp_a)
        self.assertEqual(RefMap.get(comp_a.context["parent"]), comp_li)
        self.assertEqual(RefMap.get(comp_a.context["root"]), obj)

        self.assertEqual(comp_a.root, obj)
        self.assertEqual(comp_a.parent, comp_li)

        self.assertEqual(RefMap.get(comp_sub.context["parent"]), comp_a)
        self.assertEqual(RefMap.get(comp_sub.context["root"]), comp_sub)

        comp_sub2 = SubComponent()
        obj.add(comp_sub2)
        self.assertEqual(RefMap.get(comp_sub2.context["parent"]), obj)
        self.assertEqual(RefMap.get(comp_sub2.context["root"]), comp_sub2)
Example #3
0
    def test_update_with_expression_self_parent_root(self):
        obj_self = ObjTest()
        obj_parent = ObjTest()
        obj_root = ObjTest()

        expr = 'self.b + parent.a + root.a + 10'  #Since we're updating obj_self.a we don't use self.a in expr to avoid infinite recursion
        context = {
            'self': RefMap.get_ref(obj_self),
            'parent': RefMap.get_ref(obj_parent),
            'root': RefMap.get_ref(obj_root),
            'this': RefMap.add(None)
        }
        exprc = compile_expr(expr)

        props2bind = get_props2bind(expr)
        obj_self.update_with_expression('a',
                                        exprc,
                                        context,
                                        props2bind=props2bind)

        obj_parent.a = 1
        self.assertEqual(obj_self.a, 11)
        obj_root.a = 2
        self.assertEqual(obj_self.a, 13)
        obj_self.b = 3
        self.assertEqual(obj_self.a, 16)
Example #4
0
    def test_update_with_expression_self_parent_root(self):
        obj_self = ObjTest()
        obj_parent = ObjTest()
        obj_root = ObjTest()

        expr = (
            "self.b + parent.a + root.a + 10"
        )  # Since we're updating obj_self.a we don't use self.a in expr to avoid infinite recursion
        context = {
            "self": RefMap.get_ref(obj_self),
            "parent": RefMap.get_ref(obj_parent),
            "root": RefMap.get_ref(obj_root),
            "this": RefMap.add(None),
        }
        exprc = compile_expr(expr)

        props2bind = get_props2bind(expr)
        obj_self.update_with_expression("a", exprc, context, props2bind=props2bind)

        obj_parent.a = 1
        self.assertEqual(obj_self.a, 11)
        obj_root.a = 2
        self.assertEqual(obj_self.a, 13)
        obj_self.b = 3
        self.assertEqual(obj_self.a, 16)