[Checkins] SVN: zope.schema/trunk/ Move doctests for scalar fields to Sphinx.

Tres Seaver cvs-admin at zope.org
Mon Dec 31 15:29:14 UTC 2012


Log message for revision 128939:
  Move doctests for scalar fields to Sphinx.
  
  Revert reprs in one remaining doctest in z.s._field to match Python2.

Changed:
  _U  zope.schema/trunk/
  U   zope.schema/trunk/docs/fields.rst
  U   zope.schema/trunk/src/zope/schema/_field.py
  U   zope.schema/trunk/src/zope/schema/tests/test__field.py

-=-
Modified: zope.schema/trunk/docs/fields.rst
===================================================================
--- zope.schema/trunk/docs/fields.rst	2012-12-31 14:06:30 UTC (rev 128938)
+++ zope.schema/trunk/docs/fields.rst	2012-12-31 15:29:12 UTC (rev 128939)
@@ -13,6 +13,330 @@
 look at the standard zope.app approach to using these fields to find views
 ("widgets").
 
+Scalars
+-------
+
+Scalar fields represent simple. immutable Python types.
+
+Bytes
+#####
+
+:class:`zope.schema._field.Bytes` fields contain binary data, represented
+as a sequence of bytes (``str`` in Python2, ``bytes`` in Python3).
+
+Conversion from Unicode:
+
+.. doctest::
+
+   >>> from zope.schema._compat import b
+   >>> from zope.schema._compat import u
+   >>> from zope.schema._field import Bytes
+   >>> obj = Bytes(constraint=lambda v: b('x') in v)
+   >>> obj.fromUnicode(u(" foo x.y.z bat"))
+   ' foo x.y.z bat'
+   >>> obj.fromUnicode(u(" foo y.z bat"))
+   Traceback (most recent call last):
+   ...
+   ConstraintNotSatisfied:  foo y.z bat
+
+ASCII
+#####
+
+:class:`zope.schema._field.ASCII` fields are a restricted form of
+:class:`zope.schema._field.Bytes`:  they can contain only 7-bit bytes.
+
+Validation accepts empty strings:
+
+.. doctest::
+
+   >>> from zope.schema._field import ASCII
+   >>> ascii = ASCII()
+   >>> empty = ''
+   >>> ascii._validate(empty)
+
+and all kinds of alphanumeric strings:
+
+.. doctest::
+
+   >>> alphanumeric = "Bob\'s my 23rd uncle"
+   >>> ascii._validate(alphanumeric)
+
+but fails with 8-bit (encoded) strings:
+
+.. doctest::
+
+   >>> umlauts = "Köhlerstraße"
+   >>> ascii._validate(umlauts)
+   Traceback (most recent call last):
+   ...
+   InvalidValue
+
+BytesLine
+#########
+
+:class:`zope.schema._field.BytesLine` fields are a restricted form of
+:class:`zope.schema._field.Bytes`:  they cannot contain newlines.
+
+ASCIILine
+#########
+
+:class:`zope.schema._field.BytesLine` fields are a restricted form of
+:class:`zope.schema._field.ASCII`:  they cannot contain newlines.
+
+Float
+#####
+
+:class:`zope.schema._field.Float` fields contain binary data, represented
+as a a Python ``float``.
+
+Conversion from Unicode:
+
+.. doctest::
+
+   >>> from zope.schema._field import Float
+   >>> f = Float()
+   >>> f.fromUnicode("1.25")
+   1.25
+   >>> f.fromUnicode("1.25.6") #doctest: +IGNORE_EXCEPTION_DETAIL
+   Traceback (most recent call last):
+   ...
+   ValueError: invalid literal for float(): 1.25.6
+
+Decimal
+#######
+
+:class:`zope.schema._field.Decimal` fields contain binary data, represented
+as a a Python ``decimal.Decimal``.
+
+Conversion from Unicode:
+
+.. doctest::
+
+   >>> from zope.schema._field import Decimal
+   >>> f = Decimal()
+   >>> import decimal
+   >>> isinstance(f.fromUnicode("1.25"), decimal.Decimal)
+   True
+   >>> float(f.fromUnicode("1.25"))
+   1.25
+   >>> f.fromUnicode("1.25.6")
+   Traceback (most recent call last):
+   ...
+   ValueError: invalid literal for Decimal(): 1.25.6
+
+DateTime
+########
+
+:class:`zope.schema._field.DateTime` fields contain binary data, represented
+as a a Python ``datetime.DateTime``.
+
+Date
+####
+
+:class:`zope.schema._field.Date` fields contain binary data, represented
+as a a Python ``datetime.Date``.
+
+TimeDelta
+#########
+
+:class:`zope.schema._field.TimeDelta` fields contain binary data, represented
+as a a Python ``datetime.TimeDelta``.
+
+Time
+####
+
+:class:`zope.schema._field.Time` fields contain binary data, represented
+as a a Python ``datetime.Time``.
+
+Choice
+######
+
+:class:`zope.schema._field.Choice` fields are constrained to values drawn
+from a specified set, which can be static or dynamic.
+
+Conversion from Unicode enforces the constraint:
+
+.. doctest::
+
+   >>> from zope.schema.interfaces import IFromUnicode
+   >>> from zope.schema.vocabulary import SimpleVocabulary
+   >>> from zope.schema._field import Choice
+   >>> t = Choice(
+   ...     vocabulary=SimpleVocabulary.fromValues([u('foo'),u('bar')]))
+   >>> IFromUnicode.providedBy(t)
+   True
+   >>> t.fromUnicode(u("baz"))
+   Traceback (most recent call last):
+   ...
+   ConstraintNotSatisfied: baz
+   >>> t.fromUnicode(u("foo"))
+   u'foo'
+
+URI
+###
+
+:class:`zope.schema._field.URI` fields contain native Python strings
+(``str``), matching the "scheme:data" pattern.
+
+Validation ensures that the pattern is matched:
+
+.. doctest::
+
+   >>> from zope.schema._field import URI
+   >>> uri = URI(__name__='test')
+   >>> uri.validate(b("http://www.python.org/foo/bar"))
+   >>> uri.validate(b("DAV:"))
+   >>> uri.validate(b("www.python.org/foo/bar"))
+   Traceback (most recent call last):
+   ...
+   InvalidURI: www.python.org/foo/bar
+
+Conversion from Unicode:
+
+.. doctest::
+
+   >>> uri = URI(__name__='test')
+   >>> uri.fromUnicode("http://www.python.org/foo/bar")
+   'http://www.python.org/foo/bar'
+   >>> uri.fromUnicode("          http://www.python.org/foo/bar")
+   'http://www.python.org/foo/bar'
+   >>> uri.fromUnicode("      \n    http://www.python.org/foo/bar\n")
+   'http://www.python.org/foo/bar'
+   >>> uri.fromUnicode("http://www.python.org/ foo/bar")
+   Traceback (most recent call last):
+   ...
+   InvalidURI: http://www.python.org/ foo/bar
+
+DottedName
+##########
+
+:class:`zope.schema._field.DottedName` fields contain native Python strings
+(``str``), containing zero or more "dots" separating elements of the
+name.  The minimum and maximum number of dots can be passed to the
+constructor:
+
+.. doctest::
+
+   >>> from zope.schema._field import DottedName
+   >>> DottedName(min_dots=-1)
+   Traceback (most recent call last):
+   ...
+   ValueError: min_dots cannot be less than zero
+
+   >>> DottedName(max_dots=-1)
+   Traceback (most recent call last):
+   ...
+   ValueError: max_dots cannot be less than min_dots
+
+   >>> DottedName(max_dots=1, min_dots=2)
+   Traceback (most recent call last):
+   ...
+   ValueError: max_dots cannot be less than min_dots
+
+   >>> dotted_name = DottedName(max_dots=1, min_dots=1)
+
+   >>> from zope.interface.verify import verifyObject
+   >>> from zope.schema.interfaces import IDottedName
+   >>> verifyObject(IDottedName, dotted_name)
+   True
+
+   >>> dotted_name = DottedName(max_dots=1)
+   >>> dotted_name.min_dots
+   0
+
+   >>> dotted_name = DottedName(min_dots=1)
+   >>> dotted_name.max_dots
+   >>> dotted_name.min_dots
+   1
+
+Validation ensures that the pattern is matched:
+
+.. doctest::
+
+   >>> dotted_name = DottedName(__name__='test')
+   >>> dotted_name.validate("a.b.c")
+   >>> dotted_name.validate("a")
+   >>> dotted_name.validate("   a")
+   Traceback (most recent call last):
+   ...
+   InvalidDottedName:    a
+
+   >>> dotted_name = DottedName(__name__='test', min_dots=1)
+   >>> dotted_name.validate('a.b')
+   >>> dotted_name.validate('a.b.c.d')
+   >>> dotted_name.validate('a')
+   Traceback (most recent call last):
+   ...
+   InvalidDottedName: ('too few dots; 1 required', 'a')
+
+   >>> dotted_name = DottedName(__name__='test', max_dots=0)
+   >>> dotted_name.validate('a')
+   >>> dotted_name.validate('a.b')
+   Traceback (most recent call last):
+   ...
+   InvalidDottedName: ('too many dots; no more than 0 allowed', 'a.b')
+
+   >>> dotted_name = DottedName(__name__='test', max_dots=2)
+   >>> dotted_name.validate('a')
+   >>> dotted_name.validate('a.b')
+   >>> dotted_name.validate('a.b.c')
+   >>> dotted_name.validate('a.b.c.d')
+   Traceback (most recent call last):
+   ...
+   InvalidDottedName: ('too many dots; no more than 2 allowed', 'a.b.c.d')
+
+   >>> dotted_name = DottedName(__name__='test', max_dots=1, min_dots=1)
+   >>> dotted_name.validate('a.b')
+   >>> dotted_name.validate('a')
+   Traceback (most recent call last):
+   ...
+   InvalidDottedName: ('too few dots; 1 required', 'a')
+   >>> dotted_name.validate('a.b.c')
+   Traceback (most recent call last):
+   ...
+   InvalidDottedName: ('too many dots; no more than 1 allowed', 'a.b.c')
+
+Id
+##
+
+:class:`zope.schema._field.Id` fields contain native Python strings
+(``str``), matching either the URI pattern or a "dotted name".
+
+Validation ensures that the pattern is matched:
+
+.. doctest::
+
+   >>> from zope.schema._field import Id
+   >>> id = Id(__name__='test')
+   >>> id.validate("http://www.python.org/foo/bar")
+   >>> id.validate("zope.app.content")
+   >>> id.validate("zope.app.content/a")
+   Traceback (most recent call last):
+   ...
+   InvalidId: zope.app.content/a
+   >>> id.validate("http://zope.app.content x y")
+   Traceback (most recent call last):
+   ...
+   InvalidId: http://zope.app.content x y
+
+
+Conversion from Unicode:
+
+.. doctest::
+
+   >>> id = Id(__name__='test')
+   >>> id.fromUnicode("http://www.python.org/foo/bar")
+   'http://www.python.org/foo/bar'
+   >>> id.fromUnicode(u(" http://www.python.org/foo/bar "))
+   'http://www.python.org/foo/bar'
+   >>> id.fromUnicode("http://www.python.org/ foo/bar")
+   Traceback (most recent call last):
+   ...
+   InvalidId: http://www.python.org/ foo/bar
+   >>> id.fromUnicode("      \n x.y.z \n")
+   'x.y.z'
+
+
 Collections
 -----------
 

