def test_render_non_strings2(): """ Testing nested elements and text, in a more complex way """ html = Html() body = Body() html.append(body) p = P('any string I like') p.append('even more random text') body.append(p) body.append(P('and this is a different string')) contents = render_element(html).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 # and two <p> tags assert contents.count('<p>') # we want the text, too: assert 'any string I like' in contents assert 'even more random text' in contents assert 'and this is a different string' in contents
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_page(): outfile = io.StringIO() a = Html() b = Body("Body Text") c = P("Paragraph text 1") c.append("Paragraph text 2") b.append(c) a.append(b) a.render(outfile, "") outfile.seek(0) file_contents = outfile.read() assert file_contents.startswith ("<html>\n") assert file_contents.strip().endswith ("</html>") assert ("<body>") in file_contents assert ("</body>") in file_contents assert ("<p>") in file_contents assert ("</p>") in file_contents assert ("Body Text") in file_contents assert ("Paragraph text 1") in file_contents assert ("Paragraph text 2") in file_contents
def test_Html(): element = Html('data1') element.append('data2') with open('file2.html', 'w') as file1fh: element.render(file1fh) with open('file2.html', 'r') as filefh: rendered = filefh.read() assert '<!DOCTYPE html>\n<html>\n'+element.indent+'data1\n'+element.indent+'data2\n'+'</html>\n' == rendered # noqa: E501
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_one_line_tag(): e1 = Html("") e2 = Head("") e3 = Title("Hark! A Title!") e2.append(e3) e1.append(e2) output = get_output(e1) print(output) output_lines = get_output_lines(e1) assert " <title>Hark! A Title!</title>" in output_lines
def test_render_html(): my_stuff = 'spam, spam, spam' el_object = Html(my_stuff) more_stuff = 'eggs, eggs, eggs' el_object.append(more_stuff) contents = render_element(el_object) assert contents.startswith('<html>') assert contents.endswith('</html>') assert my_stuff in contents assert more_stuff in contents
def test_html(): e = Html("this is some text") e.append("and this is some more text") file_contents = render_result(e) assert ("this is some text") in file_contents assert ("and this is some more text") in file_contents assert file_contents.startswith("<html>") assert file_contents.strip().endswith("</html>")
def test_render_page_element(): test_element = Html(content="Some content.") test_element.append('Some more content.') f = StringIO() test_element.render(f, "") assert f.getvalue() == '<!DOCTYPE html>\n<html>\n'\ 'Some content.'\ 'Some more content.'\ '</html>'
def test_html_render(): html_obj = Html("Some html text") html_obj.append("more html text") body_obj = Body("I am a body element") with open("test3.html", "w") as outfile: body_obj.render(outfile) with open("test2.html", "w") as outfile: html_obj.render(outfile)
def test_doctype_and_meta(): h = Html() b = Body() p = P("a paragraph") b.append(p) h.append(b) output = get_output(h) output_lines = get_output_lines(h) print(output) assert output_lines[0] == "<!DOCTYPE html>" assert output_lines[1] == "<meta charset=\"UTF-8\" />"
def test_multiple_element_indentation(): e1 = Html("") e2 = Body("") e3 = P("some random text") e2.append(e3) e1.append(e2) output = get_output(e1) output_lines = output.split("\n") assert " <p>" in output_lines assert " some random text" in output_lines print(output)
def test_html_tag(): # assert Html().tag == 'html' my_stuff = 'spam, spam, spam' more_stuff = '\neggs, eggs, eggs' el_object = Html(my_stuff) el_object.append(more_stuff) contents = render_element(el_object) assert contents.startswith('<!DOCTYPE html>\n<html>') assert contents.endswith('</html>') assert my_stuff in contents assert more_stuff in contents
def test_html(): outfile = io.StringIO() e = Html("this is some text") e.append("and this is some more text") e.render(outfile) outfile.seek(0) file_contents = outfile.read() assert ("this is some text") in file_contents assert ("and this is some more text") in file_contents
def test_a(): page = Html() body = Body() body.append(A("http://google.com", "link")) page.append(body) f = StringIO() page.render(f, "") assert f.getvalue() == '<!DOCTYPE html>\n<html>\n'\ ' <body>\n'\ '<a href="http://google.com">link</a> </body>\n'\ '</html>'
def test_doctype_meta(): page = Html() head = Head() head.append(Meta(charset="UTF-8")) page.append(head) f = StringIO() page.render(f, "") assert f.getvalue() == '<!DOCTYPE html>\n<html>\n'\ ' <head>\n'\ ' <meta charset="UTF-8" />\n'\ ' </head>\n'\ '</html>'
def test_selfclosingtag_with_content(): with pytest.raises(TypeError) as excinfo: page = Html() body = Body() attributes = {"class": "main bordered", "id": "top-spacer"} body.append(Hr("This should break stuff!", **attributes)) page.append(body) f = StringIO() page.render(f, "") raise TypeError("This element does not accept nested content.") assert str(excinfo.value) == "This element does not accept nested content."
def test_onelinetag(): page = Html() head = Head() head.append(Title("Here is some title text")) page.append(head) f = StringIO() page.render(f, "") assert f.getvalue() == '<!DOCTYPE html>\n<html>\n'\ ' <head>\n'\ ' <title>Here is some title text</title>\n'\ ' </head>\n'\ '</html>'
def test_h(): page = Html() body = Body() body.append(H(1, "Top Heading")) page.append(body) f = StringIO() page.render(f, "") assert f.getvalue() == '<!DOCTYPE html>\n<html>\n'\ ' <body>\n'\ ' <h1>Top Heading</h1>\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_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_Html(): outfile = io.StringIO() e = Html('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_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_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_html(): outfile = io.StringIO() h = Html("this is some text") h.append ("and some more text") assert "and some more text" in h.content assert h.content == ["this is some text", "and some more text"] h.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 ("<html>\n") assert file_contents.strip().endswith ("</html>")
def test_Html(): outfile = io.StringIO() e = Html('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("<html>") assert file_contents.strip().endswith("</html>")
def test_render(): outfile = io.StringIO() # e = Element.Html("this is also text") e = Html("this is also text") e.append("let's add even more text") e.render(outfile, "") outfile.seek(0) file_contents = outfile.read() assert ("this is also text") in file_contents assert ("let's add even more text") in file_contents # assert("<html>") in file_contents assert file_contents.startswith("<html>") # assert("</html>") in file_contents assert file_contents.strip().endswith("</html>")
def test_html_body(): outfile = io.StringIO() e = Html('This is HTML text') f = Body('This is BODY text') e.append(f) e.render(outfile) outfile.seek(0) file_contents = outfile.read() print(file_contents) assert('This is HTML text') in file_contents assert('This is BODY text') in file_contents assert file_contents.startswith("<html>") assert file_contents.strip().endswith("</html>")
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
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>")