2013-07-26 11:02:26 +00:00
|
|
|
import unittest
|
2014-07-17 12:19:04 +00:00
|
|
|
from slixmpp.test import SlixTest
|
|
|
|
from slixmpp.xmlstream.stanzabase import ElementBase, register_stanza_plugin, ET
|
2010-08-13 16:24:47 +00:00
|
|
|
|
2010-10-07 14:58:13 +00:00
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
class TestElementBase(SlixTest):
|
2010-08-13 16:24:47 +00:00
|
|
|
|
2010-08-26 22:27:18 +00:00
|
|
|
def testFixNs(self):
|
|
|
|
"""Test fixing namespaces in an XPath expression."""
|
|
|
|
|
|
|
|
e = ElementBase()
|
|
|
|
ns = "http://jabber.org/protocol/disco#items"
|
|
|
|
result = e._fix_ns("{%s}foo/bar/{abc}baz/{%s}more" % (ns, ns))
|
|
|
|
|
|
|
|
expected = "/".join(["{%s}foo" % ns,
|
|
|
|
"{%s}bar" % ns,
|
|
|
|
"{abc}baz",
|
|
|
|
"{%s}more" % ns])
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(expected == result,
|
2010-08-26 22:27:18 +00:00
|
|
|
"Incorrect namespace fixing result: %s" % str(result))
|
|
|
|
|
|
|
|
|
2010-08-13 16:24:47 +00:00
|
|
|
def testExtendedName(self):
|
|
|
|
"""Test element names of the form tag1/tag2/tag3."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo/bar/baz"
|
|
|
|
namespace = "test"
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-13 16:24:47 +00:00
|
|
|
<foo xmlns="test">
|
|
|
|
<bar>
|
|
|
|
<baz />
|
|
|
|
</bar>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
2010-08-14 00:05:24 +00:00
|
|
|
def testGetStanzaValues(self):
|
2014-09-21 16:51:06 +00:00
|
|
|
"""Test get_stanza_values using plugins and substanzas."""
|
2010-08-14 00:05:24 +00:00
|
|
|
|
|
|
|
class TestStanzaPlugin(ElementBase):
|
|
|
|
name = "foo2"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-14 00:05:24 +00:00
|
|
|
plugin_attrib = "foo2"
|
|
|
|
|
|
|
|
class TestSubStanza(ElementBase):
|
|
|
|
name = "subfoo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-14 00:05:24 +00:00
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-14 00:05:24 +00:00
|
|
|
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestStanzaPlugin, iterable=True)
|
2010-08-14 00:05:24 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['bar'] = 'a'
|
|
|
|
stanza['foo2']['baz'] = 'b'
|
|
|
|
substanza = TestSubStanza()
|
|
|
|
substanza['bar'] = 'c'
|
|
|
|
stanza.append(substanza)
|
|
|
|
|
2014-09-21 16:51:06 +00:00
|
|
|
values = stanza.get_stanza_values()
|
2012-06-05 23:54:26 +00:00
|
|
|
expected = {'lang': '',
|
|
|
|
'bar': 'a',
|
2010-08-14 00:05:24 +00:00
|
|
|
'baz': '',
|
2012-06-05 23:54:26 +00:00
|
|
|
'foo2': {'lang': '',
|
|
|
|
'bar': '',
|
2010-08-14 00:05:24 +00:00
|
|
|
'baz': 'b'},
|
2011-12-12 01:04:58 +00:00
|
|
|
'substanzas': [{'__childtag__': '{foo}foo2',
|
2012-06-05 23:54:26 +00:00
|
|
|
'lang': '',
|
2011-12-12 01:04:58 +00:00
|
|
|
'bar': '',
|
|
|
|
'baz': 'b'},
|
|
|
|
{'__childtag__': '{foo}subfoo',
|
2012-06-05 23:54:26 +00:00
|
|
|
'lang': '',
|
2010-08-14 00:05:24 +00:00
|
|
|
'bar': 'c',
|
|
|
|
'baz': ''}]}
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(values == expected,
|
2010-08-14 00:05:24 +00:00
|
|
|
"Unexpected stanza values:\n%s\n%s" % (str(expected), str(values)))
|
|
|
|
|
|
|
|
|
|
|
|
def testSetStanzaValues(self):
|
2014-09-21 16:51:06 +00:00
|
|
|
"""Test using set_stanza_values with substanzas and plugins."""
|
2010-08-14 00:05:24 +00:00
|
|
|
|
|
|
|
class TestStanzaPlugin(ElementBase):
|
|
|
|
name = "pluginfoo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-14 00:05:24 +00:00
|
|
|
plugin_attrib = "plugin_foo"
|
|
|
|
|
|
|
|
class TestStanzaPlugin2(ElementBase):
|
|
|
|
name = "pluginfoo2"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-14 00:05:24 +00:00
|
|
|
plugin_attrib = "plugin_foo2"
|
|
|
|
|
|
|
|
class TestSubStanza(ElementBase):
|
|
|
|
name = "subfoo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-14 00:05:24 +00:00
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-14 00:05:24 +00:00
|
|
|
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestSubStanza, iterable=True)
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestStanzaPlugin)
|
|
|
|
register_stanza_plugin(TestStanza, TestStanzaPlugin2)
|
2010-08-14 00:05:24 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
values = {'bar': 'a',
|
|
|
|
'baz': '',
|
|
|
|
'plugin_foo': {'bar': '',
|
|
|
|
'baz': 'b'},
|
|
|
|
'plugin_foo2': {'bar': 'd',
|
|
|
|
'baz': 'e'},
|
|
|
|
'substanzas': [{'__childtag__': '{foo}subfoo',
|
|
|
|
'bar': 'c',
|
|
|
|
'baz': ''}]}
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
stanza.values = values
|
2010-08-14 00:05:24 +00:00
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-14 00:05:24 +00:00
|
|
|
<foo xmlns="foo" bar="a">
|
|
|
|
<pluginfoo baz="b" />
|
|
|
|
<pluginfoo2 bar="d" baz="e" />
|
|
|
|
<subfoo bar="c" />
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
2010-08-14 01:33:11 +00:00
|
|
|
def testGetItem(self):
|
|
|
|
"""Test accessing stanza interfaces."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz', 'qux'}
|
2016-10-22 12:20:10 +00:00
|
|
|
sub_interfaces = {'baz'}
|
2010-08-14 01:33:11 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def get_qux(self):
|
2010-08-19 18:21:58 +00:00
|
|
|
return 'qux'
|
|
|
|
|
2010-08-14 01:33:11 +00:00
|
|
|
class TestStanzaPlugin(ElementBase):
|
|
|
|
name = "foobar"
|
|
|
|
namespace = "foo"
|
|
|
|
plugin_attrib = "foobar"
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {'fizz'}
|
2010-08-14 01:33:11 +00:00
|
|
|
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestStanza, iterable=True)
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestStanzaPlugin)
|
2010-08-14 01:33:11 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
substanza = TestStanza()
|
|
|
|
stanza.append(substanza)
|
2014-09-21 16:51:06 +00:00
|
|
|
stanza.set_stanza_values({'bar': 'a',
|
|
|
|
'baz': 'b',
|
|
|
|
'qux': 42,
|
|
|
|
'foobar': {'fizz': 'c'}})
|
2010-08-14 01:33:11 +00:00
|
|
|
|
|
|
|
# Test non-plugin interfaces
|
|
|
|
expected = {'substanzas': [substanza],
|
|
|
|
'bar': 'a',
|
|
|
|
'baz': 'b',
|
2010-08-19 18:21:58 +00:00
|
|
|
'qux': 'qux',
|
2010-08-14 01:33:11 +00:00
|
|
|
'meh': ''}
|
|
|
|
for interface, value in expected.items():
|
|
|
|
result = stanza[interface]
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(result == value,
|
2010-08-14 01:33:11 +00:00
|
|
|
"Incorrect stanza interface access result: %s" % result)
|
|
|
|
|
|
|
|
# Test plugin interfaces
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(isinstance(stanza['foobar'], TestStanzaPlugin),
|
2010-08-14 01:33:11 +00:00
|
|
|
"Incorrect plugin object result.")
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza['foobar']['fizz'] == 'c',
|
2010-08-14 01:33:11 +00:00
|
|
|
"Incorrect plugin subvalue result.")
|
|
|
|
|
2010-08-19 18:21:58 +00:00
|
|
|
def testSetItem(self):
|
|
|
|
"""Test assigning to stanza interfaces."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz', 'qux'}
|
2016-10-22 12:20:10 +00:00
|
|
|
sub_interfaces = {'baz'}
|
2010-08-19 18:21:58 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def set_qux(self, value):
|
2010-08-19 18:21:58 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
class TestStanzaPlugin(ElementBase):
|
|
|
|
name = "foobar"
|
|
|
|
namespace = "foo"
|
|
|
|
plugin_attrib = "foobar"
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {'foobar'}
|
2010-08-19 18:21:58 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestStanzaPlugin)
|
2010-08-19 18:21:58 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
|
|
|
|
stanza['bar'] = 'attribute!'
|
|
|
|
stanza['baz'] = 'element!'
|
|
|
|
stanza['qux'] = 'overridden'
|
|
|
|
stanza['foobar'] = 'plugin'
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-19 18:21:58 +00:00
|
|
|
<foo xmlns="foo" bar="attribute!">
|
|
|
|
<baz>element!</baz>
|
|
|
|
<foobar foobar="plugin" />
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
2010-08-19 23:11:12 +00:00
|
|
|
def testDelItem(self):
|
|
|
|
"""Test deleting stanza interface values."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz', 'qux'}
|
2016-10-22 12:20:10 +00:00
|
|
|
sub_interfaces = {'bar'}
|
2010-08-19 23:11:12 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def del_qux(self):
|
2010-08-19 23:11:12 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
class TestStanzaPlugin(ElementBase):
|
|
|
|
name = "foobar"
|
|
|
|
namespace = "foo"
|
|
|
|
plugin_attrib = "foobar"
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {'foobar'}
|
2010-08-19 23:11:12 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestStanzaPlugin)
|
2010-08-19 23:11:12 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['bar'] = 'a'
|
|
|
|
stanza['baz'] = 'b'
|
|
|
|
stanza['qux'] = 'c'
|
|
|
|
stanza['foobar']['foobar'] = 'd'
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-19 23:11:12 +00:00
|
|
|
<foo xmlns="foo" baz="b" qux="c">
|
|
|
|
<bar>a</bar>
|
|
|
|
<foobar foobar="d" />
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
del stanza['bar']
|
|
|
|
del stanza['baz']
|
|
|
|
del stanza['qux']
|
|
|
|
del stanza['foobar']
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-19 23:11:12 +00:00
|
|
|
<foo xmlns="foo" qux="c" />
|
|
|
|
""")
|
2010-08-13 16:24:47 +00:00
|
|
|
|
2010-08-20 00:41:26 +00:00
|
|
|
def testModifyingAttributes(self):
|
|
|
|
"""Test modifying top level attributes of a stanza's XML object."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-20 00:41:26 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-20 00:41:26 +00:00
|
|
|
<foo xmlns="foo" />
|
|
|
|
""")
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza._get_attr('bar') == '',
|
2010-08-20 00:41:26 +00:00
|
|
|
"Incorrect value returned for an unset XML attribute.")
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
stanza._set_attr('bar', 'a')
|
|
|
|
stanza._set_attr('baz', 'b')
|
2010-08-20 00:41:26 +00:00
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-20 00:41:26 +00:00
|
|
|
<foo xmlns="foo" bar="a" baz="b" />
|
|
|
|
""")
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza._get_attr('bar') == 'a',
|
2010-08-20 00:41:26 +00:00
|
|
|
"Retrieved XML attribute value is incorrect.")
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
stanza._set_attr('bar', None)
|
|
|
|
stanza._del_attr('baz')
|
2010-08-20 00:41:26 +00:00
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-20 00:41:26 +00:00
|
|
|
<foo xmlns="foo" />
|
|
|
|
""")
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza._get_attr('bar', 'c') == 'c',
|
2010-08-20 00:41:26 +00:00
|
|
|
"Incorrect default value returned for an unset XML attribute.")
|
2010-08-26 17:49:36 +00:00
|
|
|
|
2010-08-24 12:55:37 +00:00
|
|
|
def testGetSubText(self):
|
|
|
|
"""Test retrieving the contents of a sub element."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {'bar'}
|
2010-08-24 12:55:37 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def set_bar(self, value):
|
2010-08-24 12:55:37 +00:00
|
|
|
wrapper = ET.Element("{foo}wrapper")
|
|
|
|
bar = ET.Element("{foo}bar")
|
|
|
|
bar.text = value
|
|
|
|
wrapper.append(bar)
|
|
|
|
self.xml.append(wrapper)
|
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def get_bar(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_sub_text("wrapper/bar", default="not found")
|
2010-08-24 12:55:37 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza['bar'] == 'not found',
|
2010-10-18 01:38:22 +00:00
|
|
|
"Default _get_sub_text value incorrect.")
|
2010-08-24 12:55:37 +00:00
|
|
|
|
|
|
|
stanza['bar'] = 'found'
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-24 12:55:37 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<wrapper>
|
|
|
|
<bar>found</bar>
|
|
|
|
</wrapper>
|
|
|
|
</foo>
|
|
|
|
""")
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza['bar'] == 'found',
|
2010-10-18 01:38:22 +00:00
|
|
|
"_get_sub_text value incorrect: %s." % stanza['bar'])
|
2010-08-24 12:55:37 +00:00
|
|
|
|
2010-08-24 13:37:42 +00:00
|
|
|
def testSubElement(self):
|
|
|
|
"""Test setting the contents of a sub element."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-24 13:37:42 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def set_baz(self, value):
|
2010-10-18 01:38:22 +00:00
|
|
|
self._set_sub_text("wrapper/baz", text=value)
|
2010-08-24 13:37:42 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def get_baz(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_sub_text("wrapper/baz")
|
2010-08-24 13:37:42 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def set_bar(self, value):
|
2010-10-18 01:38:22 +00:00
|
|
|
self._set_sub_text("wrapper/bar", text=value)
|
2010-08-24 13:37:42 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def get_bar(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_sub_text("wrapper/bar")
|
2010-08-24 13:37:42 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['bar'] = 'a'
|
|
|
|
stanza['baz'] = 'b'
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-24 13:37:42 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<wrapper>
|
|
|
|
<bar>a</bar>
|
|
|
|
<baz>b</baz>
|
|
|
|
</wrapper>
|
|
|
|
</foo>
|
|
|
|
""")
|
2010-10-18 01:38:22 +00:00
|
|
|
stanza._set_sub_text('wrapper/bar', text='', keep=True)
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-24 13:37:42 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<wrapper>
|
|
|
|
<bar />
|
|
|
|
<baz>b</baz>
|
|
|
|
</wrapper>
|
|
|
|
</foo>
|
|
|
|
""", use_values=False)
|
|
|
|
|
|
|
|
stanza['bar'] = 'a'
|
2010-10-18 01:38:22 +00:00
|
|
|
stanza._set_sub_text('wrapper/bar', text='')
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-24 13:37:42 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<wrapper>
|
|
|
|
<baz>b</baz>
|
|
|
|
</wrapper>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
2010-08-25 14:52:07 +00:00
|
|
|
def testDelSub(self):
|
|
|
|
"""Test removing sub elements."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-25 14:52:07 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def set_bar(self, value):
|
2014-08-17 22:52:24 +00:00
|
|
|
self._set_sub_text("path/to/only/bar", value)
|
2010-08-25 14:52:07 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def get_bar(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_sub_text("path/to/only/bar")
|
2010-08-25 14:52:07 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def del_bar(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
self._del_sub("path/to/only/bar")
|
2010-08-25 14:52:07 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def set_baz(self, value):
|
2014-08-17 22:52:24 +00:00
|
|
|
self._set_sub_text("path/to/just/baz", value)
|
2010-08-25 14:52:07 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def get_baz(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_sub_text("path/to/just/baz")
|
2010-08-25 14:52:07 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def del_baz(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
self._del_sub("path/to/just/baz")
|
2010-08-25 14:52:07 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['bar'] = 'a'
|
|
|
|
stanza['baz'] = 'b'
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-25 14:52:07 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<path>
|
|
|
|
<to>
|
|
|
|
<only>
|
|
|
|
<bar>a</bar>
|
|
|
|
</only>
|
|
|
|
<just>
|
|
|
|
<baz>b</baz>
|
|
|
|
</just>
|
|
|
|
</to>
|
|
|
|
</path>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
del stanza['bar']
|
|
|
|
del stanza['baz']
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-25 14:52:07 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<path>
|
|
|
|
<to>
|
|
|
|
<only />
|
|
|
|
<just />
|
|
|
|
</to>
|
|
|
|
</path>
|
|
|
|
</foo>
|
|
|
|
""", use_values=False)
|
|
|
|
|
|
|
|
stanza['bar'] = 'a'
|
|
|
|
stanza['baz'] = 'b'
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
stanza._del_sub('path/to/only/bar', all=True)
|
2010-08-25 14:52:07 +00:00
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-25 14:52:07 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<path>
|
|
|
|
<to>
|
|
|
|
<just>
|
|
|
|
<baz>b</baz>
|
|
|
|
</just>
|
|
|
|
</to>
|
|
|
|
</path>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
2010-08-25 18:40:16 +00:00
|
|
|
def testMatch(self):
|
|
|
|
"""Test matching a stanza against an XPath expression."""
|
|
|
|
|
|
|
|
class TestSubStanza(ElementBase):
|
|
|
|
name = "sub"
|
2021-04-19 17:42:32 +00:00
|
|
|
plugin_attrib = name
|
2010-08-25 18:54:09 +00:00
|
|
|
namespace = "baz"
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {'attrib'}
|
2010-08-25 18:40:16 +00:00
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar','baz', 'qux'}
|
2016-10-22 12:20:10 +00:00
|
|
|
sub_interfaces = {'qux'}
|
2010-08-25 18:40:16 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def set_qux(self, value):
|
2010-10-18 01:38:22 +00:00
|
|
|
self._set_sub_text('qux', text=value)
|
2010-08-30 21:12:10 +00:00
|
|
|
|
2016-10-04 19:15:01 +00:00
|
|
|
def get_qux(self):
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_sub_text('qux')
|
2010-08-30 21:12:10 +00:00
|
|
|
|
2010-08-25 18:40:16 +00:00
|
|
|
class TestStanzaPlugin(ElementBase):
|
|
|
|
name = "plugin"
|
2010-08-30 18:55:30 +00:00
|
|
|
namespace = "http://test/slash/bar"
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {'attrib'}
|
2010-08-25 18:40:16 +00:00
|
|
|
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestSubStanza, iterable=True)
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestStanzaPlugin)
|
2010-08-25 18:40:16 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo"),
|
2010-08-25 18:40:16 +00:00
|
|
|
"Stanza did not match its own tag name.")
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("{foo}foo"),
|
2010-08-25 18:54:09 +00:00
|
|
|
"Stanza did not match its own namespaced name.")
|
|
|
|
|
2010-08-25 18:40:16 +00:00
|
|
|
stanza['bar'] = 'a'
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo@bar=a"),
|
2010-08-25 18:40:16 +00:00
|
|
|
"Stanza did not match its own name with attribute value check.")
|
|
|
|
|
|
|
|
stanza['baz'] = 'b'
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo@bar=a@baz=b"),
|
2010-08-25 18:40:16 +00:00
|
|
|
"Stanza did not match its own name with multiple attributes.")
|
|
|
|
|
2010-08-30 21:12:10 +00:00
|
|
|
stanza['qux'] = 'c'
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo/qux"),
|
2010-08-30 21:12:10 +00:00
|
|
|
"Stanza did not match with subelements.")
|
|
|
|
|
|
|
|
stanza['qux'] = ''
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo/qux") == False,
|
2010-08-30 21:12:10 +00:00
|
|
|
"Stanza matched missing subinterface element.")
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo/bar") == False,
|
2010-08-30 21:12:10 +00:00
|
|
|
"Stanza matched nonexistent element.")
|
|
|
|
|
2010-08-25 18:40:16 +00:00
|
|
|
stanza['plugin']['attrib'] = 'c'
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo/plugin@attrib=c"),
|
2010-08-25 18:40:16 +00:00
|
|
|
"Stanza did not match with plugin and attribute.")
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo/{http://test/slash/bar}plugin"),
|
2010-08-25 18:54:09 +00:00
|
|
|
"Stanza did not match with namespaced plugin.")
|
|
|
|
|
2010-08-25 18:40:16 +00:00
|
|
|
substanza = TestSubStanza()
|
|
|
|
substanza['attrib'] = 'd'
|
|
|
|
stanza.append(substanza)
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo/sub@attrib=d"),
|
2010-08-25 18:40:16 +00:00
|
|
|
"Stanza did not match with substanzas and attribute.")
|
2010-08-25 18:54:09 +00:00
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.match("foo/{baz}sub"),
|
2010-08-25 18:54:09 +00:00
|
|
|
"Stanza did not match with namespaced substanza.")
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
def testComparisons(self):
|
|
|
|
"""Test comparing ElementBase objects."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
stanza1 = TestStanza()
|
|
|
|
stanza1['bar'] = 'a'
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza1,
|
2010-08-26 17:49:36 +00:00
|
|
|
"Stanza object does not evaluate to True")
|
|
|
|
|
|
|
|
stanza2 = TestStanza()
|
|
|
|
stanza2['baz'] = 'b'
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza1 != stanza2,
|
2010-08-26 17:49:36 +00:00
|
|
|
"Different stanza objects incorrectly compared equal.")
|
|
|
|
|
|
|
|
stanza1['baz'] = 'b'
|
|
|
|
stanza2['bar'] = 'a'
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza1 == stanza2,
|
2010-08-26 17:49:36 +00:00
|
|
|
"Equal stanzas incorrectly compared inequal.")
|
|
|
|
|
|
|
|
def testKeys(self):
|
|
|
|
"""Test extracting interface names from a stanza object."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-26 17:49:36 +00:00
|
|
|
plugin_attrib = 'qux'
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestStanza)
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(set(stanza.keys()) == {'lang', 'bar', 'baz'},
|
2010-08-26 17:49:36 +00:00
|
|
|
"Returned set of interface keys does not match expected.")
|
|
|
|
|
|
|
|
stanza.enable('qux')
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(set(stanza.keys()) == {'lang', 'bar', 'baz', 'qux'},
|
2010-08-26 17:49:36 +00:00
|
|
|
"Incorrect set of interface and plugin keys.")
|
|
|
|
|
|
|
|
def testGet(self):
|
|
|
|
"""Test accessing stanza interfaces using get()."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['bar'] = 'a'
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.get('bar') == 'a',
|
2010-08-26 17:49:36 +00:00
|
|
|
"Incorrect value returned by stanza.get")
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza.get('baz', 'b') == 'b',
|
2010-08-26 17:49:36 +00:00
|
|
|
"Incorrect default value returned by stanza.get")
|
|
|
|
|
|
|
|
def testSubStanzas(self):
|
|
|
|
"""Test manipulating substanzas of a stanza object."""
|
|
|
|
|
|
|
|
class TestSubStanza(ElementBase):
|
|
|
|
name = "foobar"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {'qux'}
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
|
|
|
|
register_stanza_plugin(TestStanza, TestSubStanza, iterable=True)
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
substanza1 = TestSubStanza()
|
|
|
|
substanza2 = TestSubStanza()
|
|
|
|
substanza1['qux'] = 'a'
|
|
|
|
substanza2['qux'] = 'b'
|
|
|
|
|
|
|
|
# Test appending substanzas
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(len(stanza) == 0,
|
2010-08-26 17:49:36 +00:00
|
|
|
"Incorrect empty stanza size.")
|
|
|
|
|
|
|
|
stanza.append(substanza1)
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-26 17:49:36 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<foobar qux="a" />
|
|
|
|
</foo>
|
2010-10-07 23:42:28 +00:00
|
|
|
""", use_values=False)
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(len(stanza) == 1,
|
2010-08-26 17:49:36 +00:00
|
|
|
"Incorrect stanza size with 1 substanza.")
|
|
|
|
|
|
|
|
stanza.append(substanza2)
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-26 17:49:36 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<foobar qux="a" />
|
|
|
|
<foobar qux="b" />
|
|
|
|
</foo>
|
2010-10-07 23:42:28 +00:00
|
|
|
""", use_values=False)
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(len(stanza) == 2,
|
2010-08-26 17:49:36 +00:00
|
|
|
"Incorrect stanza size with 2 substanzas.")
|
|
|
|
|
|
|
|
# Test popping substanzas
|
|
|
|
stanza.pop(0)
|
2010-11-05 18:45:58 +00:00
|
|
|
self.check(stanza, """
|
2010-08-26 17:49:36 +00:00
|
|
|
<foo xmlns="foo">
|
|
|
|
<foobar qux="b" />
|
|
|
|
</foo>
|
2010-10-07 23:42:28 +00:00
|
|
|
""", use_values=False)
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
# Test iterating over substanzas
|
|
|
|
stanza.append(substanza1)
|
|
|
|
results = []
|
|
|
|
for substanza in stanza:
|
|
|
|
results.append(substanza['qux'])
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(results == ['b', 'a'],
|
2010-08-26 17:49:36 +00:00
|
|
|
"Iteration over substanzas failed: %s." % str(results))
|
|
|
|
|
|
|
|
def testCopy(self):
|
|
|
|
"""Test copying stanza objects."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
stanza1 = TestStanza()
|
|
|
|
stanza1['bar'] = 'a'
|
|
|
|
|
|
|
|
stanza2 = stanza1.__copy__()
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza1 == stanza2,
|
2010-08-26 17:49:36 +00:00
|
|
|
"Copied stanzas are not equal to each other.")
|
|
|
|
|
|
|
|
stanza1['baz'] = 'b'
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza1 != stanza2,
|
2010-08-26 17:49:36 +00:00
|
|
|
"Divergent stanza copies incorrectly compared equal.")
|
2010-08-20 00:41:26 +00:00
|
|
|
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
def testExtension(self):
|
|
|
|
"""Testing using is_extension."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
|
|
|
|
class TestExtension(ElementBase):
|
|
|
|
name = 'extended'
|
|
|
|
namespace = 'foo'
|
|
|
|
plugin_attrib = name
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {name}
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
is_extension = True
|
|
|
|
|
|
|
|
def set_extended(self, value):
|
|
|
|
self.xml.text = value
|
|
|
|
|
|
|
|
def get_extended(self):
|
|
|
|
return self.xml.text
|
|
|
|
|
|
|
|
def del_extended(self):
|
|
|
|
self.parent().xml.remove(self.xml)
|
|
|
|
|
|
|
|
register_stanza_plugin(TestStanza, TestExtension)
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['extended'] = 'testing'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo">
|
|
|
|
<extended>testing</extended>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza['extended'] == 'testing',
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
"Could not retrieve stanza extension value.")
|
|
|
|
|
|
|
|
del stanza['extended']
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
def testOverrides(self):
|
|
|
|
"""Test using interface overrides."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:20:27 +00:00
|
|
|
interfaces = {'bar', 'baz'}
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
|
|
|
|
class TestOverride(ElementBase):
|
|
|
|
name = 'overrider'
|
|
|
|
namespace = 'foo'
|
|
|
|
plugin_attrib = name
|
2016-10-22 12:20:10 +00:00
|
|
|
interfaces = {'bar'}
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
overrides = ['set_bar']
|
|
|
|
|
|
|
|
def setup(self, xml):
|
|
|
|
# Don't create XML for the plugin
|
|
|
|
self.xml = ET.Element('')
|
|
|
|
|
|
|
|
def set_bar(self, value):
|
|
|
|
if not value.startswith('override-'):
|
|
|
|
self.parent()._set_attr('bar', 'override-%s' % value)
|
|
|
|
else:
|
|
|
|
self.parent()._set_attr('bar', value)
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['bar'] = 'foo'
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo" bar="foo" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
register_stanza_plugin(TestStanza, TestOverride, overrides=True)
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['bar'] = 'foo'
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo" bar="override-foo" />
|
|
|
|
""")
|
|
|
|
|
2012-03-28 04:16:53 +00:00
|
|
|
def testBoolInterfaces(self):
|
|
|
|
"""Test using boolean interfaces."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = "foo"
|
|
|
|
namespace = "foo"
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'bar'}
|
2012-03-28 04:16:53 +00:00
|
|
|
bool_interfaces = interfaces
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo" />
|
|
|
|
""")
|
|
|
|
|
2012-06-23 01:19:17 +00:00
|
|
|
self.assertFalse(stanza['bar'],
|
2012-03-28 04:16:53 +00:00
|
|
|
"Returned True for missing bool interface element.")
|
|
|
|
|
|
|
|
stanza['bar'] = True
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo">
|
|
|
|
<bar />
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertTrue(stanza['bar'],
|
|
|
|
"Returned False for present bool interface element.")
|
|
|
|
|
|
|
|
stanza['bar'] = False
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo" />
|
|
|
|
""")
|
|
|
|
|
2012-05-05 21:01:13 +00:00
|
|
|
def testGetMultiAttrib(self):
|
|
|
|
"""Test retrieving multi_attrib substanzas."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'foo'
|
|
|
|
interfaces = set()
|
|
|
|
|
|
|
|
class TestMultiStanza1(ElementBase):
|
|
|
|
name = 'bar'
|
|
|
|
namespace = 'bar'
|
|
|
|
plugin_attrib = name
|
|
|
|
plugin_multi_attrib = 'bars'
|
|
|
|
|
|
|
|
class TestMultiStanza2(ElementBase):
|
|
|
|
name = 'baz'
|
|
|
|
namespace = 'baz'
|
|
|
|
plugin_attrib = name
|
|
|
|
plugin_multi_attrib = 'bazs'
|
2012-06-23 01:19:17 +00:00
|
|
|
|
2012-05-05 21:01:13 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True)
|
|
|
|
register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True)
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza.append(TestMultiStanza1())
|
|
|
|
stanza.append(TestMultiStanza2())
|
|
|
|
stanza.append(TestMultiStanza1())
|
|
|
|
stanza.append(TestMultiStanza2())
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo">
|
|
|
|
<bar xmlns="bar" />
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
<bar xmlns="bar" />
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
</foo>
|
|
|
|
""", use_values=False)
|
|
|
|
|
|
|
|
bars = stanza['bars']
|
|
|
|
bazs = stanza['bazs']
|
|
|
|
|
|
|
|
for bar in bars:
|
|
|
|
self.check(bar, """
|
|
|
|
<bar xmlns="bar" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
for baz in bazs:
|
|
|
|
self.check(baz, """
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
""")
|
|
|
|
|
2012-06-23 01:19:17 +00:00
|
|
|
self.assertEqual(len(bars), 2,
|
2012-05-05 21:01:13 +00:00
|
|
|
"Wrong number of <bar /> stanzas: %s" % len(bars))
|
2012-06-23 01:19:17 +00:00
|
|
|
self.assertEqual(len(bazs), 2,
|
2012-05-05 21:01:13 +00:00
|
|
|
"Wrong number of <baz /> stanzas: %s" % len(bazs))
|
|
|
|
|
|
|
|
def testSetMultiAttrib(self):
|
|
|
|
"""Test setting multi_attrib substanzas."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'foo'
|
|
|
|
interfaces = set()
|
|
|
|
|
|
|
|
class TestMultiStanza1(ElementBase):
|
|
|
|
name = 'bar'
|
|
|
|
namespace = 'bar'
|
|
|
|
plugin_attrib = name
|
|
|
|
plugin_multi_attrib = 'bars'
|
|
|
|
|
|
|
|
class TestMultiStanza2(ElementBase):
|
|
|
|
name = 'baz'
|
|
|
|
namespace = 'baz'
|
|
|
|
plugin_attrib = name
|
|
|
|
plugin_multi_attrib = 'bazs'
|
2012-06-23 01:19:17 +00:00
|
|
|
|
2012-05-05 21:01:13 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True)
|
|
|
|
register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True)
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['bars'] = [TestMultiStanza1(), TestMultiStanza1()]
|
|
|
|
stanza['bazs'] = [TestMultiStanza2(), TestMultiStanza2()]
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo">
|
|
|
|
<bar xmlns="bar" />
|
|
|
|
<bar xmlns="bar" />
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
</foo>
|
|
|
|
""", use_values=False)
|
|
|
|
|
|
|
|
self.assertEqual(len(stanza['substanzas']), 4,
|
|
|
|
"Wrong number of substanzas: %s" % len(stanza['substanzas']))
|
|
|
|
|
|
|
|
stanza['bars'] = [TestMultiStanza1()]
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo">
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
<bar xmlns="bar" />
|
|
|
|
</foo>
|
|
|
|
""", use_values=False)
|
|
|
|
|
|
|
|
self.assertEqual(len(stanza['substanzas']), 3,
|
|
|
|
"Wrong number of substanzas: %s" % len(stanza['substanzas']))
|
|
|
|
|
|
|
|
|
|
|
|
def testDelMultiAttrib(self):
|
|
|
|
"""Test deleting multi_attrib substanzas."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'foo'
|
|
|
|
interfaces = set()
|
|
|
|
|
|
|
|
class TestMultiStanza1(ElementBase):
|
|
|
|
name = 'bar'
|
|
|
|
namespace = 'bar'
|
|
|
|
plugin_attrib = name
|
|
|
|
plugin_multi_attrib = 'bars'
|
|
|
|
|
|
|
|
class TestMultiStanza2(ElementBase):
|
|
|
|
name = 'baz'
|
|
|
|
namespace = 'baz'
|
|
|
|
plugin_attrib = name
|
|
|
|
plugin_multi_attrib = 'bazs'
|
2012-06-23 01:19:17 +00:00
|
|
|
|
2012-05-05 21:01:13 +00:00
|
|
|
register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True)
|
|
|
|
register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True)
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
bars = [TestMultiStanza1(), TestMultiStanza1()]
|
|
|
|
bazs = [TestMultiStanza2(), TestMultiStanza2()]
|
|
|
|
|
|
|
|
stanza['bars'] = bars
|
|
|
|
stanza['bazs'] = bazs
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo">
|
|
|
|
<bar xmlns="bar" />
|
|
|
|
<bar xmlns="bar" />
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
</foo>
|
|
|
|
""", use_values=False)
|
|
|
|
|
|
|
|
del stanza['bars']
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="foo">
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
<baz xmlns="baz" />
|
|
|
|
</foo>
|
|
|
|
""", use_values=False)
|
|
|
|
|
|
|
|
self.assertEqual(len(stanza['substanzas']), 2,
|
|
|
|
"Wrong number of substanzas: %s" % len(stanza['substanzas']))
|
|
|
|
|
2012-06-23 01:19:17 +00:00
|
|
|
def testDefaultLang(self):
|
|
|
|
"""Test setting a normal subinterface when a default language is set"""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'test'
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'test'}
|
2012-06-23 01:19:17 +00:00
|
|
|
sub_interfaces = interfaces
|
|
|
|
lang_interfaces = interfaces
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['lang'] = 'sv'
|
|
|
|
stanza['test'] = 'hej'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" xml:lang="sv">
|
|
|
|
<test>hej</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test'], 'hej',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|sv'], 'hej',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|sv'])
|
|
|
|
|
|
|
|
def testSpecifyLangWithDefault(self):
|
|
|
|
"""Test specifying various languages."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'test'
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'test'}
|
2012-06-23 01:19:17 +00:00
|
|
|
sub_interfaces = interfaces
|
|
|
|
lang_interfaces = interfaces
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['lang'] = 'sv'
|
|
|
|
stanza['test'] = 'hej'
|
|
|
|
stanza['test|en'] = 'hi'
|
|
|
|
stanza['test|es'] = 'hola'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" xml:lang="sv">
|
|
|
|
<test>hej</test>
|
|
|
|
<test xml:lang="en">hi</test>
|
|
|
|
<test xml:lang="es">hola</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test'], 'hej',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|sv'], 'hej',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|sv'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|en'], 'hi',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|en'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|es'], 'hola',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|es'])
|
|
|
|
|
|
|
|
def testSpecifyLangWithNoDefault(self):
|
|
|
|
"""Test specifying various languages."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'test'
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'test'}
|
2012-06-23 01:19:17 +00:00
|
|
|
sub_interfaces = interfaces
|
|
|
|
lang_interfaces = interfaces
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['test'] = 'hej'
|
|
|
|
stanza['test|en'] = 'hi'
|
|
|
|
stanza['test|es'] = 'hola'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test">
|
|
|
|
<test>hej</test>
|
|
|
|
<test xml:lang="en">hi</test>
|
|
|
|
<test xml:lang="es">hola</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test'], 'hej',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|en'], 'hi',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|en'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|es'], 'hola',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|es'])
|
|
|
|
|
|
|
|
def testModifyLangInterfaceWithDefault(self):
|
|
|
|
"""Test resetting an interface when a default lang is used."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'test'
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'test'}
|
2012-06-23 01:19:17 +00:00
|
|
|
sub_interfaces = interfaces
|
|
|
|
lang_interfaces = interfaces
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['lang'] = 'es'
|
|
|
|
stanza['test'] = 'hola'
|
|
|
|
stanza['test|en'] = 'hi'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" xml:lang="es">
|
|
|
|
<test>hola</test>
|
|
|
|
<test xml:lang="en">hi</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
stanza['test'] = 'adios'
|
|
|
|
stanza['test|en'] = 'bye'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" xml:lang="es">
|
|
|
|
<test>adios</test>
|
|
|
|
<test xml:lang="en">bye</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test'], 'adios',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|es'], 'adios',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|es'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|en'], 'bye',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|en'])
|
|
|
|
|
|
|
|
stanza['test|es'] = 'hola'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" xml:lang="es">
|
|
|
|
<test>hola</test>
|
|
|
|
<test xml:lang="en">bye</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test'], 'hola',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|es'], 'hola',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|es'])
|
|
|
|
|
|
|
|
def testModifyLangInterfaceWithNoDefault(self):
|
|
|
|
"""Test resetting an interface when no default lang is used."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'test'
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'test'}
|
2012-06-23 01:19:17 +00:00
|
|
|
sub_interfaces = interfaces
|
|
|
|
lang_interfaces = interfaces
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['test'] = 'hola'
|
|
|
|
stanza['test|en'] = 'hi'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test">
|
|
|
|
<test>hola</test>
|
|
|
|
<test xml:lang="en">hi</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
stanza['test'] = 'adios'
|
|
|
|
stanza['test|en'] = 'bye'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test">
|
|
|
|
<test>adios</test>
|
|
|
|
<test xml:lang="en">bye</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test'], 'adios',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test'], 'adios',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|es'])
|
|
|
|
|
|
|
|
self.assertEqual(stanza['test|en'], 'bye',
|
|
|
|
"Incorrect subinterface value: %s" % stanza['test|en'])
|
|
|
|
|
|
|
|
def testDelInterfacesWithDefaultLang(self):
|
|
|
|
"""Test deleting interfaces with a default lang set."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'test'
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'test'}
|
2012-06-23 01:19:17 +00:00
|
|
|
sub_interfaces = interfaces
|
|
|
|
lang_interfaces = interfaces
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['lang'] = 'en'
|
|
|
|
stanza['test'] = 'hi'
|
|
|
|
stanza['test|no'] = 'hej'
|
|
|
|
stanza['test|fr'] = 'bonjour'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" xml:lang="en">
|
|
|
|
<test>hi</test>
|
|
|
|
<test xml:lang="no">hej</test>
|
|
|
|
<test xml:lang="fr">bonjour</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
del stanza['test']
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" xml:lang="en">
|
|
|
|
<test xml:lang="no">hej</test>
|
|
|
|
<test xml:lang="fr">bonjour</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
del stanza['test|no']
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" xml:lang="en">
|
|
|
|
<test xml:lang="fr">bonjour</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
def testDelInterfacesWithNoDefaultLang(self):
|
|
|
|
"""Test deleting interfaces with no default lang set."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'test'
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'test'}
|
2012-06-23 01:19:17 +00:00
|
|
|
sub_interfaces = interfaces
|
|
|
|
lang_interfaces = interfaces
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['test'] = 'hi'
|
|
|
|
stanza['test|no'] = 'hej'
|
|
|
|
stanza['test|fr'] = 'bonjour'
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test">
|
|
|
|
<test>hi</test>
|
|
|
|
<test xml:lang="no">hej</test>
|
|
|
|
<test xml:lang="fr">bonjour</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
del stanza['test']
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test">
|
|
|
|
<test xml:lang="no">hej</test>
|
|
|
|
<test xml:lang="fr">bonjour</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
del stanza['test|no']
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test">
|
|
|
|
<test xml:lang="fr">bonjour</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
def testStarLang(self):
|
|
|
|
"""Test using interface|*."""
|
|
|
|
|
|
|
|
class TestStanza(ElementBase):
|
|
|
|
name = 'foo'
|
|
|
|
namespace = 'test'
|
2016-10-22 12:21:06 +00:00
|
|
|
interfaces = {'test'}
|
2012-06-23 01:19:17 +00:00
|
|
|
sub_interfaces = interfaces
|
|
|
|
lang_interfaces = interfaces
|
|
|
|
|
2020-12-06 15:34:52 +00:00
|
|
|
data = {}
|
2012-06-23 01:19:17 +00:00
|
|
|
data['en'] = 'hi'
|
|
|
|
data['fr'] = 'bonjour'
|
|
|
|
data['no'] = 'hej'
|
|
|
|
|
|
|
|
stanza = TestStanza()
|
|
|
|
stanza['test|*'] = data
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test">
|
|
|
|
<test xml:lang="en">hi</test>
|
|
|
|
<test xml:lang="fr">bonjour</test>
|
|
|
|
<test xml:lang="no">hej</test>
|
|
|
|
</foo>
|
|
|
|
""")
|
|
|
|
|
|
|
|
data2 = stanza['test|*']
|
|
|
|
|
|
|
|
self.assertEqual(data, data2,
|
|
|
|
"Did not extract expected language data: %s" % data2)
|
|
|
|
|
|
|
|
del stanza['test|*']
|
|
|
|
|
|
|
|
self.check(stanza, """
|
|
|
|
<foo xmlns="test" />
|
|
|
|
""")
|
|
|
|
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
|
2010-08-13 16:24:47 +00:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestElementBase)
|