def test_attribute_values_inheritance(self):
        t1 = CClass(self.mcl, "T1")
        t2 = CClass(self.mcl, "T2")
        c = CClass(self.mcl, "C", superclasses=[t1, t2])
        sc = CClass(self.mcl, "C", superclasses=c)

        t1.attributes = {"i0": 0}
        t2.attributes = {"i1": 1}
        c.attributes = {"i2": 2}
        sc.attributes = {"i3": 3}

        o = CObject(sc, "o")

        for name, value in {"i0": 0, "i1": 1, "i2": 2, "i3": 3}.items():
            eq_(o.get_value(name), value)

        eq_(o.get_value("i0", t1), 0)
        eq_(o.get_value("i1", t2), 1)
        eq_(o.get_value("i2", c), 2)
        eq_(o.get_value("i3", sc), 3)

        for name, value in {"i0": 10, "i1": 11, "i2": 12, "i3": 13}.items():
            o.set_value(name, value)

        for name, value in {"i0": 10, "i1": 11, "i2": 12, "i3": 13}.items():
            eq_(o.get_value(name), value)

        eq_(o.get_value("i0", t1), 10)
        eq_(o.get_value("i1", t2), 11)
        eq_(o.get_value("i2", c), 12)
        eq_(o.get_value("i3", sc), 13)
    def test_attribute_values_same_name_inheritance(self):
        t1 = CClass(self.mcl, "T1")
        t2 = CClass(self.mcl, "T2")
        c = CClass(self.mcl, "C", superclasses=[t1, t2])
        sc = CClass(self.mcl, "C", superclasses=c)

        t1.attributes = {"i": 0}
        t2.attributes = {"i": 1}
        c.attributes = {"i": 2}
        sc.attributes = {"i": 3}

        o1 = CObject(sc)
        o2 = CObject(c)
        o3 = CObject(t1)

        eq_(o1.get_value("i"), 3)
        eq_(o2.get_value("i"), 2)
        eq_(o3.get_value("i"), 0)

        eq_(o1.get_value("i", sc), 3)
        eq_(o1.get_value("i", c), 2)
        eq_(o1.get_value("i", t2), 1)
        eq_(o1.get_value("i", t1), 0)
        eq_(o2.get_value("i", c), 2)
        eq_(o2.get_value("i", t2), 1)
        eq_(o2.get_value("i", t1), 0)
        eq_(o3.get_value("i", t1), 0)

        o1.set_value("i", 10)
        o2.set_value("i", 11)
        o3.set_value("i", 12)

        eq_(o1.get_value("i"), 10)
        eq_(o2.get_value("i"), 11)
        eq_(o3.get_value("i"), 12)

        eq_(o1.get_value("i", sc), 10)
        eq_(o1.get_value("i", c), 2)
        eq_(o1.get_value("i", t2), 1)
        eq_(o1.get_value("i", t1), 0)
        eq_(o2.get_value("i", c), 11)
        eq_(o2.get_value("i", t2), 1)
        eq_(o2.get_value("i", t1), 0)
        eq_(o3.get_value("i", t1), 12)

        o1.set_value("i", 130, sc)
        o1.set_value("i", 100, t1)
        o1.set_value("i", 110, t2)
        o1.set_value("i", 120, c)

        eq_(o1.get_value("i"), 130)

        eq_(o1.get_value("i", sc), 130)
        eq_(o1.get_value("i", c), 120)
        eq_(o1.get_value("i", t2), 110)
        eq_(o1.get_value("i", t1), 100)
    def test_attribute_values_inheritance_after_delete_superclass(self):
        t1 = CClass(self.mcl, "T1")
        t2 = CClass(self.mcl, "T2")
        c = CClass(self.mcl, "C", superclasses=[t1, t2])
        sc = CClass(self.mcl, "C", superclasses=c)

        t1.attributes = {"i0": 0}
        t2.attributes = {"i1": 1}
        c.attributes = {"i2": 2}
        sc.attributes = {"i3": 3}

        o = CObject(sc, "o")

        t2.delete()

        for name, value in {"i0": 0, "i2": 2, "i3": 3}.items():
            eq_(o.get_value(name), value)
        try:
            o.get_value("i1")
            exception_expected_()
        except CException as e:
            eq_(e.value, "attribute 'i1' unknown for 'o'")

        eq_(o.get_value("i0", t1), 0)
        try:
            o.get_value("i1", t2)
            exception_expected_()
        except CException as e:
            eq_(e.value, "attribute 'i1' unknown for ''")
        eq_(o.get_value("i2", c), 2)
        eq_(o.get_value("i3", sc), 3)

        for name, value in {"i0": 10, "i2": 12, "i3": 13}.items():
            o.set_value(name, value)
        try:
            o.set_value("i1", 11)
            exception_expected_()
        except CException as e:
            eq_(e.value, "attribute 'i1' unknown for 'o'")

        for name, value in {"i0": 10, "i2": 12, "i3": 13}.items():
            eq_(o.get_value(name), value)
        try:
            o.get_value("i1")
            exception_expected_()
        except CException as e:
            eq_(e.value, "attribute 'i1' unknown for 'o'")

        eq_(o.get_value("i0", t1), 10)
        try:
            o.get_value("i1", t2)
            exception_expected_()
        except CException as e:
            eq_(e.value, "attribute 'i1' unknown for ''")
        eq_(o.get_value("i2", c), 12)
        eq_(o.get_value("i3", sc), 13)
    def test_values_multiple_inheritance(self):
        t1 = CClass(self.mcl, "T1")
        t2 = CClass(self.mcl, "T2")
        st_a = CClass(self.mcl, "STA", superclasses=[t1, t2])
        sub_a = CClass(self.mcl, "SubA", superclasses=[st_a])
        st_b = CClass(self.mcl, "STB", superclasses=[t1, t2])
        sub_b = CClass(self.mcl, "SubB", superclasses=[st_b])
        st_c = CClass(self.mcl, "STC")
        sub_c = CClass(self.mcl, "SubC", superclasses=[st_c])

        cl = CClass(self.mcl, "M", superclasses=[sub_a, sub_b, sub_c])
        o = CObject(cl)

        t1.attributes = {"i0": 0}
        t2.attributes = {"i1": 1}
        st_a.attributes = {"i2": 2}
        sub_a.attributes = {"i3": 3}
        st_b.attributes = {"i4": 4}
        sub_b.attributes = {"i5": 5}
        st_c.attributes = {"i6": 6}
        sub_c.attributes = {"i7": 7}

        eq_(o.get_value("i0"), 0)
        eq_(o.get_value("i1"), 1)
        eq_(o.get_value("i2"), 2)
        eq_(o.get_value("i3"), 3)
        eq_(o.get_value("i4"), 4)
        eq_(o.get_value("i5"), 5)
        eq_(o.get_value("i6"), 6)
        eq_(o.get_value("i7"), 7)

        eq_(o.get_value("i0", t1), 0)
        eq_(o.get_value("i1", t2), 1)
        eq_(o.get_value("i2", st_a), 2)
        eq_(o.get_value("i3", sub_a), 3)
        eq_(o.get_value("i4", st_b), 4)
        eq_(o.get_value("i5", sub_b), 5)
        eq_(o.get_value("i6", st_c), 6)
        eq_(o.get_value("i7", sub_c), 7)

        o.set_value("i0", 10)
        o.set_value("i1", 11)
        o.set_value("i2", 12)
        o.set_value("i3", 13)
        o.set_value("i4", 14)
        o.set_value("i5", 15)
        o.set_value("i6", 16)
        o.set_value("i7", 17)

        eq_(o.get_value("i0"), 10)
        eq_(o.get_value("i1"), 11)
        eq_(o.get_value("i2"), 12)
        eq_(o.get_value("i3"), 13)
        eq_(o.get_value("i4"), 14)
        eq_(o.get_value("i5"), 15)
        eq_(o.get_value("i6"), 16)
        eq_(o.get_value("i7"), 17)
 def test_attributes_overwrite_no_defaults(self):
     cl = CClass(self.mcl, "C", attributes={
         "isBoolean": bool,
         "intVal": int})
     o = CObject(cl, "o")
     eq_(o.get_value("isBoolean"), None)
     o.set_value("isBoolean", False)
     cl.attributes = {
         "isBoolean": bool,
         "intVal": int,
         "floatVal": float}
     eq_(o.get_value("isBoolean"), False)
     eq_(o.get_value("floatVal"), None)
     eq_(o.get_value("intVal"), None)
     o.set_value("floatVal", 1.2)
     eq_(o.get_value("floatVal"), 1.2)
 def test_attribute_deleted_no_default(self):
     cl = CClass(self.mcl, "C", attributes={
         "isBoolean": bool,
         "intVal": int})
     cl.attributes = {"isBoolean": bool}
     o = CObject(cl, "o")
     eq_(o.get_value("isBoolean"), None)
     try:
         o.get_value("intVal")
         exception_expected_()
     except CException as e:
         eq_(e.value, "attribute 'intVal' unknown for 'o'")
     try:
         o.set_value("intVal", 1)
         exception_expected_()
     except CException as e:
         eq_(e.value, "attribute 'intVal' unknown for 'o'")
 def test_attribute_deleted(self):
     cl = CClass(self.mcl, "C", attributes={
         "isBoolean": True,
         "intVal": 15})
     o = CObject(cl, "o")
     eq_(o.get_value("intVal"), 15)
     cl.attributes = {
         "isBoolean": False}
     eq_(o.get_value("isBoolean"), True)
     try:
         o.get_value("intVal")
         exception_expected_()
     except CException as e:
         eq_(e.value, "attribute 'intVal' unknown for 'o'")
     try:
         o.set_value("intVal", 1)
         exception_expected_()
     except CException as e:
         eq_(e.value, "attribute 'intVal' unknown for 'o'")
 def test_attributes_overwrite(self):
     cl = CClass(self.mcl, "C", attributes={
         "isBoolean": True,
         "intVal": 15})
     o = CObject(cl, "o")
     eq_(o.get_value("intVal"), 15)
     try:
         o.get_value("floatVal")
         exception_expected_()
     except CException as e:
         eq_(e.value, "attribute 'floatVal' unknown for 'o'")
     o.set_value("intVal", 18)
     cl.attributes = {
         "isBoolean": False,
         "intVal": 19,
         "floatVal": 25.1}
     eq_(o.get_value("isBoolean"), True)
     eq_(o.get_value("floatVal"), 25.1)
     eq_(o.get_value("intVal"), 18)
     o.set_value("floatVal", 1.2)
     eq_(o.get_value("floatVal"), 1.2)
    def test_attributes_deleted_on_subclass_no_defaults(self):
        cl = CClass(self.mcl, "C", attributes={
            "isBoolean": bool,
            "intVal": int})
        cl2 = CClass(self.mcl, "C2", attributes={
            "isBoolean": bool}, superclasses=cl)

        o = CObject(cl2, "o")

        eq_(o.get_value("isBoolean"), None)
        eq_(o.get_value("isBoolean", cl), None)
        eq_(o.get_value("isBoolean", cl2), None)

        cl2.attributes = {}

        eq_(o.get_value("isBoolean"), None)
        eq_(o.get_value("intVal"), None)
        eq_(o.get_value("isBoolean", cl), None)
        try:
            o.get_value("isBoolean", cl2)
            exception_expected_()
        except CException as e:
            eq_(e.value, "attribute 'isBoolean' unknown for 'C2'")
 def test_attribute_defined_after_instance(self):
     cl = CClass(self.mcl, "C")
     o = CObject(cl, "o")
     cl.attributes = {"floatVal": float}
     o.set_value("floatVal", 15)
     eq_(o.get_value("floatVal"), 15)