def test_Body(): outfile = io.StringIO() e = Body('This is some text') e.append('This is some more text') e.render(outfile) outfile.seek(0) file_contents = outfile.read() print(file_contents) assert ('This is some text') in file_contents assert ('This is some more text') in file_contents
def test_selfclosingtag(): page = Html() body = Body() attributes = {"class": "main bordered", "id": "top-spacer"} body.append(Hr(**attributes)) page.append(body) f = StringIO() page.render(f, "") assert f.getvalue() == '<!DOCTYPE html>\n<html>\n'\ ' <body>\n'\ ' <hr class="main bordered" id="top-spacer" />\n'\ ' </body>\n'\ '</html>'
def test_Element_with_attributes(): page = Html() body = Body() attributes = {"class": "paragraph", "id": "intro"} body.append(P("Here is some paragraph text.", **attributes)) page.append(body) f = StringIO() page.render(f, "") assert f.getvalue() == '<!DOCTYPE html>\n<html>\n'\ ' <body>\n'\ ' <p class="paragraph" id="intro">Here is some paragraph text.</p>\n'\ ' </body>\n'\ '</html>'
def test_multiple_indent(): """ make sure multiple levels get indented fully """ body = Body() body.append(P("some text")) html = Html(body) file_contents = render_result(html) print(file_contents) lines = file_contents.split("\n") for i in range(3): assert lines[i].startswith(i * Element.indent + "<") assert lines[3].startswith(3 * Element.indent + "some")
def test_nesting_elements(): page = Html() body = Body() body.append(P("Here is a paragraph of text.")) body.append(P("And here is another paragraph of text.")) page.append(body) f = StringIO() page.render(f, "") assert f.getvalue() == '<!DOCTYPE html>\n<html>\n'\ ' <body>\n'\ ' <p>Here is a paragraph of text.</p>\n'\ ' <p>And here is another paragraph of text.</p>\n'\ ' </body>\n'\ '</html>'
def test_body(): outfile = io.StringIO() b = Body("this is some text") b.append ("and some more text") assert "and some more text" in b.content assert b.content == ["this is some text", "and some more text"] b.render(outfile, "") outfile.seek(0) file_contents = outfile.read() assert ("this is some text") in file_contents assert ("and some more text") in file_contents assert file_contents.startswith ("<body>\n") assert file_contents.strip().endswith ("</body>")
def test_Body(): outfile = io.StringIO() e = Body('This is some text') e.append('This is some more text') e.render(outfile) outfile.seek(0) file_contents = outfile.read() print(file_contents) assert('This is some text') in file_contents assert('This is some more text') in file_contents assert file_contents.startswith("<body>") assert file_contents.strip().endswith("</body>")
def test_render_body(): my_stuff = 'spam, spam, spam' el_object = Body(my_stuff) more_stuff = ', eggs, eggs, eggs' el_object.append(more_stuff) with open('test_render_body_output.html', 'w') as out_file: el_object.render(out_file) with open('test_render_body_output.html', 'r') as in_file: contents = in_file.read() assert contents.startswith('<body>\n') assert contents.endswith('</body>\n') assert my_stuff in contents assert more_stuff in contents #def test_render_non_strings(): # # this is crating a html page with a single body() element in it # el_object = Html(Body('any string I like')) # with open('test_render_non_strings_output.html', 'w') as out_file: # el_object.render(out_file) # with open('test_render_non_strings_output.html', 'r') as in_file: # contents = in_file.read() # # make sure extra whitespace at beginning or end doesn't mess things up. # contents = contents.strip() # print(contents) # so we can see what's going on if a test fails # so what should the results be? # the html tag is the outer tag, so the contents should start and end with that. # assert contents.startswith('<html>') # assert contents.endswith('</html>') # the body tags had better be there too # assert '<body>' in contents # assert '</body' in contents # we want the tesxt, too: # assert 'any string I like' in contents # now lets get pretty specific: # the opening tag should come before the ending tag # assert contents.index('<body>') < contents.index('</body>') # the opening tag should come before the content # assert contents.index('<body>') < contents.index('any string')
def test_multiple_indent(): """ make sure multiple levels get indented properly """ body = Body() body.append(Para("some text")) body.append(Para("even more text")) html = HTML(body) file_contents = render_element(html) print(file_contents) lines = file_contents.split("\n") for i in range(3): assert lines[i].startswith(i * Element.indent + "<") assert lines[3].startswith(3 * Element.indent + "some") assert lines[4].startswith(2 * Element.indent + "</p>") assert lines[5].startswith(2 * Element.indent + "<p>") assert lines[6].startswith(3 * Element.indent + "even ") for i in range(3): assert lines[-(i + 1)].startswith(i * Element.indent + "<")
def test_multiple_indentation(): my_stuff = "Q: Where in the world is Grover Norquist?" more_stuff = "A: Pretending the Laffer curve is relevant" el_object = Body(my_stuff) el_object.append(P(more_stuff)) contents = render_element(el_object) contents = contents.strip() print(contents) lines = contents.split('\n') assert len(lines) == 6 # Check content indentation assert lines[1].startswith(Element.extra_indent + 'Q:') assert lines[3].startswith(2 * Element.extra_indent + 'A:') print('Len of Element.extra_indent is: ', len(Element.extra_indent)) print('Len of el_object.indent is: ', len(el_object.extra_indent)) # Check body tag indentation (level 0) assert lines[0].startswith('<body>') assert lines[5].startswith('</body>') # Check paragraph tag indendation (level 1) assert lines[2].startswith(Element.extra_indent + '<p>') assert lines[4].startswith(Element.extra_indent + '</p>')
def test_step3(): outfile = io.StringIO() p = Html() h = Head() h.append(Title('This is TITLE')) p.append(h) b = Body() b.append(P('This is P')) p.append(b) p.render(outfile) outfile.seek(0) file_contents = outfile.read() print(file_contents) assert('This is TITLE') in file_contents assert('This is P') in file_contents assert file_contents.startswith("<html>") assert file_contents.strip().endswith("</html>")
def test_whole_thing(): """ Render a complete page This is not really a unit test, and does not test that the results are correct, but does ensure that it all runs, and provides output to look at """ page = Html() head = Head() head.append(Meta(charset="UTF-8")) head.append(Title("Python Class Sample page")) page.append(head) body = Body() body.append(H(2, "Python Class - Html rendering example")) body.append( P( "Here is a paragraph of text -- there could be more of them, " "but this is enough to show that we can do some text", style="text-align: center; font-style: oblique;")) body.append(Hr()) list = Ul(id="TheList", style="line-height:200%") list.append(Li("The first item in a list")) list.append(Li("This is the second item", style="color: red")) item = Li() item.append("And this is a ") item.append(A("http://google.com", "link")) item.append("to google") list.append(item) body.append(list) page.append(body) # Element.indent = " " # now render it: with open("sample_output.html", 'w') as f: page.render(f)
def test_step_2_noindent(): page = Html() body = Body() page.append(body) body.append(P("a small paragraph of text")) body.append(P("another small paragraph of text")) body.append(P("and here is a bit more")) file_contents = render_result(page).strip()
def test_full_page_with_title(): """ not much to actually test here, but good to see it put together. everything should have already been tested. """ page = Html() head = Head() head.append(Title("PythonClass Example")) page.append(head) body = Body() body.append(P("Here is a paragraph of text -- there could be more of them, " "but this is enough to show that we can do some text")) body.append(P("And here is another piece of text -- you should be able to add any number")) page.append(body) file_contents = render_result(page) print(file_contents)
def test_step2(): html_el = Html() body_el = Body() p_el = P() assert html_el.tag == 'html' assert body_el.tag == 'body' assert p_el.tag == 'p' html_el.append('hi') f = StringIO() html_el.render(f) assert f.getvalue() == ('<!DOCTYPE html>\n<html>\n hi\n</html>\n') page = Html() body = Body() body.append(P("Some text.")) body.append(P("More text.")) page.append(body) f = StringIO() page.render(f) expected = ('<!DOCTYPE html>\n<html>\n <body>\n <p>\n ' ' Some text.\n </p>\n <p>\n More text' '.\n </p>\n </body>\n</html>\n') assert f.getvalue() == expected
def test_step_2_noindent(): """ This is more if an integration test -- a number of things together this test does not yet include indentation """ page = Html() body = Body() page.append(body) body.append(P("a small paragraph of text")) body.append(P("another small paragraph of text")) body.append(P("and here is a bit more")) file_contents = render_result(page).strip() print(file_contents) assert file_contents.startswith("<html>") assert file_contents.endswith("</html>") assert "a small paragraph of text" in file_contents assert "<body>" in file_contents
#!/usr/bin/env python3 import io from html_render import Element, Html from html_render import Body from html_render import P a=Html() a.append ("some stuff") a.append ("other stuff") b=Body() b.append ("body stuff") b.append ("body stuff II") #print (a.content) #print (a.tag) #print (b.content) #print (b.tag) #a.append(b) #print (a.content) outfile = io.StringIO() a.render (outfile, "") b.render (outfile, "")