11import os
22from html_render import Element , Body , P , Html , Head , OneLineTag , Title
3+ from html_render import Hr , Br , A , Ul , Li , H , Meta
4+ from io import StringIO
35
46
5- def render_element (el_object , filename = 'test1.html' , remove = True ):
7+ def render_element_file (el_object , filename = 'test1.html' , remove = True ):
68 with open (filename , 'w' ) as out_file :
79 el_object .render (out_file )
810 with open (filename , 'r' ) as in_file :
@@ -12,6 +14,21 @@ def render_element(el_object, filename='test1.html', remove=True):
1214 return contents
1315
1416
17+ def test_new_element ():
18+ """
19+ not much here, but it shows Elements can be initialized
20+ """
21+ el_object = Element ()
22+ el_object2 = Element ('content' )
23+
24+
25+ def render_element (element , cur_ind = "" ):
26+ sio = StringIO ()
27+ element .render (sio , cur_ind )
28+ return sio .getvalue ()
29+
30+
31+
1532def test_add_content ():
1633 el_object = Element ('content' )
1734 el_object = Element ()
@@ -45,8 +62,8 @@ def test_render():
4562 more_stuff = '\n eggs, eggs, eggs'
4663 el_object = Element (my_stuff )
4764 el_object .append (more_stuff )
48- contents = render_element (el_object )
49- contents = contents . strip ( )
65+ contents = render_element (el_object ). strip ()
66+ print ( contents )
5067 assert contents .startswith ('<html>' )
5168 assert contents .endswith ('</html>' )
5269 assert my_stuff in contents
@@ -227,3 +244,195 @@ def test_title_indendation_twothings():
227244 assert len (lines ) == 1
228245 assert lines [0 ].startswith ('<title> ' )
229246 assert my_stuff in lines [0 ]
247+
248+
249+ def test_single_attribute ():
250+ # <p style="text-align: center; font-style: oblique;">
251+ # Here is a paragraph of text
252+ # </p>
253+ p = P ("Here is a paragraph of text" ,
254+ style = "text-align: center; font-style: oblique;" )
255+ results = render_element (p ).strip () # need this to be string IO I think
256+ print (results )
257+ assert results .startswith ('<p style="text-align: center; font-style: oblique;">' )
258+
259+
260+ def test_multiple_attributes ():
261+ p = P ("here is a paragraph of text" ,
262+ id = "fred" , color = "red" , size = "12px" )
263+ contents = render_element (p ).strip ()
264+ print (contents )
265+ lines = contents .split ('\n ' )
266+ assert lines [0 ].startswith ('<p ' )
267+ assert lines [0 ].endswith ('>' )
268+ assert 'id="fred"' in lines [0 ]
269+ assert 'color="red"' in lines [0 ]
270+ assert 'size="12px"' in lines [0 ]
271+
272+
273+ def test_multiple_attributes_title ():
274+ p = Title ("here is a paragraph of text" ,
275+ id = "fred" , color = "red" , size = "12px" )
276+ contents = render_element (p ).strip ()
277+ print (contents )
278+ lines = contents .split ('\n ' )
279+ assert lines [0 ].startswith ('<title ' )
280+ assert lines [0 ].endswith ('title>' )
281+ assert 'id="fred"' in lines [0 ]
282+ assert 'color="red"' in lines [0 ]
283+ assert 'size="12px"' in lines [0 ]
284+
285+
286+ def test_class_attribute ():
287+ # Use a dictionary to get around class being a reserved word
288+ atts = {"id" : "fred" , "class" : "special" , "size" : "12px" }
289+ p = P ("here is a paragraph of text" , ** atts )
290+ contents = render_element (p ).strip ()
291+ print (contents )
292+ lines = contents .split ('\n ' )
293+ assert lines [0 ].startswith ('<p ' )
294+ assert lines [0 ].endswith ('">' )
295+ assert 'id="fred"' in lines [0 ]
296+ assert 'class="special"' in lines [0 ]
297+ assert 'size="12px"' in lines [0 ]
298+
299+
300+ def test_self_closing_tag ():
301+ atts = {"id" : "fred" , "class" : "special" , "size" : "12px" }
302+ p = Hr (** atts )
303+ contents = render_element (p ).strip ()
304+ lines = contents .split ('\n ' )
305+ print (contents )
306+ assert lines [0 ].startswith ('<hr' )
307+ assert lines [0 ].endswith ("/>" )
308+ assert 'id="fred"' in lines [0 ]
309+ assert 'class="special"' in lines [0 ]
310+ assert 'size="12px"' in lines [0 ]
311+ assert len (lines ) == 1
312+
313+ def test_self_closing_tag_string ():
314+ atts = {"id" : "fred" , "class" : "special" , "size" : "12px" }
315+ p = Br ("Now Leasing" , ** atts )
316+ contents = render_element (p ).strip ()
317+ lines = contents .split ('\n ' )
318+ print (contents )
319+ assert lines [0 ].startswith ('<br' )
320+ assert lines [0 ].endswith ("/>" )
321+ assert 'id="fred"' in lines [0 ]
322+ assert 'class="special"' in lines [0 ]
323+ assert 'size="12px"' in lines [0 ]
324+ assert len (lines ) == 1
325+ assert "Now Leasing" not in lines [0 ]
326+
327+
328+ def test_anchor_element ():
329+ p = A ("http://google.com" , "link to google" )
330+ contents = render_element (p ).strip ()
331+ print (contents )
332+ assert contents .startswith ('<a href="http:' )
333+ assert contents .endswith ('</a>' )
334+ assert 'google.com' in contents
335+ assert 'link to' in contents
336+ assert contents .index ('google.com' ) < contents .index ('link to' )
337+ assert contents .index ('link to' ) < contents .index ('</a>' )
338+ lines = contents .split ('\n ' )
339+ assert len (lines )== 1
340+
341+
342+ def test_anchor_element_additional_atts ():
343+ atts = {"size" : "12px" }
344+ p = A ("http://google.com" , "link to google" , ** atts )
345+ contents = render_element (p ).strip ()
346+ print (contents )
347+ assert contents .startswith ('<a href="http:' )
348+ assert contents .endswith ('</a>' )
349+ assert 'google.com' in contents
350+ assert 'link to' in contents
351+ assert contents .index ('google.com' ) < contents .index ('link to' )
352+ assert contents .index ('link to' ) < contents .index ('</a>' )
353+ lines = contents .split ('\n ' )
354+ assert len (lines ) == 1
355+ assert 'size="12px"' in lines [0 ]
356+
357+
358+ def test_ul_element ():
359+ atts = {"size" : "12px" }
360+ list_name = "These are a few of my favorite things"
361+ p = Ul (list_name , ** atts )
362+ contents = render_element (p ).strip ()
363+ print (contents )
364+ lines = contents .split ('\n ' )
365+ assert len (lines ) == 3
366+ assert lines [0 ].startswith ('<ul size=' )
367+ assert lines [2 ].endswith ('</ul>' )
368+ assert list_name in lines [1 ]
369+ assert 'size="12px"' in lines [0 ]
370+
371+
372+ def test_li_element ():
373+ list_name = "These are a few of my favorite things"
374+ thing1 = "Roses on raindrops"
375+ atts1 = {"size" : "12px" }
376+ thing2 = "Whiskers on kittens"
377+ atts2 = {"size" : "14px" }
378+ p = Ul (list_name )
379+ p .append (Li (thing1 , ** atts1 ))
380+ p .append (Li (thing2 , ** atts2 ))
381+ contents = render_element (p ).strip ()
382+ lines = contents .split ('\n ' )
383+ print (contents )
384+ assert len (lines ) == 9
385+ assert lines [0 ].startswith ('<ul' )
386+ assert 'size="14px"' in lines [5 ]
387+ assert lines [2 ].startswith (Element .extra_indent + '<li size=' )
388+ assert lines [3 ].startswith (2 * Element .extra_indent + thing1 )
389+ assert lines [6 ].startswith (2 * Element .extra_indent + thing2 )
390+
391+
392+ def test_header_element ():
393+ # <h2> The text of the header </h2>
394+ # THIS TEST IS PASSING BUT THE RENDERED EXAMPLE LOOKS BAD
395+ text = 'The text of the header'
396+ p = H (2 , text )
397+ contents = render_element (p ).strip ()
398+ lines = contents .split ('\n ' )
399+ print (contents )
400+ assert len (lines ) == 1
401+ assert lines [0 ].startswith ('<h2>' )
402+ assert lines [0 ].endswith ('</h2>' )
403+ assert contents .index (text ) < contents .index ('</h2>' )
404+ assert contents .index (text ) > contents .index ('<h2>' )
405+
406+
407+ def test_meta_element_dict ():
408+ # <meta charset="UTF-8" />
409+ atts = {"charset" : "UTF-8" }
410+ p = Meta (** atts )
411+ contents = render_element (p ).strip ()
412+ lines = contents .split ('\n ' )
413+ print (contents )
414+ assert len (lines )== 1
415+ assert lines [0 ].startswith ('<meta charset=' )
416+ assert lines [0 ].endswith ('"UTF-8" />' )
417+
418+
419+ def test_meta_element ():
420+ # <meta charset="UTF-8" />
421+ p = Meta (charset = "UTF-8" )
422+ contents = render_element (p ).strip ()
423+ lines = contents .split ('\n ' )
424+ print (contents )
425+ assert len (lines )== 1
426+ assert lines [0 ].startswith ('<meta charset=' )
427+ assert lines [0 ].endswith ('"UTF-8" />' )
428+
429+
430+
431+
432+
433+
434+
435+
436+
437+
438+
0 commit comments