tests: added XML comparaison method to helpers
This commit is contained in:
parent
5289df976f
commit
96695b6d59
1 changed files with 48 additions and 1 deletions
|
|
@ -22,6 +22,9 @@ from twisted.words.protocols.jabber.jid import JID
|
|||
from wokkel.xmppim import RosterItem
|
||||
from sat.core.xmpp import SatRosterProtocol
|
||||
from twisted.trial.unittest import FailTest
|
||||
from twisted.trial import unittest
|
||||
from xml.etree import cElementTree as etree
|
||||
import re
|
||||
|
||||
TEST_JID_STR = u"test@example.org/SàT"
|
||||
TEST_JID = JID(u"test@example.org/SàT")
|
||||
|
|
@ -38,6 +41,8 @@ def b2s(value):
|
|||
class DifferentArgsException(FailTest):
|
||||
pass
|
||||
|
||||
class DifferentXMLException(FailTest):
|
||||
pass
|
||||
|
||||
class FakeSAT(object):
|
||||
"""Class to simulate a SAT instance"""
|
||||
|
|
@ -115,7 +120,7 @@ class FakeRosterProtocol(SatRosterProtocol):
|
|||
attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)}
|
||||
if roster_item.name:
|
||||
attrs['name'] = roster_item.name
|
||||
self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile)
|
||||
self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile)
|
||||
self.onRosterSet(roster_item)
|
||||
|
||||
|
||||
|
|
@ -130,6 +135,48 @@ class FakeClient(object):
|
|||
pass
|
||||
|
||||
|
||||
class SatTestCase(unittest.TestCase):
|
||||
|
||||
def assertEqualXML(self, xml, expected, ignore_blank = False):
|
||||
def equalElt(got_elt, exp_elt):
|
||||
if (got_elt.tag != exp_elt.tag):
|
||||
print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
|
||||
print "tag: got [%s] expected: [%s]" % (got_elt.tag, exp_elt.tag)
|
||||
return False
|
||||
if (got_elt.attrib != exp_elt.attrib):
|
||||
print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
|
||||
print "attribs: got %s expected %s" % (got_elt.attrib, exp_elt.attrib)
|
||||
return False
|
||||
if (got_elt.tail != exp_elt.tail or got_elt.text != exp_elt.text):
|
||||
print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
|
||||
print "text: got [%s] expected: [%s]" % (got_elt.text, exp_elt.text)
|
||||
print "tail: got [%s] expected: [%s]" % (got_elt.tail, exp_elt.tail)
|
||||
return False
|
||||
if (len(got_elt) != len(exp_elt)):
|
||||
print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
|
||||
print "children len: got %d expected: %d" % (len(got_elt), len(exp_elt))
|
||||
return False
|
||||
for idx, child in enumerate(got_elt):
|
||||
if not equalElt(child, exp_elt[idx]):
|
||||
return False
|
||||
return True
|
||||
|
||||
def remove_blank(xml):
|
||||
lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+',' ',xml).split('\n')]
|
||||
return '\n'.join([line for line in lines if line])
|
||||
|
||||
xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml)
|
||||
expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected)
|
||||
|
||||
if not equalElt(xml_elt, expected_elt):
|
||||
print "---"
|
||||
print "XML are not equals:"
|
||||
print "got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8')
|
||||
print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8')
|
||||
print "---"
|
||||
raise DifferentXMLException
|
||||
|
||||
|
||||
def _(text):
|
||||
return text
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue