Skip to content

Commit 5c59fac

Browse files
committed
added tests to cover step 8
1 parent ef4057d commit 5c59fac

File tree

1 file changed

+186
-42
lines changed

1 file changed

+186
-42
lines changed

students/EricAdams/session08/test_html_render.py

Lines changed: 186 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import os
55
from io import StringIO
66

7-
from html_render import Element, Body, P, Html, Head, Title
7+
from html_render import Element, Body, P, Html, Head, Title, Hr, Br, A, H,\
8+
Ul, Li, Doc, Meta
89

910

1011
# test utilities
@@ -17,11 +18,11 @@ def render_element_file(element, filename='temp_render_file.html',
1718
This version uses an actual file on disk -- yu may want to use it so you
1819
can see the file afterward.
1920
20-
:param element: the element to be rendered (its render() method will be
21-
called)
21+
:param element: the element to be rendered (its render() method will
22+
be called)
2223
2324
:param filename='temp_render_file.html': the name of the temporary file
24-
to be used.
25+
to be used.
2526
2627
:param remove=True: Whether to remove the file after using it to render.
2728
Set this to True if you want to be able to look at
@@ -45,20 +46,20 @@ def render_element(element, cur_ind=""):
4546
4647
This can be used by multiple tests.
4748
48-
:param element: the element to be rendered (its render() method will
49-
be called)
49+
:param element: the element to be rendered (its render() method will be
50+
called)
5051
5152
:param filename='temp_render_file.html': the name of the temporary file
52-
to be used.
53+
to be used.
5354
5455
:param remove=True: Whether to remove the file after using it to render.
5556
Set this to True if you want to be able to look at
56-
after the tests run.
57+
after the tests run.
5758
5859
NOTE: - this could be refactored, and still used everywhere.
5960
"""
6061
sio = StringIO()
61-
element.render(sio, cur_indent=cur_ind)
62+
element.render(sio, cur_ind)
6263
# if remove:
6364
# os.remove(filename)
6465
return sio.getvalue()
@@ -75,37 +76,33 @@ def test_new_element():
7576
# careful here -- this is testing internal implimentations
7677
# sometimes helpful as you are developing, but you may want to remove
7778
# these tests once you have more working.
78-
def test_add_content():
79-
el_object = Element('content')
80-
assert hasattr(el_object.content[0], 'render') is True
79+
# def test_add_content():
80+
# el_object = Element('content')
81+
# assert hasattr(el_object.content, 'render')
8182

82-
el_object = Element()
83-
assert el_object.content == []
83+
# el_object = Element()
84+
# assert el_object.content == []
8485

8586

86-
def test_adding_empty_string():
87-
el_object = Element('')
88-
assert el_object.content == []
87+
# def test_adding_empty_string():
88+
# el_object = Element('')
89+
# assert el_object.content == ['']
8990

9091

91-
# adding content and appending should
92-
# give a list of two render.TextWrappers
93-
def test_append_string():
94-
el_object = Element('spam, spam, spam')
95-
el_object.append(', wonderful spam')
96-
assert len(el_object.content) == 2
97-
assert hasattr(el_object.content[0], 'render')
98-
assert hasattr(el_object.content[1], 'render')
92+
# def test_append_string():
93+
# el_object = Element('spam, spam, spam')
94+
# el_object.append(', wonderful spam')
95+
# assert el_object.content == ['spam, spam, spam', ', wonderful spam']
9996

10097

101-
def test_tag_exists():
102-
assert Element.tag == 'html'
103-
el_object = Element('spam, spam, spam')
104-
assert el_object.tag == 'html'
98+
# def test_tag_exists():
99+
# assert Element.tag == 'html'
100+
# el_object = Element('spam, spam, spam')
101+
# assert el_object.tag == 'html'
105102

106103

107-
def test_indent_exists():
108-
assert Element.indent == ' '
104+
# def test_indent_exists():
105+
# assert Element.indent == ' '
109106

110107

111108
# Now we get tot he real "meat" of the tests --does the code do what
@@ -116,7 +113,6 @@ def test_render():
116113
more_stuff = 'eggs, eggs, eggs'
117114
el_object.append(more_stuff)
118115
contents = render_element(el_object).strip()
119-
print(contents)
120116
assert contents.startswith('<html>')
121117
assert contents.endswith('</html>')
122118
assert my_stuff in contents
@@ -228,8 +224,8 @@ def test_render_non_strings2():
228224
print(contents) # so we can see what's going on if a test fails
229225

230226
# so what should the results be?
231-
# the html tag is the outer tag, so the contents should start and
232-
# end with that.
227+
# the html tag is the outer tag, so the contents should start and end
228+
# with that.
233229
assert contents.startswith('<html>')
234230
assert contents.endswith('</html>')
235231

@@ -245,8 +241,8 @@ def test_render_non_strings2():
245241
assert 'even more random text' in contents
246242
assert 'and this is a different string' in contents
247243

248-
# you could, of course, test much more..but hopefully other things
249-
# are tested, too.
244+
# you could, of course, test much more..but hopefully other things are
245+
# tested, too.
250246

251247

252248
def test_indent():
@@ -273,7 +269,9 @@ def test_indent_contents():
273269

274270
print(file_contents)
275271
lines = file_contents.split("\n")
272+
print(lines)
276273
assert lines[1].startswith(Element.indent)
274+
# assert False
277275

278276

279277
def test_multiple_indent():
@@ -347,12 +345,11 @@ def test_full_page_with_title():
347345

348346
body = Body()
349347

350-
body.append(P(
351-
"Here is a paragraph of text -- there could be more of them, "
352-
"but this is enough to show that we can do some text"))
353-
body.append(P(
354-
"And here is another piece of text -- you should be able to add any"
355-
"number"))
348+
body.append(P("Here is a paragraph of text -- there could be more of"
349+
" them, but this is enough to show that we can do some"
350+
" text"))
351+
body.append(P("And here is another piece of text -- you should be able"
352+
" to add any number"))
356353

357354
page.append(body)
358355

@@ -362,3 +359,150 @@ def test_full_page_with_title():
362359

363360
# uncomment this to see results
364361
# assert False
362+
363+
364+
def test_single_attribute():
365+
# <p style="text-align: center; font-style: oblique;">
366+
# Here is a paragraph of text -- there could be more of them,
367+
# but this is enough to show that we can do some text</p>
368+
p = P("Here is a paragraph of text",
369+
style="text-align: center; font-style: oblique;")
370+
371+
results = render_element(p)
372+
373+
assert results.startswith(
374+
'<p style="text-align: center; font-style: oblique;">')
375+
376+
print(results)
377+
# assert False
378+
379+
380+
def test_multiple_attributes():
381+
# <p style="text-align: center; font-style: oblique;">
382+
# Here is a paragraph of text -- there could
383+
# be more of them, but this is enough to show that we can do some text
384+
# </p>
385+
p = P("Here is a paragraph of text",
386+
id="fred",
387+
color="red",
388+
size="12px",
389+
)
390+
391+
results = render_element(p)
392+
print(results)
393+
394+
lines = results.split('\n')
395+
assert lines[0].startswith('<p ')
396+
assert lines[0].endswith('"> ')
397+
assert 'id="fred"' in lines[0]
398+
assert 'color="red"' in lines[0]
399+
assert 'size="12px"' in lines[0]
400+
401+
402+
def test_multiple_attributes_title():
403+
t = Title("Here is a paragraph of text",
404+
id="fred",
405+
color="red",
406+
size="12px",
407+
)
408+
409+
results = render_element(t)
410+
print(results)
411+
412+
lines = results.split('\n')
413+
assert lines[0].startswith('<title ')
414+
assert lines[0].endswith('</title>')
415+
assert 'id="fred"' in lines[0]
416+
assert 'color="red"' in lines[0]
417+
assert 'size="12px"' in lines[0]
418+
419+
420+
# test class attribute
421+
def test_class_attribute():
422+
atts = {"id": "fred",
423+
"class": "special",
424+
"size": "12px",
425+
}
426+
p = P("Here is a paragraph of text",
427+
**atts)
428+
429+
results = render_element(p)
430+
print(results)
431+
432+
lines = results.split('\n')
433+
assert lines[0].startswith('<p ')
434+
assert lines[0].strip().endswith('">')
435+
assert 'id="fred"' in lines[0]
436+
assert 'class="special"' in lines[0]
437+
assert 'size="12px"' in lines[0]
438+
439+
440+
def test_hr_single_attribute():
441+
hrule = Hr(align="left")
442+
results = render_element(hrule)
443+
print(results)
444+
assert results.startswith('<hr align="left">')
445+
# assert False
446+
447+
448+
def test_hr_multiple_attributes():
449+
hrule = Hr(align="left",
450+
width="50%",
451+
)
452+
results = render_element(hrule)
453+
print(results)
454+
assert results.startswith('<hr align="left" width="50%">')
455+
456+
457+
def test_br():
458+
br = Br()
459+
results = render_element(br)
460+
print(results)
461+
assert results.startswith('<br>')
462+
# assert False
463+
464+
465+
def test_anchor_single_attribute():
466+
a = A("http://google.com", "link to google")
467+
results = render_element(a)
468+
print(results)
469+
assert results.startswith('<a href="http://google.com">link to google</a')
470+
# assert False
471+
472+
473+
def test_heading1_with_one_attribute():
474+
heading = H(1, "Heading 1", align="left")
475+
results = render_element(heading)
476+
assert results.startswith('<h1 align="left">Heading 1</h1>')
477+
print(results)
478+
# assert False
479+
480+
481+
def test_unordered_list_with_one_attribute():
482+
unord_list = Ul(align="left")
483+
results = render_element(unord_list)
484+
print(results)
485+
assert results.startswith('<ul align="left"> \n</ul>')
486+
487+
488+
def test_list_tag_with_one_attribute():
489+
li = Li(align="left")
490+
results = render_element(li)
491+
print(results)
492+
assert results.startswith('<li align="left"> \n</li>')
493+
494+
495+
def test_doc_type():
496+
doc = Doc("something")
497+
results = render_element(doc)
498+
print(repr(results))
499+
assert results.startswith('!DOCTYPE html\n<html> \n something\n</html>')
500+
# assert False
501+
502+
503+
def test_meta_single_attribute():
504+
meta_tag = Meta(charset="UTF-8")
505+
results = render_element(meta_tag)
506+
print(results)
507+
assert results.startswith('<meta charset="UTF-8">')
508+
# assert False

0 commit comments

Comments
 (0)