Beispiel #1
0
 def setUp(self):
     super(TestLaunchpadRadioWidgetWithDescription, self).setUp()
     self.request = LaunchpadTestRequest()
     field = Choice(__name__='test_field', vocabulary=self.TestEnum)
     self.field = field.bind(object())
     self.widget = LaunchpadRadioWidgetWithDescription(
         self.field, self.TestEnum, self.request)
 def setUp(self):
     super(TestLaunchpadRadioWidgetWithDescription, self).setUp()
     self.request = LaunchpadTestRequest()
     field = Choice(__name__='test_field', vocabulary=self.TestEnum)
     self.field = field.bind(object())
     self.widget = LaunchpadRadioWidgetWithDescription(
         self.field, self.TestEnum, self.request)
Beispiel #3
0
class TestLaunchpadRadioWidgetWithDescription(TestCaseWithFactory):
    """Test the LaunchpadRadioWidgetWithDescription class."""

    layer = DatabaseFunctionalLayer

    class TestEnum(EnumeratedType):
        SAFE_TERM = Item('item-1', description='Safe title')
        UNSAFE_TERM = Item('item-<2>', description='<unsafe> &nbsp; title')

    def setUp(self):
        super(TestLaunchpadRadioWidgetWithDescription, self).setUp()
        self.request = LaunchpadTestRequest()
        field = Choice(__name__='test_field', vocabulary=self.TestEnum)
        self.field = field.bind(object())
        self.widget = LaunchpadRadioWidgetWithDescription(
            self.field, self.TestEnum, self.request)

    def assertRenderItem(self, expected, method, enum_item):
        markup = method(index=1,
                        text=enum_item.title,
                        value=enum_item.name,
                        name=self.field.__name__,
                        cssClass=None)
        expected_matcher = DocTestMatches(
            expected, (doctest.NORMALIZE_WHITESPACE | doctest.REPORT_NDIFF
                       | doctest.ELLIPSIS))
        self.assertThat(markup, expected_matcher)

    def test_renderSelectedItem(self):
        # Render checked="checked" item in checked state.
        expected = (
            '<tr> <td rowspan="2">'
            '<input class="radioType" checked="checked" id="test_field.1" '
            'name="test_field" type="radio" value="SAFE_TERM" /></td> '
            '<td><label for="test_field.1">item-1</label></td> </tr> '
            '<tr> <td class="formHelp">Safe title</td> </tr>')
        self.assertRenderItem(expected, self.widget.renderSelectedItem,
                              self.TestEnum.SAFE_TERM)

    def test_renderItem(self):
        # Render item in unchecked state.
        expected = ('<tr> <td rowspan="2">'
                    '<input class="radioType" id="test_field.1" '
                    'name="test_field" type="radio" value="SAFE_TERM" /></td> '
                    '<td><label for="test_field.1">item-1</label></td> </tr> '
                    '<tr> <td class="formHelp">Safe title</td> </tr>')
        self.assertRenderItem(expected, self.widget.renderItem,
                              self.TestEnum.SAFE_TERM)

    def test_renderSelectedItem_unsafe_content(self):
        # Render selected item escapes unsafe markup.
        expected = (
            '<...>item-&lt;2&gt;<...>&lt;unsafe&gt; &amp;nbsp; title<...>')
        self.assertRenderItem(expected, self.widget.renderSelectedItem,
                              self.TestEnum.UNSAFE_TERM)

    def test_renderItem_unsafe_content(self):
        # Render item escapes unsafe markup.
        expected = (
            '<...>item-&lt;2&gt;<...>&lt;unsafe&gt; &amp;nbsp; title<...>')
        self.assertRenderItem(expected, self.widget.renderItem,
                              self.TestEnum.UNSAFE_TERM)

    def test_renderExtraHint(self):
        # If an extra hint is specified, it is rendered.
        self.widget.extra_hint = "Hello World"
        self.widget.extra_hint_class = 'hint_class'
        expected = ('<div class="hint_class">Hello World</div>')
        hint_html = self.widget.renderExtraHint()
        self.assertEqual(expected, hint_html)
class TestLaunchpadRadioWidgetWithDescription(TestCaseWithFactory):
    """Test the LaunchpadRadioWidgetWithDescription class."""

    layer = DatabaseFunctionalLayer

    class TestEnum(EnumeratedType):
        SAFE_TERM = Item('item-1', description='Safe title')
        UNSAFE_TERM = Item('item-<2>', description='<unsafe> &nbsp; title')

    def setUp(self):
        super(TestLaunchpadRadioWidgetWithDescription, self).setUp()
        self.request = LaunchpadTestRequest()
        field = Choice(__name__='test_field', vocabulary=self.TestEnum)
        self.field = field.bind(object())
        self.widget = LaunchpadRadioWidgetWithDescription(
            self.field, self.TestEnum, self.request)

    def assertRenderItem(self, expected, method, enum_item):
        markup = method(
            index=1, text=enum_item.title, value=enum_item.name,
            name=self.field.__name__, cssClass=None)
        expected_matcher = DocTestMatches(
            expected, (doctest.NORMALIZE_WHITESPACE |
                       doctest.REPORT_NDIFF | doctest.ELLIPSIS))
        self.assertThat(markup, expected_matcher)

    def test_renderSelectedItem(self):
        # Render checked="checked" item in checked state.
        expected = (
            '<tr> <td rowspan="2">'
            '<input class="radioType" checked="checked" id="test_field.1" '
            'name="test_field" type="radio" value="SAFE_TERM" /></td> '
            '<td><label for="test_field.1">item-1</label></td> </tr> '
            '<tr> <td class="formHelp">Safe title</td> </tr>')
        self.assertRenderItem(
            expected, self.widget.renderSelectedItem, self.TestEnum.SAFE_TERM)

    def test_renderItem(self):
        # Render item in unchecked state.
        expected = (
            '<tr> <td rowspan="2">'
            '<input class="radioType" id="test_field.1" '
            'name="test_field" type="radio" value="SAFE_TERM" /></td> '
            '<td><label for="test_field.1">item-1</label></td> </tr> '
            '<tr> <td class="formHelp">Safe title</td> </tr>')
        self.assertRenderItem(
            expected, self.widget.renderItem, self.TestEnum.SAFE_TERM)

    def test_renderSelectedItem_unsafe_content(self):
        # Render selected item escapes unsafe markup.
        expected = (
            '<...>item-&lt;2&gt;<...>&lt;unsafe&gt; &amp;nbsp; title<...>')
        self.assertRenderItem(
            expected,
            self.widget.renderSelectedItem, self.TestEnum.UNSAFE_TERM)

    def test_renderItem_unsafe_content(self):
        # Render item escapes unsafe markup.
        expected = (
            '<...>item-&lt;2&gt;<...>&lt;unsafe&gt; &amp;nbsp; title<...>')
        self.assertRenderItem(
            expected, self.widget.renderItem, self.TestEnum.UNSAFE_TERM)

    def test_renderExtraHint(self):
        # If an extra hint is specified, it is rendered.
        self.widget.extra_hint = "Hello World"
        self.widget.extra_hint_class = 'hint_class'
        expected = (
            '<div class="hint_class">Hello World</div>')
        hint_html = self.widget.renderExtraHint()
        self.assertEqual(expected, hint_html)