Author: test
Date: 2006-04-13 10:00:00 -0600 (Thu, 13 Apr 2006)
New Revision: 1694

Added:
   FormEncode/trunk/tests/non_empty.txt
Modified:
   FormEncode/trunk/formencode/api.py
   FormEncode/trunk/formencode/compound.py
   FormEncode/trunk/formencode/doctest_xml_compare.py
   FormEncode/trunk/formencode/validators.py
   FormEncode/trunk/tests/test_doctests.py
Log:
Made not_empty=False imply that empty is always okay.  Patch from Michele Cella

Modified: FormEncode/trunk/formencode/api.py
===================================================================
--- FormEncode/trunk/formencode/api.py  2006-04-12 07:30:07 UTC (rev 1693)
+++ FormEncode/trunk/formencode/api.py  2006-04-13 16:00:00 UTC (rev 1694)
@@ -300,12 +300,14 @@
         try:
             if self.strip and isinstance(value, (str, unicode)):
                 value = value.strip()
-            if not value and value != 0:
-                # False/0 are not "empty"
-                if self.if_empty is not NoDefault:
-                    return self.if_empty
+            if self.is_empty(value):
                 if self.not_empty:
                     raise Invalid(self.message('empty', state), value, state)
+                else:
+                    if self.if_empty is not NoDefault:
+                        return self.if_empty
+                    else:
+                        return self.empty_value(value)
             vo = self.validate_other
             if vo:
                 vo(value, state)
@@ -327,12 +329,11 @@
             if self.strip and isinstance(value, (str, unicode)):
                 value = value.strip()
             if not self.accept_python:
-                if self.not_empty:
-                    if not value and value != 0:
+                if self.is_empty(value):
+                    if self.not_empty:
                         raise Invalid(self.message('empty', state),
                                       value, state)
-                else:
-                    if self.is_empty(value):
+                    else:
                         return self.empty_value(value)
                 vp = self.validate_python
                 if vp:
@@ -358,6 +359,7 @@
                 return self.if_invalid_python
 
     def is_empty(self, value):
+        # None and '' are "empty"
         return value is None or value == ''
 
     def empty_value(self, value):

Modified: FormEncode/trunk/formencode/compound.py
===================================================================
--- FormEncode/trunk/formencode/compound.py     2006-04-12 07:30:07 UTC (rev 
1693)
+++ FormEncode/trunk/formencode/compound.py     2006-04-13 16:00:00 UTC (rev 
1694)
@@ -52,11 +52,11 @@
     def attempt_convert(self, value, state, convertFunc):
         raise NotImplementedError, "Subclasses must implement attempt_convert"
 
-    def to_python(self, value, state=None):
+    def _to_python(self, value, state=None):
         return self.attempt_convert(value, state,
                                     to_python)
     
-    def from_python(self, value, state=None):
+    def _from_python(self, value, state=None):
         return self.attempt_convert(value, state,
                                     from_python)
 
@@ -87,6 +87,13 @@
         else:
             return self.if_invalid
 
+    def not_empty__get(self):
+        not_empty = True
+        for validator in self.validators:
+            not_empty = not_empty and validator.not_empty
+        return not_empty
+    not_empty = property(not_empty__get)
+
 class All(CompoundValidator):
 
     """
@@ -151,3 +158,10 @@
                 return v
         return NoDefault
     if_missing = property(if_missing__get)
+
+    def not_empty__get(self):
+        not_empty = False
+        for validator in self.validators:
+            not_empty = not_empty or validator.not_empty
+        return not_empty
+    not_empty = property(not_empty__get)

Modified: FormEncode/trunk/formencode/doctest_xml_compare.py
===================================================================
--- FormEncode/trunk/formencode/doctest_xml_compare.py  2006-04-12 07:30:07 UTC 
(rev 1693)
+++ FormEncode/trunk/formencode/doctest_xml_compare.py  2006-04-13 16:00:00 UTC 
(rev 1694)
@@ -49,7 +49,10 @@
             got_xml = make_xml(got)
             got_norm = make_string(got_xml)
         except XMLParseError, e:
-            got_norm = '(bad XML: %s)' % e
+            if example.want.startswith('<'):
+                got_norm = '(bad XML: %s)' % e
+            else:
+                return actual
         s = '%s\nXML Wanted: %s\nXML Got   : %s\n' % (
             actual, want_norm, got_norm)
         if got_xml and want_xml:

Modified: FormEncode/trunk/formencode/validators.py
===================================================================
--- FormEncode/trunk/formencode/validators.py   2006-04-12 07:30:07 UTC (rev 
1693)
+++ FormEncode/trunk/formencode/validators.py   2006-04-13 16:00:00 UTC (rev 
1694)
@@ -183,6 +183,9 @@
             raise Invalid(msg, value, state)
         return value
 
+    def is_empty(self, value):
+        return False
+
 class Wrapper(FancyValidator):
 
     """
@@ -404,6 +407,7 @@
         >>> ne.to_python(0)
         0
     """
+    not_empty = True
 
     messages = {
         'empty': "Please enter a value",
@@ -692,9 +696,9 @@
         >>> index.to_python(5)
         Traceback (most recent call last):
         Invalid: Index out of range
-        >>> index.to_python(None)
+        >>> index(not_empty=True).to_python(None)
         Traceback (most recent call last):
-        Invalid: Must be an integer index
+        Invalid: Please enter a value
         >>> index.from_python('five')
         Traceback (most recent call last):
         Invalid: Item 'five' was not found in the list
@@ -867,6 +871,9 @@
         return bool(value)
     _from_python = _to_python        
 
+    def empty_value(self, value):
+        return False
+
 class Int(FancyValidator):
 
     """