Modified: zope.schema/trunk/src/zope/schema/_field.py
===================================================================
--- zope.schema/trunk/src/zope/schema/_field.py	2012-12-31 14:06:30 UTC (rev 128938)
+++ zope.schema/trunk/src/zope/schema/_field.py	2012-12-31 15:29:12 UTC (rev 128939)
@@ -133,17 +133,8 @@
     _type = binary_type
 
     def fromUnicode(self, uc):
+        """ See IFromUnicode.
         """
-        >>> obj = Bytes(constraint=lambda v: b('x') in v)
-
-        >>> obj.fromUnicode(u(" foo x.y.z bat"))
-        ' foo x.y.z bat'
-        >>> obj.fromUnicode(u(" foo y.z bat"))
-        Traceback (most recent call last):
-        ...
-        ConstraintNotSatisfied:  foo y.z bat
-
-        """
         v = make_binary(uc)
         self.validate(v)
         return v
@@ -159,25 +150,6 @@
     __doc__ = IASCII.__doc__
 
     def _validate(self, value):
-        """
-        >>> ascii = ASCII()
-
-        Make sure we accept empty strings:
-
-        >>> empty = ''
-        >>> ascii._validate(empty)
-
-        and all kinds of alphanumeric strings:
-
-        >>> alphanumeric = "Bob\'s my 23rd uncle"
-        >>> ascii._validate(alphanumeric)
-
-        >>> umlauts = "Köhlerstraße"
-        >>> ascii._validate(umlauts)
-        Traceback (most recent call last):
-        ...
-        InvalidValue
-        """
         super(ASCII, self)._validate(value)
         if not value:
             return
@@ -217,15 +189,8 @@
         super(Float, self).__init__(*args, **kw)
 
     def fromUnicode(self, uc):
+        """ See IFromUnicode.
         """
-        >>> f = Float()
-        >>> f.fromUnicode("1.25")
-        1.25
-        >>> f.fromUnicode("1.25.6") #doctest: +IGNORE_EXCEPTION_DETAIL
-        Traceback (most recent call last):
-        ...
-        ValueError: invalid literal for float(): 1.25.6
-        """
         v = float(uc)
         self.validate(v)
         return v
