def test_a(): html = hr.Html() head = hr.Head() head.append(hr.Title("PythonClass = Revision 1087:")) html.append(head) body = hr.Body() horizontal_rule = hr.Hr() body.append(horizontal_rule) body.append(hr.A("http://google.com", "link")) html.append(body) f = StringIO() html.render(f) f.seek(0) text = f.read().strip() print(text) assert text.find("<a href=\"http://google.com\">link</a>") != -1
def test_step8_output(step8_sample_output): """copied and pasted from run_html_render.py, step7. Expect this gives same result as test html""" page = hr.Html() head = hr.Head() head.append(hr.Meta(charset="UTF-8")) head.append(hr.Title("PythonClass = Revision 1087:")) page.append(head) body = hr.Body() body.append(hr.H(2, "PythonClass - Example")) body.append( hr.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.Hr()) list = hr.Ul(id="TheList", style="line-height:200%") list.append(hr.Li("The first item in a list")) list.append(hr.Li("This is the second item", style="color: red")) item = hr.Li() item.append("And this is a ") item.append(hr.A("http://google.com", "link")) item.append("to google") list.append(item) body.append(list) page.append(body) render_page(page, "test_html_output8.html") with open('test_html_output8.html') as f: generated_file = f.read() assert generated_file == step8_sample_output
def test_adjust_indent(): """ make sure multiple levels get indented fully with adjusted indent """ hr.Element.indent = 2 body = hr.Body() body.append(hr.P("some text")) html = hr.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 * (" " * hr.Element.indent) + "<") assert lines[4].startswith(3 * (" " * hr.Element.indent) + "some")
def test_ul(): html = hr.Html() body = hr.Body() body.append(hr.H(2, "xxx")) ul = hr.Ul(id="TheList", style="line-height:200%") body.append(ul) html.append(body) f = StringIO() html.render(f) f.seek(0) text = f.read().strip() print(text) assert text.find("<ul style=\"line-height:200%\" id=\"TheList\">") != -1 \ or text.find("<ul id=\"TheList\" style=\"line-height:200%\">") != -1 assert text.find("</ul>") != -1
def test_next(): #Instantiate our test objects. e = hr.Element() b = hr.Body() p = hr.P("a paragraph of text") b.append(p) p = hr.P("another paragraph of text") b.append(p) f = StringIO() e.append(b) e.render(f) f.seek(0) text = f.read().strip() print(text) assert False
def step3(): """ Step 3 """ page = hr.Html() head = hr.Head() head.append(hr.Title("PythonClass = Revision 1087:")) page.append(head) body = hr.Body() body.append( hr.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( hr. P("And here is another piece of text -- you should be able to add any number" )) page.append(body) render_page(page, "test_html_output3.html")
def test_sub_element_title(): page = hr.Html() page.append("some plain text.") header = hr.Head("A HEAD paragraph of text") page.append(header) header.append(hr.Title("A Title paragraph of text")) page.append(hr.Body(" Body text.")) file_contents = render_result(page) 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. assert "some plain text" in file_contents assert "A HEAD paragraph of text" in file_contents assert "A Title paragraph" in file_contents assert file_contents.index("HEAD paragraph") < file_contents.index( "A Title paragraph")
def html_report(): """ Saves a report in HTML format """ now = datetime.datetime.now() rendered_page = StringIO() page = hr.Html() head = hr.Head() head.append(hr.Meta(charset="UTF-8")) head.append(hr.Title("Mailroom OO Report")) page.append(head) body = hr.Body() body.append(hr.H(2, "Donation Report")) table = hr.Table(border=1, width=900) table.append(hr.Caption(f'Mailroom Database')) table.append(hr.Th('Name')) table.append(hr.Th('Total Given')) table.append(hr.Th('# of Gifts')) table.append(hr.Th('Average Gift')) for name in mailroom.database: # style="text-align:(left,center,right)"" can go on TR for the whole row or each TD table_row = hr.Tr(style="text-align:right") table_row.append(hr.Td(f'{mailroom.database[name].name}')) table_row.append( hr.Td(f'${mailroom.database[name].total_donations:,.2f}')) if mailroom.database[name].donations == []: table_row.append(hr.Td('0')) else: table_row.append( hr.Td(f'{len(mailroom.database[name].donations)}')) table_row.append( hr.Td(f'${mailroom.database[name].average_donation:,.2f}')) table.append(table_row) table_row = hr.Tr() table_row.append( hr.Td(f'Generated {now.strftime("%Y-%m-%d %H:%M:%S")}', colspan=4)) table.append(table_row) body.append(table) page.append(body) page.render(rendered_page, ' ') with open('./mailroom.html', 'w') as outfile: outfile.write(rendered_page.getvalue()) return f'HTML report saved to ./mailroom.html'
def test_step2_output(step2_sample_output): page = hr.Html() body = hr.Body() body.append( hr.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( hr. P("And here is another piece of text -- you should be able to add any number" )) page.append(body) render_page(page, "test_html_output2.html") with open('test_html_output2.html') as f: generated_file = f.read() assert generated_file == step2_sample_output
def test_Html_Head_Title_Body_P_Hr_A_real_Content(): page = hr.Html() head = hr.Head() head.append(hr.Title("PythonClass = Revision 1087:")) page.append(head) body = hr.Body() body.append( hr.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.Hr()) body.append("And this is a ") body.append(hr.A("http://google.com", "link")) body.append("to google") page.append(body) render_page(page, "page.html") testdata = open("page.html").read() assert testdata == "<html>\n <head>\n <title>PythonClass = Revision 1087:</title>\n </head>\n <body>\n <p style=\"text-align: center; font-style: oblique;\">\n Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text\n </p>\n <hr />\n And this is a \n <a href=\"http://google.com\">link</a>\n to google\n </body>\n</html>"
def test_indent(): # Test that indentation is working as expected page = hr.Html() body = hr.Body() head = hr.Head() head.append(hr.Title("Title")) body.append(hr.P("Paragraph 1")) page.append(head) page.append(body) contents = render_result(page) assert contents.startswith("<html>") lines = contents.split("\n") assert lines[1] == " <head>" assert lines[2].startswith(" <title>") assert lines[3] == " </head>" assert lines[4] == " <body>" assert lines[5].startswith(" <p>") assert lines[6] == " </body>" assert lines[7].startswith("</html>")
def step6(): """ Step 6 """ page = hr.Html() head = hr.Head() head.append(hr.Title("PythonClass = Revision 1087:")) page.append(head) body = hr.Body() body.append( hr.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.Hr()) body.append("And this is a ") body.append(hr.A("http://google.com", "link")) body.append("to google") page.append(body) render_page(page, "test_html_output6.html")
def test_indentation(): e = hr.Element() b = hr.Body() p = hr.P('a paragraph of text') b.append(p) p = hr.P('a paragraph of text') b.append(p) e.append(b) f = StringIO() e.render(f) f.seek(0) text = f.read().strip() assert text.startswith('<html>') assert text.endswith('</html>') # these tests need work. adjust indent of <p> has no affect on test passing assert ' <html>' not in text assert ' <body>\n' in text assert ' </body>' in text assert ' <p>' in text assert ' </p>' in text
def test_hierarchical_1(self): para1 = hr.P(consts.strs_before[0]) para1.append(consts.strs_before[1]) para2 = hr.P(consts.strs_before[2]) para2.append(consts.strs_before[3]) para3 = hr.P(consts.strs_before[4]) para3.append(consts.strs_before[5]) bod = hr.Body() bod.append(para1) bod.append(para2) bod.append(para3) pg = hr.Html(bod) with open(consts.test_filename, 'w') as self.fobj: self.assertTrue(pg.render(self.fobj, ' ')) with open(consts.test_filename, 'r') as self.file: self.strs_out = self.file.readlines() self.assertEqual(len(self.strs_out), 14) # Verify total lines self.assertEqual(self.strs_out[0], "<!DOCTYPE html>\n") self.assertEqual(self.strs_out[1], "<html>\n") self.assertEqual(self.strs_out[2], " <body>\n") self.assertEqual(self.strs_out[3], " <p>\n") self.assertEqual( self.strs_out[4], " " + consts.strs_after[0] + " " + consts.strs_after[1] + "\n") self.assertEqual(self.strs_out[5], " </p>\n") self.assertEqual(self.strs_out[6], " <p>\n") self.assertEqual( self.strs_out[7], " " + consts.strs_after[2] + " " + consts.strs_after[3] + "\n") self.assertEqual(self.strs_out[8], " </p>\n") self.assertEqual(self.strs_out[9], " <p>\n") self.assertEqual( self.strs_out[10], " " + consts.strs_after[4] + " " + consts.strs_after[5] + "\n") self.assertEqual(self.strs_out[-3], " </p>\n") self.assertEqual(self.strs_out[-2], " </body>\n") self.assertEqual(self.strs_out[-1], "</html>\n") del pg, bod, para1, para2, para3
def test_nest(): e = hr.Html() head = hr.Head() e.append(head) title = hr.Title("Your title goes here") head.append(title) body = hr.Body() e.append(body) p = hr.Paragraph("A paragraph of text", style="testy") body.append(p) br = hr.LineBreak() body.append(br) p = hr.Paragraph("Another paragraph of text") body.append(p) f = StringIO() e.render(f) f.seek(0) text = f.read().strip() print(text) assert False
def test_body_p_creation(): """A function to test the creation of paragraph and body objects as well as testing the fucntionality of the appending function.""" page = hr.Html() body = hr.Body() p = hr.P("And the base keeps running, running...") assert p.content[0] == ("And the base keeps running, running...") assert p.open_tag == ("<p>") body.append(p) assert body.content[1].content[0] == ( "And the base keeps running, running...") assert body.open_tag == ("<body>") page.append(body) assert page.content[1].content[1].content[0] == ( "And the base keeps running, running...") assert page.open_tag == ("<html>")
def test_list(): page = hr.Html() body = hr.Body() list = hr.Ul(id="TheList", style="line-height:200%") list.append(hr.Li("The first item in a list")) list.append(hr.Li("This is the second item", style="color: red")) item = hr.Li() item.append("And this is a ") item.append(hr.A("http://google.com", "link")) item.append("to google") list.append(item) body.append(list) page.append(body) contents = render_result(page) assert '<ul "id=TheList" "style=line-height:200%">' in contents assert "<li>\nThe first item in a list\n</li>" in contents.replace( " ", "") assert '<li "style=color: red">\nThis is the second item\n</li>' in contents.replace( " ", "") assert '<li>\nAnd this is a \n<a href=http://google.com>link</a>\nto google\n</li>' in contents.replace( " ", "") assert "</li>\n</ul>" in contents.replace(" ", "")
def test_recursive_render_with_multiple_paragraphs(): page = hr.Html() body = hr.Body() p1 = hr.P("Here is a paragraph of text") p2 = hr.P() p2.append("And here is another piece of text") body.append(p1) body.append(p2) page.append(body) f = StringIO() page.render(f) expected = "<!DOCTYPE html>\n" \ "<html>\n" \ " <body>\n" \ " <p>\n" \ " Here is a paragraph of text\n" \ " </p>\n" \ " <p>\n" \ " And here is another piece of text\n" \ " </p>\n" \ " </body>\n" \ "</html>\n" assert f.getvalue() == expected
def test_A_204(self): # Test `a` rendering within doc hierarchy self.e2 = hr.P( " \t Here's \t Britannica's \nworst\t nightmare: \n\n\t ") self.e3 = hr.A(" https://www.wikipedia.org/ ", '\t \t \n Grand \t \tWiki \t\n \t') self.e2.append(self.e3) self.e2.append(" \t . \n ") self.e1 = hr.Body(self.e2) with open(consts.test_filename, 'w') as self.fobj: self.assertTrue(self.e1.render(self.fobj, ' ')) with open(consts.test_filename, 'r') as self.file: self.strs_out = self.file.readlines() self.assertEqual(len(self.strs_out), 7) # Verify total lines self.assertEqual(self.strs_out[0], '<body>\n') self.assertEqual(self.strs_out[1], ' <p>\n') self.assertEqual(self.strs_out[2], " Here's Britannica's worst nightmare:\n") self.assertEqual( self.strs_out[3], ' ' '<a href="https://www.wikipedia.org/">Grand Wiki</a>\n') self.assertEqual(self.strs_out[4], ' .\n') self.assertEqual(self.strs_out[5], ' </p>\n') self.assertEqual(self.strs_out[6], '</body>\n')
def test_Body_Ul_test_Content_style(): body = hr.Body() unordered = hr.Ul(id="Test 53", style="Test 54") body.append(unordered) testdata = body.content assert testdata == "<body>\n <ul id=\"Test 53\" style=\"Test 54\">\n </ul>\n</body>"
def test_Body_H_test_Content(): body = hr.Body() body.append(hr.H(2, "Test 50")) testdata = body.content assert testdata == "<body>\n <h2>Test 50</h2>\n</body>"
def test_Body_A_test_Content(): body = hr.Body() body.append(hr.A("Test 32", "Test 33")) testdata = body.content assert testdata == "<body>\n <a href=\"Test 32\">Test 33</a>\n</body>"
def test_Body_test_Content_2(): body = hr.Body() body.append("Test 31") testdata = body.content assert testdata == "<body>\n Test 31\n</body>"
def test_Body_P_Hr_test_Content_style(): body = hr.Body() body.append(hr.P("Test 24", style="Test 25")) body.append(hr.Hr()) testdata = body.content assert testdata == "<body>\n <p style=\"Test 25\">\n Test 24\n </p>\n <hr />\n</body>"
# page.append(body) # render_page(page, "test_html_output6.html") # # Step 7 # ######### page = html_render.Html() head = html_render.Head() head.append(html_render.Title("PythonClass = Revision 1087:")) page.append(head) body = html_render.Body() body.append(html_render.H(2, "PythonClass - Class 6 example")) body.append( html_render.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(html_render.Hr()) list = html_render.Ul(id="TheList", style="line-height:200%") list.append(html_render.Li("The first item in a list")) list.append(html_render.Li("This is the second item", style="color: red"))
def test_Body(): body = hr.Body() testdata = body.content assert testdata == "<body>\n</body>"
def test_Body_P_test_Content(): body = hr.Body() body.append(hr.P("Test 4")) body.append(hr.P("Test 5")) testdata = body.content assert testdata == "<body>\n <p>\n Test 4\n </p>\n <p>\n Test 5\n </p>\n</body>"
def test_Body_P_test_Content_style(): body = hr.Body() body.append(hr.P("Test 17", style="Test 18")) testdata = body.content assert testdata == "<body>\n <p style=\"Test 18\">\n Test 17\n </p>\n</body>"
# page.append(body) # # render_page(page, "test_html_output7.html") # # Step 8 # ######## page = hr.Html() head = hr.Head() head.append(hr.Meta(charset="UTF-8")) head.append(hr.Title("PythonClass = Revision 1087:")) page.append(head) body = hr.Body() body.append(hr.H(2, "PythonClass - Example")) body.append( hr.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.Hr()) list = hr.Ul(id="TheList", style="line-height:200%") list.append(hr.Li("The first item in a list")) list.append(hr.Li("This is the second item", style="color: red"))
def test_Body_Hr_2(): body = hr.Body() body.append(hr.Hr()) testdata = body.content assert testdata == "<body>\n <hr />\n</body>"