From c20fab0f6c28bcbfbe54db687be056a9b5088ad4 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 13 Aug 2010 12:24:47 -0400 Subject: [PATCH] Updated ElementBase.setup, and added unit tests. --- sleekxmpp/xmlstream/stanzabase.py | 48 ++++++++++++++++++++----------- tests/test_elementbase.py | 23 +++++++++++++++ 2 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 tests/test_elementbase.py diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 6762792..b11d59e 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -76,6 +76,38 @@ class ElementBase(object): self.iterables.append(sub(child, self)) break + def setup(self, xml=None): + """ + Initialize the stanza's XML contents. + + Will return True if XML was generated according to the stanza's + definition. + + Arguments: + xml -- Optional XML object to use for the stanza's content + instead of generating XML. + """ + if self.xml is None: + self.xml = xml + + if self.xml is None: + # Generate XML from the stanza definition + for ename in self.name.split('/'): + new = ET.Element("{%s}%s" % (self.namespace, ename)) + if self.xml is None: + self.xml = new + else: + last_xml.append(new) + last_xml = new + if self.parent is not None: + self.parent().xml.append(self.xml) + + # We had to generate XML + return True + else: + # We did not generate XML + return False + @property def attrib(self): #backwards compatibility return self @@ -159,22 +191,6 @@ class ElementBase(object): def findall(self, xpath): return self.xml.findall(xpath) - def setup(self, xml=None): - if self.xml is None: - self.xml = xml - if self.xml is None: - for ename in self.name.split('/'): - new = ET.Element("{%(namespace)s}%(name)s" % {'name': self.name, 'namespace': self.namespace}) - if self.xml is None: - self.xml = new - else: - self.xml.append(new) - if self.parent is not None: - self.parent().xml.append(self.xml) - return True #had to generate XML - else: - return False - def enable(self, attrib): self.initPlugin(attrib) return self diff --git a/tests/test_elementbase.py b/tests/test_elementbase.py new file mode 100644 index 0000000..1b018b4 --- /dev/null +++ b/tests/test_elementbase.py @@ -0,0 +1,23 @@ +from . sleektest import * +from sleekxmpp.xmlstream.stanzabase import ElementBase + +class TestElementBase(SleekTest): + + def testExtendedName(self): + """Test element names of the form tag1/tag2/tag3.""" + + class TestStanza(ElementBase): + name = "foo/bar/baz" + namespace = "test" + + stanza = TestStanza() + self.checkStanza(TestStanza, stanza, """ + + + + + + """) + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestElementBase)