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_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_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_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_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_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_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_step8(): html_el = Html('test') f = StringIO() html_el.render(f) assert f.getvalue() == ('<!DOCTYPE html>\n<html>\n test\n</html>\n') meta = Meta(charset="UTF-8") assert meta.attr == {'charset': 'UTF-8'} f = StringIO() meta.render(f) assert f.getvalue() == ('<meta charset="UTF-8" />\n')
def test_indent_contents(): html = Html("some content") file_contents = render_result(html, ind="") print(file_contents) lines = file_contents.split("\n") assert lines[1].startswith(Element.indent)
def test_html_indentation(): e = Html("a test") output = get_output(e) print(output) assert "<html>\n" in output assert output.endswith("\n</html>") output_lines = output.split("\n") assert " a test" in output_lines
def text_sub_element(): page = Html() page.append("some plain text.") page.append(P("A simple paragraph of text")) page.append("Some more plain text.") file_contents = render_result(page) assert "some plain text" in file_contents assert "A simple paragraph of text" in file_contents assert "Some more plain text." in file_contents assert "some plain text" in file_contents
def test_indent(): """ Tests that the indentation gets passed through to the renderer """ html = Html("some content") file_contents = render_result(html, ind=" ") print(file_contents) lines = file_contents.split("\n") assert lines[0].startswith(" <") assert lines[-1].startswith(" <")
def test_indent_contents(): """ The contents in a element should be indented more than the tag by the amount in the indent class attribute """ html = Html("some content") file_contents = render_result(html, ind="") print(file_contents) lines = file_contents.split("\n") assert lines[1].startswith(Element.indent)
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_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_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_mulitiple_indent(): 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_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_sub_element(): """ tests that you can add another element and still render properly """ page = Html() page.append("some plain text.") page.append(P("A simple paragraph of text")) page.append("Some more plain text.") file_contents = render_result(page) # note: the above tests should make sure that the tags are getting rendered. assert "some plain text" in file_contents assert "A simple paragraph of text" in file_contents assert "Some more plain text." in file_contents assert "some plain text" in file_contents
def test_indent(): """ Tests that the indentation gets passed through to the renderer """ html = Html("some content") cur_ind = 6 * " " file_contents = render_element(html, cur_ind=cur_ind) print(file_contents) lines = file_contents.split("\n") assert lines[0].startswith(cur_ind + "<") assert lines[1].startswith(cur_ind + Element.indent + "som") assert lines[-1].startswith(cur_ind + "<")
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_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): # this needed to be adapted to the <DOCTYPE> tag assert lines[i + 1].startswith(i * Element.indent + "<") assert lines[4].startswith(3 * Element.indent + "some")
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_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(): 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_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>'