def test_compound_model_property(self): class Object(object): def __init__(self): self.__attr = 'value' def attr(): def fget(self): return self.__attr def fset(self, attr): self.__attr = attr return locals() attr = property(**attr()) o = Object() m = model.CompoundModel(o) mc = ayame.MarkupContainer('a', m) mc.add(ayame.Component('attr')) self.assert_equal(len(mc.children), 1) self.assert_equal(o.attr, 'value') self.assert_equal(mc.find('attr').model.object, 'value') mc.find('attr').model.object = 'new_value' self.assert_equal(o.attr, 'new_value') self.assert_equal(mc.find('attr').model.object, 'new_value')
def test_compound_model_replace(self): o = {'b': 'b', 'c': 'c'} m = model.CompoundModel(o) mc = ayame.MarkupContainer('a', m) mc.add(ayame.MarkupContainer('b')) self.assert_equal(len(mc.children), 1) self.assert_equal(mc.find('b').model.object, 'b') mc.find('b').add(ayame.Component('c')) self.assert_equal(len(mc.children), 1) self.assert_equal(len(mc.find('b').children), 1) self.assert_equal(mc.find('b:c').model.object, 'c') mc.model = model.CompoundModel(object()) self.assert_is_none(mc.find('b').model.object) self.assert_is_none(mc.find('b:c').model.object) with self.assert_raises_regex(AttributeError, '^b$'): setattr(mc.find('b').model, 'object', '') with self.assert_raises_regex(AttributeError, '^c$'): setattr(mc.find('b:c').model, 'object', '') self.assert_equal(mc.render(''), '')
def test_compound_model_dict(self): o = {'mapping': 'value'} m = model.CompoundModel(o) mc = ayame.MarkupContainer('a', m) mc.add(ayame.Component('mapping')) self.assert_equal(len(mc.children), 1) self.assert_equal(o['mapping'], 'value') self.assert_equal(mc.find('mapping').model.object, 'value') mc.find('mapping').model.object = 'new_value' self.assert_equal(o['mapping'], 'new_value') self.assert_equal(mc.find('mapping').model.object, 'new_value')
def test_compound_model_method_noncallable(self): class Object(object): get_method = set_method = None o = Object() m = model.CompoundModel(o) mc = ayame.MarkupContainer('a', m) mc.add(ayame.Component('method')) self.assert_equal(len(mc.children), 1) self.assert_is_none(mc.find('method').model.object) with self.assert_raises_regex(AttributeError, '^method$'): mc.find('method').model.object = 'new_value'
def test_compound_model_attr(self): class Object(object): attr = 'value' o = Object() m = model.CompoundModel(o) mc = ayame.MarkupContainer('a', m) mc.add(ayame.Component('attr')) self.assert_equal(len(mc.children), 1) self.assert_equal(o.attr, 'value') self.assert_equal(mc.find('attr').model.object, 'value') mc.find('attr').model.object = 'new_value' self.assert_equal(o.attr, 'new_value') self.assert_equal(mc.find('attr').model.object, 'new_value')
def test_property_list_view(self): def populate_item(li): li.add(basic.Label('c', li.model.object)) root = markup.Element(self.of('root'), attrib={markup.AYAME_ID: 'b'}) label = markup.Element(self.of('label'), attrib={markup.AYAME_ID: 'c'}) root.append(label) m = model.CompoundModel({'b': [str(i) for i in five.range(3)]}) mc = ayame.MarkupContainer('a', m) mc.add(basic.PropertyListView('b', None, populate_item)) root = mc.render(root) self.assert_equal(root.qname, self.of('root')) self.assert_equal(root.attrib, {}) self.assert_equal(len(root), 3) label = root[0] self.assert_equal(label.qname, self.of('label')) self.assert_equal(label.attrib, {}) self.assert_equal(label.children, ['0']) label = root[1] self.assert_equal(label.qname, self.of('label')) self.assert_equal(label.attrib, {}) self.assert_equal(label.children, ['1']) label = root[2] self.assert_equal(label.qname, self.of('label')) self.assert_equal(label.attrib, {}) self.assert_equal(label.children, ['2']) self.assert_equal(len(mc.children), 1) lv = mc.children[0] self.assert_equal(len(lv.children), 3) self.assert_equal(lv.children[0].index, 0) self.assert_equal(lv.children[1].index, 1) self.assert_equal(lv.children[2].index, 2) self.assert_is_instance(lv.children[0].model, model.CompoundModel) self.assert_is_instance(lv.children[1].model, model.CompoundModel) self.assert_is_instance(lv.children[2].model, model.CompoundModel) lv.children[0].model.object = 10 lv.children[1].model.object = 11 lv.children[2].model.object = 12 self.assert_equal(lv.children[0].model.object, 10) self.assert_equal(lv.children[1].model.object, 11) self.assert_equal(lv.children[2].model.object, 12)
def test_property_list_view_render_body_only(self): def populate_item(li): li.add(basic.Label('c', li.model.object)) li.find('c').render_body_only = True root = markup.Element(self.of('root'), attrib={markup.AYAME_ID: 'b'}) root.append('[') label = markup.Element(self.of('label'), attrib={markup.AYAME_ID: 'c'}) root.append(label) root.append(']') m = model.CompoundModel({'b': [str(i) for i in five.range(3)]}) mc = ayame.MarkupContainer('a', m) mc.add(basic.PropertyListView('b', None, populate_item)) root = mc.render(root) self.assert_equal(root.qname, self.of('root')) self.assert_equal(root.attrib, {}) self.assert_equal(len(root), 9) root.normalize() self.assert_equal(root.children, ['[0][1][2]'])
def test_compound_model_method(self): class Object(object): def __init__(self): self.__method = 'value' def get_method(self): return self.__method def set_method(self, method): self.__method = method o = Object() m = model.CompoundModel(o) mc = ayame.MarkupContainer('a', m) mc.add(ayame.Component('method')) self.assert_equal(len(mc.children), 1) self.assert_equal(o.get_method(), 'value') self.assert_equal(mc.find('method').model.object, 'value') mc.find('method').model.object = 'new_value' self.assert_equal(o.get_method(), 'new_value') self.assert_equal(mc.find('method').model.object, 'new_value')