changeset 2512:68496057ee03

wikiutil.get_* tests + fixes
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 22 Jul 2007 18:53:36 +0200
parents e85e1ebb3459
children 6de0e14591f7
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 166 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Sun Jul 22 16:24:24 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Sun Jul 22 18:53:36 2007 +0200
@@ -315,4 +315,144 @@
             assert expected == result
 
 
+class TestArgGetters:
+    def testGetBoolean(self):
+        tests = [
+            # default testing for None value
+            (None, None, None, None),
+            (None, None, False, False),
+            (None, None, True, True),
+
+            # some real values
+            (u'0', None, None, False),
+            (u'1', None, None, True),
+            (u'false', None, None, False),
+            (u'true', None, None, True),
+            (u'FALSE', None, None, False),
+            (u'TRUE', None, None, True),
+            (u'no', None, None, False),
+            (u'yes', None, None, True),
+            (u'NO', None, None, False),
+            (u'YES', None, None, True),
+        ]
+        for arg, name, default, expected in tests:
+            assert wikiutil.get_bool(self.request, arg, name, default) == expected
+
+    def testGetBooleanRaising(self):
+        # wrong default type
+        py.test.raises(AssertionError, wikiutil.get_bool, self.request, None, None, 42)
+
+        # anything except None or unicode raises TypeError
+        py.test.raises(TypeError, wikiutil.get_bool, self.request, True)
+        py.test.raises(TypeError, wikiutil.get_bool, self.request, 42)
+        py.test.raises(TypeError, wikiutil.get_bool, self.request, 42.0)
+        py.test.raises(TypeError, wikiutil.get_bool, self.request, '')
+        py.test.raises(TypeError, wikiutil.get_bool, self.request, tuple())
+        py.test.raises(TypeError, wikiutil.get_bool, self.request, [])
+        py.test.raises(TypeError, wikiutil.get_bool, self.request, {})
+
+        # any value not convertable to boolean raises ValueError
+        py.test.raises(ValueError, wikiutil.get_bool, self.request, u'')
+        py.test.raises(ValueError, wikiutil.get_bool, self.request, u'42')
+        py.test.raises(ValueError, wikiutil.get_bool, self.request, u'wrong')
+        py.test.raises(ValueError, wikiutil.get_bool, self.request, u'"True"') # must not be quoted!
+
+    def testGetInt(self):
+        tests = [
+            # default testing for None value
+            (None, None, None, None),
+            (None, None, -23, -23),
+            (None, None, 42, 42),
+
+            # some real values
+            (u'0', None, None, 0),
+            (u'42', None, None, 42),
+            (u'-23', None, None, -23),
+        ]
+        for arg, name, default, expected in tests:
+            assert wikiutil.get_int(self.request, arg, name, default) == expected
+
+    def testGetIntRaising(self):
+        # wrong default type
+        py.test.raises(AssertionError, wikiutil.get_int, self.request, None, None, 42.23)
+
+        # anything except None or unicode raises TypeError
+        py.test.raises(TypeError, wikiutil.get_int, self.request, True)
+        py.test.raises(TypeError, wikiutil.get_int, self.request, 42)
+        py.test.raises(TypeError, wikiutil.get_int, self.request, 42.0)
+        py.test.raises(TypeError, wikiutil.get_int, self.request, '')
+        py.test.raises(TypeError, wikiutil.get_int, self.request, tuple())
+        py.test.raises(TypeError, wikiutil.get_int, self.request, [])
+        py.test.raises(TypeError, wikiutil.get_int, self.request, {})
+
+        # any value not convertable to int raises ValueError
+        py.test.raises(ValueError, wikiutil.get_int, self.request, u'')
+        py.test.raises(ValueError, wikiutil.get_int, self.request, u'23.42')
+        py.test.raises(ValueError, wikiutil.get_int, self.request, u'wrong')
+        py.test.raises(ValueError, wikiutil.get_int, self.request, u'"4711"') # must not be quoted!
+
+    def testGetFloat(self):
+        tests = [
+            # default testing for None value
+            (None, None, None, None),
+            (None, None, -23.42, -23.42),
+            (None, None, 42.23, 42.23),
+
+            # some real values
+            (u'0', None, None, 0),
+            (u'42.23', None, None, 42.23),
+            (u'-23.42', None, None, -23.42),
+            (u'-23.42E3', None, None, -23.42E3),
+            (u'23.42E-3', None, None, 23.42E-3),
+        ]
+        for arg, name, default, expected in tests:
+            assert wikiutil.get_float(self.request, arg, name, default) == expected
+
+    def testGetFloatRaising(self):
+        # wrong default type
+        py.test.raises(AssertionError, wikiutil.get_float, self.request, None, None, u'42')
+
+        # anything except None or unicode raises TypeError
+        py.test.raises(TypeError, wikiutil.get_float, self.request, True)
+        py.test.raises(TypeError, wikiutil.get_float, self.request, 42)
+        py.test.raises(TypeError, wikiutil.get_float, self.request, 42.0)
+        py.test.raises(TypeError, wikiutil.get_float, self.request, '')
+        py.test.raises(TypeError, wikiutil.get_float, self.request, tuple())
+        py.test.raises(TypeError, wikiutil.get_float, self.request, [])
+        py.test.raises(TypeError, wikiutil.get_float, self.request, {})
+
+        # any value not convertable to int raises ValueError
+        py.test.raises(ValueError, wikiutil.get_float, self.request, u'')
+        py.test.raises(ValueError, wikiutil.get_float, self.request, u'wrong')
+        py.test.raises(ValueError, wikiutil.get_float, self.request, u'"47.11"') # must not be quoted!
+
+    def testGetUnicode(self):
+        tests = [
+            # default testing for None value
+            (None, None, None, None),
+            (None, None, u'', u''),
+            (None, None, u'abc', u'abc'),
+
+            # some real values
+            (u'', None, None, u''),
+            (u'abc', None, None, u'abc'),
+            (u'"abc"', None, None, u'"abc"'),
+        ]
+        for arg, name, default, expected in tests:
+            assert wikiutil.get_unicode(self.request, arg, name, default) == expected
+
+    def testGetUnicodeRaising(self):
+        # wrong default type
+        py.test.raises(AssertionError, wikiutil.get_unicode, self.request, None, None, 42)
+
+        # anything except None or unicode raises TypeError
+        py.test.raises(TypeError, wikiutil.get_unicode, self.request, True)
+        py.test.raises(TypeError, wikiutil.get_unicode, self.request, 42)
+        py.test.raises(TypeError, wikiutil.get_unicode, self.request, 42.0)
+        py.test.raises(TypeError, wikiutil.get_unicode, self.request, '')
+        py.test.raises(TypeError, wikiutil.get_unicode, self.request, tuple())
+        py.test.raises(TypeError, wikiutil.get_unicode, self.request, [])
+        py.test.raises(TypeError, wikiutil.get_unicode, self.request, {})
+
+
 coverage_modules = ['MoinMoin.wikiutil']