@@ -240,18 +205,8 @@
         super(Decimal, self).__init__(*args, **kw)
 
     def fromUnicode(self, uc):
+        """ See IFromUnicode.
         """
-        >>> f = Decimal()
-        >>> import decimal
-        >>> isinstance(f.fromUnicode("1.25"), decimal.Decimal)
-        True
-        >>> float(f.fromUnicode("1.25"))
-        1.25
-        >>> f.fromUnicode("1.25.6")
-        Traceback (most recent call last):
-        ...
-        ValueError: invalid literal for Decimal(): 1.25.6
-        """
         try:
             v = decimal.Decimal(uc)
         except decimal.InvalidOperation:
@@ -357,19 +312,8 @@
         return clone
 
     def fromUnicode(self, str):
+        """ See IFromUnicode.
         """
-        >>> from zope.schema.vocabulary import SimpleVocabulary
-        >>> t = Choice(
-        ...     vocabulary=SimpleVocabulary.fromValues([u('foo'),u('bar')]))
-        >>> IFromUnicode.providedBy(t)
-        True
-        >>> t.fromUnicode(u("baz"))
-        Traceback (most recent call last):
-        ...
-        ConstraintNotSatisfied: baz
-        >>> t.fromUnicode(u("foo"))
-        u'foo'
-        """
         self.validate(str)
         return str
 
@@ -389,6 +333,104 @@
             raise ConstraintNotSatisfied(value)
 
 
+_isuri = r"[a-zA-z0-9+.-]+:" # scheme
+_isuri += r"\S*$" # non space (should be pickier)
+_isuri = re.compile(_isuri).match
+
+ at implementer(IURI, IFromUnicode)
+class URI(NativeStringLine):
+    """URI schema field
+    """
+
+    def _validate(self, value):
+        super(URI, self)._validate(value)
+        if _isuri(value):
+            return
+
+        raise InvalidURI(value)
+
+    def fromUnicode(self, value):
+        """ See IFromUnicode.
+        """
+        v = str(value.strip())
+        self.validate(v)
+        return v
+
+
+_isdotted = re.compile(
+    r"([a-zA-Z][a-zA-Z0-9_]*)"
+    r"([.][a-zA-Z][a-zA-Z0-9_]*)*"
+    # use the whole line
+    r"$").match
+
+
+ at implementer(IDottedName)
+class DottedName(NativeStringLine):
+    """Dotted name field.
+
+    Values of DottedName fields must be Python-style dotted names.
+    """
+
+    def __init__(self, *args, **kw):
+        self.min_dots = int(kw.pop("min_dots", 0))
+        if self.min_dots < 0:
+            raise ValueError("min_dots cannot be less than zero")
+        self.max_dots = kw.pop("max_dots", None)
+        if self.max_dots is not None:
+            self.max_dots = int(self.max_dots)
+            if self.max_dots < self.min_dots:
+                raise ValueError("max_dots cannot be less than min_dots")
+        super(DottedName, self).__init__(*args, **kw)
+
+    def _validate(self, value):
+        """
+
+        """
+        super(DottedName, self)._validate(value)
+        if not _isdotted(value):
+            raise InvalidDottedName(value)
+        dots = value.count(".")
+        if dots < self.min_dots:
+            raise InvalidDottedName("too few dots; %d required" % self.min_dots,
+                                    value)
+        if self.max_dots is not None and dots > self.max_dots:
+            raise InvalidDottedName("too many dots; no more than %d allowed" %
+                                    self.max_dots, value)
+
+    def fromUnicode(self, value):
+        v = value.strip()
+        if not isinstance(v, self._type):
+            v = v.encode('ascii')
+        self.validate(v)
+        return v
+
+
+ at implementer(IId, IFromUnicode)
+class Id(NativeStringLine):
+    """Id field
+
+    Values of id fields must be either uris or dotted names.
+    """
+
+    def _validate(self, value):
+        super(Id, self)._validate(value)
+        if _isuri(value):
+            return
+        if _isdotted(value) and "." in value:
+            return
+
+        raise InvalidId(value)
+
+    def fromUnicode(self, value):
+        """ See IFromUnicode.
+        """
+        v = value.strip()
+        if not isinstance(v, self._type):
+            v = v.encode('ascii')
+        self.validate(v)
+        return v
+
+
 @implementer(IInterfaceField)
 class InterfaceField(Field):
     __doc__ = IInterfaceField.__doc__
@@ -415,8 +457,8 @@
     To validate a sequence of various values:
 
        >>> errors = _validate_sequence(field, (b('foo'), u('bar'), 1))
