annotate src/test/helpers.py @ 771:bfabeedbf32e

core: i18n refactoring: - _() is no more installed in __builtin__ - instead, there is a new sat.core.i18n module - added D_() method for deferred translation - languageSwitch method allow to dynamically change translation language - import gettext is tested against ImportError, and dummy methods are used when not available (mainly useful for Libervia)
author Goffi <goffi@goffi.org>
date Sun, 29 Dec 2013 17:06:01 +0100
parents f1a2831d549d
children 9810f22ba733
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
335
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
4 # SAT: a jabber client
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
335
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
10 # (at your option) any later version.
335
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
15 # GNU Affero General Public License for more details.
335
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
335
Goffi <goffi@goffi.org>
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 696
diff changeset
20 from sat.core.i18n import _
335
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import __builtin__
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber.jid import JID
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
23 from wokkel.xmppim import RosterItem
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
24 from sat.core.xmpp import SatRosterProtocol
647
63b6e684bbfa tests: Use of FailTest for DifferentArgsException instead of Exception, so the test fail instead of raising en error.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
25 from twisted.trial.unittest import FailTest
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
26 from twisted.trial import unittest
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
27 from xml.etree import cElementTree as etree
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
28 import re
335
Goffi <goffi@goffi.org>
parents:
diff changeset
29
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
30 TEST_JID_STR = u"test@example.org/SàT"
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
31 TEST_JID = JID(u"test@example.org/SàT")
335
Goffi <goffi@goffi.org>
parents:
diff changeset
32 TEST_PROFILE = 'test_profile'
Goffi <goffi@goffi.org>
parents:
diff changeset
33
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
34 def b2s(value):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
35 """Convert a bool to a unicode string used in bridge
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
36 @param value: boolean value
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
37 @return: unicode conversion, according to bridge convention
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
38
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
39 """
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
40 return u"True" if value else u"False"
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
41
647
63b6e684bbfa tests: Use of FailTest for DifferentArgsException instead of Exception, so the test fail instead of raising en error.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
42 class DifferentArgsException(FailTest):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
43 pass
Goffi <goffi@goffi.org>
parents:
diff changeset
44
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
45 class DifferentXMLException(FailTest):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
46 pass
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
47
335
Goffi <goffi@goffi.org>
parents:
diff changeset
48 class FakeSAT(object):
Goffi <goffi@goffi.org>
parents:
diff changeset
49 """Class to simulate a SAT instance"""
Goffi <goffi@goffi.org>
parents:
diff changeset
50
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def __init__(self):
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.bridge = FakeBridge()
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.memory = FakeMemory()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
54 self.trigger = FakeTriggerManager()
335
Goffi <goffi@goffi.org>
parents:
diff changeset
55
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
56 def delContact(self, to, profile_key):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
57 #TODO
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
58 pass
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
59
335
Goffi <goffi@goffi.org>
parents:
diff changeset
60
Goffi <goffi@goffi.org>
parents:
diff changeset
61 class FakeBridge(object):
Goffi <goffi@goffi.org>
parents:
diff changeset
62 """Class to simulate and test bridge calls"""
Goffi <goffi@goffi.org>
parents:
diff changeset
63
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def expectCall(self, name, *check_args, **check_kwargs):
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def checkCall(*args, **kwargs):
Goffi <goffi@goffi.org>
parents:
diff changeset
66 if args != check_args or kwargs != check_kwargs:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 print "\n\n--------------------"
Goffi <goffi@goffi.org>
parents:
diff changeset
68 print "Args are not equals:"
Goffi <goffi@goffi.org>
parents:
diff changeset
69 print "args\n----\n%s (sent)\n%s (wanted)" % (args, check_args)
Goffi <goffi@goffi.org>
parents:
diff changeset
70 print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs)
Goffi <goffi@goffi.org>
parents:
diff changeset
71 print "--------------------\n\n"
Goffi <goffi@goffi.org>
parents:
diff changeset
72 raise DifferentArgsException
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
73
335
Goffi <goffi@goffi.org>
parents:
diff changeset
74 setattr(self, name, checkCall)
Goffi <goffi@goffi.org>
parents:
diff changeset
75
694
4284b6ad8aa3 tests: plugin text syntaxes sanitisation tests
Goffi <goffi@goffi.org>
parents: 693
diff changeset
76 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False):
4284b6ad8aa3 tests: plugin text syntaxes sanitisation tests
Goffi <goffi@goffi.org>
parents: 693
diff changeset
77 pass
335
Goffi <goffi@goffi.org>
parents:
diff changeset
78
Goffi <goffi@goffi.org>
parents:
diff changeset
79 class FakeMemory(object):
Goffi <goffi@goffi.org>
parents:
diff changeset
80 """Class to simulate and test memory object"""
Goffi <goffi@goffi.org>
parents:
diff changeset
81
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def getProfileName(self, profile_key):
Goffi <goffi@goffi.org>
parents:
diff changeset
83 return profile_key
Goffi <goffi@goffi.org>
parents:
diff changeset
84
669
ffb716804580 core, bridge: extra parameter is saved in history:
Goffi <goffi@goffi.org>
parents: 649
diff changeset
85 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
86 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
87
335
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'):
Goffi <goffi@goffi.org>
parents:
diff changeset
89 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
90
484
23cbdf0a0777 core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents: 480
diff changeset
91 def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
92 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
93
335
Goffi <goffi@goffi.org>
parents:
diff changeset
94 def addWaitingSub(self, type, contact_jid, profile_key):
Goffi <goffi@goffi.org>
parents:
diff changeset
95 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
96
335
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def delWaitingSub(self, contact_jid, profile_key):
Goffi <goffi@goffi.org>
parents:
diff changeset
98 pass
Goffi <goffi@goffi.org>
parents:
diff changeset
99
694
4284b6ad8aa3 tests: plugin text syntaxes sanitisation tests
Goffi <goffi@goffi.org>
parents: 693
diff changeset
100 def updateParams(self, xml):
4284b6ad8aa3 tests: plugin text syntaxes sanitisation tests
Goffi <goffi@goffi.org>
parents: 693
diff changeset
101 pass
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
102
335
Goffi <goffi@goffi.org>
parents:
diff changeset
103 class FakeTriggerManager(object):
Goffi <goffi@goffi.org>
parents:
diff changeset
104
Goffi <goffi@goffi.org>
parents:
diff changeset
105 def add(self, point_name, callback):
Goffi <goffi@goffi.org>
parents:
diff changeset
106 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
107
335
Goffi <goffi@goffi.org>
parents:
diff changeset
108 def point(self, point_name, *args, **kwargs):
Goffi <goffi@goffi.org>
parents:
diff changeset
109 """We always return true to continue the action"""
Goffi <goffi@goffi.org>
parents:
diff changeset
110 return True
Goffi <goffi@goffi.org>
parents:
diff changeset
111
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
112 class FakeRosterProtocol(SatRosterProtocol):
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
113
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
114 def __init__(self, host, parent):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
115 SatRosterProtocol.__init__(self, host)
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
116 self.parent = parent
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
117 self.addItem(TEST_JID)
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
118
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
119 def addItem(self, jid, *args, **kwargs):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
120 if not args and not kwargs:
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
121 # defaults values setted for the tests only
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
122 kwargs["subscriptionTo"] = True
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
123 kwargs["subscriptionFrom"] = True
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
124 roster_item = RosterItem(jid, *args, **kwargs)
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
125 attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)}
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
126 if roster_item.name:
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
127 attrs['name'] = roster_item.name
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
128 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile)
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
129 self.onRosterSet(roster_item)
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
130
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
131
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
132 class FakeClient(object):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
133 def __init__(self, host):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
134 self.host = host
335
Goffi <goffi@goffi.org>
parents:
diff changeset
135 self.profile = 'test_profile'
Goffi <goffi@goffi.org>
parents:
diff changeset
136 self.jid = TEST_JID
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
137 self.roster = FakeRosterProtocol(host, self)
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
138
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
139 def send(self, obj):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
140 pass
335
Goffi <goffi@goffi.org>
parents:
diff changeset
141
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
142
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
143 class SatTestCase(unittest.TestCase):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
144
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
145 def assertEqualXML(self, xml, expected, ignore_blank = False):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
146 def equalElt(got_elt, exp_elt):
696
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
147 if ignore_blank:
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
148 for elt in got_elt, exp_elt:
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
149 for attr in ('text','tail'):
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
150 value = getattr(elt, attr)
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
151 try:
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
152 value = value.strip() or None
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
153 except AttributeError:
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
154 value = None
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
155 setattr(elt, attr, value)
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
156 if (got_elt.tag != exp_elt.tag):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
157 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
158 print "tag: got [%s] expected: [%s]" % (got_elt.tag, exp_elt.tag)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
159 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
160 if (got_elt.attrib != exp_elt.attrib):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
161 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
162 print "attribs: got %s expected %s" % (got_elt.attrib, exp_elt.attrib)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
163 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
164 if (got_elt.tail != exp_elt.tail or got_elt.text != exp_elt.text):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
165 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
166 print "text: got [%s] expected: [%s]" % (got_elt.text, exp_elt.text)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
167 print "tail: got [%s] expected: [%s]" % (got_elt.tail, exp_elt.tail)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
168 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
169 if (len(got_elt) != len(exp_elt)):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
170 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
171 print "children len: got %d expected: %d" % (len(got_elt), len(exp_elt))
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
172 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
173 for idx, child in enumerate(got_elt):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
174 if not equalElt(child, exp_elt[idx]):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
175 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
176 return True
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
177
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
178 def remove_blank(xml):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
179 lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+',' ',xml).split('\n')]
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
180 return '\n'.join([line for line in lines if line])
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
181
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
182 xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
183 expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
184
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
185 if not equalElt(xml_elt, expected_elt):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
186 print "---"
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
187 print "XML are not equals:"
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
188 print "got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8')
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
189 print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8')
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
190 print "---"
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
191 raise DifferentXMLException
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
192
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
193
335
Goffi <goffi@goffi.org>
parents:
diff changeset
194 def _(text):
Goffi <goffi@goffi.org>
parents:
diff changeset
195 return text
Goffi <goffi@goffi.org>
parents:
diff changeset
196
Goffi <goffi@goffi.org>
parents:
diff changeset
197 __builtin__.__dict__['_'] = _