Exemplo n.º 1
0
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')
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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_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')
Exemplo n.º 6
0
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
Exemplo n.º 8
0
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>'
Exemplo n.º 9
0
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."
Exemplo n.º 10
0
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>'
Exemplo n.º 11
0
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>'
Exemplo n.º 12
0
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>'
Exemplo n.º 13
0
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>'
Exemplo n.º 14
0
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>'
Exemplo n.º 15
0
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
Exemplo n.º 16
0
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>'
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
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>")
Exemplo n.º 19
0
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>")
Exemplo n.º 20
0
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>")
Exemplo n.º 21
0
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>")
Exemplo n.º 22
0
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>")
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
Exemplo n.º 24
0
a.append ("some stuff")
a.append ("other stuff")

b=Body()
b.append ("body stuff")
b.append ("body stuff II")

#print (a.content)
#print (a.tag)

#print (b.content)
#print (b.tag)

#a.append(b)
#print (a.content)

outfile = io.StringIO()	
a.render (outfile, "")
b.render (outfile, "")

a.append(b)
#print (a.__class__.__name__)
a.render (outfile, "")
#print (a.content)

#c = "a string"
#print (c.__class__.__name__)

f = open('test.out', 'w')
f.write (a)
Exemplo n.º 25
0
def test_html():
    test = Html(content='an html tag')
    f = StringIO()
    test.render(f)
    assert f.getvalue(
    ) == '<!DOCTYPE html>\n<html>\n    an html tag\n</html>\n'