[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form/tests - test_vocabularywidget.py:1.1.2.11

Fred L. Drake, Jr. fred@zope.com
Tue, 6 May 2003 14:50:17 -0400


Update of /cvs-repository/Zope3/src/zope/app/browser/form/tests
In directory cvs.zope.org:/tmp/cvs-serv17914

Modified Files:
      Tag: schema-vocabulary-branch
	test_vocabularywidget.py 
Log Message:
refactor tests into three classes: common, single-selection, and
multi-selection


=== Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py 1.1.2.10 => 1.1.2.11 ===
--- Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py:1.1.2.10	Tue May  6 14:20:46 2003
+++ Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py	Tue May  6 14:50:16 2003
@@ -81,17 +81,46 @@
 
 
 class SampleContent:
-    def __init__(self, value):
-        self.f = value
+    pass
 
 
-class VocabularyWidgetTests(PlacelessSetup, unittest.TestCase):
+class VocabularyWidgetTestBase(PlacelessSetup, unittest.TestCase):
 
     def setUp(self):
         PlacelessSetup.setUp(self)
+        self.registerViews()
+        # multi-selection:
+
+    _marker = object()
+
+    def makeField(self, vocabulary, value=_marker):
+        field = self.fieldClass(vocabulary=vocabulary, __name__="f")
+        content = SampleContent()
+        if value is self._marker:
+            value = self.defaultFieldValue
+        content.f = value
+        return field.bind(content)
+
+    # modified from test_browserwidget.BrowserWidgetTest:
+    def verifyResult(self, result, check_list):
+        for check in check_list:
+            self.assert_(result.find(check) >= 0,
+                         "%r not found in %r" % (check, result))
+
+    def test_field_indirection(self):
+        bound = self.makeField(SampleVocabulary())
+        w = getView(bound, "display", TestRequest())
+        self.assertEqual(w(), "foo")
+
+
+class SingleSelectionTests(VocabularyWidgetTestBase):
+
+    defaultFieldValue = "splat"
+    fieldClass = vocabulary.VocabularyField
+
+    def registerViews(self):
         # This is equivalent to the default configuration for
         # vocabulary field view registration from configure.zcml.
-        # single-selection:
         provideView(vocabulary.IVocabularyField,
                     "display",
                     IBrowserPresentation,
@@ -100,17 +129,7 @@
                     "edit",
                     IBrowserPresentation,
                     widget.VocabularyFieldEditWidget)
-        # multi-selection:
-        provideView(vocabulary.IVocabularyMultiField,
-                    "display",
-                    IBrowserPresentation,
-                    widget.VocabularyMultiFieldDisplayWidget)
-        provideView(vocabulary.IVocabularyMultiField,
-                    "edit",
-                    IBrowserPresentation,
-                    widget.VocabularyMultiFieldEditWidget)
         # Register the "basic" widgets:
-        # single-selection:
         provideView(vocabulary.IVocabulary,
                     "field-display-widget",
                     IBrowserPresentation,
@@ -119,53 +138,21 @@
                     "field-edit-widget",
                     IBrowserPresentation,
                     widget.VocabularyEditWidget)
-        # multi-selection:
-        provideView(vocabulary.IVocabulary,
-                    "field-display-multi-widget",
-                    IBrowserPresentation,
-                    widget.VocabularyMultiDisplayWidget)
-        provideView(vocabulary.IVocabulary,
-                    "field-edit-multi-widget",
-                    IBrowserPresentation,
-                    widget.VocabularyMultiEditWidget)
-        # The following widget registrations support the specific
-        # sample vocabulary we're using:
+        # The following widget registration supports the specific
+        # sample vocabulary we're using, used to demonstrate how to
+        # override widget selection based on vocabulary:
         provideView(ISampleVocabulary,
                     "field-display-widget",
                     IBrowserPresentation,
                     SampleDisplayWidget)
 
-    def makeField(self, cls, vocabulary, value="splat"):
-        field = cls(vocabulary=vocabulary, __name__="f")
-        return field.bind(SampleContent(value))
-
-    # modified from test_browserwidget.BrowserWidgetTest:
-    def verifyResult(self, result, check_list):
-        for check in check_list:
-            self.assert_(result.find(check) >= 0,
-                         "%r not found in %r" % (check, result))
-
-    def test_field_indirection(self):
-        bound = self.makeField(vocabulary.VocabularyField, SampleVocabulary())
-        w = getView(bound, "display", TestRequest())
-        self.assertEqual(w(), "foo")
-
-    def test_multi_field_indirection(self):
-        bound = self.makeField(vocabulary.VocabularyMultiField,
-                               SampleVocabulary())
-        # XXX this can be finished once the previous test is working,
-        # and we figure out the right way to handle multi-selects from
-        # the widgets
-
     def test_simple_display(self):
-        bound = self.makeField(vocabulary.VocabularyField,
-                               BasicVocabulary(["splat", "foobar"]))
+        bound = self.makeField(BasicVocabulary(["splat", "foobar"]))
         w = getView(bound, "display", TestRequest())
         self.assertEqual(w(), "splat")
 
     def test_simple_display_with_form_value(self):
-        bound = self.makeField(vocabulary.VocabularyField,
-                               BasicVocabulary(["splat", "foobar"]))
+        bound = self.makeField(BasicVocabulary(["splat", "foobar"]))
         request = TestRequest(QUERY_STRING='field.f=foobar')
         request.processInputs()
         w = getView(bound, "display", request)
@@ -174,7 +161,7 @@
 
     def test_simple_edit(self):
         vocab = BasicVocabulary(["splat", "foobar"])
-        bound = self.makeField(vocabulary.VocabularyField, vocab)
+        bound = self.makeField(vocab)
         w = getView(bound, "edit", TestRequest())
         self.assert_(not w.haveData())
         self.verifyResult(w(), [
@@ -200,8 +187,7 @@
             ])
 
     def test_simple_edit_with_form_value(self):
-        bound = self.makeField(vocabulary.VocabularyField,
-                               BasicVocabulary(["splat", "foobar"]))
+        bound = self.makeField(BasicVocabulary(["splat", "foobar"]))
         request = TestRequest(QUERY_STRING='field.f=foobar')
         request.processInputs()
         w = getView(bound, "edit", request)
@@ -215,9 +201,42 @@
             'value="foobar"',
             ])
 
