""" open/DurusWorks/qpy/test/utest_xml.py """ from sancho.utest import UTest from qpy import xml, xml_quote import sys if sys.version.startswith("3"): unicode_string = str else: from __builtin__ import unicode as unicode_string class T(object): pass class TestXml (UTest): def test_xml(self): u = xml() assert str(u) == '' assert u == xml('') assert u == xml(None) assert u == '' assert xml('<') == '<' assert xml_quote('<') == '<' u += 1 u += None assert u == '1' assert xml('a%s') % '&' == 'a&' assert xml('a%(a)s%(b)s') % dict(a='&', b=xml('<')) == 'a&<' assert xml('a%s%s') % ('&', xml('<')) == 'a&<' t = (1,2) try: t += u assert 0 except TypeError: pass t = [] t += u assert t == list(u) def test_a(self): s = xml('<') + '<' assert isinstance(s, xml) and s == '<<' s = '<' + xml('<') assert isinstance(s, xml) and s == '<<', '__radd__ not working.' s = xml('%s') % '<' assert isinstance(s, xml) and s == '<' s = xml('%s') % xml('<') assert isinstance(s, xml) and s == '<' s = xml('%(a)s') % dict(a='<') assert isinstance(s, xml) and s == '<' s = unicode_string('%s') % xml('a') assert isinstance(s, unicode_string) assert s == 'a' s = xml('a') s += '<' assert s == 'a<' def test_quote_wrapper(self): assert xml("%0.2f") % 2 == '2.00' assert type(xml("%0.2f") % 2) is xml assert xml("%(a)s %(b)0.2f") % dict( a=unicode_string('<'), b=2) == '< 2.00' assert type(xml("%(a)s %(b)0.2f") % dict( a=unicode_string('a'), b=2)) is xml if str is not unicode_string: assert xml("%(a)r %(b)0.2f") % dict( a=unicode_string('<'), b=2) == "u'<' 2.00" else: print(repr(xml("%(a)r %(b)0.2f") % dict( a=unicode_string('<'), b=2))) assert xml("%(a)r %(b)0.2f") % dict( a=unicode_string('<'), b=2) == "'<' 2.00" assert type(xml("%(a)r %(b)0.2f")) is xml x = xml('&') assert x.__html__() is x def test_pickle(self): from pickle import loads, dumps s = xml('hello') assert s == loads(dumps(xml(s), 2)) def test_format(self): if not hasattr('', 'format'): return assert xml("{0:.2f}").format(2) == '2.00' assert type(xml("{0:.2f}").format(2)) is xml assert xml("{a:s} {b:0.2f}").format( a=unicode_string('<'), b=2) == '< 2.00' assert xml("{a:s} {b:0.2f}").format( a=unicode_string('<'), b=2) == '< 2.00' t = T() t.num = 3 t.string = 'ABC' t.tbq = '<' assert xml("{a:s} {b:0.2f} {a_t.num:0.2f} {a_t.string} {a_t.tbq}").format( a=unicode_string('<'), b=2, a_t=t) == '< 2.00 3.00 ABC <' assert xml("{a:s} {b:0.2f}").format( a=unicode_string(repr('<')), b=2) == "'<' 2.00" assert type(xml("{a:s} {b:0.2f}")) is xml assert xml is type( xml("{a:s} {b:0.2f} {a_t.num:0.2f} {a_t.string} {a_t.tbq}").format( a=unicode_string('<'), b=2, a_t=t)) def test_format_refs(self): if not hasattr(sys, 'gettotalrefcount'): return # This python is not compiled with pydebug on. refs = sys.gettotalrefcount before = 0 after = 0 x = 0 before = refs() for x in range(100): self.test_format() after = refs() assert after - before == 0, after - before if __name__ == '__main__': TestXml()