Updated ElementBase._setSubText and added unit tests.

_setSubText can now handle elements specified by an XPath expression, and
will build up the element tree as needed, reusing an existing elements in
the path.
This commit is contained in:
Lance Stout
2010-08-24 09:37:42 -04:00
parent 203986dd7c
commit c8f406d1b3
2 changed files with 94 additions and 12 deletions

View File

@@ -301,5 +301,56 @@ class TestElementBase(SleekTest):
self.failUnless(stanza['bar'] == 'found',
"_getSubText value incorrect: %s." % stanza['bar'])
def testSubElement(self):
"""Test setting the contents of a sub element."""
class TestStanza(ElementBase):
name = "foo"
namespace = "foo"
interfaces = set(('bar', 'baz'))
def setBaz(self, value):
self._setSubText("wrapper/baz", text=value)
def getBaz(self):
return self._getSubText("wrapper/baz")
def setBar(self, value):
self._setSubText("wrapper/bar", text=value)
def getBar(self):
return self._getSubText("wrapper/bar")
stanza = TestStanza()
stanza['bar'] = 'a'
stanza['baz'] = 'b'
self.checkStanza(TestStanza, stanza, """
<foo xmlns="foo">
<wrapper>
<bar>a</bar>
<baz>b</baz>
</wrapper>
</foo>
""")
stanza._setSubText('bar', text='', keep=True)
self.checkStanza(TestStanza, stanza, """
<foo xmlns="foo">
<wrapper>
<bar />
<baz>b</baz>
</wrapper>
</foo>
""", use_values=False)
stanza['bar'] = 'a'
stanza._setSubText('bar', text='')
self.checkStanza(TestStanza, stanza, """
<foo xmlns="foo">
<wrapper>
<baz>b</baz>
</wrapper>
</foo>
""")
suite = unittest.TestLoader().loadTestsFromTestCase(TestElementBase)