diff options
| author | Baptiste Mispelon <bmispelon@gmail.com> | 2013-04-13 02:02:28 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2013-04-13 16:37:27 +0200 |
| commit | 9ac4dbd7b53d187ca54f28e247d3a120660938ca (patch) | |
| tree | cec2d080457e97c7b6dab5593789d6921acf379b /tests | |
| parent | c4186c2fec6f5418c81366a911792bf5295db494 (diff) | |
Fixed #4592: Made CheckboxSelectMultiple more like RadioSelect
I refactored RadioSelect and CheckboxSelectMultiple to
make them inherit from a base class, allowing them to share
the behavior of being able to iterate over their subwidgets.
Thanks to Matt McClanahan for the initial patch and to
Claude Paroz for the review.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/forms_tests/tests/test_widgets.py | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/tests/forms_tests/tests/test_widgets.py b/tests/forms_tests/tests/test_widgets.py index 3c782713f3..4664553aa7 100644 --- a/tests/forms_tests/tests/test_widgets.py +++ b/tests/forms_tests/tests/test_widgets.py @@ -15,7 +15,7 @@ from django.utils import six from django.utils.translation import activate, deactivate from django.test import TestCase from django.test.utils import override_settings -from django.utils.encoding import python_2_unicode_compatible +from django.utils.encoding import python_2_unicode_compatible, force_text from ..models import Article @@ -656,7 +656,7 @@ beatle J R Ringo False""") <label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br /> <label><input type="radio" name="beatle" value="R" /> Ringo</label>""") - # A RadioFieldRenderer object also allows index access to individual RadioInput + # A RadioFieldRenderer object also allows index access to individual RadioChoiceInput w = RadioSelect() r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) self.assertHTMLEqual(str(r[1]), '<label><input type="radio" name="beatle" value="P" /> Paul</label>') @@ -665,11 +665,8 @@ beatle J R Ringo False""") self.assertFalse(r[1].is_checked()) self.assertEqual((r[1].name, r[1].value, r[1].choice_value, r[1].choice_label), ('beatle', 'J', 'P', 'Paul')) - try: + with self.assertRaises(IndexError): r[10] - self.fail("This offset should not exist.") - except IndexError: - pass # Choices are escaped correctly w = RadioSelect() @@ -817,6 +814,25 @@ beatle J R Ringo False""") <li><label for="abc_2"><input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" /> C</label></li> </ul>""") + w = CheckboxSelectMultiple() + r = w.get_renderer('abc', 'b', choices=[(c, c.upper()) for c in 'abc']) + # You can iterate over the CheckboxFieldRenderer to get individual elements + expected = [ + '<label><input type="checkbox" name="abc" value="a" /> A</label>', + '<label><input checked="checked" type="checkbox" name="abc" value="b" /> B</label>', + '<label><input type="checkbox" name="abc" value="c" /> C</label>', + ] + for output, expected in zip(r, expected): + self.assertHTMLEqual(force_text(output), expected) + + # You can access individual elements + self.assertHTMLEqual(force_text(r[1]), + '<label><input checked="checked" type="checkbox" name="abc" value="b" /> B</label>') + + # Out-of-range errors are propagated + with self.assertRaises(IndexError): + r[42] + def test_multi(self): class MyMultiWidget(MultiWidget): def decompress(self, value): |
