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

Fred L. Drake, Jr. fred@zope.com
Mon, 5 May 2003 13:56:56 -0400


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

Modified Files:
      Tag: schema-vocabulary-branch
	test_vocabularywidget.py 
Log Message:
- added test of the simple edit widget
- general cleanup of support code


=== Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py 1.1.2.3 => 1.1.2.4 ===
--- Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py:1.1.2.3	Fri May  2 18:06:24 2003
+++ Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py	Mon May  5 13:56:55 2003
@@ -32,29 +32,47 @@
 
 class SampleTerm(object):
     __implements__ = vocabulary.ITerm
-    value = "splat"
-    extra = 42
+
+    def __init__(self, value):
+        self.value = value
 
 
 class BasicVocabulary(object):
     __implements__ = vocabulary.IVocabulary
 
+    def __init__(self, values=None):
+        if values is None:
+            values = ["splat"]
+        self._values = values
+
     def __contains__(self, value):
-        return value == "splat"
+        return value in self._values
+
+    def __iter__(self):
+        return BasicIterator(self._values)
 
     def __len__(self):
-        return 1
+        return len(self._values)
 
     def getTerm(self, value):
-        if value == "splat":
-            return SampleTerm()
-        raise LookupError("%r didn't 'splat'!" % value)
+        if value in self._values:
+            return SampleTerm(value)
+        raise LookupError("%r not a vocabulary member" % value)
+
+class BasicIterator(object):
+    def __init__(self, values):
+        self._next = iter(values).next
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        return SampleTerm(self._next())
 
 class SampleVocabulary(BasicVocabulary):
     __implements__ = ISampleVocabulary
 
 
-
 class SampleDisplayWidget(widget.VocabularyWidgetBase):
     __implements__ = IBrowserWidget
 
@@ -63,12 +81,18 @@
 
 
 class SampleContent:
-    def __init__(self):
-        self.field = "splat"
+    def __init__(self, value):
+        self.myfield = value
 
 
 class VocabularyWidgetTests(PlacelessSetup, unittest.TestCase):
 
+    # copied from test_browserwidget.BrowserWidgetTest:
+    def _verifyResult(self, result, check_list):
+        for check in check_list:
+            self.assertNotEqual(-1, result.find(check),
+                                '"'+check+'" not found in "'+result+'"')
+
     def setUp(self):
         PlacelessSetup.setUp(self)
         # This is equivalent to the default configuration for
@@ -91,20 +115,25 @@
                     "edit",
                     IBrowserPresentation,
                     widget.VocabularyMultiFieldEditWidget)
-        # The following widget registrations support the specific
-        # sample vocabulary we're using:
+        # Register the "basic" widgets:
         provideView(vocabulary.IVocabulary,
                     "field-display-widget",
                     IBrowserPresentation,
                     widget.VocabularyDisplayWidget)
+        provideView(vocabulary.IVocabulary,
+                    "field-edit-widget",
+                    IBrowserPresentation,
+                    widget.VocabularyEditWidget)
+        # The following widget registrations support the specific
+        # sample vocabulary we're using:
         provideView(ISampleVocabulary,
                     "field-display-widget",
                     IBrowserPresentation,
                     SampleDisplayWidget)
 
-    def makeFields(self, cls, vocabulary):
-        field = cls(vocabulary=vocabulary, __name__="field")
-        return field, field.bind(SampleContent())
+    def makeFields(self, cls, vocabulary, value="splat"):
+        field = cls(vocabulary=vocabulary, __name__="myfield")
+        return field, field.bind(SampleContent(value))
 
     def test_field_indirection(self):
         field, bound = self.makeFields(vocabulary.VocabularyField,
@@ -121,9 +150,48 @@
 
     def test_simple_display(self):
         field, bound = self.makeFields(vocabulary.VocabularyField,
-                                       BasicVocabulary())
+                                       BasicVocabulary(["splat", "foobar"]))
         w = getView(bound, "display", TestRequest())
         self.assertEqual(w(), "splat")
+        field, bound = self.makeFields(vocabulary.VocabularyField,
+                                       BasicVocabulary(["splat", "foobar"]),
+                                       "foobar")
+        w = getView(bound, "display", TestRequest())
+        self.assertEqual(w(), "foobar")
+
+    def test_simple_edit(self):
+        vocab = BasicVocabulary(["splat", "foobar"])
+        field, bound = self.makeFields(vocabulary.VocabularyField, vocab)
+        w = getView(bound, "edit", TestRequest())
+        self._verifyResult(w(), [
+            'selected="selected"',
+            'id="field.myfield"',
+            'name="field.myfield"',
+            '>splat<',
+            ])
+        field, bound = self.makeFields(vocabulary.VocabularyField, vocab,
+                                       "foobar")
+        w = getView(bound, "edit", TestRequest())
+        self._verifyResult(w(), [
+            'selected="selected"',
+            'id="field.myfield"',
+            'name="field.myfield"',
+            'value="splat"',
+            'class="vocabularyType"',
+            '>splat<',
+            '>foobar<',
+            ])
+        s1, s2 = w.renderItems("splat")
+        self._verifyResult(s1, [
+            'selected="selected"',
+            'value="splat"',
+            '>splat<',
+            ])
+        self._verifyResult(s2, [
+            'value="foobar"',
+            '>foobar<',
+            ])
+        self.assert_(s2.find('selected') < 0)
 
 
 def test_suite():