Changeset 51:d6bbf24ef053


Ignore:
Timestamp:
Jan 13, 2009, 7:30:35 PM (12 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@162
Message:

Address Data Form type checking issues.

Author: ralphm.
Reviewer: tofu.
Fixes #41.

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/data_form.py

    r50 r51  
    220220            for value in self.values:
    221221                if self.fieldType == 'boolean':
    222                     # We send out the textual representation of boolean values
    223                     value = bool(int(value))
     222                    if isinstance(value, (str, unicode)):
     223                        checkValue = value.lower()
     224                        if not checkValue in ('0', '1', 'false', 'true'):
     225                            raise ValueError("Not a boolean")
     226                        value = checkValue in ('1', 'true')
     227                    value = bool(value)
    224228                elif self.fieldType in ('jid-single', 'jid-multi'):
    225                     value = value.full()
     229                    if not hasattr(value, 'full'):
     230                        value = JID(value)
    226231
    227232                newValues.append(value)
     
    249254            if self.fieldType == 'boolean':
    250255                value = unicode(value).lower()
     256            elif self.fieldType in ('jid-single', 'jid-multi'):
     257                value = value.full()
     258
    251259            field.addElement('value', content=value)
    252260
  • wokkel/test/test_data_form.py

    r50 r51  
    88from twisted.trial import unittest
    99from twisted.words.xish import domish
    10 
    11 from wokkel.data_form import Field, Form, Option, FieldNameRequiredError
     10from twisted.words.protocols.jabber import jid
     11
     12from wokkel import data_form
    1213
    1314NS_X_DATA = 'jabber:x:data'
    1415
    15 
    16 
    1716class OptionTest(unittest.TestCase):
    1817    """
    19     Tests for L{Option}.
     18    Tests for L{data_form.Option}.
    2019    """
    2120
    2221    def test_toElement(self):
    23         option = Option('value', 'label')
     22        option = data_form.Option('value', 'label')
    2423        element = option.toElement()
    2524        self.assertEquals('option', element.name)
     
    3433class FieldTest(unittest.TestCase):
    3534    """
    36     Tests for L{Field}.
     35    Tests for L{data_form.Field}.
    3736    """
    3837
     
    4140        Test basic field initialization.
    4241        """
    43         field = Field(var='test')
     42        field = data_form.Field(var='test')
    4443        self.assertEqual('text-single', field.fieldType)
    4544        self.assertEqual('test', field.var)
    4645
    4746
    48     def test_noFieldName(self):
    49         field = Field()
    50         self.assertRaises(FieldNameRequiredError, field.toElement)
    51 
    52 
    5347    def test_toElement(self):
    5448        """
    5549        Test rendering to a DOM.
    5650        """
    57         field = Field(var='test')
     51        field = data_form.Field(var='test')
    5852        element = field.toElement()
    5953
     
    7165        Always render the field type, if different from list-single.
    7266        """
    73         field = Field('hidden', var='test')
     67        field = data_form.Field('hidden', var='test')
    7468        element = field.toElement()
    7569
     
    8175        Always render the field type, if asForm is True.
    8276        """
    83         field = Field(var='test')
     77        field = data_form.Field(var='test')
    8478        element = field.toElement(True)
    8579
     
    9185        Test rendering to a DOM with options.
    9286        """
    93         field = Field('list-single', var='test')
    94         field.options = [Option(u'option1'), Option(u'option2')]
     87        field = data_form.Field('list-single', var='test')
     88        field.options = [data_form.Option(u'option1'),
     89                         data_form.Option(u'option2')]
    9590        element = field.toElement(True)
    9691
     
    10297        Test rendering to a DOM with a label.
    10398        """
    104         field = Field(var='test', label=u'my label')
     99        field = data_form.Field(var='test', label=u'my label')
    105100        element = field.toElement(True)
    106101
     
    112107        Test rendering to a DOM with options.
    113108        """
    114         field = Field(var='test', desc=u'My desc')
     109        field = data_form.Field(var='test', desc=u'My desc')
    115110        element = field.toElement(True)
    116111
     
    126121        Test rendering to a DOM with options.
    127122        """
    128         field = Field(var='test', required=True)
     123        field = data_form.Field(var='test', required=True)
    129124        element = field.toElement(True)
    130125
     
    135130
    136131
     132    def test_toElementJID(self):
     133        field = data_form.Field(fieldType='jid-single', var='test',
     134                                value=jid.JID(u'test@example.org'))
     135        element = field.toElement()
     136        self.assertEqual(u'test@example.org', unicode(element.value))
     137
     138
     139    def test_typeCheckNoFieldName(self):
     140        """
     141        A field not of type fixed must have a var.
     142        """
     143        field = data_form.Field(fieldType='list-single')
     144        self.assertRaises(data_form.FieldNameRequiredError, field.typeCheck)
     145
     146
     147    def test_typeCheckTooManyValues(self):
     148        """
     149        Expect an exception if too many values are set, depending on type.
     150        """
     151        field = data_form.Field(fieldType='list-single', var='test',
     152                                values=[u'value1', u'value2'])
     153        self.assertRaises(data_form.TooManyValuesError, field.typeCheck)
     154
     155
     156    def test_typeCheckBooleanFalse(self):
     157        """
     158        Test possible False values for a boolean field.
     159        """
     160        field = data_form.Field(fieldType='boolean', var='test')
     161
     162        for value in (False, 0, '0', 'false', 'False', []):
     163            field.value = value
     164            field.typeCheck()
     165            self.assertIsInstance(field.value, bool)
     166            self.assertFalse(field.value)
     167
     168
     169    def test_typeCheckBooleanTrue(self):
     170        """
     171        Test possible True values for a boolean field.
     172        """
     173        field = data_form.Field(fieldType='boolean', var='test')
     174
     175        for value in (True, 1, '1', 'true', 'True', ['test']):
     176            field.value = value
     177            field.typeCheck()
     178            self.assertIsInstance(field.value, bool)
     179            self.assertTrue(field.value)
     180
     181
     182    def test_typeCheckBooleanBad(self):
     183        """
     184        A bad value for a boolean field should raise a ValueError
     185        """
     186        field = data_form.Field(fieldType='boolean', var='test')
     187        field.value = 'test'
     188        self.assertRaises(ValueError, field.typeCheck)
     189
     190
     191    def test_typeCheckJID(self):
     192        """
     193        The value of jid field should be a JID or coercable to one.
     194        """
     195        field = data_form.Field(fieldType='jid-single', var='test',
     196                                value=jid.JID('test@example.org'))
     197        field.typeCheck()
     198
     199
     200    def test_typeCheckJIDString(self):
     201        """
     202        The string value of jid field should be coercable into a JID.
     203        """
     204        field = data_form.Field(fieldType='jid-single', var='test',
     205                                value='test@example.org')
     206        field.typeCheck()
     207        self.assertEquals(jid.JID(u'test@example.org'), field.value)
     208
     209
     210    def test_typeCheckJIDBad(self):
     211        """
     212        An invalid JID string should raise an exception.
     213        """
     214        field = data_form.Field(fieldType='jid-single', var='test',
     215                                value='test@@example.org')
     216        self.assertRaises(jid.InvalidFormat, field.typeCheck)
     217
     218
    137219    def test_fromElementType(self):
    138220        element = domish.Element((NS_X_DATA, 'field'))
    139221        element['type'] = 'fixed'
    140         field = Field.fromElement(element)
     222        field = data_form.Field.fromElement(element)
    141223        self.assertEquals('fixed', field.fieldType)
    142224
     
    144226    def test_fromElementNoType(self):
    145227        element = domish.Element((NS_X_DATA, 'field'))
    146         field = Field.fromElement(element)
     228        field = data_form.Field.fromElement(element)
    147229        self.assertEquals(None, field.fieldType)
    148230
     
    151233        element = domish.Element((NS_X_DATA, 'field'))
    152234        element.addElement("value", content="text")
    153         field = Field.fromElement(element)
     235        field = data_form.Field.fromElement(element)
    154236        self.assertEquals('text', field.value)
    155237
     
    158240class FormTest(unittest.TestCase):
    159241    """
    160     Tests for L{Form}.
     242    Tests for L{data_form.Form}.
    161243    """
    162244
     
    166248        """
    167249
    168         form = Form('result')
     250        form = data_form.Form('result')
    169251        self.assertEqual('result', form.formType)
    170252
     
    173255        The toElement method returns a form's DOM representation.
    174256        """
    175         form = Form('result')
     257        form = data_form.Form('result')
    176258        element = form.toElement()
    177259
     
    185267    def test_fromElement(self):
    186268        """
    187         The fromElement static method creates a L{Form} from a L{DOM.
     269        C{fromElement} creates a L{data_form.Form} from a DOM representation.
    188270        """
    189271        element = domish.Element((NS_X_DATA, 'x'))
    190272        element['type'] = 'result'
    191         form = Form.fromElement(element)
     273        form = data_form.Form.fromElement(element)
    192274
    193275        self.assertEquals('result', form.formType)
     
    202284        """
    203285        element = domish.Element((NS_X_DATA, 'form'))
    204         self.assertRaises(Exception, Form.fromElement, element)
     286        self.assertRaises(Exception, data_form.Form.fromElement, element)
    205287
    206288
     
    210292        """
    211293        element = domish.Element(('myns', 'x'))
    212         self.assertRaises(Exception, Form.fromElement, element)
     294        self.assertRaises(Exception, data_form.Form.fromElement, element)
    213295
    214296
     
    216298        element = domish.Element((NS_X_DATA, 'x'))
    217299        element.addElement('title', content='My title')
    218         form = Form.fromElement(element)
     300        form = data_form.Form.fromElement(element)
    219301
    220302        self.assertEquals('My title', form.title)
     
    224306        element = domish.Element((NS_X_DATA, 'x'))
    225307        element.addElement('instructions', content='instruction')
    226         form = Form.fromElement(element)
     308        form = data_form.Form.fromElement(element)
    227309
    228310        self.assertEquals(['instruction'], form.instructions)
     
    232314        element.addElement('instructions', content='instruction 1')
    233315        element.addElement('instructions', content='instruction 2')
    234         form = Form.fromElement(element)
     316        form = data_form.Form.fromElement(element)
    235317
    236318        self.assertEquals(['instruction 1', 'instruction 2'], form.instructions)
     
    240322        element = domish.Element((NS_X_DATA, 'x'))
    241323        element.addElement('field')
    242         form = Form.fromElement(element)
     324        form = data_form.Form.fromElement(element)
    243325
    244326        self.assertEquals(1, len(form.fieldList))
     
    250332        element.addElement('field')['var'] = 'field1'
    251333        element.addElement('field')['var'] = 'field2'
    252         form = Form.fromElement(element)
     334        form = data_form.Form.fromElement(element)
    253335
    254336        self.assertEquals(2, len(form.fieldList))
Note: See TracChangeset for help on using the changeset viewer.