+
+class MultiSelectionTests(VocabularyWidgetTestBase):
+
+    defaultFieldValue = ["splat"]
+    fieldClass = vocabulary.VocabularyMultiField
+
+    def registerViews(self):
+        # This is equivalent to the default configuration for
+        # vocabulary field view registration from configure.zcml.
+        provideView(vocabulary.IVocabularyMultiField,
+                    "display",
+                    IBrowserPresentation,
+                    widget.VocabularyMultiFieldDisplayWidget)
+        provideView(vocabulary.IVocabularyMultiField,
+                    "edit",
+                    IBrowserPresentation,
+                    widget.VocabularyMultiFieldEditWidget)
+        # Bind widgets to the vocabulary fields:
+        provideView(vocabulary.IVocabulary,
+                    "field-display-multi-widget",
+                    IBrowserPresentation,
+                    widget.VocabularyMultiDisplayWidget)
+        provideView(vocabulary.IVocabulary,
+                    "field-edit-multi-widget",
+                    IBrowserPresentation,
+                    widget.VocabularyMultiEditWidget)
+        # The following widget registration supports the specific
+        # sample vocabulary we're using, used to demonstrate how to
+        # override widget selection based on vocabulary:
+        provideView(ISampleVocabulary,
+                    "field-display-multi-widget",
+                    IBrowserPresentation,
+                    SampleDisplayWidget)
+
     def test_multi_display_without_value(self):
-        bound = self.makeField(vocabulary.VocabularyMultiField,
-                               BasicVocabulary(["splat", "foobar", "frob"]),
+        bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]),
                                None)
         w = getView(bound, "display", TestRequest())
         self.assert_(not w.haveData())
@@ -230,8 +249,7 @@
             ])
 
     def test_multi_display_with_value(self):
-        bound = self.makeField(vocabulary.VocabularyMultiField,
-                               BasicVocabulary(["splat", "foobar", "frob"]),
+        bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]),
                                ["foobar", "frob"])
         w = getView(bound, "display", TestRequest())
         self.assert_(not w.haveData())
@@ -253,8 +271,7 @@
             ])
 
     def test_multi_display_with_form_data(self):
-        bound = self.makeField(vocabulary.VocabularyMultiField,
-                               BasicVocabulary(["splat", "foobar", "frob"]),
+        bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]),
                                ["foobar", "frob"])
         request = TestRequest(QUERY_STRING='field.f:list=splat')
         request.processInputs()
@@ -275,8 +292,7 @@
         self.assert_(s.find("frob") < 0)
 
     def test_multi_edit(self):
-        bound = self.makeField(vocabulary.VocabularyMultiField,
-                               BasicVocabulary(["splat", "foobar", "frob"]))
+        bound = self.makeField(BasicVocabulary(["splat", "foobar", "frob"]))
         w = getView(bound, "edit", TestRequest())
         self.assert_(not w.haveData())
         self.verifyResult(w(), [
@@ -308,8 +324,7 @@
         self.assert_(s3.find('selected') < 0)
 
     def test_multi_edit_with_form_value(self):
-        bound = self.makeField(vocabulary.VocabularyMultiField,
-                               BasicVocabulary(["splat", "foobar"]))
+        bound = self.makeField(BasicVocabulary(["splat", "foobar"]))
         request = TestRequest(
             QUERY_STRING='field.f:list=foobar&field.f:list=splat')
         request.processInputs()
@@ -328,8 +343,9 @@
 
 
 def test_suite():
-    return unittest.makeSuite(VocabularyWidgetTests)
-
+    suite = unittest.makeSuite(SingleSelectionTests)
+    suite.addTest(unittest.makeSuite(MultiSelectionTests))
+    return suite
 
 if __name__ == '__main__':
     unittest.main()