@@ -89,19 +89,6 @@ def serialize_html(input, options):
89
89
return serializer .render (stream , encoding )
90
90
91
91
92
- def runSerializerTest (input , expected , options ):
93
- encoding = options .get ("encoding" , None )
94
-
95
- if encoding :
96
- expected = list (map (lambda x : x .encode (encoding ), expected ))
97
-
98
- result = serialize_html (input , options )
99
- if len (expected ) == 1 :
100
- assert expected [0 ] == result , "Expected:\n %s\n Actual:\n %s\n Options:\n %s" % (expected [0 ], result , str (options ))
101
- elif result not in expected :
102
- assert False , "Expected: %s, Received: %s" % (expected , result )
103
-
104
-
105
92
def throwsWithLatin1 (input ):
106
93
with pytest .raises (UnicodeEncodeError ):
107
94
serialize_html (input , {"encoding" : "iso-8859-1" })
@@ -120,13 +107,13 @@ def testDoctypeSystemId():
120
107
121
108
122
109
def testCdataCharacters ():
123
- runSerializerTest ([["StartTag" , "http://www.w3.org/1999/xhtml" , "style" , {}], ["Characters" , "\u0101 " ]],
124
- ["<style>ā" ], {"encoding" : "iso-8859-1" })
110
+ test_serializer ([["StartTag" , "http://www.w3.org/1999/xhtml" , "style" , {}], ["Characters" , "\u0101 " ]],
111
+ ["<style>ā" ], {"encoding" : "iso-8859-1" })
125
112
126
113
127
114
def testCharacters ():
128
- runSerializerTest ([["Characters" , "\u0101 " ]],
129
- ["ā" ], {"encoding" : "iso-8859-1" })
115
+ test_serializer ([["Characters" , "\u0101 " ]],
116
+ ["ā" ], {"encoding" : "iso-8859-1" })
130
117
131
118
132
119
def testStartTagName ():
@@ -138,9 +125,9 @@ def testAttributeName():
138
125
139
126
140
127
def testAttributeValue ():
141
- runSerializerTest ([["StartTag" , "http://www.w3.org/1999/xhtml" , "span" ,
142
- [{"namespace" : None , "name" : "potato" , "value" : "\u0101 " }]]],
143
- ["<span potato=ā>" ], {"encoding" : "iso-8859-1" })
128
+ test_serializer ([["StartTag" , "http://www.w3.org/1999/xhtml" , "span" ,
129
+ [{"namespace" : None , "name" : "potato" , "value" : "\u0101 " }]]],
130
+ ["<span potato=ā>" ], {"encoding" : "iso-8859-1" })
144
131
145
132
146
133
def testEndTagName ():
@@ -165,7 +152,7 @@ def testSpecQuoteAttribute(c):
165
152
else :
166
153
output_ = ['<span foo="%s">' % c ]
167
154
options_ = {"quote_attr_values" : "spec" }
168
- runSerializerTest (input_ , output_ , options_ )
155
+ test_serializer (input_ , output_ , options_ )
169
156
170
157
171
158
@pytest .mark .parametrize ("c" , list ("\t \n \u000C \x20 \r \" '=<>`"
@@ -184,7 +171,7 @@ def testLegacyQuoteAttribute(c):
184
171
else :
185
172
output_ = ['<span foo="%s">' % c ]
186
173
options_ = {"quote_attr_values" : "legacy" }
187
- runSerializerTest (input_ , output_ , options_ )
174
+ test_serializer (input_ , output_ , options_ )
188
175
189
176
190
177
@pytest .fixture
@@ -217,9 +204,23 @@ def testEntityNoResolve(lxml_parser):
217
204
assert result == '<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>'
218
205
219
206
220
- def test_serializer ():
207
+ def param_serializer ():
221
208
for filename in get_data_files ('serializer-testdata' , '*.test' , os .path .dirname (__file__ )):
222
209
with open (filename ) as fp :
223
210
tests = json .load (fp )
224
211
for test in tests ['tests' ]:
225
- yield runSerializerTest , test ["input" ], test ["expected" ], test .get ("options" , {})
212
+ yield test ["input" ], test ["expected" ], test .get ("options" , {})
213
+
214
+
215
+ @pytest .mark .parametrize ("input, expected, options" , param_serializer ())
216
+ def test_serializer (input , expected , options ):
217
+ encoding = options .get ("encoding" , None )
218
+
219
+ if encoding :
220
+ expected = list (map (lambda x : x .encode (encoding ), expected ))
221
+
222
+ result = serialize_html (input , options )
223
+ if len (expected ) == 1 :
224
+ assert expected [0 ] == result , "Expected:\n %s\n Actual:\n %s\n Options:\n %s" % (expected [0 ], result , str (options ))
225
+ elif result not in expected :
226
+ assert False , "Expected: %s, Received: %s" % (expected , result )
0 commit comments