changeset 3371:8ac95a64f1a8

really make required_arg take choices (including test)
author Johannes Berg <johannes AT sipsolutions DOT net>
date Thu, 20 Mar 2008 21:42:32 +0100
parents fee75398b525
children 55b787c6c083 c20aa5b0158e
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Thu Mar 20 21:39:55 2008 +0100
+++ b/MoinMoin/_tests/test_wikiutil.py	Thu Mar 20 21:42:32 2008 +0100
@@ -540,6 +540,9 @@
         assert a == 7
         assert choice == u'a'
 
+    def _test_invoke_choice_required(self, i=wikiutil.required_arg((u'b', u'a'))):
+        assert i == u'a'
+
     def _test_trailing(self, a, _trailing_args=[]):
         assert _trailing_args == [u'a']
 
@@ -616,6 +619,10 @@
         ief(self.request, self._test_invoke_float_required, u'1.4')
         ief(self.request, self._test_invoke_float_required, u'i=1.4')
         py.test.raises(ValueError, ief, self.request,
+                       self._test_invoke_choice_required, u'')
+        ief(self.request, self._test_invoke_choice_required, u'a')
+        ief(self.request, self._test_invoke_choice_required, u'i=a')
+        py.test.raises(ValueError, ief, self.request,
                        self._test_invoke_float_required, u',')
 
     def testConstructors(self):
--- a/MoinMoin/wikiutil.py	Thu Mar 20 21:39:55 2008 +0100
+++ b/MoinMoin/wikiutil.py	Thu Mar 20 21:42:32 2008 +0100
@@ -1645,8 +1645,8 @@
         Initialise a required_arg
         @param argtype: the type the argument should have
         """
-        if not (argtype in (bool, int, long, float, complex, unicode, tuple, list) or
-                isinstance(argtype, IEFArgument)):
+        if not (argtype in (bool, int, long, float, complex, unicode) or
+                isinstance(argtype, (IEFArgument, tuple, list))):
             raise TypeError("argtype must be a valid type")
         self.argtype = argtype
 
@@ -1705,7 +1705,13 @@
                 return None
             return default.parse_argument(value)
         elif isinstance(default, required_arg):
-            return _convert_arg(request, value, default.argtype, name)
+            if isinstance(default.argtype, (tuple, list)):
+                # treat choice specially and return None if no choice
+                # is given in the value
+                choices = [None] + list(default.argtype)
+                return get_choice(request, value, name, choices)
+            else:
+                return _convert_arg(request, value, default.argtype, name)
         return value
 
     assert isinstance(fixed_args, (list, tuple))