[Zope3-checkins] CVS: Zope3/lib/python/Zope/Schema/tests - testBoolField.py:1.4 testDatetime.py:1.2 testDictField.py:1.4 testField.py:1.4 testFloatField.py:1.4 testIntField.py:1.4 testListField.py:1.4 testStrField.py:1.5 testTupleField.py:1.4

Jim Fulton jim@zope.com
Mon, 11 Nov 2002 15:24:36 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Schema/tests
In directory cvs.zope.org:/tmp/cvs-serv15816/tests

Modified Files:
	testBoolField.py testDatetime.py testDictField.py testField.py 
	testFloatField.py testIntField.py testListField.py 
	testStrField.py testTupleField.py 
Log Message:
Added TextLine and Line field types to model the common case of
single-line text.

Added a binding protocol to support fields whos validation or
properties depend on specific instances implementing the fields.
See the doc string for IField.


=== Zope3/lib/python/Zope/Schema/tests/testBoolField.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/Schema/tests/testBoolField.py:1.3	Wed Sep 18 11:05:51 2002
+++ Zope3/lib/python/Zope/Schema/tests/testBoolField.py	Mon Nov 11 15:24:35 2002
@@ -21,6 +21,8 @@
 class BoolTest(FieldTestBase):
     """Test the Bool Field."""
 
+    _Field_Factory = Bool
+
     def testValidate(self):
         field = Bool(title=u'Bool field', description=u'',
                      readonly=False, required=False)        


=== Zope3/lib/python/Zope/Schema/tests/testDatetime.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/tests/testDatetime.py:1.1	Fri Oct  4 14:24:55 2002
+++ Zope3/lib/python/Zope/Schema/tests/testDatetime.py	Mon Nov 11 15:24:35 2002
@@ -22,6 +22,8 @@
 class DatetimeTest(FieldTestBase):
     """Test the Datetime Field."""
 
+    _Field_Factory = Datetime
+
     def testValidate(self):
         field = Datetime(title=u'Datetime field', description=u'',
                         readonly=False, required=False)


=== Zope3/lib/python/Zope/Schema/tests/testDictField.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/Schema/tests/testDictField.py:1.3	Wed Sep 18 11:05:51 2002
+++ Zope3/lib/python/Zope/Schema/tests/testDictField.py	Mon Nov 11 15:24:35 2002
@@ -21,6 +21,8 @@
 class DictTest(FieldTestBase):
     """Test the Dict Field."""
 
+    _Field_Factory = Dict
+
     def testValidate(self):
         field = Dict(title=u'Dict field',
                      description=u'', readonly=False, required=False)


=== Zope3/lib/python/Zope/Schema/tests/testField.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/Schema/tests/testField.py:1.3	Wed Sep 18 11:05:51 2002
+++ Zope3/lib/python/Zope/Schema/tests/testField.py	Mon Nov 11 15:24:35 2002
@@ -28,13 +28,27 @@
             return
         self.fail('Expected ValidationError')
 
+    def test_bind(self):
+        field = self._Field_Factory(
+            __name__ = 'x',
+            title=u'Not required field', description=u'',
+            readonly=False, required=False)
 
-class FieldTest(FieldTestBase):
-    """Test generic Field."""
+        class C(object):
+            x=None
+
+        c = C()
+        field2 = field.bind(c)
+
+        self.assertEqual(field2.context, c)
+        for n in ('__class__', '__name__', 'title', 'description',
+                  'readonly', 'required'):
+            self.assertEquals(getattr(field2, n), getattr(field, n), n)
 
     def testValidate(self):
-        field = Field(title=u'Not required field', description=u'',
-                      readonly=False, required=False)
+        field = self._Field_Factory(
+            title=u'Not required field', description=u'',
+            readonly=False, required=False)
         field.validate(None)
         field.validate('foo')
         field.validate(1)
@@ -42,8 +56,9 @@
         field.validate('')
     
     def testValidateRequired(self):
