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_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_attributes2(): e = P("A paragraph of text", style="text-align: center", id="intro") file_contents = render_result(e).strip() print(file_contents) # so we can see it if the test fails # note: The previous tests should make sure that the tags are getting # properly rendered, so we don't need to test that here. # so using only a "P" tag is fine assert "A paragraph of text" in file_contents # but make sure the embedded element's tags get rendered! # first test the end tag is there -- same as always: assert file_contents.endswith("</p>") # but now the opening tag is far more complex # but it starts the same: assert file_contents.startswith( "<p ") # make sure there's space after the p # order of the tags is not important in html, so we need to # make sure not to test for that # but each attribute should be there: assert 'style="text-align: center"' in file_contents assert 'id="intro"' in file_contents # # just to be sure -- there should be a closing bracket to the opening tag assert file_contents[:-1].index(">") > file_contents.index('id="intro"') assert file_contents[:file_contents.index(">")].count(" ") == 3 assert False
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_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_p(): e = P("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
def test_br_in_p(): p = P("here is a small paragraph of text") p.append(Br()) p.append("And here is some more text after a line break") file_contents = render_result(p).split('\n') print(file_contents) assert file_contents[2].strip() == "<br />"
def test_P(): element = P('data1') element.append('data2') with open('file3.html', 'w') as file1fh: element.render(file1fh) with open('file3.html', 'r') as filefh: rendered = filefh.read() assert '<p>\n'+element.indent+'data1\n'+element.indent+'data2\n'+'</p>\n' == rendered # noqa: E501
def test_multiple_attributes(): p = P("here is a paragraph of text", id="fred", color="red", size="12px") contents = render_element(p).strip() print(contents) lines = contents.split('\n') assert lines[0].startswith('<p ') assert lines[0].endswith('>') assert 'id="fred"' in lines[0] assert 'color="red"' in lines[0] assert 'size="12px"' in lines[0]
def test_render_P(): my_stuff = 'spam, spam, spam' p = P(my_stuff) more_stuff = 'eggs, eggs, eggs' p.append(more_stuff) contents = render_element(p).strip() assert contents.startswith('<p>') assert contents.endswith('</p>') assert my_stuff in contents assert more_stuff in contents
def test_single_attribute(): # <p style="text-align: center; font-style: oblique;"> # Here is a paragraph of text # </p> p = P("Here is a paragraph of text", style="text-align: center; font-style: oblique;") results = render_element(p).strip() # need this to be string IO I think print(results) assert results.startswith( '<p style="text-align: center; font-style: oblique;">')
def test_p_render(): outfile = io.StringIO() p = P("this is some text") p.render(outfile, ind=4) file_contents = outfile.getvalue() print(file_contents)
def test_step4(): p = P('hi', style='some_style', style2='other_style') assert p.content == ['hi'] assert p.indent == 4 assert p.attr == {'style': 'some_style', 'style2': 'other_style'} f = StringIO() p.render(f) assert f.getvalue() == ('<p style="some_style" style2="other_style">\n' ' hi\n</p>\n')
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_p(): e = P("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("<p>") assert file_contents.strip().endswith("</p>")
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 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_paragraph_tag(): # assert P().tag == 'p' my_stuff = 'spam, spam, spam' more_stuff = '\neggs, eggs, eggs' el_object = P(my_stuff) el_object.append(more_stuff) contents = render_element(el_object) contents = contents.strip() assert contents.startswith('<p>') assert contents.endswith('</p>') assert my_stuff in contents assert more_stuff in contents
def test_class_attribute(): # Use a dictionary to get around class being a reserved word atts = {"id": "fred", "class": "special", "size": "12px"} p = P("here is a paragraph of text", **atts) contents = render_element(p).strip() print(contents) lines = contents.split('\n') assert lines[0].startswith('<p ') assert lines[0].endswith('">') assert 'id="fred"' in lines[0] assert 'class="special"' in lines[0] assert 'size="12px"' in lines[0]
def test_p(): outfile = io.StringIO() e = P("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_attributes(): p = P("a little text", id="error", name="fred") outfile = io.StringIO() p.render(outfile) result = outfile.getvalue() print(result) assert 'name="fred"' in result assert 'id="error"' in result line1 = result.split("\n")[0].strip() assert line1.startswith("<") assert line1.endswith(">")
def test_multiple_indent(): """ make sure multiple levels get indented properly """ body = Body() body.append(P("some text")) body.append(P("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_non_str(): """ you should be able to pass anything in, and it will get "stringified" """ e = P(34) # a number e.append((3, 4, 5)) # even a tuple file_contents = render_result(e) print(file_contents) assert ("34") in file_contents assert ("(3, 4, 5)") in 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_single_attribute(): # <p style="text-align: center; font-style: oblique;"> # Here is a Pgraph of text -- there could be more of them, but this is enough to show that we can do some text # </p> p = P("Here is a paragraph of text", style="text-align: center; font-style: oblique;") results = render_element(p) assert results.startswith( '<p style="text-align: center; font-style: oblique;">') print(results)
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_P(): outfile = io.StringIO() e = P('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_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>'