--- a/MoinMoin/wikiutil.py	Sun Jul 22 16:24:24 2007 +0200
+++ b/MoinMoin/wikiutil.py	Sun Jul 22 18:53:36 2007 +0200
@@ -1325,22 +1325,23 @@
         return ret_positional
 
 
-def get_boolean(request, arg, name=None, default=None):
+def get_bool(request, arg, name=None, default=None):
     """
     For use with values returned from parse_quoted_separated or given
     as macro parameters, return a boolean from a unicode string.
-    Valid input is 'true'/'false', 'yes'/'no' and '1'/'0' or the empty
-    string or None for the default value.
+    Valid input is 'true'/'false', 'yes'/'no' and '1'/'0' or None for
+    the default value.
 
     @param request: A request instance
     @param arg: The argument, may be None or a unicode string
     @param name: Name of the argument, for error messages
-    @param default: default value if arg is empty; should be the same
-                    as any default value for arg
-    @rtype: boolean
+    @param default: default value if arg is None
+    @rtype: boolean or None
     @returns: the boolean value of the string according to above rules
+              (or default value)
     """
     _ = request.getText
+    assert default is None or isinstance(default, bool)
     if arg is None:
         return default
     elif not isinstance(arg, unicode):
@@ -1350,8 +1351,6 @@
         return False
     elif arg in [u'1', u'true', u'yes']:
         return True
-    elif arg == u'':
-        return default
     else:
         if name:
             raise ValueError(_('Argument "%s" must be a boolean value') % name)
@@ -1364,23 +1363,23 @@
     For use with values returned from parse_quoted_separated or given
     as macro parameters, return an integer from a unicode string
     containing the decimal representation of a number.
-    The empty string or None are valid input and yield the default value.
+    None is a valid input and yields the default value.
 
     @param request: A request instance
     @param arg: The argument, may be None or a unicode string
     @param name: Name of the argument, for error messages
-    @param default: default value if arg is empty; should be the same
-                    as any default value for arg
-    @rtype: int or long
-    @returns: the integer value of the string
+    @param default: default value if arg is None
+    @rtype: int or None
+    @returns: the integer value of the string (or default value)
     """
     _ = request.getText
+    assert default is None or isinstance(default, int)
     if arg is None:
         return default
     elif not isinstance(arg, unicode):
         raise TypeError('argument must be None or unicode')
     try:
-        arg = int(arg)
+        return int(arg)
     except ValueError:
         if name:
             raise ValueError(_('Argument "%s" must be an integer value') % name)
@@ -1392,49 +1391,49 @@
     """
     For use with values returned from parse_quoted_separated or given
     as macro parameters, return a float from a unicode string.
-    The empty string or None are valid input and yield the default value.
+    None is a valid input and yields the default value.
 
     @param request: A request instance
     @param arg: The argument, may be None or a unicode string
     @param name: Name of the argument, for error messages
-    @param default: default value if arg is empty; should be the same
-                    as any default value for arg
-    @rtype: float
-    @returns: the float value of the string
+    @param default: default return value if arg is None
+    @rtype: float or None
+    @returns: the float value of the string (or default value)
     """
     _ = request.getText
+    assert default is None or isinstance(default, float)
     if arg is None:
         return default
     elif not isinstance(arg, unicode):
         raise TypeError('argument must be None or unicode')
     try:
-        arg = float(arg)
+        return float(arg)
     except ValueError:
         if name:
             raise ValueError(
                 _('Argument "%s" must be a floating point value') % name)
         else:
-            raise ValueError(_('Argument must be an integer value'))
+            raise ValueError(_('Argument must be a floating point value'))
 
 
 def get_unicode(request, arg, name=None, default=None):
     """
     For use with values returned from parse_quoted_separated or given
     as macro parameters, return a unicode string from a unicode string.
-    The empty string or None are valid input and yield the default value.
+    None is a valid input and yields the default value.
 
     @param request: A request instance
     @param arg: The argument, may be None or a unicode string
     @param name: Name of the argument, for error messages
-    @param default: default value if arg is empty; should be the same
-                    as any default value for arg
-    @rtype: float
-    @returns: the float value of the string
+    @param default: default return value if arg is None;
+    @rtype: unicode or None
+    @returns: the unicode string (or default value)
     """
+    assert default is None or isinstance(default, unicode)
     if arg is None:
         return default
     elif not isinstance(arg, unicode):
-        raise TypeError('argument must be None or unicode')
+        raise TypeError('Argument must be None or unicode')
 
     return arg