def test_pretty(): tests = [ (html.p("apple", "tree"), b"<p>appletree</p>"), (html.p("apple", html.br(), "tree"), b"<p>apple<br />tree</p>"), (html.p(php.php("apple")), b"<p>\n\t<?php apple?>\n</p>"), (html.p(php.php("apple"), "tree"), b"<p><?php apple?>tree</p>"), ( html.div(2*html.p("apple", "tree"), html.br()), b"<div>\n\t<p>appletree</p>\n\t<p>appletree</p>\n\t<br />\n</div>" ), ( html.div( php.php("apple"), html.p("apple", "tree"), html.div( html.p("apple"), html.p("tree"), ), html.br() ), b"<div>\n\t<?php apple?>\n\t<p>appletree</p>\n\t<div>\n\t\t<p>apple</p>\n\t\t<p>tree</p>\n\t</div>\n\t<br />\n</div>" ), ( html.ul( ul4.for_("name in names"), html.li( ul4.printx("name"), ), ul4.end("for"), ), b"<ul>\n\t<?for name in names?>\n\t\t<li>\n\t\t\t<?printx name?>\n\t\t</li>\n\t<?end for?>\n</ul>" ), ( xsc.Frag( ul4.if_("n == 0"), html.span("zero"), ul4.elif_("n == 1"), html.span("one"), ul4.else_(), html.span("many"), ul4.end("if"), ), b"<?if n == 0?>\n\t<span>zero</span>\n<?elif n == 1?>\n\t<span>one</span>\n<?else ?>\n\t<span>many</span>\n<?end if?>" ), ( xsc.Frag( ul4.def_("spam"), ul4.printx("eggs"), ul4.end("def"), ), b"<?def spam?>\n\t<?printx eggs?>\n<?end def?>" ), ] for (got, exp) in tests: got.pretty().bytes() == exp
def test_noelementsortext(): with xsc.Pool(): class el1(xsc.Element): xmlns = "ns1" model = sims.NoElementsOrText() class el2(xsc.Element): xmlns = "ns2" e = el1() e.bytes(validate=True) e = el1("foo") with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.IllegalTextWarning) e = el1(php.php("gurk")) e.bytes(validate=True) e = el1(xsc.Comment("gurk")) e.bytes(validate=True) e = el1(el1()) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.ElementWarning) # Elements from a different namespace are OK e = el1(el2()) e.bytes(validate=True)
def test_publishprocinst(): node = php.php("x") assert node.bytes(prefixdefault=False) == b"""<?php x?>""" assert node.bytes(prefixdefault=None) == b"""<?php x?>""" assert node.bytes(prefixdefault="p") == b"""<?php x?>""" assert node.bytes(prefixdefault=True) == b"""<?php x?>""" assert node.bytes(prefixes={php: False}) == b"""<?php x?>""" assert node.bytes(prefixes={php: None}) == b"""<?php x?>""" assert node.bytes(prefixes={php: "p"}) == b"""<?php x?>""" assert node.bytes(prefixes={php: True}) == b"""<?php x?>"""
def test_pretty(): tests = [ (html.p("apple", "tree"), b"<p>appletree</p>"), (html.p("apple", html.br(), "tree"), b"<p>apple<br />tree</p>"), (html.p(php.php("apple")), b"<p>\n\t<?php apple?>\n</p>"), (html.p(php.php("apple"), "tree"), b"<p><?php apple?>tree</p>"), (html.div(2 * html.p("apple", "tree"), html.br()), b"<div>\n\t<p>appletree</p>\n\t<p>appletree</p>\n\t<br />\n</div>"), (html.div(php.php("apple"), html.p("apple", "tree"), html.div( html.p("apple"), html.p("tree"), ), html.br()), b"<div>\n\t<?php apple?>\n\t<p>appletree</p>\n\t<div>\n\t\t<p>apple</p>\n\t\t<p>tree</p>\n\t</div>\n\t<br />\n</div>" ), (html.ul( ul4.for_("name in names"), html.li(ul4.printx("name"), ), ul4.end("for"), ), b"<ul>\n\t<?for name in names?>\n\t\t<li>\n\t\t\t<?printx name?>\n\t\t</li>\n\t<?end for?>\n</ul>" ), (xsc.Frag( ul4.if_("n == 0"), html.span("zero"), ul4.elif_("n == 1"), html.span("one"), ul4.else_(), html.span("many"), ul4.end("if"), ), b"<?if n == 0?>\n\t<span>zero</span>\n<?elif n == 1?>\n\t<span>one</span>\n<?else ?>\n\t<span>many</span>\n<?end if?>" ), (xsc.Frag( ul4.def_("spam"), ul4.printx("eggs"), ul4.end("def"), ), b"<?def spam?>\n\t<?printx eggs?>\n<?end def?>"), ] for (got, exp) in tests: got.pretty().bytes() == exp
def check(encoding): node = xsc.Frag( html.div( php.php("echo $foo"), abbr.html(), html.div("gurk", class_="hurz"), "\u3042", )) s = node.bytes(encoding=encoding) node2 = parse.tree(s, parse.Expat(), parse.NS(html), xsc.Pool(html, php, abbr)) assert node == node2
def check(encoding): node = xsc.Frag( html.div( php.php("echo $foo"), abbr.html(), html.div("gurk", class_="hurz"), "\u3042", ) ) s = node.bytes(encoding=encoding) node2 = parse.tree(s, parse.Expat(), parse.NS(html), xsc.Pool(html, php, abbr)) assert node == node2
def test_elements(): with xsc.Pool(): class el11(xsc.Element): xmlname = "el1" xmlns = "ns1" class el12(xsc.Element): xmlname = "el2" xmlns = "ns1" class el21(xsc.Element): xmlname = "el1" xmlns = "ns2" class el22(xsc.Element): xmlname = "el2" xmlns = "ns2" el11.model = sims.Elements(el11, el21) e = el11() e.bytes(validate=True) e = el11("foo") with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.IllegalTextWarning) e = el11(php.php("gurk")) e.bytes(validate=True) e = el11(xsc.Comment("gurk")) e.bytes(validate=True) e = el11(el11()) e.bytes(validate=True) e = el11(el21()) e.bytes(validate=True) e = el11(el12()) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.WrongElementWarning) e = el11(el22()) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.WrongElementWarning)
def test_empty3(): e = el1(php.php("gurk")) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.EmptyElementWithContentWarning)
align="left", ), html.div( html.h1("The headline", html.img(src="root:gurk.gif")), html.p("The ", html.em("first"), " paragraph."), html.div( html.h2("The ", html.em("important"), " headline"), html.p("The ", html.em("second"), " ", html.em("important"), " paragraph."), id="id42", ), html.div(id="id23"), class_="foo", ), html.p(html.em("only")), php.php("echo $footer;"), abbr.xist(), ) def test_levels(): def check(node, expr, ids): assert "".join(str(e.attrs.id) for e in node.walknodes(expr)) == ids ds = [html.div(id=id) for id in range(8)] ds[1].append(ds[4:7]) ds[2].append(ds[7]) ds[0].append(ds[1:4]) # ____0____ # / | \ # _1_ 2 3