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

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


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

Modified Files:
      Tag: schema-vocabulary-branch
	test_vocabularywidget.py 
Log Message:
general cleanup of the tests:
- makeFields():
  - renamed to makeField()
  - only return the bound field; unbound wasn't used
- shorten the __name__ of the test field
- simplify some tests to avoid extra object creation (without reducing
  coverage)


=== Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py 1.1.2.8 => 1.1.2.9 ===
--- Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py:1.1.2.8	Tue May  6 12:47:03 2003
+++ Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py	Tue May  6 14:16:24 2003
@@ -82,16 +82,16 @@
 
 class SampleContent:
     def __init__(self, value):
-        self.myfield = value
+        self.f = value
 
 
 class VocabularyWidgetTests(PlacelessSetup, unittest.TestCase):
 
-    # copied from test_browserwidget.BrowserWidgetTest:
+    # modified 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+'"')
+            self.assert_(result.find(check) >= 0,
+                         "%r not found in %r" % (check, result))
 
     def setUp(self):
         PlacelessSetup.setUp(self)
@@ -141,38 +141,32 @@
                     IBrowserPresentation,
                     SampleDisplayWidget)
 
-    def makeFields(self, cls, vocabulary, value="splat"):
-        field = cls(vocabulary=vocabulary, __name__="myfield")
-        return field, field.bind(SampleContent(value))
+    def makeField(self, cls, vocabulary, value="splat"):
+        field = cls(vocabulary=vocabulary, __name__="f")
+        return field.bind(SampleContent(value))
 
     def test_field_indirection(self):
-        field, bound = self.makeFields(vocabulary.VocabularyField,
-                                       SampleVocabulary())
+        bound = self.makeField(vocabulary.VocabularyField, SampleVocabulary())
         w = getView(bound, "display", TestRequest())
         self.assertEqual(w(), "foo")
 
     def test_multi_field_indirection(self):
