33
44Basic tests for rendering HTML
55"""
6+
67import pytest
78
89from pyhtml import (
1920 script ,
2021 span ,
2122 title ,
23+ pre ,
2224)
2325
2426
2527def test_renders_single_element ():
2628 doc = body ()
2729
28- assert str (doc ) == ' <body></body>'
30+ assert str (doc ) == " <body></body>"
2931
3032
3133def test_renders_elements_with_children ():
@@ -34,12 +36,14 @@ def test_renders_elements_with_children():
3436 div (),
3537 )
3638
37- assert str (doc ) == '\n ' .join ([
38- '<body>' ,
39- ' <span></span>' ,
40- ' <div></div>' ,
41- '</body>' ,
42- ])
39+ assert str (doc ) == "\n " .join (
40+ [
41+ "<body>" ,
42+ " <span></span>" ,
43+ " <div></div>" ,
44+ "</body>" ,
45+ ]
46+ )
4347
4448
4549def test_renders_deeply_nested_children ():
@@ -49,15 +53,17 @@ def test_renders_deeply_nested_children():
4953 ),
5054 )
5155
52- assert str (doc ) == '\n ' .join ([
53- '<body>' ,
54- ' <div>' ,
55- ' <span>' ,
56- ' Hello world' ,
57- ' </span>' ,
58- ' </div>' ,
59- '</body>' ,
60- ])
56+ assert str (doc ) == "\n " .join (
57+ [
58+ "<body>" ,
59+ " <div>" ,
60+ " <span>" ,
61+ " Hello world" ,
62+ " </span>" ,
63+ " </div>" ,
64+ "</body>" ,
65+ ]
66+ )
6167
6268
6369def test_renders_attributes ():
@@ -67,11 +73,13 @@ def test_renders_attributes():
6773
6874
6975def test_comment_renders ():
70- assert str (Comment ("Hello world" )) == '\n ' .join ([
71- '<!--' ,
72- ' Hello world' ,
73- '-->' ,
74- ])
76+ assert str (Comment ("Hello world" )) == "\n " .join (
77+ [
78+ "<!--" ,
79+ " Hello world" ,
80+ "-->" ,
81+ ]
82+ )
7583
7684
7785def test_comments_not_callable ():
@@ -83,64 +91,71 @@ def test_comments_not_callable():
8391def test_call_adds_attrs ():
8492 """Calling a tag adds additional attributes"""
8593 # Note that order is important - elements added first should appear first
86- assert str (body (foo = "bar" )(baz = "bat" )) \
87- == '<body foo="bar" baz="bat"></body>'
94+ assert (
95+ str (body (foo = "bar" )(baz = "bat" )) == '<body foo="bar" baz="bat"></body>'
96+ )
8897
8998
9099def test_call_adds_children ():
91100 """Calling a tag adds additional children"""
92101 # Note that order is important again
93- assert str (body ("Hello" )("World" )) == '\n ' .join ([
94- '<body>' ,
95- ' Hello' ,
96- ' World' ,
97- '</body>' ,
98- ])
102+ assert str (body ("Hello" )("World" )) == "\n " .join (
103+ [
104+ "<body>" ,
105+ " Hello" ,
106+ " World" ,
107+ "</body>" ,
108+ ]
109+ )
99110
100111
101112def test_call_adds_mixed_attrs_children ():
102113 """Calling a tag adds more properties"""
103- assert str (body (foo = "bar" )("Hello" )) == "\n " .join ([
104- '<body foo="bar">' ,
105- ' Hello' ,
106- '</body>' ,
107- ])
114+ assert str (body (foo = "bar" )("Hello" )) == "\n " .join (
115+ [
116+ '<body foo="bar">' ,
117+ " Hello" ,
118+ "</body>" ,
119+ ]
120+ )
108121
109122
110123def test_call_adds_mixed_attrs_children_link ():
111124 """Calling a tag adds more properties, using an anchor tag"""
112125 assert str (
113126 a (href = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" )("Click me" )
114- ) == "\n " .join ([
115- '<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">' ,
116- ' Click me' ,
117- '</a>' ,
118- ])
127+ ) == "\n " .join (
128+ [
129+ '<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">' ,
130+ " Click me" ,
131+ "</a>" ,
132+ ]
133+ )
119134
120135
121136def test_call_adds_mixed_attrs_children_script ():
122137 """Calling a tag adds more properties, using a script tag"""
123- assert str (
124- script ( type = "blah/blah" )( "// Some JS" )
125- ) == " \n " . join ([
126- '<script type="blah/blah">' ,
127- ' // Some JS' ,
128- '</script>' ,
129- ] )
138+ assert str (script ( type = "blah/blah" )( "// Some JS" )) == " \n " . join (
139+ [
140+ '<script type="blah/blah">' ,
141+ " // Some JS" ,
142+ "</script>" ,
143+ ]
144+ )
130145
131146
132147def test_call_adds_mixed_attrs_children_script_2 ():
133148 """Calling a tag adds more properties, using a script tag"""
134- assert str (
135- script ( "// Some JS" )( type = "blah/blah" )
136- ) == " \n " . join ([
137- '<script type="blah/blah">' ,
138- ' // Some JS' ,
139- '</script>' ,
140- ] )
149+ assert str (script ( "// Some JS" )( type = "blah/blah" )) == " \n " . join (
150+ [
151+ '<script type="blah/blah">' ,
152+ " // Some JS" ,
153+ "</script>" ,
154+ ]
155+ )
141156
142157
143- def test_tags_with_trailing_undercore_render_without ():
158+ def test_tags_with_trailing_underscore_render_without ():
144159 """
145160 Some tags have a trailing underscore to avoid name collisions. When
146161 rendering to HTML, is this removed?
@@ -161,26 +176,28 @@ def test_larger_page():
161176 ),
162177 )
163178
164- assert str (my_website ) == '\n ' .join ([
165- '<!DOCTYPE html>' ,
166- '<html>' ,
167- ' <head>' ,
168- ' <title>' ,
169- ' Hello, world!' ,
170- ' </title>' ,
171- ' <script type="text/javascript" '
172- 'src="http://example.com/script.js"></script>' ,
173- ' </head>' ,
174- ' <body>' ,
175- ' <h1>' ,
176- ' Hello, world!' ,
177- ' </h1>' ,
178- ' <span>' ,
179- ' This is my amazing website rendered with PyHTML Enhanced!' ,
180- ' </span>' ,
181- ' </body>' ,
182- '</html>' ,
183- ])
179+ assert str (my_website ) == "\n " .join (
180+ [
181+ "<!DOCTYPE html>" ,
182+ "<html>" ,
183+ " <head>" ,
184+ " <title>" ,
185+ " Hello, world!" ,
186+ " </title>" ,
187+ ' <script type="text/javascript" '
188+ 'src="http://example.com/script.js"></script>' ,
189+ " </head>" ,
190+ " <body>" ,
191+ " <h1>" ,
192+ " Hello, world!" ,
193+ " </h1>" ,
194+ " <span>" ,
195+ " This is my amazing website rendered with PyHTML Enhanced!" ,
196+ " </span>" ,
197+ " </body>" ,
198+ "</html>" ,
199+ ]
200+ )
184201
185202
186203def test_format_through_repr ():
@@ -197,16 +214,18 @@ def test_flatten_element_lists():
197214 """
198215 doc = body ([span ("Hello" ), span ("world" )])
199216
200- assert str (doc ) == "\n " .join ([
201- "<body>" ,
202- " <span>" ,
203- " Hello" ,
204- " </span>" ,
205- " <span>" ,
206- " world" ,
207- " </span>" ,
208- "</body>" ,
209- ])
217+ assert str (doc ) == "\n " .join (
218+ [
219+ "<body>" ,
220+ " <span>" ,
221+ " Hello" ,
222+ " </span>" ,
223+ " <span>" ,
224+ " world" ,
225+ " </span>" ,
226+ "</body>" ,
227+ ]
228+ )
210229
211230
212231def test_flatten_element_generators ():
@@ -216,12 +235,14 @@ def test_flatten_element_generators():
216235 """
217236 doc = body (c for c in "hi" )
218237
219- assert str (doc ) == "\n " .join ([
220- "<body>" ,
221- " h" ,
222- " i" ,
223- "</body>" ,
224- ])
238+ assert str (doc ) == "\n " .join (
239+ [
240+ "<body>" ,
241+ " h" ,
242+ " i" ,
243+ "</body>" ,
244+ ]
245+ )
225246
226247
227248def test_flatten_element_other_sequence ():
@@ -231,12 +252,14 @@ def test_flatten_element_other_sequence():
231252 """
232253 doc = body (("h" , "i" ))
233254
234- assert str (doc ) == "\n " .join ([
235- "<body>" ,
236- " h" ,
237- " i" ,
238- "</body>" ,
239- ])
255+ assert str (doc ) == "\n " .join (
256+ [
257+ "<body>" ,
258+ " h" ,
259+ " i" ,
260+ "</body>" ,
261+ ]
262+ )
240263
241264
242265def test_classes_can_render ():
@@ -245,11 +268,13 @@ def test_classes_can_render():
245268 """
246269 doc = body (br )
247270
248- assert str (doc ) == "\n " .join ([
249- "<body>" ,
250- " <br/>" ,
251- "</body>" ,
252- ])
271+ assert str (doc ) == "\n " .join (
272+ [
273+ "<body>" ,
274+ " <br/>" ,
275+ "</body>" ,
276+ ]
277+ )
253278
254279
255280def test_boolean_tag_attributes_true ():
@@ -273,3 +298,24 @@ def test_tag_with_pre_content():
273298 Do tags with defined pre-content render correctly
274299 """
275300 assert str (html ()) == "<!DOCTYPE html>\n <html></html>"
301+
302+
303+ def test_whitespace_sensitive_no_content ():
304+ """
305+ Do whitespace-sensitive tags render properly when they have no content?
306+ """
307+ assert str (pre ()) == "<pre></pre>"
308+
309+
310+ def test_whitespace_sensitive_with_content ():
311+ """
312+ Do whitespace-sensitive tags render properly when they have content?
313+ """
314+ assert str (pre ("hi" )) == "<pre>hi</pre>"
315+
316+
317+ def test_whitespace_sensitive_with_attrs ():
318+ """
319+ Do whitespace-sensitive tags render properly when they have attributes?
320+ """
321+ assert str (pre (test = "test" )("hi" )) == '<pre test="test">hi</pre>'
0 commit comments