def test_render_non_strings(): # this is creating a html page with a single body() element in it # and a string inside that. el_object = HTML(Body('any string I like')) contents = render_element(el_object) # 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 = Para('any string I like') p.append('even more random text') body.append(p) body.append(Para('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_Body(): element = Body('data1') element.append('data2') with open('file4.html', 'w') as file1fh: element.render(file1fh) with open('file4.html', 'r') as filefh: rendered = filefh.read() assert '<body>\n'+element.indent+'data1\n'+element.indent+'data2\n'+'</body>\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_render_body(): my_stuff = 'spam, spam, spam' el_object = Body(my_stuff) more_stuff = 'eggs, eggs, eggs' el_object.append(more_stuff) contents = render_element(el_object).strip() assert contents.startswith('<body>') assert contents.endswith('</body>') assert my_stuff in contents assert more_stuff in contents
def test_body_render(): outfile = io.StringIO() b = Body("this is some text") b.render(outfile, ind=4) file_contents = outfile.getvalue() print(file_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_body(): e = Body("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("<body>") assert file_contents.strip().endswith("</body>")
def test_render(self): p = Body() test_str = "this is a Body render test" p.append(test_str) f = StringIO() p.render(f) expected = [ "<body>", p.indent * ' ' + "this is a Body render test", "</body>" ] expected_page = "\n".join(expected) + "\n" self.assertEqual(expected_page, f.getvalue())
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_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_body(): outfile = io.StringIO() e = Body("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_body_tag(): # assert Body().tag == 'body' my_stuff = 'spam, spam, spam' more_stuff = '\neggs, eggs, eggs' el_object = Body(my_stuff) el_object.append(more_stuff) contents = render_element(el_object) contents = contents.strip() assert contents.startswith('<body>') assert contents.endswith('</body>') assert my_stuff in contents assert more_stuff in contents
def test_title_indendation_twothings(): my_stuff = "Q: Where in the world is Grover Norquist?" more_stuff = "A: Pretending the Laffer curve is relevant" el_object = Title(my_stuff) body_object = Body(more_stuff) el_object.append(body_object) contents = render_element(el_object) contents = contents.strip() print(contents) lines = contents.split('\n') assert len(lines) == 1 assert lines[0].startswith('<title> ') assert my_stuff in lines[0]
def test_render_body(): my_stuff = 'hamma, hamma, hamma' el_object = Body(my_stuff) more_stuff = "umma, umma, umma" el_object.append(more_stuff) with open('testb', 'w') as out_file: el_object.render(out_file) with open('testb', 'r') as in_file: contents = in_file.read() assert contents.startswith('<body') assert contents.endswith('</body>') assert my_stuff in contents assert more_stuff in contents
def test_render_non_strings(): my_stuff = 'any string I like' el_object = Element(Body(my_stuff)) contents = render_element(el_object) contents = contents.strip() print(contents) assert my_stuff in contents assert contents.startswith('<html>') assert contents.endswith('</html>') assert '<body>' in contents assert '</body>' in contents assert contents.index('<body>') < contents.index(my_stuff) assert contents.index('<body>') < contents.index('</body>')
def test_render_body(): mystuff = 'spam, spam, spam' el_object = Body(mystuff) morestuff = 'eggs, eggs, eggs' el_object.append(morestuff) with open('text2.htm', 'w') as out_file: el_object.render(out_file) with open('text2.htm', 'r') as in_file: contents = in_file.read() assert contents.startswith('<body>') assert contents.endswith('</body>') assert mystuff in contents assert morestuff in 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_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_render_page(): my_stuff = 'nerp' more_stuff = 'eggs, eggs, eggs' el_object = Body(my_stuff) el_object.append(more_stuff) with open('test1', 'w') as out_file: el_object.render(out_file) with open('test1', 'r') as in_file: contents = in_file.read() assert contents.startswith('<body>') assert contents.endswith('</body>') assert my_stuff in contents assert more_stuff in contents
def test_render_body(): some_stuff = 'blueberries and raspberries' el_object = Body(some_stuff) more_stuff = 'strawberries' el_object.append(more_stuff) with open('test1.txt', 'w') as out_file: el_object.render(out_file) with open('test1.txt', 'r') as in_file: contents = in_file.read() assert contents.startswith('<body>') assert contents.endswith('</body>') assert some_stuff in contents assert more_stuff in contents
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_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_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_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_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_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>'