[Zope3-checkins] CVS: Zope3/src/zope/schema/tests - test_vocabulary.py:1.1.2.2

Fred L. Drake, Jr. fred@zope.com
Fri, 2 May 2003 11:32:19 -0400


Update of /cvs-repository/Zope3/src/zope/schema/tests
In directory cvs.zope.org:/tmp/cvs-serv2607

Modified Files:
      Tag: schema-vocabulary-branch
	test_vocabulary.py 
Log Message:
- add tests for VocabularyMultiField
- some refactoring


=== Zope3/src/zope/schema/tests/test_vocabulary.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/schema/tests/test_vocabulary.py:1.1.2.1	Fri May  2 10:11:16 2003
+++ Zope3/src/zope/schema/tests/test_vocabulary.py	Fri May  2 11:32:19 2003
@@ -14,20 +14,19 @@
 
 """Test of the VocabularyField and related support APIs."""
 
+import sys
 import unittest
 
 from zope.schema.interfaces import ValidationError
 from zope.schema import vocabulary
 
 
-class Thing:
-    def __init__(self, object, name):
-        self.object = object
-        self.name = name
-
 class DummyRegistry(vocabulary.VocabularyRegistry):
     def get(self, object, name):
-        return Thing(object, name)
+        v = SampleVocabulary()
+        v.object = object
+        v.name = name
+        return v
 
 
 class BaseTest(unittest.TestCase):
@@ -79,28 +78,45 @@
 class VocabularyFieldTests(BaseTest):
     """Tests of the VocabularyField implementation."""
 
-    def test_preconstructed_vocabulary(self):
+    def check_preconstructed(self, cls, okval, badval):
         v = SampleVocabulary()
-        field = vocabulary.VocabularyField(vocabulary=v)
+        field = cls(vocabulary=v)
         self.assert_(field.vocabulary is v)
         self.assert_(field.vocabularyName is None)
         bound = field.bind(None)
         self.assert_(bound.vocabulary is v)
         self.assert_(bound.vocabularyName is None)
+        bound.default = okval
+        self.assertEqual(bound.default, okval)
         self.assertRaises(ValidationError,
-                          setattr, bound, "default", 42)
+                          setattr, bound, "default", badval)
 
-    def test_constructed_vocabulary(self):
+    def test_preconstructed_vocabulary(self):
+        self.check_preconstructed(vocabulary.VocabularyField, 1, 42)
+
+    def test_preconstructed_vocabulary_multi(self):
+        self.check_preconstructed(vocabulary.VocabularyMultiField,
+                                  [1], [1, 42])
+
+    def check_constructed(self, cls, okval, badval):
         vocabulary.setVocabularyRegistry(DummyRegistry())
-        field = vocabulary.VocabularyField(vocabulary="vocab")
+        field = cls(vocabulary="vocab")
         self.assert_(field.vocabulary is None)
         self.assertEqual(field.vocabularyName, "vocab")
         o = object()
         bound = field.bind(o)
-        v = bound.vocabulary
-        self.assert_(isinstance(v, Thing))
-        self.assert_(v.object is o)
-        self.assertEqual(v.name, "vocab")
+        self.assert_(isinstance(bound.vocabulary, SampleVocabulary))
+        bound.default = okval
+        self.assertEqual(bound.default, okval)
+        self.assertRaises(ValidationError,
+                          setattr, bound, "default", badval)
+
+    def test_constructed_vocabulary(self):
+        self.check_constructed(vocabulary.VocabularyField, 1, 42)
+
+    def test_constructed_vocabulary_multi(self):
+        self.check_constructed(vocabulary.VocabularyMultiField,
+                               [1], [1, 42])
 
 
 def test_suite():