From 100e504b7fdcb889f20abbb2b4843fb01e0d267c Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 22 Jun 2012 18:19:17 -0700 Subject: Resolve xml:lang issue with duplicated elements depending on ordering. --- tests/test_stanza_element.py | 321 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 315 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/test_stanza_element.py b/tests/test_stanza_element.py index 1b47e733..b7ccdb87 100644 --- a/tests/test_stanza_element.py +++ b/tests/test_stanza_element.py @@ -1,5 +1,6 @@ from sleekxmpp.test import * from sleekxmpp.xmlstream.stanzabase import ElementBase +from sleekxmpp.thirdparty import OrderedDict class TestElementBase(SleekTest): @@ -760,7 +761,7 @@ class TestElementBase(SleekTest): """) - self.assertFalse(stanza['bar'], + self.assertFalse(stanza['bar'], "Returned True for missing bool interface element.") stanza['bar'] = True @@ -797,7 +798,7 @@ class TestElementBase(SleekTest): namespace = 'baz' plugin_attrib = name plugin_multi_attrib = 'bazs' - + register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) @@ -829,9 +830,9 @@ class TestElementBase(SleekTest): """) - self.assertEqual(len(bars), 2, + self.assertEqual(len(bars), 2, "Wrong number of stanzas: %s" % len(bars)) - self.assertEqual(len(bazs), 2, + self.assertEqual(len(bazs), 2, "Wrong number of stanzas: %s" % len(bazs)) def testSetMultiAttrib(self): @@ -853,7 +854,7 @@ class TestElementBase(SleekTest): namespace = 'baz' plugin_attrib = name plugin_multi_attrib = 'bazs' - + register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) @@ -906,7 +907,7 @@ class TestElementBase(SleekTest): namespace = 'baz' plugin_attrib = name plugin_multi_attrib = 'bazs' - + register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) @@ -938,5 +939,313 @@ class TestElementBase(SleekTest): self.assertEqual(len(stanza['substanzas']), 2, "Wrong number of substanzas: %s" % len(stanza['substanzas'])) + def testDefaultLang(self): + """Test setting a normal subinterface when a default language is set""" + + class TestStanza(ElementBase): + name = 'foo' + namespace = 'test' + interfaces = set(['test']) + sub_interfaces = interfaces + lang_interfaces = interfaces + + stanza = TestStanza() + stanza['lang'] = 'sv' + stanza['test'] = 'hej' + + self.check(stanza, """ + + hej + + """) + + 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' + interfaces = set(['test']) + 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, """ + + hej + hi + hola + + """) + + 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' + interfaces = set(['test']) + sub_interfaces = interfaces + lang_interfaces = interfaces + + stanza = TestStanza() + stanza['test'] = 'hej' + stanza['test|en'] = 'hi' + stanza['test|es'] = 'hola' + + self.check(stanza, """ + + hej + hi + hola + + """) + + 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' + interfaces = set(['test']) + sub_interfaces = interfaces + lang_interfaces = interfaces + + stanza = TestStanza() + stanza['lang'] = 'es' + stanza['test'] = 'hola' + stanza['test|en'] = 'hi' + + self.check(stanza, """ + + hola + hi + + """) + + stanza['test'] = 'adios' + stanza['test|en'] = 'bye' + + self.check(stanza, """ + + adios + bye + + """) + + 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, """ + + hola + bye + + """) + + 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' + interfaces = set(['test']) + sub_interfaces = interfaces + lang_interfaces = interfaces + + stanza = TestStanza() + stanza['test'] = 'hola' + stanza['test|en'] = 'hi' + + self.check(stanza, """ + + hola + hi + + """) + + stanza['test'] = 'adios' + stanza['test|en'] = 'bye' + + self.check(stanza, """ + + adios + bye + + """) + + 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' + interfaces = set(['test']) + 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, """ + + hi + hej + bonjour + + """) + + del stanza['test'] + + self.check(stanza, """ + + hej + bonjour + + """) + + del stanza['test|no'] + + self.check(stanza, """ + + bonjour + + """) + + def testDelInterfacesWithNoDefaultLang(self): + """Test deleting interfaces with no default lang set.""" + + class TestStanza(ElementBase): + name = 'foo' + namespace = 'test' + interfaces = set(['test']) + sub_interfaces = interfaces + lang_interfaces = interfaces + + stanza = TestStanza() + stanza['test'] = 'hi' + stanza['test|no'] = 'hej' + stanza['test|fr'] = 'bonjour' + + self.check(stanza, """ + + hi + hej + bonjour + + """) + + del stanza['test'] + + self.check(stanza, """ + + hej + bonjour + + """) + + del stanza['test|no'] + + self.check(stanza, """ + + bonjour + + """) + + def testStarLang(self): + """Test using interface|*.""" + + class TestStanza(ElementBase): + name = 'foo' + namespace = 'test' + interfaces = set(['test']) + sub_interfaces = interfaces + lang_interfaces = interfaces + + data = OrderedDict() + data['en'] = 'hi' + data['fr'] = 'bonjour' + data['no'] = 'hej' + + stanza = TestStanza() + stanza['test|*'] = data + + self.check(stanza, """ + + hi + bonjour + hej + + """) + + data2 = stanza['test|*'] + + self.assertEqual(data, data2, + "Did not extract expected language data: %s" % data2) + + del stanza['test|*'] + + self.check(stanza, """ + + """) + suite = unittest.TestLoader().loadTestsFromTestCase(TestElementBase) -- cgit v1.2.3