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

Fred L. Drake, Jr. fred@zope.com
Fri, 2 May 2003 12:39:10 -0400


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

Added Files:
      Tag: schema-vocabulary-branch
	test_vocabularywidget.py 
Log Message:
Tests of the vocabulary field widget support.
These don't work yet.


=== Added File Zope3/src/zope/app/browser/form/tests/test_vocabularywidget.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

"""Tests of the vocabulary field widget machinery."""

import unittest

from zope.app.browser.form import widget
from zope.app.interfaces.browser.form import IBrowserWidget
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.component import getView
from zope.component.view import provideView
from zope.publisher.browser import TestRequest
from zope.publisher.interfaces.browser import IBrowserPresentation

from zope.schema import vocabulary


class ISampleVocabulary(vocabulary.IVocabulary):
    """Specialized interface so we can hook views onto a vocabulary."""

class SampleTerm(object):
    __implements__ = vocabulary.ITerm
    value = "splat"
    extra = 42

class SampleVocabulary(object):
    __implements__ = ISampleVocabulary

    def __contains__(self, value):
        return value == "splat"

    def __len__(self):
        return 1

    def getTerm(self, value):
        if value == "splat":
            return SampleTerm()
        raise LookupError("%r didn't 'splat'!" % value)


class SampleDisplayWidget(widget.BrowserWidget):
    __implements__ = IBrowserWidget

    cssClass = 'vocab-display-widget'


class VocabularyWidgetTests(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        PlacelessSetup.setUp(self)
        # This is equivalent to the default configuration for
        # vocabulary field view registration from configure.zcml.
        # single-selection:
        provideView(vocabulary.IVocabularyField,
                    "display",
                    IBrowserPresentation,
                    widget.VocabularyFieldDisplayWidget)
        provideView(vocabulary.IVocabularyField,
                    "edit",
                    IBrowserPresentation,
                    widget.VocabularyFieldEditWidget)
        # multi-selection:
        provideView(vocabulary.IVocabularyMultiField,
                    "display",
                    IBrowserPresentation,
                    widget.VocabularyMultiFieldDisplayWidget)
        provideView(vocabulary.IVocabularyMultiField,
                    "edit",
                    IBrowserPresentation,
                    widget.VocabularyMultiFieldEditWidget)
        # The following widget registrations support the specific
        # sample vocabulary we're using:
        provideView(ISampleVocabulary,
                    "field-display-widget",
                    IBrowserPresentation,
                    SampleDisplayWidget)

    def makeFields(self, cls):
        field = cls(vocabulary=SampleVocabulary())
        return field, field.bind("Yowza!")

    def donttest_field_indirection(self):
        field, bound = self.makeFields(vocabulary.VocabularyField)
        w = getView(bound, "display", TestRequest())

    def test_multi_field_indirection(self):
        field, bound = self.makeFields(vocabulary.VocabularyMultiField)
        # XXX this can be finished once the previous test is working


def test_suite():
    return unittest.makeSuite(VocabularyWidgetTests)


if __name__ == '__main__':
    unittest.main()