diff options
| author | Preston Timmons <prestontimmons@gmail.com> | 2015-08-30 21:13:42 -0500 |
|---|---|---|
| committer | Preston Timmons <prestontimmons@gmail.com> | 2015-08-31 23:03:55 -0500 |
| commit | 4c30fa905d9d47b3a2ee82095b1fe56cc2ec2ab5 (patch) | |
| tree | 2bb04d4ecef539e4ebefbd6d750dfd240ddd7f0c /tests/forms_tests/widget_tests/test_textinput.py | |
| parent | 5153a3bfdcec82324d67ff79862384288cf6afe6 (diff) | |
Rewrote form widget tests as proper unittests.
This is preparation for landing the template-based widget rendering
patch and goes a long way to making these tests more useful for future
development. The old doctest heritage is strong here.
Diffstat (limited to 'tests/forms_tests/widget_tests/test_textinput.py')
| -rw-r--r-- | tests/forms_tests/widget_tests/test_textinput.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/forms_tests/widget_tests/test_textinput.py b/tests/forms_tests/widget_tests/test_textinput.py new file mode 100644 index 0000000000..33e455a160 --- /dev/null +++ b/tests/forms_tests/widget_tests/test_textinput.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.forms import TextInput +from django.utils.safestring import mark_safe + +from .base import WidgetTest + + +class TextInputTest(WidgetTest): + widget = TextInput() + + def test_render(self): + self.check_html(self.widget, 'email', '', html='<input type="text" name="email" />') + + def test_render_none(self): + self.check_html(self.widget, 'email', None, html='<input type="text" name="email" />') + + def test_render_value(self): + self.check_html(self.widget, 'email', 'test@example.com', html=( + '<input type="text" name="email" value="test@example.com" />' + )) + + def test_render_boolean(self): + """ + Boolean values are rendered to their string forms ("True" and + "False"). + """ + self.check_html(self.widget, 'get_spam', False, html=( + '<input type="text" name="get_spam" value="False" />' + )) + self.check_html(self.widget, 'get_spam', True, html=( + '<input type="text" name="get_spam" value="True" />' + )) + + def test_render_quoted(self): + self.check_html( + self.widget, 'email', 'some "quoted" & ampersanded value', + html='<input type="text" name="email" value="some "quoted" & ampersanded value" />', + ) + + def test_render_custom_attrs(self): + self.check_html( + self.widget, 'email', 'test@example.com', attrs={'class': 'fun'}, + html='<input type="text" name="email" value="test@example.com" class="fun" />', + ) + + def test_render_unicode(self): + self.check_html( + self.widget, 'email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}, + html=( + '<input type="text" name="email" ' + 'value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" class="fun" />' + ), + ) + + def test_constructor_attrs(self): + widget = TextInput(attrs={'class': 'fun', 'type': 'email'}) + self.check_html(widget, 'email', '', html='<input type="email" class="fun" name="email" />') + self.check_html( + widget, 'email', 'foo@example.com', + html='<input type="email" class="fun" value="foo@example.com" name="email" />', + ) + + def test_attrs_precedence(self): + """ + `attrs` passed to render() get precedence over those passed to the + constructor + """ + widget = TextInput(attrs={'class': 'pretty'}) + self.check_html( + widget, 'email', '', attrs={'class': 'special'}, + html='<input type="text" class="special" name="email" />', + ) + + def test_attrs_safestring(self): + widget = TextInput(attrs={'onBlur': mark_safe("function('foo')")}) + self.check_html(widget, 'email', '', html='<input onBlur="function(\'foo\')" type="text" name="email" />') |
