[Zope3-checkins] CVS: Zope3/src/zope/app/introspector/tests - __init__.py:1.1 test_introspector.py:1.1 test_introspectorview.py:1.1

Philipp von Weitershausen philikon at philikon.de
Mon Mar 1 05:18:22 EST 2004


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

Added Files:
	__init__.py test_introspector.py test_introspectorview.py 
Log Message:
Moved all introspector code, including interfaces and browser views, to
the zope.app.introspector package.


=== Added File Zope3/src/zope/app/introspector/tests/__init__.py ===
# make this directory a package


=== Added File Zope3/src/zope/app/introspector/tests/test_introspector.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""
$Id: test_introspector.py,v 1.1 2004/03/01 10:18:21 philikon Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite

from zope.interface import Interface, Attribute, implements, directlyProvides
from zope.interface.verify import verifyObject
from zope.component.service import serviceManager, defineService
from zope.testing.cleanup import CleanUp

from zope.app.process.bootstrap import addConfigureService
from zope.app.services.servicenames import Interfaces
from zope.app.services.interface import LocalInterfaceService
from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.component.globalinterfaceservice import provideInterface
from zope.app.component.globalinterfaceservice import InterfaceService
from zope.app.interfaces.component import IInterfaceService

from zope.app.introspector import Introspector
from zope.app.introspector.interfaces import IIntrospector

class ITestClass(Interface):
    def drool():
        """...drool..."""

class BaseTestClass:
    """This is stupid base class"""
    pass

class TestClass(BaseTestClass):
    """This is my stupid doc string"""
    implements(ITestClass)
    def drool(self):
        pass

class I(Interface):
    """bah blah"""

class I2(I):
    """eek"""

class I3(I, I2):
    """This is dummy doc string"""

    testAttribute1 = Attribute("""This is a dummy attribute.""")
    testAttribute2 = Attribute("""This is a dummy attribute.""")

    def one(param):
        """method one"""

    def two(param1, param2):
        """method two"""

class WeirdClass:
    def namesAndDescriptions(self):
        return "indeed"

class TestIntrospector(CleanUp, TestCase):
    """Test Introspector."""

    def setUp(self):
        service = InterfaceService()
        defineService(Interfaces, IInterfaceService)
        serviceManager.provideService(Interfaces, service)
        provideInterface = service.provideInterface
        provideInterface('zope.app.introspector.tests.test_introspector.I', I)
        provideInterface('zope.app.introspector.tests.test_introspector.I2', I2)
        provideInterface('zope.app.introspector.tests.test_introspector.I3', I3)
        provideInterface('zope.app.introspector.tests.test_introspector.I4', I4)
        provideInterface('zope.app.introspector.tests.test_introspector.M1', M1)
        provideInterface('zope.app.introspector.tests.test_introspector.M2', M2)
        provideInterface('zope.app.introspector.tests.test_introspector.M3', M3)
        provideInterface('zope.app.introspector.tests.test_introspector.M4', M4)
        provideInterface('zope.app.introspector.tests.test_introspector.ITestClass',
                         ITestClass)

    def test_isInterface(self):
        ints = Introspector(ITestClass)
        self.assertEqual(ints.isInterface(), 1)

        ints = Introspector(TestClass())
        self.assertEqual(ints.isInterface(), 0)

        ints = Introspector(WeirdClass())
        self.assertEqual(ints.isInterface(), 0)

        verifyObject(IIntrospector, ints)

    def test_getClass(self):
        ints = Introspector(TestClass())
        request = {}
        ints.setRequest(request)
        self.assertEqual(ints.getClass(), 'TestClass')

    def testIntrospectorOnClass(self):
        request = {}
        ints = Introspector(TestClass)
        self.assertEqual(ints.isInterface(), 0)
        request['PATH_INFO'] = (
            '++module++zope.app.introspector.tests.test_introspector.TestClass')
        ints.setRequest(request)
        self.assertEqual(ints.getClass(), 'TestClass')

        self.assertEqual(
            ints.getBaseClassNames(),
            ['zope.app.introspector.tests.test_introspector.BaseTestClass'])
        self.assertEqual(
            ints.getModule(),
            'zope.app.introspector.tests.test_introspector')
        self.assertEqual(ints.getDocString(), "This is my stupid doc string")
        self.assertEqual(ints.getInterfaces(), (ITestClass,))
        self.assertEqual(
            ints.getInterfaceNames(),
            ['zope.app.introspector.tests.test_introspector.ITestClass'])
        self.assertEqual(ints.getExtends(), (BaseTestClass,))

    def testIntrospectorOnInterface(self):
        request = {}
        ints = Introspector(I3)
        self.assertEqual(ints.isInterface(), 1)
        request['PATH_INFO'] = (
            '++module++zope.app.introspector.tests.test_introspector.I3')
        ints.setRequest(request)
        self.assertEqual(
            ints.getModule(),
            'zope.app.introspector.tests.test_introspector')
        self.assertEqual(ints.getExtends(), (I, I2, ))
        self.assertEqual(
            ints.getDocString(),
            "This is dummy doc string")
        Iname = 'I3'
        bases = ['zope.app.introspector.tests.test_introspector.I',
                 'zope.app.introspector.tests.test_introspector.I2']
        desc = 'This is dummy doc string'
        m1_name = 'one'
        m1_signature = '(param)'
        m1_desc = 'method one'
        m2_name = 'two'
        m2_signature = '(param1, param2)'
        m2_desc = 'method two'
        methods = [(m1_name, m1_signature, m1_desc),
                   (m2_name, m2_signature, m2_desc),]
        attr_name1 = 'testAttribute1'
        attr_desc1 = 'This is a dummy attribute.'
        attr_name2 = 'testAttribute2'
        attr_desc2 = 'This is a dummy attribute.'
        attributes = [(attr_name1, attr_desc1),
                      (attr_name2, attr_desc2), ]
        details = [Iname, bases, desc, methods, attributes]
        self.assertEqual(ints.getInterfaceDetails(), details)

    def test_getDirectlyProvided(self):
        ob = TestClass()
        ints = Introspector(ob)
        self.assertEqual(tuple(ints.getDirectlyProvided()), ())
        directlyProvides(ob, I, I2)
        ints = Introspector(ob)
        self.assertEqual(tuple(ints.getDirectlyProvided()), (I, I2))

    def test_getDirectlyProvidedNames(self):
        ob = TestClass()
        ints = Introspector(ob)
        self.assertEqual(tuple(ints.getDirectlyProvidedNames()), ())
        directlyProvides(ob, I, I2)
        ints = Introspector(ob)
        self.assertEqual(tuple(ints.getDirectlyProvidedNames()),
                         ('zope.app.introspector.tests.test_introspector.I',
                          'zope.app.introspector.tests.test_introspector.I2'))


