From 9ac4dbd7b53d187ca54f28e247d3a120660938ca Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Sat, 13 Apr 2013 02:02:28 +0200 Subject: 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. --- tests/forms_tests/tests/test_widgets.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'tests/forms_tests') 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""")
""") - # 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]), '') @@ -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""")
  • """) + 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 = [ + '', + '', + '', + ] + for output, expected in zip(r, expected): + self.assertHTMLEqual(force_text(output), expected) + + # You can access individual elements + self.assertHTMLEqual(force_text(r[1]), + '') + + # Out-of-range errors are propagated + with self.assertRaises(IndexError): + r[42] + def test_multi(self): class MyMultiWidget(MultiWidget): def decompress(self, value): -- cgit v1.3