changeset 2455:fe71235f1f7c

ParameterParser: changed to return fixed_count, arg_dict, putting positional args into arg_dict[pos], more tests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 21 Jul 2007 23:00:25 +0200
parents 18c71299e2bb
children 15502589ece6
files MoinMoin/_tests/test_wikiutil.py MoinMoin/macro/Hits.py MoinMoin/wikiutil.py
diffstat 3 files changed, 52 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Sat Jul 21 21:40:39 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Sat Jul 21 23:00:25 2007 +0200
@@ -158,39 +158,59 @@
     def testParameterParser(self):
         tests = [
             # trivial
-            ('', '', {}),
+            ('', '', 0, {}),
+
+            # fixed
+            ('%s%i%f%b', '"test",42,23.0,True', 4, {0: 'test', 1: 42, 2: 23.0, 3: True}),
+
+            # fixed and named
+            ('%s%(x)i%(y)i', '"test"', 1, {0: 'test', 'x': None, 'y': None}),
+            ('%s%(x)i%(y)i', '"test",1', 1, {0: 'test', 'x': 1, 'y': None}),
+            ('%s%(x)i%(y)i', '"test",1,2', 1, {0: 'test', 'x': 1, 'y': 2}),
+            ('%s%(x)i%(y)i', '"test",x=1', 1, {0: 'test', 'x': 1, 'y': None}),
+            ('%s%(x)i%(y)i', '"test",x=1,y=2', 1, {0: 'test', 'x': 1, 'y': 2}),
+            ('%s%(x)i%(y)i', '"test",y=2', 1, {0: 'test', 'x': None, 'y': 2}),
+
+            # test mixed acceptance
+            ("%ifs", '100', 1, {0: 100}),
+            ("%ifs", '100.0', 1, {0: 100.0}),
+            ("%ifs", '"100"', 1, {0: "100"}),
 
             # boolean
-            ("%(t)b%(f)b", '', {'t': None, 'f': None}),
-            ("%(t)b%(f)b", 't=1', {'t': True, 'f': None}),
-            ("%(t)b%(f)b", 'f=False', {'t': None, 'f': False}),
-            ("%(t)b%(f)b", 't=True, f=0', {'t': True, 'f': False}),
+            ("%(t)b%(f)b", '', 0, {'t': None, 'f': None}),
+            ("%(t)b%(f)b", 't=1', 0, {'t': True, 'f': None}),
+            ("%(t)b%(f)b", 'f=False', 0, {'t': None, 'f': False}),
+            ("%(t)b%(f)b", 't=True, f=0', 0, {'t': True, 'f': False}),
 
             # integer
-            ("%(width)i%(height)i", '', {'width': None, 'height': None}),
-            ("%(width)i%(height)i", 'width=100', {'width': 100, 'height': None}),
-            ("%(width)i%(height)i", 'height=200', {'width': None, 'height': 200}),
-            ("%(width)i%(height)i", 'width=100, height=200', {'width': 100, 'height': 200}),
+            ("%(width)i%(height)i", '', 0, {'width': None, 'height': None}),
+            ("%(width)i%(height)i", 'width=100', 0, {'width': 100, 'height': None}),
+            ("%(width)i%(height)i", 'height=200', 0, {'width': None, 'height': 200}),
+            ("%(width)i%(height)i", 'width=100, height=200', 0, {'width': 100, 'height': 200}),
 
             # float
-            ("%(width)f%(height)f", '', {'width': None, 'height': None}),
-            ("%(width)f%(height)f", 'width=100.0', {'width': 100.0, 'height': None}),
-            ("%(width)f%(height)f", 'height=2.0E2', {'width': None, 'height': 200.0}),
-            ("%(width)f%(height)f", 'width=1000.0E-1, height=200.0', {'width': 100.0, 'height': 200.0}),
+            ("%(width)f%(height)f", '', 0, {'width': None, 'height': None}),
+            ("%(width)f%(height)f", 'width=100.0', 0, {'width': 100.0, 'height': None}),
+            ("%(width)f%(height)f", 'height=2.0E2', 0, {'width': None, 'height': 200.0}),
+            ("%(width)f%(height)f", 'width=1000.0E-1, height=200.0', 0, {'width': 100.0, 'height': 200.0}),
 
             # string
-            ("%(width)s%(height)s", '', {'width': None, 'height': None}),
-            ("%(width)s%(height)s", 'width="really wide"', {'width': 'really wide', 'height': None}),
-            ("%(width)s%(height)s", 'height="not too high"', {'width': None, 'height': 'not too high'}),
-            ("%(width)s%(height)s", 'width="really wide", height="not too high"', {'width': 'really wide', 'height': 'not too high'}),
+            ("%(width)s%(height)s", '', 0, {'width': None, 'height': None}),
+            ("%(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', {'width': 100, 'height': None}),
-            ("%(width)s%(height)s", 'width=100, height=200', {'width': 100, 'height': 200}),
-        ]
-        for format, args, result in tests:
+            ("%(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'}),
+
+            ]
+        for format, args, expected_fixed_count, expected_dict in tests:
             argParser = wikiutil.ParameterParser(format)
-            arg_list, arg_dict = argParser.parse_parameters(args)
-            assert arg_dict == result
+            fixed_count, arg_dict = argParser.parse_parameters(args)
+            assert (fixed_count, arg_dict) == (expected_fixed_count, expected_dict)
 
     def testTooMuchWantedArguments(self):
         args = 'width=100, height=200, alt=Example'
--- a/MoinMoin/macro/Hits.py	Sat Jul 21 21:40:39 2007 +0200
+++ b/MoinMoin/macro/Hits.py	Sat Jul 21 23:00:25 2007 +0200
@@ -27,7 +27,7 @@
         self.this_page = macro.formatter.page.page_name
         argParser = wikiutil.ParameterParser("%(all)s%(filter)s")
         try:
-            self.arg_list, self.arg_dict = argParser.parse_parameters(args)
+            self.fixed_count, self.arg_dict = argParser.parse_parameters(args)
             self.error = None
         except ValueError, err:
             self.error = str(err)
--- a/MoinMoin/wikiutil.py	Sat Jul 21 21:40:39 2007 +0200
+++ b/MoinMoin/wikiutil.py	Sat Jul 21 23:00:25 2007 +0200
@@ -1359,6 +1359,7 @@
 
         i = 0
         start = 0
+        fixed_count = 0
         named = False
 
         while start < len(params):
@@ -1384,7 +1385,7 @@
             else:
                 raise ValueError("Parameter parser code does not fit param_re regex")
 
-            parameter_list.append(value)
+            #parameter_list.append(value)
             name = match.group("name")
             if name:
                 if name not in self.param_dict:
@@ -1402,6 +1403,8 @@
                 raise ValueError("only named parameters allowed after first named parameter")
             else:
                 nr = i
+                if nr not in self.param_dict.values():
+                    fixed_count = nr + 1
                 parameter_list[nr] = value
 
             # Let's populate and map our dictionary to what's been found
@@ -1411,7 +1414,10 @@
 
             i += 1
 
-        return parameter_list, parameter_dict
+        for i in range(fixed_count):
+            parameter_dict[i] = parameter_list[i]
+
+        return fixed_count, parameter_dict
 
 """ never used:
     def _check_type(value, type, format):