1. from django.forms import CharField, Form, TextInput
    
  2. from django.utils.safestring import mark_safe
    
  3. 
    
  4. from .base import WidgetTest
    
  5. 
    
  6. 
    
  7. class TextInputTest(WidgetTest):
    
  8.     widget = TextInput()
    
  9. 
    
  10.     def test_render(self):
    
  11.         self.check_html(
    
  12.             self.widget, "email", "", html='<input type="text" name="email">'
    
  13.         )
    
  14. 
    
  15.     def test_render_none(self):
    
  16.         self.check_html(
    
  17.             self.widget, "email", None, html='<input type="text" name="email">'
    
  18.         )
    
  19. 
    
  20.     def test_render_value(self):
    
  21.         self.check_html(
    
  22.             self.widget,
    
  23.             "email",
    
  24.             "[email protected]",
    
  25.             html=('<input type="text" name="email" value="[email protected]">'),
    
  26.         )
    
  27. 
    
  28.     def test_render_boolean(self):
    
  29.         """
    
  30.         Boolean values are rendered to their string forms ("True" and
    
  31.         "False").
    
  32.         """
    
  33.         self.check_html(
    
  34.             self.widget,
    
  35.             "get_spam",
    
  36.             False,
    
  37.             html=('<input type="text" name="get_spam" value="False">'),
    
  38.         )
    
  39.         self.check_html(
    
  40.             self.widget,
    
  41.             "get_spam",
    
  42.             True,
    
  43.             html=('<input type="text" name="get_spam" value="True">'),
    
  44.         )
    
  45. 
    
  46.     def test_render_quoted(self):
    
  47.         self.check_html(
    
  48.             self.widget,
    
  49.             "email",
    
  50.             'some "quoted" & ampersanded value',
    
  51.             html=(
    
  52.                 '<input type="text" name="email" '
    
  53.                 'value="some &quot;quoted&quot; &amp; ampersanded value">'
    
  54.             ),
    
  55.         )
    
  56. 
    
  57.     def test_render_custom_attrs(self):
    
  58.         self.check_html(
    
  59.             self.widget,
    
  60.             "email",
    
  61.             "[email protected]",
    
  62.             attrs={"class": "fun"},
    
  63.             html=(
    
  64.                 '<input type="text" name="email" value="[email protected]" class="fun">'
    
  65.             ),
    
  66.         )
    
  67. 
    
  68.     def test_render_unicode(self):
    
  69.         self.check_html(
    
  70.             self.widget,
    
  71.             "email",
    
  72.             "ŠĐĆŽćžšđ",
    
  73.             attrs={"class": "fun"},
    
  74.             html=(
    
  75.                 '<input type="text" name="email" '
    
  76.                 'value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" class="fun">'
    
  77.             ),
    
  78.         )
    
  79. 
    
  80.     def test_constructor_attrs(self):
    
  81.         widget = TextInput(attrs={"class": "fun", "type": "email"})
    
  82.         self.check_html(
    
  83.             widget, "email", "", html='<input type="email" class="fun" name="email">'
    
  84.         )
    
  85.         self.check_html(
    
  86.             widget,
    
  87.             "email",
    
  88.             "[email protected]",
    
  89.             html=(
    
  90.                 '<input type="email" class="fun" value="[email protected]" name="email">'
    
  91.             ),
    
  92.         )
    
  93. 
    
  94.     def test_attrs_precedence(self):
    
  95.         """
    
  96.         `attrs` passed to render() get precedence over those passed to the
    
  97.         constructor
    
  98.         """
    
  99.         widget = TextInput(attrs={"class": "pretty"})
    
  100.         self.check_html(
    
  101.             widget,
    
  102.             "email",
    
  103.             "",
    
  104.             attrs={"class": "special"},
    
  105.             html='<input type="text" class="special" name="email">',
    
  106.         )
    
  107. 
    
  108.     def test_attrs_safestring(self):
    
  109.         widget = TextInput(attrs={"onBlur": mark_safe("function('foo')")})
    
  110.         self.check_html(
    
  111.             widget,
    
  112.             "email",
    
  113.             "",
    
  114.             html='<input onBlur="function(\'foo\')" type="text" name="email">',
    
  115.         )
    
  116. 
    
  117.     def test_use_required_attribute(self):
    
  118.         # Text inputs can safely trigger the browser validation.
    
  119.         self.assertIs(self.widget.use_required_attribute(None), True)
    
  120.         self.assertIs(self.widget.use_required_attribute(""), True)
    
  121.         self.assertIs(self.widget.use_required_attribute("resume.txt"), True)
    
  122. 
    
  123.     def test_fieldset(self):
    
  124.         class TestForm(Form):
    
  125.             template_name = "forms_tests/use_fieldset.html"
    
  126.             field = CharField(widget=self.widget)
    
  127. 
    
  128.         form = TestForm()
    
  129.         self.assertIs(self.widget.use_fieldset, False)
    
  130.         self.assertHTMLEqual(
    
  131.             '<div><label for="id_field">Field:</label>'
    
  132.             '<input type="text" name="field" required id="id_field"></div>',
    
  133.             form.render(),
    
  134.         )