@@ -1073,7 +1080,10 @@
                 return [value]
 
     def empty_value(self, value):
-        return []
+        if self.use_set:
+            return sets.Set([])
+        else:
+            return []
 
 class Email(FancyValidator):
     r"""
@@ -1100,12 +1110,14 @@
         >>> e.to_python('[EMAIL PROTECTED]')
         '[EMAIL PROTECTED]'
         >>> e = Email(resolve_domain=True)
+        >>> e.resolve_domain
+        True
         >>> e.to_python('[EMAIL PROTECTED]')
         '[EMAIL PROTECTED]'
-        >>> e.to_python('[EMAIL PROTECTED]')
+        >>> e.to_python('[EMAIL PROTECTED]')
         Traceback (most recent call last):
             ...
-        Invalid: The domain of the email address does not exist (the portion 
after the @: thisdomaindoesnotexistithink.com)
+        Invalid: The domain of the email address does not exist (the portion 
after the @: thisdomaindoesnotexistithinkforsure.com)
         
     """ 
 
@@ -1201,7 +1213,7 @@
         >>> 
u.to_python('http://this.domain.does.not.exists.formencode.org/test.html')
         Traceback (most recent call last):
             ...
-        Invalid: An error occured when trying to connect to the server: (7, 
'No address associated with nodename')
+        Invalid: An error occured when trying to connect to the server: (7, 
'No address associated with ...')
         
     """
 

Added: FormEncode/trunk/tests/non_empty.txt
===================================================================
--- FormEncode/trunk/tests/non_empty.txt        2006-04-12 07:30:07 UTC (rev 
1693)
+++ FormEncode/trunk/tests/non_empty.txt        2006-04-13 16:00:00 UTC (rev 
1694)
@@ -0,0 +1,79 @@
[EMAIL PROTECTED]:~/Progetti/TurboGears/svn/thirdparty/formencode/formencode$ 
python
+Python 2.4.3 (#2, Mar 30 2006, 14:45:01)
+[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu3)] on linux2
+Type "help", "copyright", "credits" or "license" for more information.
+>>> from formencode import validators
+>>> int = validators.Int()
+>>> int.not_empty
+False
+>>> int.to_python('')
+>>> int.to_python(None)
+>>> int.to_python(0)
+0
+>>> int.to_python(False)
+0
+>>> int.to_python('1')
+1
+>>> int.to_python('1a')
+Traceback (most recent call last):
+    ...
+Invalid: Please enter an integer value
+>>> int.not_empty = True
+>>> int.to_python('')
+Traceback (most recent call last):
+    ...
+Invalid: Please enter a value
+>>> int.to_python(None)
+Traceback (most recent call last):
+    ...
+Invalid: Please enter a value
+>>> int.to_python('1')
+1
+>>> int.to_python('1a')
+Traceback (most recent call last):
+    ...
+Invalid: Please enter an integer value
+>>> from formencode import compound
+>>> any = compound.Any(validators.Int(), validators.NotEmpty())
+>>> any.not_empty
+False
+>>> any.to_python('')
+>>> any.to_python(None)
+>>> any = compound.Any(validators.Int(), validators.Empty())
+>>> any.not_empty
+False
+>>> any = compound.All(validators.Int(), validators.NotEmpty())
+>>> any.not_empty
+True
+>>> any.to_python('')
+Traceback (most recent call last):
+    ...
+Invalid: Please enter a value
+>>> from formencode.foreach import ForEach
+>>> from formencode.validators import Int
+>>> foreach = ForEach(Int())
+>>> foreach.to_python('')
+>>> foreach.to_python(None)
+>>> foreach.to_python('1')
+[1]
+>>> foreach.to_python('2')
+[2]
+>>> foreach.to_python(['2', '3'])
+[2, 3]
+>>> foreach.not_empty = True
+>>> foreach.to_python('1')
+[1]
+>>> foreach.to_python('')
+Traceback (most recent call last):
+    ...
+Invalid: Please enter a value
+>>> foreach.if_empty = []
+>>> foreach.to_python('')
+Traceback (most recent call last):
+    ...
+Invalid: Please enter a value
+>>> foreach.not_empty = False
+>>> foreach.to_python('')
+[]
+>>> foreach.not_empty = False
+

Modified: FormEncode/trunk/tests/test_doctests.py
===================================================================
--- FormEncode/trunk/tests/test_doctests.py     2006-04-12 07:30:07 UTC (rev 
1693)
+++ FormEncode/trunk/tests/test_doctests.py     2006-04-13 16:00:00 UTC (rev 
1694)
@@ -15,8 +15,9 @@
 doctest_xml_compare.install()
 
 text_files = [
-    'htmlfill.txt',
-    'Validator.txt',
+    'docs/htmlfill.txt',
+    'docs/Validator.txt',
+    'tests/non_empty.txt',
     ]
 
 from formencode import validators
@@ -29,10 +30,10 @@
         args = text_files + modules
     for fn in args:
         if isinstance(fn, str):
-            fn = os.path.join(base, 
-                              'docs', fn)
-            doctest.testfile(fn, module_relative=False)
+            fn = os.path.join(base, fn)
+            doctest.testfile(fn, module_relative=False,
+                             optionflags=doctest.ELLIPSIS)
         else:
-            doctest.testmod(fn)
+            doctest.testmod(fn, optionflags=doctest.ELLIPSIS)
 
 



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
FormEncode-CVS mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-cvs

Reply via email to