-        field = Field(title=u'Required field', description=u'',
-                      readonly=False, required=True)
+        field = self._Field_Factory(
+            title=u'Required field', description=u'',
+            readonly=False, required=True)
         field.validate('foo')
         field.validate(1)
         field.validate(0)
@@ -51,6 +66,13 @@
             
         self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
                                     field.validate, None)
+
+class FieldTest(FieldTestBase):
+    """Test generic Field."""
+
+    
+    _Field_Factory = Field
+
 
     def testSillyDefault(self):
         


=== Zope3/lib/python/Zope/Schema/tests/testFloatField.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/Schema/tests/testFloatField.py:1.3	Wed Sep 18 11:05:51 2002
+++ Zope3/lib/python/Zope/Schema/tests/testFloatField.py	Mon Nov 11 15:24:35 2002
@@ -21,6 +21,8 @@
 class FloatTest(FieldTestBase):
     """Test the Float Field."""
 
+    _Field_Factory = Float
+
     def testValidate(self):
         field = Float(title=u'Float field', description=u'',
                         readonly=False, required=False)


=== Zope3/lib/python/Zope/Schema/tests/testIntField.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/Schema/tests/testIntField.py:1.3	Wed Sep 18 11:05:51 2002
+++ Zope3/lib/python/Zope/Schema/tests/testIntField.py	Mon Nov 11 15:24:35 2002
@@ -21,6 +21,8 @@
 class IntTest(FieldTestBase):
     """Test the Int Field."""
 
+    _Field_Factory = Int
+    
     def testValidate(self):
         field = Int(title=u'Int field', description=u'',
                         readonly=False, required=False)


=== Zope3/lib/python/Zope/Schema/tests/testListField.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/Schema/tests/testListField.py:1.3	Wed Sep 18 11:05:51 2002
+++ Zope3/lib/python/Zope/Schema/tests/testListField.py	Mon Nov 11 15:24:35 2002
@@ -21,6 +21,8 @@
 class ListTest(FieldTestBase):
     """Test the List Field."""
 
+    _Field_Factory = List
+
     def testValidate(self):
         field = List(title=u'List field', description=u'',
                      readonly=False, required=False)


