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

Fred L. Drake, Jr. fred@zope.com
Wed, 14 May 2003 13:49:09 -0400


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

Modified Files:
      Tag: schema-vocabulary-branch
	test_vocabulary.py 
Log Message:
- allow additional keyword args to be associated with the call to a
  vocabulary factory
- add tests for the ZCML integration


=== Zope3/src/zope/app/schema/tests/test_vocabulary.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/app/schema/tests/test_vocabulary.py:1.1.2.1	Thu May  8 13:22:49 2003
+++ Zope3/src/zope/app/schema/tests/test_vocabulary.py	Wed May 14 13:49:08 2003
@@ -17,6 +17,18 @@
 import unittest
 
 from zope.app.schema import vocabulary
+from zope.configuration.tests.test_xml import TempFile
+from zope.configuration.xmlconfig import XMLConfig
+
+
+class MyContext:
+    def resolve(self, name):
+        return MyFactory
+
+class MyFactory:
+    def __init__(self, context, **kw):
+        self.ob = context
+        self.kw = kw
 
 
 class VocabularyServiceTests(unittest.TestCase):
@@ -31,6 +43,54 @@
         self.assertRaises(LookupError,
                           vocabulary.vocabularyService.get,
                           None, "missing-vocabulary")
+
+    def test_passing_keywords_from_zcml(self):
+        text = """\
+        <zopeConfigure xmlns='http://namespaces.zope.org/zope'>
+          <include package='zope.configuration' file='metameta.zcml' />
+          <include package='zope.configuration' file='meta.zcml' />
+          <include package='zope.app.component' file='meta.zcml' />
+          <include package='zope.app.schema' file='meta.zcml' />
+
+          <include package='zope.app.schema' />
+
+          <vocabulary
+              name='foo'
+              factory='zope.app.schema.tests.test_vocabulary.MyFactory'
+              some='SOME'
+              other='OTHER'
+              />
+        </zopeConfigure>
+        """
+        f = TempFile()
+        try:
+            f.write(text)
+            f.flush()
+            x = XMLConfig(f.name)
+            x()
+        finally:
+            f.close()
+        r = vocabulary.vocabularyService.get(object(), "foo")
+
+    def test_action_with_keywords(self):
+        # make sure the action machinery works, aside from ZCML concerns
+        actions = vocabulary.register(MyContext(), "my-vocab", ".maker",
+                                      filter="my-filter", another="keyword")
+        self.assertEqual(len(actions), 1)
+        descriminator, callable, args, kw = actions[0]
+        # check our expectations of the action:
+        self.assertEqual(len(args), 2)
+        self.assertEqual(args[0], "my-vocab")
+        self.assertEqual(kw, {})
+        self.assert_(isinstance(args[1], vocabulary.FactoryKeywordPasser))
+        # enact the registration:
+        callable(*args, **kw)
+        # make sure the factory behaves as expected:
+        context = object()
+        vocab = vocabulary.vocabularyService.get(context, "my-vocab")
+        self.assert_(vocab.ob is context)
+        self.assertEqual(vocab.kw, {"filter": "my-filter",
+                                    "another": "keyword"})
 
 
 def test_suite():