1
2import py
3from py._xmlgen import unicode, html, raw
4import sys
5
6class ns(py.xml.Namespace):
7    pass
8
9def test_escape():
10    uvalue = py.builtin._totext('\xc4\x85\xc4\x87\n\xe2\x82\xac\n', 'utf-8')
11    class A:
12        def __unicode__(self):
13            return uvalue
14        def __str__(self):
15            x = self.__unicode__()
16            if sys.version_info[0] < 3:
17                return x.encode('utf-8')
18            return x
19    y = py.xml.escape(uvalue)
20    assert y == uvalue
21    x = py.xml.escape(A())
22    assert x == uvalue
23    if sys.version_info[0] < 3:
24        assert isinstance(x, unicode)
25        assert isinstance(y, unicode)
26        y = py.xml.escape(uvalue.encode('utf-8'))
27        assert y == uvalue
28
29
30def test_tag_with_text():
31    x = ns.hello("world")
32    u = unicode(x)
33    assert u == "<hello>world</hello>"
34
35def test_class_identity():
36    assert ns.hello is ns.hello
37
38def test_tag_with_text_and_attributes():
39    x = ns.some(name="hello", value="world")
40    assert x.attr.name == 'hello'
41    assert x.attr.value == 'world'
42    u = unicode(x)
43    assert u == '<some name="hello" value="world"/>'
44
45def test_tag_with_subclassed_attr_simple():
46    class my(ns.hello):
47        class Attr(ns.hello.Attr):
48            hello="world"
49    x = my()
50    assert x.attr.hello == 'world'
51    assert unicode(x) == '<my hello="world"/>'
52
53def test_tag_with_raw_attr():
54    x = html.object(data=raw('&'))
55    assert unicode(x) == '<object data="&"></object>'
56
57def test_tag_nested():
58    x = ns.hello(ns.world())
59    unicode(x) # triggers parentifying
60    assert x[0].parent is x
61    u = unicode(x)
62    assert u == '<hello><world/></hello>'
63
64def test_list_nested():
65    x = ns.hello([ns.world()]) #pass in a list here
66    u = unicode(x)
67    assert u == '<hello><world/></hello>'
68
69def test_tag_xmlname():
70    class my(ns.hello):
71        xmlname = 'world'
72    u = unicode(my())
73    assert u == '<world/>'
74
75def test_tag_with_text_entity():
76    x = ns.hello('world & rest')
77    u = unicode(x)
78    assert u == "<hello>world &amp; rest</hello>"
79
80def test_tag_with_text_and_attributes_entity():
81    x = ns.some(name="hello & world")
82    assert x.attr.name == "hello & world"
83    u = unicode(x)
84    assert u == '<some name="hello &amp; world"/>'
85
86def test_raw():
87    x = ns.some(py.xml.raw("<p>literal</p>"))
88    u = unicode(x)
89    assert u == "<some><p>literal</p></some>"
90
91
92def test_html_name_stickyness():
93    class my(html.p):
94        pass
95    x = my("hello")
96    assert unicode(x) == '<p>hello</p>'
97
98def test_stylenames():
99    class my:
100        class body(html.body):
101            style = html.Style(font_size = "12pt")
102    u = unicode(my.body())
103    assert u == '<body style="font-size: 12pt"></body>'
104
105def test_class_None():
106    t = html.body(class_=None)
107    u = unicode(t)
108    assert u == '<body></body>'
109
110def test_alternating_style():
111    alternating = (
112        html.Style(background="white"),
113        html.Style(background="grey"),
114    )
115    class my(html):
116        class li(html.li):
117            def style(self):
118                i = self.parent.index(self)
119                return alternating[i%2]
120            style = property(style)
121
122    x = my.ul(
123            my.li("hello"),
124            my.li("world"),
125            my.li("42"))
126    u = unicode(x)
127    assert u == ('<ul><li style="background: white">hello</li>'
128                     '<li style="background: grey">world</li>'
129                     '<li style="background: white">42</li>'
130                 '</ul>')
131
132def test_singleton():
133    h = html.head(html.link(href="foo"))
134    assert unicode(h) == '<head><link href="foo"/></head>'
135
136    h = html.head(html.script(src="foo"))
137    assert unicode(h) == '<head><script src="foo"></script></head>'
138
139def test_inline():
140    h = html.div(html.span('foo'), html.span('bar'))
141    assert (h.unicode(indent=2) ==
142            '<div><span>foo</span><span>bar</span></div>')
143
144def test_object_tags():
145    o = html.object(html.object())
146    assert o.unicode(indent=0) == '<object><object></object></object>'
147