-        field, bound = self.makeFields(vocabulary.VocabularyMultiField,
-                                       SampleVocabulary())
+        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):
-        field, bound = self.makeFields(vocabulary.VocabularyField,
-                                       BasicVocabulary(["splat", "foobar"]))
+        bound = self.makeField(vocabulary.VocabularyField,
+                               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_display_with_form_value(self):
-        field, bound = self.makeFields(vocabulary.VocabularyField,
-                                       BasicVocabulary(["splat", "foobar"]))
-        request = TestRequest(QUERY_STRING='field.myfield=foobar')
+        bound = self.makeField(vocabulary.VocabularyField,
+                               BasicVocabulary(["splat", "foobar"]))
+        request = TestRequest(QUERY_STRING='field.f=foobar')
         request.processInputs()
         w = getView(bound, "display", request)
         self.assert_(w.haveData())
@@ -180,45 +174,35 @@
 
     def test_simple_edit(self):
         vocab = BasicVocabulary(["splat", "foobar"])
-        field, bound = self.makeFields(vocabulary.VocabularyField, vocab)
+        bound = self.makeField(vocabulary.VocabularyField, vocab)
         w = getView(bound, "edit", TestRequest())
         self.assert_(not w.haveData())
         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.assert_(not w.haveData())
-        self._verifyResult(w(), [
-            'selected="selected"',
-            'id="field.myfield"',
-            'name="field.myfield"',
+            'id="field.f"',
+            'name="field.f"',
             'class="vocabularyType"',
             'value="splat"',
             '>splat<',
             'value="foobar"',
             '>foobar<',
             ])
-        s1, s2 = w.renderItems("splat")
+        s1, s2 = w.renderItems("foobar")
         self._verifyResult(s1, [
-            'selected="selected"',
             'value="splat"',
             '>splat<',
             ])
+        self.assert_(s1.find('selected') < 0)
         self._verifyResult(s2, [
+            'selected="selected"',
             'value="foobar"',
             '>foobar<',
             ])
-        self.assert_(s2.find('selected') < 0)
 
     def test_simple_edit_with_form_value(self):
-        field, bound = self.makeFields(vocabulary.VocabularyField,
-                                       BasicVocabulary(["splat", "foobar"]))
-        request = TestRequest(QUERY_STRING='field.myfield=foobar')
+        bound = self.makeField(vocabulary.VocabularyField,
+                               BasicVocabulary(["splat", "foobar"]))
+        request = TestRequest(QUERY_STRING='field.f=foobar')
         request.processInputs()
         w = getView(bound, "edit", request)
         self.assert_(w.haveData())
@@ -226,43 +210,42 @@
         self.assert_(isinstance(w, widget.VocabularyEditWidget))
         self._verifyResult(w.hidden(), [
             '<input',
-            'id="field.myfield"',
-            'name="field.myfield"',
+            'id="field.f"',
+            'name="field.f"',
             'value="foobar"',
             ])
 
     def test_multi_display_without_value(self):
-        field, bound = self.makeFields(vocabulary.VocabularyMultiField,
-                                       BasicVocabulary(["splat", "foobar",
-                                                        "frob"]),
-                                       None)
+        bound = self.makeField(vocabulary.VocabularyMultiField,
+                               BasicVocabulary(["splat", "foobar", "frob"]),
+                               None)
         w = getView(bound, "display", TestRequest())
         self.assert_(not w.haveData())
         self._verifyResult(w(), [
             '<span',
-            'id="field.myfield"',
-            'name="field.myfield"',
+            'id="field.f"',
+            'name="field.f"',
             'class="vocabularyType"',
             '</span>',
             ])
 
     def test_multi_display_with_value(self):
-        field, bound = self.makeFields(vocabulary.VocabularyMultiField,
-                                       BasicVocabulary(["splat", "foobar",
-                                                        "frob"]),
-                                       ["foobar", "frob"])
+        bound = self.makeField(vocabulary.VocabularyMultiField,
+                               BasicVocabulary(["splat", "foobar", "frob"]),
+                               ["foobar", "frob"])
         w = getView(bound, "display", TestRequest())
         self.assert_(not w.haveData())
         self._verifyResult(w(), [
             '<ol',
-            'id="field.myfield"',
-            'name="field.myfield"',
+            'id="field.f"',
+            'name="field.f"',
             'class="vocabularyType"',
             '</ol>',
             ])
         w.cssClass = 'test'
-        s = w.renderItems(['foobar'])[0]
-        self._verifyResult(s, [
+        items = w.renderItems(['foobar'])
+        self.assertEqual(len(items), 1)
+        self._verifyResult(items[0], [
             '<li',
             'class="test-item"',
             '>foobar<',
@@ -270,19 +253,18 @@
             ])
 
     def test_multi_display_with_form_data(self):
-        field, bound = self.makeFields(vocabulary.VocabularyMultiField,
-                                       BasicVocabulary(["splat", "foobar",
-                                                        "frob"]),
-                                       ["foobar", "frob"])
-        request = TestRequest(QUERY_STRING='field.myfield:list=splat')
+        bound = self.makeField(vocabulary.VocabularyMultiField,
+                               BasicVocabulary(["splat", "foobar", "frob"]),
+                               ["foobar", "frob"])
+        request = TestRequest(QUERY_STRING='field.f:list=splat')
         request.processInputs()
         w = getView(bound, "display", request)
         self.assert_(w.haveData())
         s = w()
         self._verifyResult(s, [
             '<ol',
-            'id="field.myfield"',
-            'name="field.myfield"',
+            'id="field.f"',
+            'name="field.f"',
             'class="vocabularyType"',
             '<li',
             '>splat<',
@@ -293,14 +275,13 @@
         self.assert_(s.find("frob") < 0)
 
     def test_multi_edit(self):
-        field, bound = self.makeFields(vocabulary.VocabularyMultiField,
-                                       BasicVocabulary(["splat", "foobar",
-                                                        "frob"]))
+        bound = self.makeField(vocabulary.VocabularyMultiField,
+                               BasicVocabulary(["splat", "foobar", "frob"]))
         w = getView(bound, "edit", TestRequest())
         self.assert_(not w.haveData())
         self._verifyResult(w(), [
-            'id="field.myfield"',
-            'name="field.myfield:list"',
+            'id="field.f"',
+            'name="field.f:list"',
             'class="vocabularyType"',
             'value="splat"',
             '>splat<',
@@ -327,10 +308,10 @@
         self.assert_(s3.find('selected') < 0)
 
     def test_multi_edit_with_form_value(self):
-        field, bound = self.makeFields(vocabulary.VocabularyMultiField,
-                                       BasicVocabulary(["splat", "foobar"]))
+        bound = self.makeField(vocabulary.VocabularyMultiField,
+                               BasicVocabulary(["splat", "foobar"]))
         request = TestRequest(
-            QUERY_STRING='field.myfield:list=foobar&field.myfield:list=splat')
+            QUERY_STRING='field.f:list=foobar&field.f:list=splat')
         request.processInputs()
         w = getView(bound, "edit", request)
         self.assert_(w.haveData())
@@ -340,7 +321,7 @@
         self._verifyResult(w.hidden(), [
             '<input',
             'type="hidden"',
-            'name="field.myfield:list"',
+            'name="field.f:list"',
             'value="foobar"',
             'value="splat"',
             ])