changeset 2456:15502589ece6

ParameterParser: check types, remove unused code, tests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 21 Jul 2007 23:48:56 +0200
parents fe71235f1f7c
children e0b1fc786797
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 24 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Sat Jul 21 23:00:25 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Sat Jul 21 23:48:56 2007 +0200
@@ -199,9 +199,9 @@
             ("%(width)s%(height)s", 'width="really wide"', 0, {'width': 'really wide', 'height': None}),
             ("%(width)s%(height)s", 'height="not too high"', 0, {'width': None, 'height': 'not too high'}),
             ("%(width)s%(height)s", 'width="really wide", height="not too high"', 0, {'width': 'really wide', 'height': 'not too high'}),
-            # XXX for the next 2 tests: unclear: wanted str, given int, shall that give int?
-            ("%(width)s%(height)s", 'width=100', 0, {'width': 100, 'height': None}),
-            ("%(width)s%(height)s", 'width=100, height=200', 0, {'width': 100, 'height': 200}),
+            # conversion from given type to expected type
+            ("%(width)s%(height)s", 'width=100', 0, {'width': '100', 'height': None}),
+            ("%(width)s%(height)s", 'width=100, height=200', 0, {'width': '100', 'height': '200'}),
 
             # complex test
             ("%i%sf%s%ifs%(a)s|%(b)s", ' 4,"DI\'NG", b=retry, a="DING"', 2, {0: 4, 1: "DI'NG", 'a': 'DING', 'b': 'retry'}),
@@ -222,6 +222,16 @@
         argParser = wikiutil.ParameterParser("%(width)s%(height)s")
         py.test.raises(ValueError, argParser.parse_parameters, args)
 
+    def testWrongTypeFixedPosArgument(self):
+        args = '0.0'
+        argParser = wikiutil.ParameterParser("%b")
+        py.test.raises(ValueError, argParser.parse_parameters, args)
+
+    def testWrongTypeNamedArgument(self):
+        args = 'flag=0.0'
+        argParser = wikiutil.ParameterParser("%(flag)b")
+        py.test.raises(ValueError, argParser.parse_parameters, args)
+
 
 coverage_modules = ['MoinMoin.wikiutil']
 
--- a/MoinMoin/wikiutil.py	Sat Jul 21 23:00:25 2007 +0200
+++ b/MoinMoin/wikiutil.py	Sat Jul 21 23:48:56 2007 +0200
@@ -1349,9 +1349,6 @@
                                         self.optional)
 
     def parse_parameters(self, params):
-        """
-        (4, 2)
-        """
         #Default list to "None"s
         parameter_list = [None] * len(self.param_list)
         parameter_dict = dict([(key, None) for key in self.param_dict])
@@ -1396,6 +1393,7 @@
                     raise ValueError("parameter '%s' specified twice" % name)
                 else:
                     check_list[nr] = 1
+                value = self._check_type(value, type, self.param_list[nr])
                 parameter_dict[name] = value
                 parameter_list[nr] = value
                 named = True
@@ -1405,7 +1403,7 @@
                 nr = i
                 if nr not in self.param_dict.values():
                     fixed_count = nr + 1
-                parameter_list[nr] = value
+                parameter_list[nr] = self._check_type(value, type, self.param_list[nr])
 
             # Let's populate and map our dictionary to what's been found
             for name in self.param_dict:
@@ -1419,8 +1417,7 @@
 
         return fixed_count, parameter_dict
 
-""" never used:
-    def _check_type(value, type, format):
+    def _check_type(self, value, type, format):
         if type == 'n' and 's' in format: # n as s
             return value
 
@@ -1431,36 +1428,21 @@
             if 'f' in format:
                 return float(value) # i -> f
             elif 'b' in format:
-                return value # i -> b
-        elif type == 'f':
-            if 'b' in format:
-                return value  # f -> b
+                return value != 0 # i -> b
         elif type == 's':
             if 'b' in format:
-                return value.lower() != 'false' # s-> b
+                if value.lower() == 'false':
+                    return False # s-> b
+                elif value.lower() == 'true':
+                    return True # s-> b
+                else:
+                    raise ValueError('%r does not match format %r' % (value, format))
 
         if 's' in format: # * -> s
             return str(value)
-        else:
-            pass # XXX error
-
-def main():
-    pattern = "%i%sf%s%ifs%(a)s|%(b)s"
-    param = ' 4,"DI\'NG", b=retry, a="DING"'
-
-    #p_list, p_dict = parse_parameters(param)
 
-    print 'Pattern :', pattern
-    print 'Param :', param
+        raise ValueError('%r does not match format %r' % (value, format))
 
-    P = ParameterParser(pattern)
-    print P
-    print P.parse_parameters(param)
-
-
-if __name__=="__main__":
-    main()
-"""
 
 #############################################################################
 ### Misc