def test_text_list_property_set(): class Root(et.Element): tags = text_list_property('tag') r = Root('root') r.tags = ['set1', 'set2'] assert tostring(r) == '<root><tag>set1</tag><tag>set2</tag></root>' r.tags = ['set3', 'set4'] assert tostring(r) == '<root><tag>set3</tag><tag>set4</tag></root>'
def test_text_property(): class Root(et.Element): abc = text_property('abc') r = Root('root') assert r.abc is None r.abc = '12' assert tostring(r) == '<root><abc>12</abc></root>'
def test_text_list_property_extend(): class Root(et.Element): tags = text_list_property('tag') r = Root('root') assert len(r.tags) == 0 r.tags.extend(['c', 'd']) assert tostring(r) == '<root><tag>c</tag><tag>d</tag></root>'
def test_text_list_property_del(): class Root(et.Element): tags = text_list_property('tag') r = Root('root') r.tags = ['del1', 'del2'] del r.tags assert tostring(r) == '<root></root>'
def test_text_list_property_append(): class Root(et.Element): tags = text_list_property('tag') r = Root('root') assert len(r.tags) == 0 r.tags.append('a') r.tags.append('b') assert tostring(r) == '<root><tag>a</tag><tag>b</tag></root>'
def test_element_list_property(): class Root(et.Element): abc = element_list_property('abc') r = Root('root') assert r.abc is not None assert len(r.abc) == 0 r.abc.append(et.Element('abc')) r.abc.new_sub().text = 'ddd' assert tostring(r) == '<root><abc></abc><abc>ddd</abc></root>'
def test_element_property(): class Root(et.Element): abc = element_property('abc') r = Root('root') assert r.abc is None r.abc = et.Element('abc') r.abc.text = '12' r.abc.set('x', '33') assert tostring(r) == '<root><abc x="33">12</abc></root>'
def test_element_list_property_with_type(): class Name(et.Element): pass class Root(et.Element): abc: Name = element_list_property('abc', eltype=Name) r = Root('root') assert r.abc is not None assert len(r.abc) == 0 with pytest.raises(TypeError): r.abc.append(et.Element('abc')) subel = r.abc.new_sub() assert isinstance(subel, Name) subel.text = 'ddd' assert tostring(r) == '<root><abc>ddd</abc></root>'
def test_text_list_property(): class Root(et.Element): tags = text_list_property('tag') r = Root('root') assert tostring(r) == '<root></root>'