Refs #31026 -- Added extra form render tests.

This commit is contained in:
David Smith 2021-09-10 08:04:05 +01:00 committed by Mariusz Felisiak
parent 91e8b95d5b
commit 4ca508a689
1 changed files with 36 additions and 0 deletions

View File

@ -20,6 +20,7 @@ from django.forms.utils import ErrorList
from django.http import QueryDict
from django.template import Context, Template
from django.test import SimpleTestCase
from django.test.utils import override_settings
from django.utils.datastructures import MultiValueDict
from django.utils.safestring import mark_safe
@ -1248,6 +1249,24 @@ value="Should escape < & > and <script>alert('xss')&lt
self.assertEqual(f.errors, error_dict)
f.as_table()
self.assertEqual(f.errors, error_dict)
self.assertHTMLEqual(
f.as_table(),
'<tr><td colspan="2"><ul class="errorlist nonfield"><li>Form error</li>'
'<li>(Hidden field hidden_input) This field is required.</li></ul>'
'<input type="hidden" name="hidden_input" id="id_hidden_input"></td></tr>',
)
self.assertHTMLEqual(
f.as_ul(),
'<li><ul class="errorlist nonfield"><li>Form error</li>'
'<li>(Hidden field hidden_input) This field is required.</li></ul>'
'<input type="hidden" name="hidden_input" id="id_hidden_input"></li>',
)
self.assertHTMLEqual(
f.as_p(),
'<ul class="errorlist nonfield"><li>Form error</li>'
'<li>(Hidden field hidden_input) This field is required.</li></ul>'
'<p><input type="hidden" name="hidden_input" id="id_hidden_input"></p>',
)
def test_dynamic_construction(self):
# It's possible to construct a Form dynamically by adding to the self.fields
@ -3637,6 +3656,23 @@ Password: <input type="password" name="password" required>
self.assertIsInstance(field_copy, CustomCharField)
self.assertIsNot(field_copy.error_messages, field.error_messages)
def test_label_does_not_include_new_line(self):
form = Person()
field = form['first_name']
self.assertEqual(
field.label_tag(),
'<label for="id_first_name">First name:</label>',
)
@override_settings(USE_THOUSAND_SEPARATOR=True)
def test_label_attrs_not_localized(self):
form = Person()
field = form['first_name']
self.assertHTMLEqual(
field.label_tag(attrs={'number': 9999}),
'<label number="9999" for="id_first_name">First name:</label>',
)
class CustomRenderer(DjangoTemplates):
pass