changeset 2558:fb05ca2c0bdc

allow using complex datatype in macros
author Johannes Berg <johannes AT sipsolutions DOT net>
date Wed, 25 Jul 2007 10:49:49 +0200
parents cc5a81bda460
children ba76f4025416
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 99 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Wed Jul 25 10:49:36 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Wed Jul 25 10:49:49 2007 +0200
@@ -452,6 +452,53 @@
         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 testGetComplex(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),
+            (u'23.42E-3+3.04j', None, None, 23.42E-3+3.04j),
+            (u'3.04j', None, None, 3.04j),
+            (u'-3.04j', None, None, -3.04j),
+            (u'23.42E-3+3.04i', None, None, 23.42E-3+3.04j),
+            (u'3.04i', None, None, 3.04j),
+            (u'-3.04i', None, None, -3.04j),
+            (u'-3', None, None, -3L),
+            (u'-300000000000000000000', None, None, -300000000000000000000L),
+        ]
+        for arg, name, default, expected in tests:
+            assert wikiutil.get_complex(self.request, arg, name, default) == expected
+
+    def testGetComplexRaising(self):
+        # wrong default type
+        py.test.raises(AssertionError, wikiutil.get_complex, self.request, None, None, u'42')
+
+        # anything except None or unicode raises TypeError
+        py.test.raises(TypeError, wikiutil.get_complex, self.request, True)
+        py.test.raises(TypeError, wikiutil.get_complex, self.request, 42)
+        py.test.raises(TypeError, wikiutil.get_complex, self.request, 42.0)
+        py.test.raises(TypeError, wikiutil.get_complex, self.request, 3j)
+        py.test.raises(TypeError, wikiutil.get_complex, self.request, '')
+        py.test.raises(TypeError, wikiutil.get_complex, self.request, tuple())
+        py.test.raises(TypeError, wikiutil.get_complex, self.request, [])
+        py.test.raises(TypeError, wikiutil.get_complex, self.request, {})
+
+        # any value not convertable to int raises ValueError
+        py.test.raises(ValueError, wikiutil.get_complex, self.request, u'')
+        py.test.raises(ValueError, wikiutil.get_complex, self.request, u'3jj')
+        py.test.raises(ValueError, wikiutil.get_complex, self.request, u'3Ij')
+        py.test.raises(ValueError, wikiutil.get_complex, self.request, u'3i-3i')
+        py.test.raises(ValueError, wikiutil.get_complex, self.request, u'wrong')
+        py.test.raises(ValueError, wikiutil.get_complex, self.request, u'"47.11"') # must not be quoted!
+
     def testGetUnicode(self):
         tests = [
             # default testing for None value
@@ -655,5 +702,19 @@
         assert ief(self.request, has_many_defaults, u'd=4,c=3', [1, 2])
         assert ief(self.request, has_many_defaults, u'd=4', [1, 2, 3])
 
+    def testInvokeComplex(self):
+        ief = wikiutil.invoke_extension_function
+
+        def has_complex(a=complex, b=complex):
+            assert a == b
+            return True
+
+        assert ief(self.request, has_complex, u'3-3i, 3-3j')
+        assert ief(self.request, has_complex, u'2i, 2j')
+        assert ief(self.request, has_complex, u'b=2i, a=2j')
+        assert ief(self.request, has_complex, u'2.007, 2.007')
+        assert ief(self.request, has_complex, u'2.007', [2.007])
+        assert ief(self.request, has_complex, u'b=2.007', [2.007])
+
 
 coverage_modules = ['MoinMoin.wikiutil']
--- a/MoinMoin/wikiutil.py	Wed Jul 25 10:49:36 2007 +0200
+++ b/MoinMoin/wikiutil.py	Wed Jul 25 10:49:49 2007 +0200
@@ -1427,7 +1427,40 @@
                     name, arg))
         else:
             raise ValueError(
-                _('Argument must be a boolean value, not "%s"') % arg)
+                _('Argument must be a floating point value, not "%s"') % arg)
+
+
+def get_complex(request, arg, name=None, default=None):
+    """
+    For use with values returned from parse_quoted_separated or given
+    as macro parameters, return a complex from a unicode string.
+    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 return value if arg is None
+    @rtype: complex or None
+    @returns: the complex value of the string (or default value)
+    """
+    _ = request.getText
+    assert default is None or isinstance(default, (int, long, float, complex))
+    if arg is None:
+        return default
+    elif not isinstance(arg, unicode):
+        raise TypeError('Argument must be None or unicode')
+    try:
+        # allow writing 'i' instead of 'j'
+        arg = arg.replace('i', 'j').replace('I', 'j')
+        return complex(arg)
+    except ValueError:
+        if name:
+            raise ValueError(
+                _('Argument "%s" must be a complex value, not "%s"') % (
+                    name, arg))
+        else:
+            raise ValueError(
+                _('Argument must be a complex value, not "%s"') % arg)
 
 
 def get_unicode(request, arg, name=None, default=None):
@@ -1534,6 +1567,8 @@
             return get_int(request, value, name, default)
         elif isinstance(default, float):
             return get_float(request, value, name, default)
+        elif isinstance(default, complex):
+            return get_complex(request, value, name, default)
         elif isinstance(default, unicode):
             return get_unicode(request, value, name, default)
         elif isinstance(default, tuple) or isinstance(default, list):
@@ -1544,6 +1579,8 @@
             return get_int(request, value, name)
         elif default is float:
             return get_float(request, value, name)
+        elif default is complex:
+            return get_complex(request, value, name)
         elif isinstance(default, required_arg):
             return _convert_arg(request, value, default.argtype, name)
         return value