|
From: <sub...@co...> - 2007-10-21 00:19:10
|
Author: ianb
Date: 2007-10-20 18:19:10 -0600 (Sat, 20 Oct 2007)
New Revision: 3100
Modified:
FormEncode/trunk/docs/news.txt
FormEncode/trunk/formencode/validators.py
FormEncode/trunk/tests/test_validators.py
Log:
Add min/max options to Int, from Felix Schwarz
Modified: FormEncode/trunk/docs/news.txt
===================================================================
--- FormEncode/trunk/docs/news.txt 2007-10-21 00:18:05 UTC (rev 3099)
+++ FormEncode/trunk/docs/news.txt 2007-10-21 00:19:10 UTC (rev 3100)
@@ -37,6 +37,9 @@
* ``validators.UnicodeString`` properly handles non-Unicode inputs.
+* ``validators.Int`` takes ``min`` and ``max`` options (from Felix
+ Schwarz).
+
0.7.1
-----
Modified: FormEncode/trunk/formencode/validators.py
===================================================================
--- FormEncode/trunk/formencode/validators.py 2007-10-21 00:18:05 UTC (rev 3099)
+++ FormEncode/trunk/formencode/validators.py 2007-10-21 00:19:10 UTC (rev 3100)
@@ -915,8 +915,20 @@
messages = {
'integer': _("Please enter an integer value"),
+ 'tooLow': _("Please enter an integer above %(min)i"),
+ 'tooHigh': _("Please enter an integer below %(max)i"),
}
+ min = None
+ max = None
+
+ def __initargs__(self, args):
+ if self.min != None:
+ self.min = int(self.min)
+ if self.max != None:
+ self.max = int(self.max)
+
+
def _to_python(self, value, state):
try:
return int(value)
@@ -924,6 +936,14 @@
raise Invalid(self.message('integer', state),
value, state)
+ def validate_python(self, value, state):
+ if self.min != None and value < self.min:
+ msg = self.message("tooLow", state, min=self.min)
+ raise Invalid(msg, value, state)
+ if self.max != None and value > self.max:
+ msg = self.message("tooHigh", state, max=self.max)
+ raise Invalid(msg, value, state)
+
_from_python = _to_python
class Number(FancyValidator):
@@ -1115,6 +1135,9 @@
value = value.encode(self.outputEncoding)
return value
+ def empty_value(self, value):
+ return u''
+
class Set(FancyValidator):
"""
Modified: FormEncode/trunk/tests/test_validators.py
===================================================================
--- FormEncode/trunk/tests/test_validators.py 2007-10-21 00:18:05 UTC (rev 3099)
+++ FormEncode/trunk/tests/test_validators.py 2007-10-21 00:19:10 UTC (rev 3100)
@@ -1,8 +1,8 @@
-from formencode.validators import String, UnicodeString, Invalid
+from formencode.validators import String, UnicodeString, Invalid, Int
def validate(validator, value):
try:
- validator.to_python(value)
+ return validator.to_python(value)
return None
except Invalid, e:
return e.unpack_errors()
@@ -38,8 +38,47 @@
assert sv.from_python(2) == "2"
assert sv.from_python([]) == ""
+
def test_unicode():
un = UnicodeString()
assert un.to_python(12) == u'12'
assert type(un.to_python(12)) is unicode
-
+
+def test_unicode_empty():
+ iv = UnicodeString()
+ for input in [None, "", u""]:
+ result = iv.to_python(input)
+ assert u"" == result, result
+ assert isinstance(result, unicode)
+
+
+def test_int_min():
+ messages = Int().message
+ iv = Int(min=5)
+ assert iv.to_python("5") == 5
+ assert validate(iv, "1") == messages('tooLow', None, min=5)
+
+def test_int_max():
+ messages = Int().message
+ iv = Int(max=10)
+ assert iv.to_python("10") == 10
+ assert validate(iv, "15") == messages('tooHigh', None, max=10)
+
+def test_int_minmax_optional():
+ messages = Int().message
+ iv = Int(min=5, max=10, if_empty=None)
+ assert iv.to_python("") == None
+ assert iv.to_python(None) == None
+ assert iv.to_python('7') == 7
+ assert validate(iv, "1") == messages('tooLow', None, min=5)
+ assert validate(iv, "15") == messages('tooHigh', None, max=10)
+
+def test_int_minmax_optional():
+ messages = Int().message
+ iv = Int(min=5, max=10, not_empty=True)
+ assert validate(iv, None) == messages('empty', None)
+ assert validate(iv, "1") == messages('tooLow', None, min=5)
+ assert validate(iv, "15") == messages('tooHigh', None, max=10)
+
+
+
|