=== Zope3/lib/python/Zope/Schema/tests/testStrField.py 1.4 => 1.5 ===
--- Zope3/lib/python/Zope/Schema/tests/testStrField.py:1.4	Wed Sep 18 11:05:51 2002
+++ Zope3/lib/python/Zope/Schema/tests/testStrField.py	Mon Nov 11 15:24:35 2002
@@ -15,7 +15,7 @@
 $Id$
 """
 from unittest import TestSuite, main, makeSuite
-from Zope.Schema import Bytes, Text, ErrorNames
+from Zope.Schema import Bytes, Line, Text, TextLine, ErrorNames
 from Zope.Schema.Exceptions import ValidationError 
 from testField import FieldTestBase
 
@@ -23,8 +23,8 @@
     """Test the Str Field."""
 
     def testValidate(self):
-        field = self._Str(title=u'Str field', description=u'',
-                          readonly=False, required=False)
+        field = self._Field_Factory(title=u'Str field', description=u'',
+                                    readonly=False, required=False)
         field.validate(None)
         field.validate(self._convert('foo'))
         field.validate(self._convert(''))
@@ -34,8 +34,9 @@
         # Note that if we want to require non-empty strings,
         # we need to set the min-length to 1.
         
-        field = self._Str(title=u'Str field', description=u'',
-                          readonly=False, required=True, min_length=1)
+        field = self._Field_Factory(
+            title=u'Str field', description=u'',
+            readonly=False, required=True, min_length=1)
         field.validate(self._convert('foo'))
 
         self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
@@ -44,10 +45,11 @@
                                     field.validate, self._convert(''))
 
     def testAllowedValues(self):
-        field = self._Str(title=u'Str field', description=u'',
-                          readonly=False, required=False,
-                          allowed_values=(self._convert('foo'),
-                                          self._convert('bar')))
+        field = self._Field_Factory(
+            title=u'Str field', description=u'',
+            readonly=False, required=False,
+            allowed_values=(self._convert('foo'),
+                            self._convert('bar')))
         field.validate(None)
         field.validate(self._convert('foo'))
 
@@ -55,8 +57,9 @@
                                     field.validate, self._convert('blah'))
 
     def testValidateMinLength(self):
-        field = self._Str(title=u'Str field', description=u'',
-                          readonly=False, required=False, min_length=3)
+        field = self._Field_Factory(
+            title=u'Str field', description=u'',
+            readonly=False, required=False, min_length=3)
         field.validate(None)
         field.validate(self._convert('333'))
         field.validate(self._convert('55555'))
@@ -69,8 +72,9 @@
                                     field.validate, self._convert('1'))
 
     def testValidateMaxLength(self):
-        field = self._Str(title=u'Str field', description=u'',
-                          readonly=False, required=False, max_length=5)
+        field = self._Field_Factory(
+            title=u'Str field', description=u'',
+            readonly=False, required=False, max_length=5)
         field.validate(None)
         field.validate(self._convert(''))
         field.validate(self._convert('333'))
@@ -82,9 +86,10 @@
                                     self._convert('999999999'))
 
     def testValidateMinLengthAndMaxLength(self):
-        field = self._Str(title=u'Str field', description=u'',
-                          readonly=False, required=False,
-                          min_length=3, max_length=5)
+        field = self._Field_Factory(
+            title=u'Str field', description=u'',
+            readonly=False, required=False,
+            min_length=3, max_length=5)
 
         field.validate(None)
         field.validate(self._convert('333'))
@@ -100,27 +105,54 @@
         self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
                                     self._convert('999999999'))
 
-class BytesTest(StrTest):
-    _Str = Bytes
+
+class MultiLine:
+
+    def test_newlines(self):
+        field = self._Field_Factory(title=u'Str field')
+        field.validate(self._convert('hello\nworld'))
+    
+
+class BytesTest(StrTest, MultiLine):
+    _Field_Factory = Bytes
     _convert = str
 
     def testBadStringType(self):
-        field = Bytes()
+        field = self._Field_Factory()
         self.assertRaises(ValidationError, field.validate, u'hello')
         
 
-class TextTest(StrTest):
-    _Str = Text
+class TextTest(StrTest, MultiLine):
+    _Field_Factory = Text
     def _convert(self, v):
         return unicode(v, 'ascii')
 
     def testBadStringType(self):
-        field = Text()
+        field = self._Field_Factory()
         self.assertRaises(ValidationError, field.validate, 'hello')
 
+class SingleLine:
+
+    def test_newlines(self):
+        field = self._Field_Factory(title=u'Str field')
+        self.assertRaisesErrorNames(ErrorNames.ConstraintNotSatisfied,
+                                    field.validate,
+                                    self._convert('hello\nworld'))    
+
+class LineTest(SingleLine, BytesTest):
+    _Field_Factory = Line
+
+class TextLineTest(SingleLine, TextTest):
+    _Field_Factory = TextLine
+
 
 def test_suite():
-    return TestSuite((makeSuite(BytesTest), makeSuite(TextTest)))
+    return TestSuite((
+        makeSuite(BytesTest),
+        makeSuite(TextTest),
+        makeSuite(LineTest),
+        makeSuite(TextLineTest),
+        ))
 
 if __name__ == '__main__':
     main(defaultTest='test_suite')


=== Zope3/lib/python/Zope/Schema/tests/testTupleField.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/Schema/tests/testTupleField.py:1.3	Wed Sep 18 11:05:51 2002
+++ Zope3/lib/python/Zope/Schema/tests/testTupleField.py	Mon Nov 11 15:24:35 2002
@@ -21,6 +21,8 @@
 class TupleTest(FieldTestBase):
     """Test the Tuple Field."""
 
+    _Field_Factory = Tuple
+
     def testValidate(self):
         field = Tuple(title=u'Tuple field', description=u'',
                       readonly=False, required=False)