diff src/test/helpers.py @ 693:65b30bc7f1b3

tests: added XML comparaison method to helpers
author Goffi <goffi@goffi.org>
date Tue, 12 Nov 2013 14:16:11 +0100
parents ffb716804580
children 4284b6ad8aa3
line wrap: on
line diff
--- a/src/test/helpers.py	Tue Nov 12 14:14:36 2013 +0100
+++ b/src/test/helpers.py	Tue Nov 12 14:16:11 2013 +0100
@@ -22,6 +22,9 @@
 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 @@
 class DifferentArgsException(FailTest):
     pass
 
+class DifferentXMLException(FailTest):
+    pass
 
 class FakeSAT(object):
     """Class to simulate a SAT instance"""
@@ -115,7 +120,7 @@
         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 @@
         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