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_render_indent():
    my_stuff = 'indentedstuff'
    el_object = HTML(my_stuff)
    el_object.indent = 5
    with open('test-ind', "w") as out_file:
        el_object.render(out_file)
    with open('test-ind', 'r') as in_file:
        contents = in_file.read()
    assert el_object.indent == 5
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_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_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_element(html, cur_ind="")

    print(file_contents)
    lines = file_contents.split("\n")
    assert lines[1].startswith(Element.indent)
def test_indent():
    """
    Tests that the indentation gets passed through to the renderer
    """
    html = HTML("some content")
    file_contents = render_element(html, cur_ind="   ")

    print(file_contents)
    lines = file_contents.split("\n")

    assert lines[0].startswith("   <")
    assert lines[-1].startswith("   <")
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(
        Para("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(
        Para(
            "And here is another piece of text -- you should be able to add any number"
        ))

    page.append(body)

    file_contents = render_element(page)

    print(file_contents)
def test_render_HTML():
    my_stuff = 'hamma, hamma, hamma'
    el_object = HTML(my_stuff)
    more_stuff = "umma, umma, umma"
    el_object.append(more_stuff)
    with open('testh', 'w') as out_file:
        el_object.render(out_file)
    with open('testh', 'r') as in_file:
        contents = in_file.read()
    assert contents.startswith('<html>')
    assert contents.endswith('</html>')
    assert my_stuff in contents
    assert more_stuff in contents
def test_multiple_indent():
    """
    make sure multiple levels get indented properly
    """
    body = Body()
    body.append(Para("some text"))
    body.append(Para("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 + "<")