[Zope3-checkins] CVS: Zope3/src/zope/schema/tests - tabcomplete.py:1.5 test_tabcomplete.py:1.5
Fred L. Drake, Jr.
fred@zope.com
Wed, 18 Jun 2003 15:44:33 -0400
Update of /cvs-repository/Zope3/src/zope/schema/tests
In directory cvs.zope.org:/tmp/cvs-serv17520
Modified Files:
tabcomplete.py test_tabcomplete.py
Log Message:
Revise example to match the current approach to supporting queryable
vocabularies.
=== Zope3/src/zope/schema/tests/tabcomplete.py 1.4 => 1.5 ===
--- Zope3/src/zope/schema/tests/tabcomplete.py:1.4 Wed Jun 18 15:02:46 2003
+++ Zope3/src/zope/schema/tests/tabcomplete.py Wed Jun 18 15:44:32 2003
@@ -15,8 +15,14 @@
"""Example vocabulary for tab completion."""
-from zope.schema.interfaces import ITerm, IVocabulary
-from zope.interface import implements
+from zope.schema.interfaces import ITerm, IVocabulary, IVocabularyQuery
+from zope.interface import implements, Interface
+
+
+class IPrefixQuery(IVocabularyQuery):
+
+ def queryForPrefix(prefix):
+ """Return a vocabulary that contains terms beginning with prefix."""
class Term:
@@ -48,19 +54,29 @@
def __contains__(self, value):
return value in self._values
+ def __iter__(self):
+ return TermIterator(self._values)
+
+ def __len__(self):
+ return len(self._values)
+
+ def getQuery(self):
+ return PrefixQuery(self)
+
def getTerm(self, value):
if value in self._values:
return Term(value)
raise LookupError(value)
- def __iter__(self):
- return TermIterator(self._values)
- def __len__(self):
- return len(self._values)
+class PrefixQuery:
+ implements(IPrefixQuery)
+
+ def __init__(self, vocabulary):
+ self.vocabulary = vocabulary
def queryForPrefix(self, prefix):
- L = [v for v in self._values if v.startswith(prefix)]
+ L = [v for v in self.vocabulary._values if v.startswith(prefix)]
if L:
return CompletionVocabulary(L)
else:
=== Zope3/src/zope/schema/tests/test_tabcomplete.py 1.4 => 1.5 ===
--- Zope3/src/zope/schema/tests/test_tabcomplete.py:1.4 Wed Jun 18 15:02:46 2003
+++ Zope3/src/zope/schema/tests/test_tabcomplete.py Wed Jun 18 15:44:32 2003
@@ -16,7 +16,7 @@
import unittest
-from zope.schema.interfaces import ITerm
+from zope.schema.interfaces import ITerm, IVocabularyQuery
from zope.schema.tests import tabcomplete
@@ -26,15 +26,21 @@
self.vocab = tabcomplete.CompletionVocabulary(['abc', 'def'])
def test_successful_query(self):
- subset = self.vocab.queryForPrefix("a")
+ query = self.vocab.getQuery()
+ subset = query.queryForPrefix("a")
L = [term.value for term in subset]
self.assertEqual(L, ["abc"])
- subset = self.vocab.queryForPrefix("def")
+ subset = query.queryForPrefix("def")
L = [term.value for term in subset]
self.assertEqual(L, ["def"])
def test_failed_query(self):
- self.assertRaises(LookupError, self.vocab.queryForPrefix, "g")
+ query = self.vocab.getQuery()
+ self.assertRaises(LookupError, query.queryForPrefix, "g")
+
+ def test_query_interface(self):
+ query = self.vocab.getQuery()
+ self.assert_(IVocabularyQuery.isImplementedBy(query))
def test_getTerm(self):
term = self.vocab.getTerm("abc")