-       >>> errors
-       [WrongType(b'foo', <type 'unicode'>, ''), WrongType(1, <type 'unicode'>, '')]
+       >>> errors # XXX assumes Python2 reprs
+       [WrongType('foo', <type 'unicode'>, ''), WrongType(1, <type 'unicode'>, '')]
 
     The only valid value in the sequence is the second item. The others
     generated errors.
@@ -424,9 +466,9 @@
     We can use the optional errors argument to collect additional errors
     for a new sequence:
 
-        >>> errors = _validate_sequence(field, (2, u('baz')), errors)
-        >>> errors
-        [WrongType(b'foo', <type 'unicode'>, ''), WrongType(1, <type 'unicode'>, ''), WrongType(2, <type 'unicode'>, '')]
+       >>> errors = _validate_sequence(field, (2, u('baz')), errors)
+       >>> errors # XXX assumes Python2 reprs
+       [WrongType('foo', <type 'unicode'>, ''), WrongType(1, <type 'unicode'>, ''), WrongType(2, <type 'unicode'>, '')]
 
     """
     if errors is None:
@@ -647,220 +689,3 @@
         if clone.value_type is not None:
             clone.value_type = clone.value_type.bind(object)
         return clone
-
-
-_isuri = r"[a-zA-z0-9+.-]+:" # scheme
-_isuri += r"\S*$" # non space (should be pickier)
-_isuri = re.compile(_isuri).match
-
- at implementer(IURI, IFromUnicode)
-class URI(NativeStringLine):
-    """URI schema field
-    """
-
-    def _validate(self, value):
-        """
-        >>> uri = URI(__name__='test')
-        >>> uri.validate(b("http://www.python.org/foo/bar"))
-        >>> uri.validate(b("DAV:"))
-        >>> uri.validate(b("www.python.org/foo/bar"))
-        Traceback (most recent call last):
-        ...
-        InvalidURI: www.python.org/foo/bar
-        """
-
-        super(URI, self)._validate(value)
-        if _isuri(value):
-            return
-
-        raise InvalidURI(value)
-
-    def fromUnicode(self, value):
-        """
-        >>> uri = URI(__name__='test')
-        >>> uri.fromUnicode("http://www.python.org/foo/bar")
-        'http://www.python.org/foo/bar'
-        >>> uri.fromUnicode("          http://www.python.org/foo/bar")
-        'http://www.python.org/foo/bar'
-        >>> uri.fromUnicode("      \\n    http://www.python.org/foo/bar\\n")
-        'http://www.python.org/foo/bar'
-        >>> uri.fromUnicode("http://www.python.org/ foo/bar")
-        Traceback (most recent call last):
-        ...
-        InvalidURI: http://www.python.org/ foo/bar
-        """
-        v = str(value.strip())
-        self.validate(v)
-        return v
-
-
-_isdotted = re.compile(
-    r"([a-zA-Z][a-zA-Z0-9_]*)"
-    r"([.][a-zA-Z][a-zA-Z0-9_]*)*"
-    # use the whole line
-    r"$").match
-
-
- at implementer(IId, IFromUnicode)
-class Id(NativeStringLine):
-    """Id field
-
-    Values of id fields must be either uris or dotted names.
-    """
-
-    def _validate(self, value):
-        """
-        >>> id = Id(__name__='test')
-        >>> id.validate("http://www.python.org/foo/bar")
-        >>> id.validate("zope.app.content")
-        >>> id.validate("zope.app.content/a")
-        Traceback (most recent call last):
-        ...
-        InvalidId: zope.app.content/a
-        >>> id.validate("http://zope.app.content x y")
-        Traceback (most recent call last):
-        ...
-        InvalidId: http://zope.app.content x y
-        """
-        super(Id, self)._validate(value)
-        if _isuri(value):
-            return
-        if _isdotted(value) and "." in value:
-            return
-
-        raise InvalidId(value)
-
-    def fromUnicode(self, value):
-        """
-        >>> id = Id(__name__='test')
-        >>> id.fromUnicode("http://www.python.org/foo/bar")
-        'http://www.python.org/foo/bar'
-        >>> id.fromUnicode(u(" http://www.python.org/foo/bar "))
-        'http://www.python.org/foo/bar'
-        >>> id.fromUnicode("http://www.python.org/ foo/bar")
-        Traceback (most recent call last):
-        ...
-        InvalidId: http://www.python.org/ foo/bar
-        >>> id.fromUnicode("      \\n x.y.z \\n")
-        'x.y.z'
-
-        """
-        v = value.strip()
-        if not isinstance(v, self._type):
-            v = v.encode('ascii')
-        self.validate(v)
-        return v
-
-
- at implementer(IDottedName)
-class DottedName(NativeStringLine):
-    """Dotted name field.
-
-    Values of DottedName fields must be Python-style dotted names.
-    """
-
-    def __init__(self, *args, **kw):
-        """
-        >>> DottedName(min_dots=-1)
-        Traceback (most recent call last):
-        ...
-        ValueError: min_dots cannot be less than zero
-
-        >>> DottedName(max_dots=-1)
-        Traceback (most recent call last):
-        ...
-        ValueError: max_dots cannot be less than min_dots
-
-        >>> DottedName(max_dots=1, min_dots=2)
-        Traceback (most recent call last):
-        ...
-        ValueError: max_dots cannot be less than min_dots
-
-        >>> dotted_name = DottedName(max_dots=1, min_dots=1)
-
-        >>> from zope.interface.verify import verifyObject
-        >>> verifyObject(IDottedName, dotted_name)
-        True
-
-        >>> dotted_name = DottedName(max_dots=1)
-        >>> dotted_name.min_dots
-        0
-
-        >>> dotted_name = DottedName(min_dots=1)
-        >>> dotted_name.max_dots
-        >>> dotted_name.min_dots
-        1
-        """
-        self.min_dots = int(kw.pop("min_dots", 0))
-        if self.min_dots < 0:
-            raise ValueError("min_dots cannot be less than zero")
-        self.max_dots = kw.pop("max_dots", None)
-        if self.max_dots is not None:
-            self.max_dots = int(self.max_dots)
-            if self.max_dots < self.min_dots:
-                raise ValueError("max_dots cannot be less than min_dots")
-        super(DottedName, self).__init__(*args, **kw)
-
-    def _validate(self, value):
-        """
-        >>> dotted_name = DottedName(__name__='test')
-        >>> dotted_name.validate("a.b.c")
-        >>> dotted_name.validate("a")
-        >>> dotted_name.validate("   a")
-        Traceback (most recent call last):
-        ...
-        InvalidDottedName:    a
-
-        >>> dotted_name = DottedName(__name__='test', min_dots=1)
-        >>> dotted_name.validate('a.b')
-        >>> dotted_name.validate('a.b.c.d')
-        >>> dotted_name.validate('a')
-        Traceback (most recent call last):
-        ...
-        InvalidDottedName: ('too few dots; 1 required', 'a')
-
-        >>> dotted_name = DottedName(__name__='test', max_dots=0)
-        >>> dotted_name.validate('a')
-        >>> dotted_name.validate('a.b')
-        Traceback (most recent call last):
-        ...
-        InvalidDottedName: ('too many dots; no more than 0 allowed', 'a.b')
-
-        >>> dotted_name = DottedName(__name__='test', max_dots=2)
-        >>> dotted_name.validate('a')
-        >>> dotted_name.validate('a.b')
-        >>> dotted_name.validate('a.b.c')
-        >>> dotted_name.validate('a.b.c.d')
-        Traceback (most recent call last):
-        ...
-        InvalidDottedName: ('too many dots; no more than 2 allowed', 'a.b.c.d')
-
-        >>> dotted_name = DottedName(__name__='test', max_dots=1, min_dots=1)
-        >>> dotted_name.validate('a.b')
-        >>> dotted_name.validate('a')
-        Traceback (most recent call last):
-        ...
-        InvalidDottedName: ('too few dots; 1 required', 'a')
-        >>> dotted_name.validate('a.b.c')
-        Traceback (most recent call last):
-        ...
-        InvalidDottedName: ('too many dots; no more than 1 allowed', 'a.b.c')
-
-        """
-        super(DottedName, self)._validate(value)
-        if not _isdotted(value):
-            raise InvalidDottedName(value)
-        dots = value.count(".")
-        if dots < self.min_dots:
-            raise InvalidDottedName("too few dots; %d required" % self.min_dots,
-                                    value)
-        if self.max_dots is not None and dots > self.max_dots:
-            raise InvalidDottedName("too many dots; no more than %d allowed" %
-                                    self.max_dots, value)
-
-    def fromUnicode(self, value):
-        v = value.strip()
-        if not isinstance(v, self._type):
-            v = v.encode('ascii')
-        self.validate(v)
-        return v

Modified: zope.schema/trunk/src/zope/schema/tests/test__field.py
===================================================================
--- zope.schema/trunk/src/zope/schema/tests/test__field.py	2012-12-31 14:06:30 UTC (rev 128938)
+++ zope.schema/trunk/src/zope/schema/tests/test__field.py	2012-12-31 15:29:12 UTC (rev 128939)
@@ -948,6 +948,259 @@
         self.assertRaises(ConstraintNotSatisfied, clone._validate, 42)
 
 
+class URITests(unittest.TestCase):
+
+    def _getTargetClass(self):
+        from zope.schema._field import URI
+        return URI
+
+    def _makeOne(self, *args, **kw):
+        return self._getTargetClass()(*args, **kw)
+
+    def test_class_conforms_to_IURI(self):
+        from zope.interface.verify import verifyClass
+        from zope.schema.interfaces import IURI
+        verifyClass(IURI, self._getTargetClass())
+
+    def test_instance_conforms_to_IURI(self):
+        from zope.interface.verify import verifyObject
+        from zope.schema.interfaces import IURI
+        verifyObject(IURI, self._makeOne())
+
+    def test_validate_wrong_types(self):
+        from zope.schema.interfaces import WrongType
+        from zope.schema._compat import non_native_string
+        field = self._makeOne()
+        self.assertRaises(WrongType, field.validate, non_native_string(''))
+        self.assertRaises(WrongType, field.validate, 1)
+        self.assertRaises(WrongType, field.validate, 1.0)
+        self.assertRaises(WrongType, field.validate, ())
+        self.assertRaises(WrongType, field.validate, [])
+        self.assertRaises(WrongType, field.validate, {})
+        self.assertRaises(WrongType, field.validate, set())
+        self.assertRaises(WrongType, field.validate, frozenset())
+        self.assertRaises(WrongType, field.validate, object())
+
+    def test_validate_not_required(self):
+        field = self._makeOne(required=False)
+        field.validate('http://example.com/')
+        field.validate(None)
+
+    def test_validate_required(self):
+        from zope.schema.interfaces import RequiredMissing
+        field = self._makeOne()
+        field.validate('http://example.com/')
+        self.assertRaises(RequiredMissing, field.validate, None)
+
+    def test_validate_not_a_uri(self):
+        from zope.schema.interfaces import ConstraintNotSatisfied
+        from zope.schema.interfaces import InvalidURI
+        field = self._makeOne()
+        self.assertRaises(InvalidURI, field.validate, '')
+        self.assertRaises(InvalidURI, field.validate, 'abc')
+        self.assertRaises(InvalidURI, field.validate, '\xab\xde')
+        self.assertRaises(ConstraintNotSatisfied,
+                          field.validate, 'http://example.com/\nDAV:')
+
+    def test_fromUnicode_ok(self):
+        from zope.schema._compat import u
+        field = self._makeOne()
+        self.assertEqual(field.fromUnicode(u('http://example.com/')),
+                         'http://example.com/')
+
+    def test_fromUnicode_invalid(self):
+        from zope.schema.interfaces import ConstraintNotSatisfied
+        from zope.schema.interfaces import InvalidURI
+        from zope.schema._compat import u
+        field = self._makeOne()
+        self.assertRaises(InvalidURI, field.fromUnicode, u(''))
+        self.assertRaises(InvalidURI, field.fromUnicode, u('abc'))
+        self.assertRaises(ConstraintNotSatisfied,
+                          field.fromUnicode, u('http://example.com/\nDAV:'))
+
+
+class DottedNameTests(unittest.TestCase):
+
+    def _getTargetClass(self):
+        from zope.schema._field import DottedName
+        return DottedName
+
+    def _makeOne(self, *args, **kw):
+        return self._getTargetClass()(*args, **kw)
+
+    def test_class_conforms_to_IDottedName(self):
+        from zope.interface.verify import verifyClass
+        from zope.schema.interfaces import IDottedName
+        verifyClass(IDottedName, self._getTargetClass())
+
+    def test_instance_conforms_to_IDottedName(self):
+        from zope.interface.verify import verifyObject
+        from zope.schema.interfaces import IDottedName
+        verifyObject(IDottedName, self._makeOne())
+
+    def test_ctor_defaults(self):
+        dotted = self._makeOne()
+        self.assertEqual(dotted.min_dots, 0)
+        self.assertEqual(dotted.max_dots, None)
+
+    def test_ctor_min_dots_invalid(self):
+        self.assertRaises(ValueError, self._makeOne, min_dots=-1)
+
+    def test_ctor_min_dots_valid(self):
+        dotted = self._makeOne(min_dots=1)
+        self.assertEqual(dotted.min_dots, 1)
+
+    def test_ctor_max_dots_invalid(self):
+        self.assertRaises(ValueError, self._makeOne, min_dots=2, max_dots=1)
+
+    def test_ctor_max_dots_valid(self):
+        dotted = self._makeOne(max_dots=2)
+        self.assertEqual(dotted.max_dots, 2)
+
+    def test_validate_wrong_types(self):
+        from zope.schema.interfaces import WrongType
+        from zope.schema._compat import non_native_string
+        field = self._makeOne()
+        self.assertRaises(WrongType, field.validate, non_native_string(''))
+        self.assertRaises(WrongType, field.validate, 1)
+        self.assertRaises(WrongType, field.validate, 1.0)
+        self.assertRaises(WrongType, field.validate, ())
+        self.assertRaises(WrongType, field.validate, [])
+        self.assertRaises(WrongType, field.validate, {})
+        self.assertRaises(WrongType, field.validate, set())
+        self.assertRaises(WrongType, field.validate, frozenset())
+        self.assertRaises(WrongType, field.validate, object())
+
+    def test_validate_not_required(self):
+        field = self._makeOne(required=False)
+        field.validate('name')
+        field.validate('dotted.name')
+        field.validate(None)
+
+    def test_validate_required(self):
+        from zope.schema.interfaces import RequiredMissing
+        field = self._makeOne()
+        field.validate('name')
+        field.validate('dotted.name')
+        self.assertRaises(RequiredMissing, field.validate, None)
+
+    def test_validate_w_min_dots(self):
+        from zope.schema.interfaces import InvalidDottedName
+        field = self._makeOne(min_dots=1)
+        self.assertRaises(InvalidDottedName, field.validate, 'name')
+        field.validate('dotted.name')
+        field.validate('moar.dotted.name')
+
+    def test_validate_w_max_dots(self):
+        from zope.schema.interfaces import InvalidDottedName
+        field = self._makeOne(max_dots=1)
+        field.validate('name')
+        field.validate('dotted.name')
+        self.assertRaises(InvalidDottedName,
+                          field.validate, 'moar.dotted.name')
+
+    def test_validate_not_a_dotted_name(self):
+        from zope.schema.interfaces import ConstraintNotSatisfied
+        from zope.schema.interfaces import InvalidDottedName
+        field = self._makeOne()
+        self.assertRaises(InvalidDottedName, field.validate, '')
+        self.assertRaises(InvalidDottedName, field.validate, '\xab\xde')
+        self.assertRaises(ConstraintNotSatisfied,
+                          field.validate, 'http://example.com/\nDAV:')
+
+    def test_fromUnicode_dotted_name_ok(self):
+        from zope.schema._compat import u
+        field = self._makeOne()
+        self.assertEqual(field.fromUnicode(u('dotted.name')), 'dotted.name')
+
+    def test_fromUnicode_invalid(self):
+        from zope.schema.interfaces import ConstraintNotSatisfied
+        from zope.schema.interfaces import InvalidDottedName
+        from zope.schema._compat import u
+        field = self._makeOne()
+        self.assertRaises(InvalidDottedName, field.fromUnicode, u(''))
+        self.assertRaises(ConstraintNotSatisfied,
+                          field.fromUnicode, u('http://example.com/\nDAV:'))
+
+
+class IdTests(unittest.TestCase):
+
+    def _getTargetClass(self):
+        from zope.schema._field import Id
+        return Id
+
+    def _makeOne(self, *args, **kw):
+        return self._getTargetClass()(*args, **kw)
+
+    def test_class_conforms_to_IId(self):
+        from zope.interface.verify import verifyClass
+        from zope.schema.interfaces import IId
+        verifyClass(IId, self._getTargetClass())
+
+    def test_instance_conforms_to_IId(self):
+        from zope.interface.verify import verifyObject
+        from zope.schema.interfaces import IId
+        verifyObject(IId, self._makeOne())
+
+    def test_validate_wrong_types(self):
+        from zope.schema.interfaces import WrongType
+        from zope.schema._compat import non_native_string
+        field = self._makeOne()
+        self.assertRaises(WrongType, field.validate, non_native_string(''))
+        self.assertRaises(WrongType, field.validate, 1)
+        self.assertRaises(WrongType, field.validate, 1.0)
+        self.assertRaises(WrongType, field.validate, ())
+        self.assertRaises(WrongType, field.validate, [])
+        self.assertRaises(WrongType, field.validate, {})
+        self.assertRaises(WrongType, field.validate, set())
+        self.assertRaises(WrongType, field.validate, frozenset())
+        self.assertRaises(WrongType, field.validate, object())
+
+    def test_validate_not_required(self):
+        field = self._makeOne(required=False)
+        field.validate('http://example.com/')
+        field.validate('dotted.name')
+        field.validate(None)
+
+    def test_validate_required(self):
+        from zope.schema.interfaces import RequiredMissing
+        field = self._makeOne()
+        field.validate('http://example.com/')
+        field.validate('dotted.name')
+        self.assertRaises(RequiredMissing, field.validate, None)
+
+    def test_validate_not_a_uri(self):
+        from zope.schema.interfaces import ConstraintNotSatisfied
+        from zope.schema.interfaces import InvalidId
+        field = self._makeOne()
+        self.assertRaises(InvalidId, field.validate, '')
+        self.assertRaises(InvalidId, field.validate, 'abc')
+        self.assertRaises(InvalidId, field.validate, '\xab\xde')
+        self.assertRaises(ConstraintNotSatisfied,
+                          field.validate, 'http://example.com/\nDAV:')
+
+    def test_fromUnicode_url_ok(self):
+        from zope.schema._compat import u
+        field = self._makeOne()
+        self.assertEqual(field.fromUnicode(u('http://example.com/')),
+                         'http://example.com/')
+
+    def test_fromUnicode_dotted_name_ok(self):
+        from zope.schema._compat import u
+        field = self._makeOne()
+        self.assertEqual(field.fromUnicode(u('dotted.name')), 'dotted.name')
+
+    def test_fromUnicode_invalid(self):
+        from zope.schema.interfaces import ConstraintNotSatisfied
+        from zope.schema.interfaces import InvalidId
+        from zope.schema._compat import u
+        field = self._makeOne()
+        self.assertRaises(InvalidId, field.fromUnicode, u(''))
+        self.assertRaises(InvalidId, field.fromUnicode, u('abc'))
+        self.assertRaises(ConstraintNotSatisfied,
+                          field.fromUnicode, u('http://example.com/\nDAV:'))
+
+
 class InterfaceFieldTests(unittest.TestCase):
 
     def _getTargetClass(self):
@@ -1767,259 +2020,6 @@
         self.assertEqual(field2.value_type.context, context)
 
 
-class URITests(unittest.TestCase):
-
-    def _getTargetClass(self):
-        from zope.schema._field import URI
-        return URI
-
-    def _makeOne(self, *args, **kw):
-        return self._getTargetClass()(*args, **kw)
-
-    def test_class_conforms_to_IURI(self):
-        from zope.interface.verify import verifyClass
-        from zope.schema.interfaces import IURI
-        verifyClass(IURI, self._getTargetClass())
-
-    def test_instance_conforms_to_IURI(self):
-        from zope.interface.verify import verifyObject
-        from zope.schema.interfaces import IURI
-        verifyObject(IURI, self._makeOne())
-
-    def test_validate_wrong_types(self):
-        from zope.schema.interfaces import WrongType
-        from zope.schema._compat import non_native_string
-        field = self._makeOne()
-        self.assertRaises(WrongType, field.validate, non_native_string(''))
-        self.assertRaises(WrongType, field.validate, 1)
-        self.assertRaises(WrongType, field.validate, 1.0)
-        self.assertRaises(WrongType, field.validate, ())
-        self.assertRaises(WrongType, field.validate, [])
-        self.assertRaises(WrongType, field.validate, {})
-        self.assertRaises(WrongType, field.validate, set())
-        self.assertRaises(WrongType, field.validate, frozenset())
-        self.assertRaises(WrongType, field.validate, object())
-
-    def test_validate_not_required(self):
-        field = self._makeOne(required=False)
-        field.validate('http://example.com/')
-        field.validate(None)
-
-    def test_validate_required(self):
-        from zope.schema.interfaces import RequiredMissing
-        field = self._makeOne()
-        field.validate('http://example.com/')
-        self.assertRaises(RequiredMissing, field.validate, None)
-
-    def test_validate_not_a_uri(self):
-        from zope.schema.interfaces import ConstraintNotSatisfied
-        from zope.schema.interfaces import InvalidURI
-        field = self._makeOne()
-        self.assertRaises(InvalidURI, field.validate, '')
-        self.assertRaises(InvalidURI, field.validate, 'abc')
-        self.assertRaises(InvalidURI, field.validate, '\xab\xde')
-        self.assertRaises(ConstraintNotSatisfied,
-                          field.validate, 'http://example.com/\nDAV:')
-
-    def test_fromUnicode_ok(self):
-        from zope.schema._compat import u
-        field = self._makeOne()
-        self.assertEqual(field.fromUnicode(u('http://example.com/')),
-                         'http://example.com/')
-
-    def test_fromUnicode_invalid(self):
-        from zope.schema.interfaces import ConstraintNotSatisfied
-        from zope.schema.interfaces import InvalidURI
-        from zope.schema._compat import u
-        field = self._makeOne()
-        self.assertRaises(InvalidURI, field.fromUnicode, u(''))
-        self.assertRaises(InvalidURI, field.fromUnicode, u('abc'))
-        self.assertRaises(ConstraintNotSatisfied,
-                          field.fromUnicode, u('http://example.com/\nDAV:'))
-
-
-class IdTests(unittest.TestCase):
-
-    def _getTargetClass(self):
-        from zope.schema._field import Id
-        return Id
-
-    def _makeOne(self, *args, **kw):
-        return self._getTargetClass()(*args, **kw)
-
-    def test_class_conforms_to_IId(self):
-        from zope.interface.verify import verifyClass
-        from zope.schema.interfaces import IId
-        verifyClass(IId, self._getTargetClass())
-
-    def test_instance_conforms_to_IId(self):
-        from zope.interface.verify import verifyObject
-        from zope.schema.interfaces import IId
-        verifyObject(IId, self._makeOne())
-
-    def test_validate_wrong_types(self):
-        from zope.schema.interfaces import WrongType
-        from zope.schema._compat import non_native_string
-        field = self._makeOne()
-        self.assertRaises(WrongType, field.validate, non_native_string(''))
-        self.assertRaises(WrongType, field.validate, 1)
-        self.assertRaises(WrongType, field.validate, 1.0)
-        self.assertRaises(WrongType, field.validate, ())
-        self.assertRaises(WrongType, field.validate, [])
-        self.assertRaises(WrongType, field.validate, {})
-        self.assertRaises(WrongType, field.validate, set())
-        self.assertRaises(WrongType, field.validate, frozenset())
-        self.assertRaises(WrongType, field.validate, object())
-
-    def test_validate_not_required(self):
-        field = self._makeOne(required=False)
-        field.validate('http://example.com/')
-        field.validate('dotted.name')
-        field.validate(None)
-
-    def test_validate_required(self):
-        from zope.schema.interfaces import RequiredMissing
-        field = self._makeOne()
-        field.validate('http://example.com/')
-        field.validate('dotted.name')
-        self.assertRaises(RequiredMissing, field.validate, None)
-
-    def test_validate_not_a_uri(self):
-        from zope.schema.interfaces import ConstraintNotSatisfied
-        from zope.schema.interfaces import InvalidId
-        field = self._makeOne()
-        self.assertRaises(InvalidId, field.validate, '')
-        self.assertRaises(InvalidId, field.validate, 'abc')
-        self.assertRaises(InvalidId, field.validate, '\xab\xde')
-        self.assertRaises(ConstraintNotSatisfied,
-                          field.validate, 'http://example.com/\nDAV:')
-
-    def test_fromUnicode_url_ok(self):
-        from zope.schema._compat import u
-        field = self._makeOne()
-        self.assertEqual(field.fromUnicode(u('http://example.com/')),
-                         'http://example.com/')
-
-    def test_fromUnicode_dotted_name_ok(self):
-        from zope.schema._compat import u
-        field = self._makeOne()
-        self.assertEqual(field.fromUnicode(u('dotted.name')), 'dotted.name')
-
-    def test_fromUnicode_invalid(self):
-        from zope.schema.interfaces import ConstraintNotSatisfied
-        from zope.schema.interfaces import InvalidId
-        from zope.schema._compat import u
-        field = self._makeOne()
-        self.assertRaises(InvalidId, field.fromUnicode, u(''))
-        self.assertRaises(InvalidId, field.fromUnicode, u('abc'))
-        self.assertRaises(ConstraintNotSatisfied,
-                          field.fromUnicode, u('http://example.com/\nDAV:'))
-
-
-class DottedNameTests(unittest.TestCase):
-
-    def _getTargetClass(self):
-        from zope.schema._field import DottedName
-        return DottedName
-
-    def _makeOne(self, *args, **kw):
-        return self._getTargetClass()(*args, **kw)
-
-    def test_class_conforms_to_IDottedName(self):
-        from zope.interface.verify import verifyClass
-        from zope.schema.interfaces import IDottedName
-        verifyClass(IDottedName, self._getTargetClass())
-
-    def test_instance_conforms_to_IDottedName(self):
-        from zope.interface.verify import verifyObject
-        from zope.schema.interfaces import IDottedName
-        verifyObject(IDottedName, self._makeOne())
-
-    def test_ctor_defaults(self):
-        dotted = self._makeOne()
-        self.assertEqual(dotted.min_dots, 0)
-        self.assertEqual(dotted.max_dots, None)
-
-    def test_ctor_min_dots_invalid(self):
-        self.assertRaises(ValueError, self._makeOne, min_dots=-1)
-
-    def test_ctor_min_dots_valid(self):
-        dotted = self._makeOne(min_dots=1)
-        self.assertEqual(dotted.min_dots, 1)
-
-    def test_ctor_max_dots_invalid(self):
-        self.assertRaises(ValueError, self._makeOne, min_dots=2, max_dots=1)
-
-    def test_ctor_max_dots_valid(self):
-        dotted = self._makeOne(max_dots=2)
-        self.assertEqual(dotted.max_dots, 2)
-
-    def test_validate_wrong_types(self):
-        from zope.schema.interfaces import WrongType
-        from zope.schema._compat import non_native_string
-        field = self._makeOne()
-        self.assertRaises(WrongType, field.validate, non_native_string(''))
-        self.assertRaises(WrongType, field.validate, 1)
-        self.assertRaises(WrongType, field.validate, 1.0)
-        self.assertRaises(WrongType, field.validate, ())
-        self.assertRaises(WrongType, field.validate, [])
-        self.assertRaises(WrongType, field.validate, {})
-        self.assertRaises(WrongType, field.validate, set())
-        self.assertRaises(WrongType, field.validate, frozenset())
-        self.assertRaises(WrongType, field.validate, object())
-
-    def test_validate_not_required(self):
-        field = self._makeOne(required=False)
-        field.validate('name')
-        field.validate('dotted.name')
-        field.validate(None)
-
-    def test_validate_required(self):
-        from zope.schema.interfaces import RequiredMissing
-        field = self._makeOne()
-        field.validate('name')
-        field.validate('dotted.name')
-        self.assertRaises(RequiredMissing, field.validate, None)
-
-    def test_validate_w_min_dots(self):
-        from zope.schema.interfaces import InvalidDottedName
-        field = self._makeOne(min_dots=1)
-        self.assertRaises(InvalidDottedName, field.validate, 'name')
-        field.validate('dotted.name')
-        field.validate('moar.dotted.name')
-
-    def test_validate_w_max_dots(self):
-        from zope.schema.interfaces import InvalidDottedName
-        field = self._makeOne(max_dots=1)
-        field.validate('name')
-        field.validate('dotted.name')
-        self.assertRaises(InvalidDottedName,
-                          field.validate, 'moar.dotted.name')
-
-    def test_validate_not_a_dotted_name(self):
-        from zope.schema.interfaces import ConstraintNotSatisfied
-        from zope.schema.interfaces import InvalidDottedName
-        field = self._makeOne()
-        self.assertRaises(InvalidDottedName, field.validate, '')
-        self.assertRaises(InvalidDottedName, field.validate, '\xab\xde')
-        self.assertRaises(ConstraintNotSatisfied,
-                          field.validate, 'http://example.com/\nDAV:')
-
-    def test_fromUnicode_dotted_name_ok(self):
-        from zope.schema._compat import u
-        field = self._makeOne()
-        self.assertEqual(field.fromUnicode(u('dotted.name')), 'dotted.name')
-
-    def test_fromUnicode_invalid(self):
-        from zope.schema.interfaces import ConstraintNotSatisfied
-        from zope.schema.interfaces import InvalidDottedName
-        from zope.schema._compat import u
-        field = self._makeOne()
-        self.assertRaises(InvalidDottedName, field.fromUnicode, u(''))
-        self.assertRaises(ConstraintNotSatisfied,
-                          field.fromUnicode, u('http://example.com/\nDAV:'))
-
-
 class DummyInstance(object):
     pass
 



More information about the checkins mailing list