class I4(I3):
    foo = Attribute("Not a marker")

class M1(Interface): pass

class M2(I2): pass

class M3(I3): pass

class M4(I4): pass

class Content:
    implements(I3)

    def one(self, a): pass
    def two(self, a, b): pass

class TestMarkerInterfaces(PlacefulSetup, TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        self.createStandardServices()
        addConfigureService(self.rootFolder, Interfaces, LocalInterfaceService)
        provideInterface('zope.app.introspector.tests.test_introspector.I', I)
        provideInterface('zope.app.introspector.tests.test_introspector.I2', I2)
        provideInterface('zope.app.introspector.tests.test_introspector.I3', I3)
        provideInterface('zope.app.introspector.tests.test_introspector.I4', I4)
        provideInterface('zope.app.introspector.tests.test_introspector.M1', M1)
        provideInterface('zope.app.introspector.tests.test_introspector.M2', M2)
        provideInterface('zope.app.introspector.tests.test_introspector.M3', M3)
        provideInterface('zope.app.introspector.tests.test_introspector.M4', M4)

    def test_getMarkerInterfaces(self):
        ints = Introspector(Content())
        expected = [M1, M2, M3]
        expected.sort()
        self.assertEqual(ints.getMarkerInterfaces(), tuple(expected))

    def test_getMarkerInterfaceNames(self):
        ints = Introspector(Content())
        expected = ['zope.app.introspector.tests.test_introspector.M1',
                    'zope.app.introspector.tests.test_introspector.M2',
                    'zope.app.introspector.tests.test_introspector.M3']
        expected.sort()
        self.assertEqual(ints.getMarkerInterfaceNames(), tuple(expected))


    def test_getDirectMarkers(self):
        ints = Introspector(Content())
        self.assertEqual(ints.getDirectMarkersOf(I3), (M3,))

def test_suite():
    suite = TestSuite()
    suite.addTest(makeSuite(TestIntrospector))
    suite.addTest(makeSuite(TestMarkerInterfaces))
    return suite


if __name__ == '__main__':
    main(defaultTest='test_suite')


=== Added File Zope3/src/zope/app/introspector/tests/test_introspectorview.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.
#
##############################################################################
"""Introspector view tests

$Id: test_introspectorview.py,v 1.1 2004/03/01 10:18:21 philikon Exp $
"""
import unittest

from zope.interface import Interface, directlyProvidedBy
from zope.interface import directlyProvides, implements
from zope.publisher.browser import TestRequest

from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.services.interface import LocalInterfaceService
from zope.app.services.servicenames import Interfaces
from zope.app.tests import setup
from zope.app.component.globalinterfaceservice import provideInterface
from zope.app.tests import ztapi

from zope.app.introspector import Introspector
from zope.app.introspector.browser import IntrospectorView
from zope.app.introspector.interfaces import IIntrospector


class I1(Interface):
    pass

id = 'zope.app.introspector.tests.test_introspector.I1'

class I2(Interface):
    pass

id2 = 'zope.app.introspector.tests.test_introspector.I2'

class TestIntrospectorView(PlacefulSetup, unittest.TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        self.rootFolder = setup.buildSampleFolderTree()
        mgr = setup.createServiceManager(self.rootFolder)
        service = setup.addService(mgr, Interfaces, LocalInterfaceService())

        provideInterface(id, I1)
        provideInterface(id2, I2)
        ztapi.provideAdapter(None, IIntrospector, Introspector)


    def test_getInterfaceURL(self):
        request = TestRequest()
        view = IntrospectorView(self.rootFolder, request)

        self.assertEqual(
            view.getInterfaceURL(id),
            'http://127.0.0.1/++etc++site/default/Interfaces/detail.html?id=%s'
            % id)

        self.assertEqual(view.getInterfaceURL('zope.app.INonexistent'),
                         '')

    def test_update(self):
        class Context:
            implements(Interface)

        context = Context()
        request = TestRequest()
        request.form['ADD']= ''
        request.form['add_%s' % id] = 'on'
        request.form['add_%s' % id2] = 'on'
        view = IntrospectorView(context, request)
        view.update()
        self.assert_(I1 in directlyProvidedBy(context))
        self.assert_(I2 in directlyProvidedBy(context))

        context = Context()
        directlyProvides(context, I1)
        request = TestRequest()
        request.form['REMOVE']= ''
        request.form['rem_%s' % id] = 'on'
        view = IntrospectorView(context, request)
        view.update()
        self.assertEqual(tuple(directlyProvidedBy(context)), ())


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestIntrospectorView))
    return suite


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




More information about the Zope3-Checkins mailing list