[Zope3-checkins] CVS: Zope3/src/zope/app/utility/browser/tests - __init__.py:1.1 test_registered.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Mar 11 17:05:04 EST 2004


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

Added Files:
	__init__.py test_registered.py 
Log Message:
Moved local utility service implementation to zope.app.utility till Jim 
will provide the new one.

Note: I forgot this module in an earlier checkin today.


=== Added File Zope3/src/zope/app/utility/browser/tests/__init__.py ===
"""Tests of the utility service views."""


=== Added File Zope3/src/zope/app/utility/browser/tests/test_registered.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 for the registered view support.

$Id: test_registered.py,v 1.1 2004/03/11 22:05:03 srichter Exp $
"""
import unittest

from zope.app.tests import ztapi
from zope.app.utility.browser import Utilities
from zope.app.tests import placelesssetup
from zope.interface import Interface, implements
from zope.publisher.browser import BrowserView, TestRequest


class IFoo(Interface):
    """Sample interface."""
    def someMethod(someArg):
        """Sample interface method."""

class IBar(IFoo):
    """Derived interface."""
    def anotherMethod():
        """Another example method interface."""

class IStub(Interface):
    """Interface used to bind an absolute_url view to stub objects."""

class Stub:
    # Does triple duty as a stub for a registration, ay registration
    # stack, and a component!

    implements(IStub)

    def __init__(self, url=None):
        self.url = url

    # registration registry
    def active(self):
        if self.url:
            return self
        else:
            return None

    def info(self):
        return [{'registration': self}]

    # registration
    def getComponent(self):
        return self

    def usageSummary(self):
        return ""

class StubAbsoluteURL(BrowserView):
    def __str__(self):
        return self.context.url

class StubLocalUtilityService:
    def getRegisteredMatching(self):
        return [
            # (iface, name, configregistry)
            (IFoo, '', Stub("1")),
            (IFoo, 'myfoo-1', Stub("2")),
            (IFoo, 'myfoo-2', Stub()),
            (IBar, '', Stub()),
            (IBar, 'mybar-1', Stub("3"))
            ]


class UtilitiesView(Utilities, BrowserView):
    """Adding BrowserView to Utilities; this is usually done by ZCML."""


class RegisteredTest(placelesssetup.PlacelessSetup, unittest.TestCase):

    def test_utility(self):
        ztapi.browserView(IStub,
                          "absolute_url",
                          StubAbsoluteURL)
        utilityservice = StubLocalUtilityService()
        request = TestRequest()
        utilities = UtilitiesView(utilityservice, request)
        ifname1 = __name__ + ".IFoo"
        ifname2 = __name__ + ".IBar"
        def confurl(ifname, name):
            return ("@@configureutility.html?interface=%s&name=%s"
                    % (ifname, name))
        expected = [{"interface": ifname2,
                     "name": "",
                     "url": "",
                     "summary": "",
                     "configurl": confurl(ifname2, '')},
                    {"interface": ifname2,
                     "name": "mybar-1",
                     "url": "3",
                     "summary": "",
                     "configurl": confurl(ifname2, 'mybar-1')},
                    {"interface": ifname1,
                     "name": "",
                     "url": "1",
                     "summary": "",
                     "configurl": confurl(ifname1, '')},
                    {"interface": ifname1,
                     "name": "myfoo-1",
                     "url": "2",
                     "summary": "",
                     "configurl": confurl(ifname1, 'myfoo-1')},
                    {"interface": ifname1,
                     "name": "myfoo-2",
                     "url": "",
                     "summary": "",
                     "configurl": confurl(ifname1, 'myfoo-2')},
                    ]
        result = utilities.getConfigs()
        self.assertEqual(len(expected), len(result))
        for r, e in zip(result, expected):
            ri = r.items()
            ri.sort()
            ei = e.items()
            ei.sort()
            self.assertEqual(ri, ei)


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




More information about the Zope3-Checkins mailing list