Skip to content

Commit 12c147c

Browse files
committed
forms: simplify code and improve test
make the response more likely to be a real-world response
1 parent dd11e56 commit 12c147c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

tests/test_forms.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,26 +288,33 @@ def test_textarea_emptyfirstline(self):
288288
class TestFormLint(unittest.TestCase):
289289

290290
def test_form_lint(self):
291-
form = webtest.Form(None, '''<form>
291+
def _build_response(html):
292+
return webtest.TestResponse('<body>{}</body>'.format(html))
293+
294+
html = '''<form>
292295
<input type="text" name="field"/>
293-
</form>''')
296+
</form>'''
297+
form = webtest.Form(_build_response(html), html)
294298
self.assertRaises(AttributeError, form.lint)
295299

296-
form = webtest.Form(None, '''<form>
300+
html = '''<form>
297301
<input type="text" id="myfield" name="field"/>
298-
</form>''')
302+
</form>'''
303+
form = webtest.Form(_build_response(html), html)
299304
self.assertRaises(AttributeError, form.lint)
300305

301-
form = webtest.Form(None, '''<form>
306+
html = '''<form>
302307
<label for="myfield">my field</label>
303308
<input type="text" id="myfield" name="field"/>
304-
</form>''')
309+
</form>'''
310+
form = webtest.Form(_build_response(html), html)
305311
form.lint()
306312

307-
form = webtest.Form(None, '''<form>
313+
html = '''<form>
308314
<label class="field" for="myfield" role="r">my field</label>
309315
<input type="text" id="myfield" name="field"/>
310-
</form>''')
316+
</form>'''
317+
form = webtest.Form(_build_response(html), html)
311318
form.lint()
312319

313320

webtest/forms.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,10 @@ def _parse_fields(self):
433433
field_order = []
434434
tags = ('input', 'select', 'textarea', 'button')
435435
inner_elts = self.html.find_all(tags)
436-
if self.response:
437-
def _form_elt_filter(tag):
438-
return tag in inner_elts or tag.attrs.get('form') == self.id
439-
elements = self.response.html.find_all(_form_elt_filter)
440-
else:
441-
elements = inner_elts
436+
def _form_elt_filter(tag):
437+
return tag in inner_elts or (
438+
tag.attrs.get('form') == self.id and tag.name in tags)
439+
elements = self.response.html.find_all(_form_elt_filter)
442440
for pos, node in enumerate(elements):
443441
attrs = dict(node.attrs)
444442
tag = node.name

0 commit comments

Comments
 (0)