[Zope3-checkins] CVS: Zope3/src/zope/interface/tests - __init__.py:1.1.2.1 dummy.py:1.1.2.1 ifoo.py:1.1.2.1 test_adapter.py:1.1.2.1 test_document.py:1.1.2.1 test_iadapter.py:1.1.2.1 test_iimplementor.py:1.1.2.1 test_implementor.py:1.1.2.1 test_implements.py:1.1.2.1 test_interface.py:1.1.2.1 test_sorting.py:1.1.2.1 test_type.py:1.1.2.1 test_verify.py:1.1.2.1 test_visitimplements.py:1.1.2.1 unitfixtures.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:32:59 -0500


Update of /cvs-repository/Zope3/src/zope/interface/tests
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/interface/tests

Added Files:
      Tag: NameGeddon-branch
	__init__.py dummy.py ifoo.py test_adapter.py test_document.py 
	test_iadapter.py test_iimplementor.py test_implementor.py 
	test_implements.py test_interface.py test_sorting.py 
	test_type.py test_verify.py test_visitimplements.py 
	unitfixtures.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/interface/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/interface/tests/dummy.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.
# 
##############################################################################
from zope.interface.tests.ifoo import IFoo

__implements__ = IFoo

def bar( baz ):
    pass


=== Added File Zope3/src/zope/interface/tests/ifoo.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.
# 
##############################################################################
from zope.interface import Interface

class IFoo( Interface ):
    """
        Dummy interface for unit tests.
    """

    def bar( baz ):
        """
            Just a note.
        """


=== Added File Zope3/src/zope/interface/tests/test_adapter.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.
#
##############################################################################
"""

Revision information:
$Id: test_adapter.py,v 1.1.2.1 2002/12/23 19:32:57 jim Exp $
"""

from unittest import TestCase, main, makeSuite
from zope.interface.tests.test_iadapter import TestIAdapterRegistry

class Test(TestIAdapterRegistry, TestCase):

    def _new(self):
        from zope.interface.adapter import AdapterRegistry
        return AdapterRegistry()

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/interface/tests/test_document.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.
# 
##############################################################################
"""

Revision information:
$Id: test_document.py,v 1.1.2.1 2002/12/23 19:32:57 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.interface import Interface
from zope.interface.element import Attribute

class Test(TestCase):

    def testBlech(self):
        from zope.interface.document import asStructuredText

        self.assertEqual(asStructuredText(I2), '''\
I2

 I2 doc

 This interface extends:

  o _I1

 Attributes:

  a1 -- no documentation

  a2 -- a2 doc

 Methods:

  f21() -- f21 doc

  f22() -- no documentation

  f23() -- f23 doc

''')
  

def test_suite():
    return makeSuite(Test)

class _I1(Interface):
    def f11(): pass
    def f12(): pass

class I2(_I1):
    "I2 doc"

    a1 = Attribute('a1')
    a2 = Attribute('a2', 'a2 doc')
    
    def f21(): "f21 doc"
    def f22(): pass
    def f23(): "f23 doc"

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


=== Added File Zope3/src/zope/interface/tests/test_iadapter.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.
#
##############################################################################
"""

Revision information:
$Id: test_iadapter.py,v 1.1.2.1 2002/12/23 19:32:57 jim Exp $
"""

import unittest

from zope.interface import Interface

class R1(Interface): pass
class R12(Interface): pass
class R2(R1): pass
class R3(R2): pass
class R4(R3): pass

class P1(Interface): pass
class P2(P1): pass
class P3(P2): pass
class P4(P3): pass

class TestIAdapterRegistry(unittest.TestCase):

    def _new(self):
        # subclass should override to return registry to test
        pass

    def testImplementsIAdapterRegistry(self):
        from zope.interface.verify import verifyObject
        from zope.interface.interfaces import IAdapterRegistry
        
        registry = self._new()

        verifyObject(IAdapterRegistry, registry)

    def __registery(self):        
        registry = self._new()

        registry.register(None, P3, 'default P3')
        registry.register(Interface, P3, 'any P3')
        registry.register(R2, P3, 'R2 P3')

        return registry

    def testBadRequire(self):
        registry = self._new()
        self.assertRaises(TypeError, registry.register, 42, P3, '')

    def testBadProvide(self):
        registry = self._new()
        self.assertRaises(TypeError, registry.register, R2, None, '')
        

    def test_get(self):
        registry = self.__registery()

        for R in [R2, R3, R4, (R12, R2), (R12, R4)]:
            for P in [P1, P2, P3]:
                self.assertEqual(registry.get((R, P)), 'R2 P3')

        for R in [None, R1, R2, R3, R4, (R12, R2), (R12, R4)]:
            self.assertEqual(registry.get((R, P4)), None)

        for P in [P1, P2, P3]:
            self.assertEqual(registry.get((R1, P)), 'any P3')

        for P in [P1, P2, P3]:
            self.assertEqual(registry.get((None, P)), 'default P3')

    def test_getForObject(self):
        registry = self.__registery()

        class C: pass
        c = C()

        for R in [R2, R3, R4, (R12, R2), (R12, R4)]:
            c.__implements__ = R
            for P in [P1, P2, P3]:
                self.assertEqual(registry.getForObject(c, P), 'R2 P3')

        for R in [None, R1, R2, R3, R4, (R12, R2), (R12, R4)]:
            c.__implements__ = R
            self.assertEqual(registry.getForObject(c, None), None)

        c.__implements__ = R1
        for P in [P1, P2, P3]:
            self.assertEqual(registry.getForObject(c, P), 'any P3')

        c = C()
        for P in [P1, P2, P3]:
            self.assertEqual(registry.getForObject(c, P), 'default P3')

    def test_get_w_filter(self):
        registry = self.__registery()

        for R in [R2, R3, R4, (R12, R2), (R12, R4)]:
            for P in [P1, P2, P3]:
                self.assertEqual(
                    registry.get((R, P), filter=lambda o: o.startswith('a')),
                    'any P3')
                self.assertEqual(
                    registry.get((R, P), filter=lambda o: o.startswith('d')),
                    'default P3')
                self.assertEqual(
                    registry.get((R, P), filter=lambda o: o.startswith('z')),
                    None)

    def test_getForObject_w_filter(self):
        registry = self.__registery()

        class C: pass
        c = C()

        for R in [R2, R3, R4, (R12, R2), (R12, R4)]:
            c.__implements__ = R
            for P in [P1, P2, P3]:
                self.assertEqual(
                    registry.getForObject(c, P,
                                          filter=lambda o: o.startswith('a')),
                    'any P3')
                self.assertEqual(
                    registry.getForObject(c, P,
                                          filter=lambda o: o.startswith('d')),
                    'default P3')
                self.assertEqual(
                    registry.getForObject(c, P,
                                          filter=lambda o: o.startswith('z')),
                    None)

    def test_getRegistered(self):
        registry = self.__registery()
         
        # Get something that was registered directly
        self.assertEqual(registry.getRegistered(R2, P3), 'R2 P3')
        self.assertEqual(registry.getRegistered(Interface, P3), 'any P3')
        self.assertEqual(registry.getRegistered(None, P3), 'default P3')
         
        # this mustn't return anything that was not registered directly
        self.assertEqual(registry.getRegistered(R3, P3), None)
        self.assertEqual(registry.getRegistered(R2, P2), None)

    def test_getRegisteredMatching_all(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching())
        got.sort()
        expect = [
            (None, P3, 'default P3'),
            (Interface, P3, 'any P3'),
            (R2, P3, 'R2 P3'),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_required_R1(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            required_interfaces = (R1, )
            ))
        got.sort()
        expect = [
            (None, P3, 'default P3'),
            (Interface, P3, 'any P3'),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_required_multiple(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            required_interfaces = (R12, R2)
            ))
        got.sort()
        expect = [
            (None, P3, 'default P3'),
            (Interface, P3, 'any P3'),
            (R2, P3, 'R2 P3'),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_provided_P1(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            provided_interfaces = (P1, )
            ))
        got.sort()
        expect = [
            (None, P3, 'default P3'),
            (Interface, P3, 'any P3'),
            (R2, P3, 'R2 P3'),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_provided_P2(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            provided_interfaces = (P3, )
            ))
        got.sort()
        expect = [
            (None, P3, 'default P3'),
            (Interface, P3, 'any P3'),
            (R2, P3, 'R2 P3'),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_required_and_provided_1(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            required_interfaces = (R4, R12),
            provided_interfaces = (P1, ),
            ))
        got.sort()
        expect = [
            (None, P3, 'default P3'),
            (Interface, P3, 'any P3'),
            (R2, P3, 'R2 P3'),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_required_and_provided_2(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            required_interfaces = (R4, R12),
            provided_interfaces = (P3, ),
            ))
        got.sort()
        expect = [
            (None, P3, 'default P3'),
            (Interface, P3, 'any P3'),
            (R2, P3, 'R2 P3'),
            ]
        expect.sort()
        self.assertEqual(got, expect)
        

    def test_getRegisteredMatching_required_and_provided_exact(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            required_interfaces = (R2, ),
            provided_interfaces = (P3, ),
            ))
        got.sort()
        expect = [
            (None, P3, 'default P3'),
            (Interface, P3, 'any P3'),
            (R2, P3, 'R2 P3'),
            ]
        expect.sort()
        self.assertEqual(got, expect)
        
        
        



=== Added File Zope3/src/zope/interface/tests/test_iimplementor.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.
#
##############################################################################
"""

Revision information:
$Id: test_iimplementor.py,v 1.1.2.1 2002/12/23 19:32:57 jim Exp $
"""

import unittest
from zope.interface import Interface

class R1(Interface): pass
class R12(Interface): pass
class R2(R1): pass
class R3(R2): pass
class R4(R3): pass

class P1(Interface): pass
class P2(P1): pass
class P3(P2): pass
class P4(P3): pass

class TestIImplementorRegistry(unittest.TestCase):

    def _new(self):
        # subclass must define method to return registry
        raise NotImplementedError

    def testImplementsIImplementorRegistry(self):
        from zope.interface.verify import verifyObject
        from zope.interface.interfaces \
             import IImplementorRegistry
        
        registry = self._new()
        verifyObject(IImplementorRegistry, registry)

    def __registry(self):        
        registry = self._new()
        registry.register(P3, 'C3')
        return registry

    def test_get(self):
        registry = self.__registry()

        for P in [P1, P2, P3]:
            self.assertEqual(registry.get(P), 'C3')

        self.assertEqual(registry.get(P4), None)

        registry.register(P1, 'C1')
        registry.register(P2, 'C3')
        
    def testBadProvide(self):
        registry = self.__registry()
        self.assertRaises(TypeError, registry.register, None, '')


=== Added File Zope3/src/zope/interface/tests/test_implementor.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.
#
##############################################################################
"""

Revision information:
$Id: test_implementor.py,v 1.1.2.1 2002/12/23 19:32:57 jim Exp $
"""

from unittest import TestCase, main, makeSuite
from zope.interface.tests.test_iimplementor import TestIImplementorRegistry

class Test(TestIImplementorRegistry, TestCase):

    def _new(self):
        from zope.interface.implementor import ImplementorRegistry
        return ImplementorRegistry()

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/interface/tests/test_implements.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.
# 
##############################################################################

from zope.interface import Interface
from zope.interface.implements import implements
from zope.interface.interfaces import DoesNotImplement, BrokenImplementation
from zope.interface.interfaces import BrokenMethodImplementation

import unittest, sys

class Test(unittest.TestCase):

    def testSimple(self):

        class I(Interface):
            def f(): pass

        class C: pass

        self.assertRaises(BrokenImplementation, implements, C, I)

        C.f=lambda self: None

        implements(C, I)

        self.assertEqual(C.__implements__, I)

    def testAdd(self):

        class I(Interface):
            def f(): pass

        class I2(Interface):
            def g(): pass

        class C:
            __implements__=I2

        self.assertRaises(BrokenImplementation, implements, C, I)
        self.assertRaises(BrokenImplementation, implements, C, I2)

        C.f=lambda self: None

        implements(C, I)

        self.assertEqual(C.__implements__, (I2, I))
        self.assertRaises(BrokenImplementation, implements, C, I2)

        C.g=C.f
        implements(C, I)
        implements(C, I2)


def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/interface/tests/test_interface.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.
#
##############################################################################

import unittest
import zope.interface
from zope.interface.tests.unitfixtures import *  # hehehe
from zope.interface.interfaces import BrokenImplementation
from zope.interface.implements import instancesOfObjectImplements
from zope.interface.implements import objectImplements
from zope.interface import Interface
from zope.interface.element import Attribute

class InterfaceTests(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testClassImplements(self):
        assert IC.isImplementedByInstancesOf(C)

        assert I1.isImplementedByInstancesOf(A)
        assert I1.isImplementedByInstancesOf(B)
        assert not I1.isImplementedByInstancesOf(C)
        assert I1.isImplementedByInstancesOf(D)
        assert I1.isImplementedByInstancesOf(E)

        assert not I2.isImplementedByInstancesOf(A)
        assert I2.isImplementedByInstancesOf(B)
        assert not I2.isImplementedByInstancesOf(C)
        assert not I2.isImplementedByInstancesOf(D)
        assert not I2.isImplementedByInstancesOf(E)

    def testUtil(self):
        f = instancesOfObjectImplements
        assert IC in f(C)
        assert I1 in f(A)
        assert not I1 in f(C)
        assert I2 in f(B)
        assert not I2 in f(C)

        f = objectImplements
        assert IC in f(C())
        assert I1 in f(A())
        assert not I1 in f(C())
        assert I2 in f(B())
        assert not I2 in f(C())


    def testObjectImplements(self):
        assert IC.isImplementedBy(C())

        assert I1.isImplementedBy(A())
        assert I1.isImplementedBy(B())
        assert not I1.isImplementedBy(C())
        assert I1.isImplementedBy(D())
        assert I1.isImplementedBy(E())

        assert not I2.isImplementedBy(A())
        assert I2.isImplementedBy(B())
        assert not I2.isImplementedBy(C())
        assert not I2.isImplementedBy(D())
        assert not I2.isImplementedBy(E())

    def testDeferredClass(self):
        a = A()
        self.assertRaises(BrokenImplementation, a.ma)


    def testInterfaceExtendsInterface(self):
        assert BazInterface.extends(BobInterface)
        assert BazInterface.extends(BarInterface)
        assert BazInterface.extends(FunInterface)
        assert not BobInterface.extends(FunInterface)
        assert not BobInterface.extends(BarInterface)
        assert BarInterface.extends(FunInterface)
        assert not BarInterface.extends(BazInterface)

    def testVerifyImplementation(self):
        from zope.interface.verify import verifyClass
        assert verifyClass(FooInterface, Foo)
        assert Interface.isImplementedBy(I1)

    def test_names(self):
        names = list(_I2.names()); names.sort()
        self.assertEqual(names, ['f21', 'f22', 'f23'])
        names = list(_I2.names(1)); names.sort()
        self.assertEqual(names, ['a1', 'f11', 'f12', 'f21', 'f22', 'f23'])

    def test_namesAndDescriptions(self):
        names = [nd[0] for nd in _I2.namesAndDescriptions()]; names.sort()
        self.assertEqual(names, ['f21', 'f22', 'f23'])
        names = [nd[0] for nd in _I2.namesAndDescriptions(1)]; names.sort()
        self.assertEqual(names, ['a1', 'f11', 'f12', 'f21', 'f22', 'f23'])

        for name, d in _I2.namesAndDescriptions(1):
            self.assertEqual(name, d.__name__)

    def test_getDescriptionFor(self):
        self.assertEqual(_I2.getDescriptionFor('f11').__name__, 'f11')
        self.assertEqual(_I2.getDescriptionFor('f22').__name__, 'f22')
        self.assertEqual(_I2.queryDescriptionFor('f33', self), self)
        self.assertRaises(KeyError, _I2.getDescriptionFor, 'f33')

    def test___getitem__(self):
        self.assertEqual(_I2['f11'].__name__, 'f11')
        self.assertEqual(_I2['f22'].__name__, 'f22')
        self.assertEqual(_I2.get('f33', self), self)
        self.assertRaises(KeyError, _I2.__getitem__, 'f33')

    def test___contains__(self):
        self.failUnless('f11' in _I2)
        self.failIf('f33' in _I2)

    def test___iter__(self):
        names = list(iter(_I2))
        names.sort()
        self.assertEqual(names, ['a1', 'f11', 'f12', 'f21', 'f22', 'f23'])

    def testAttr(self):
        description = _I2.getDescriptionFor('a1')
        self.assertEqual(description.__name__, 'a1')
        self.assertEqual(description.__doc__, 'This is an attribute')


    def testFunctionAttributes(self):
        # Make sure function attributes become tagged values.
        meth = _I1['f12']
        self.assertEqual(meth.getTaggedValue('optional'), 1)

class _I1(Interface):

    a1 = Attribute("This is an attribute")

    def f11(): pass
    def f12(): pass
    f12.optional = 1

class _I1_(_I1): pass
class _I1__(_I1_): pass

class _I2(_I1__):
    def f21(): pass
    def f22(): pass
    f23 = f22


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

def main():
    unittest.TextTestRunner().run(test_suite())

if __name__=="__main__":
    main()



=== Added File Zope3/src/zope/interface/tests/test_sorting.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.
#
##############################################################################
"""Test interface sorting

$Id: test_sorting.py,v 1.1.2.1 2002/12/23 19:32:57 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite

from zope.interface import Interface

class I1(Interface): pass
class I2(I1): pass
class I3(I1): pass
class I4(Interface): pass
class I5(I4): pass
class I6(I2): pass


class Test(TestCase):

    def test(self):
        l = [I1, I3, I5, I6, I4, I2]
        l.sort()
        self.assertEqual(l, [I1, I2, I3, I4, I5, I6])

    def test_w_None(self):
        l = [I1, None, I3, I5, None, I6, I4, I2]
        l.sort()
        self.assertEqual(l, [I1, I2, I3, I4, I5, I6, None, None])

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

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


=== Added File Zope3/src/zope/interface/tests/test_type.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.
# 
##############################################################################
"""

Revision information:
$Id: test_type.py,v 1.1.2.1 2002/12/23 19:32:57 jim Exp $
"""

import unittest, sys
from zope.interface.type import TypeRegistry
from zope.interface import Interface

def getAllForObject(reg, ob):
    all = list(reg.getAllForObject(ob))
    all.sort()
    return all

class Test(unittest.TestCase):

    def test(self):
        class I1(Interface): pass
        class I2(I1): pass
        class I3(I2): pass

        reg = TypeRegistry()
        reg.register(I2, 2)

        class C1: __implements__ = I1
        class C2: __implements__ = I2
        class C3: __implements__ = I3
        class C: pass

        self.assertEqual(getAllForObject(reg, C1()), [])
        self.assertEqual(getAllForObject(reg, C2()), [2])
        self.assertEqual(getAllForObject(reg, C3()), [2])
        self.assertEqual(getAllForObject(reg,  C()), [])

        self.assertEqual(reg.get(I1), None)
        self.assertEqual(reg.get(I2), 2)
        self.assertEqual(reg.get(I3), None)

        reg.register(I1, 1)

        self.assertEqual(getAllForObject(reg, C1()), [1])
        self.assertEqual(getAllForObject(reg, C2()), [1, 2])
        self.assertEqual(getAllForObject(reg, C3()), [1, 2])
        self.assertEqual(getAllForObject(reg,  C()), [])

        self.assertEqual(reg.get(I1), 1)
        self.assertEqual(reg.get(I2), 2)
        self.assertEqual(reg.get(I3), None)

        reg.register(I3, 3)

        self.assertEqual(getAllForObject(reg, C1()), [1])
        self.assertEqual(getAllForObject(reg, C2()), [1, 2])
        self.assertEqual(getAllForObject(reg, C3()), [1, 2, 3])
        self.assertEqual(getAllForObject(reg,  C()), [])

        self.assertEqual(reg.get(I1), 1)
        self.assertEqual(reg.get(I2), 2)
        self.assertEqual(reg.get(I3), 3)

    def testSetdefault(self):
        class I(Interface):
            pass
        reg = TypeRegistry()
        x = reg.setdefault(I, 1)
        y = reg.setdefault(I, 2)
        self.assertEqual(x, y)
        self.assertEqual(x, 1)

    def testDup(self):
        class I1(Interface): pass
        class I2(I1): pass
        class I3(I1): pass
        class I4(I2, I3): pass
        class C1: __implements__ = I1
        class C2: __implements__ = I2
        class C3: __implements__ = I3
        class C4: __implements__ = I4
        class C5: __implements__ = I1, I2, I3, I4
        class C: pass

        reg = TypeRegistry()
        reg.register(I1, 1)
        reg.register(I2, 2)
        reg.register(I3, 3)

        self.assertEqual(getAllForObject(reg, C1()), [1])
        self.assertEqual(getAllForObject(reg, C2()), [1, 2])
        self.assertEqual(getAllForObject(reg, C3()), [1, 3])
        self.assertEqual(getAllForObject(reg, C4()), [1, 2, 3])
        self.assertEqual(getAllForObject(reg, C5()), [1, 2, 3])
        self.assertEqual(getAllForObject(reg,  C()), [])

    def testBadRequire(self):
        registry = TypeRegistry()
        self.assertRaises(TypeError, registry.register, 42, '')

def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/interface/tests/test_verify.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.
# 
##############################################################################
"""

Revision information:
$Id: test_verify.py,v 1.1.2.1 2002/12/23 19:32:57 jim Exp $
"""


from zope.interface import Interface
from zope.interface.verify import verifyClass, verifyObject
from zope.interface.interfaces import DoesNotImplement, BrokenImplementation
from zope.interface.interfaces import BrokenMethodImplementation

import unittest, sys

class Test(unittest.TestCase):

    def testNotImplemented(self):

        class C: pass

        class I(Interface): pass

        self.assertRaises(DoesNotImplement, verifyClass, I, C)

        C.__implements__=I

        verifyClass(I, C)

    def testMissingAttr(self):

        class I(Interface):
            def f(): pass

        class C: 

            __implements__=I

        self.assertRaises(BrokenImplementation, verifyClass, I, C)

        C.f=lambda self: None

        verifyClass(I, C)

    def testMissingAttr_with_Extended_Interface(self):

        class II(Interface):
            def f():
                pass

        class I(II):
            pass

        class C: 

            __implements__=I

        self.assertRaises(BrokenImplementation, verifyClass, I, C)

        C.f=lambda self: None

        verifyClass(I, C)

    def testWrongArgs(self):

        class I(Interface):
            def f(a): pass

        class C:

            def f(self, b): pass 

            __implements__=I

        # We no longer require names to match.
        #self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a: None

        verifyClass(I, C)

        C.f=lambda self, **kw: None

        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a, *args: None

        verifyClass(I, C)

        C.f=lambda self, a, *args, **kw: None

        verifyClass(I, C)

        C.f=lambda self, *args: None

        verifyClass(I, C)

    def testExtraArgs(self):

        class I(Interface):
            def f(a): pass

        class C:

            def f(self, a, b): pass 

            __implements__=I

        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a: None

        verifyClass(I, C)

        C.f=lambda self, a, b=None: None

        verifyClass(I, C)

    def testNoVar(self):

        class I(Interface):
            def f(a, *args): pass

        class C:

            def f(self, a): pass 

            __implements__=I

        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a, *foo: None

        verifyClass(I, C)

    def testNoKW(self):

        class I(Interface):
            def f(a, **args): pass

        class C:

            def f(self, a): pass 

            __implements__=I

        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a, **foo: None

        verifyClass(I, C)

    def testModule(self):

        from zope.interface.tests.ifoo import IFoo
        from zope.interface.tests import dummy

        verifyObject(IFoo, dummy)
        


def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/interface/tests/test_visitimplements.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.
# 
##############################################################################
import unittest, sys

from zope.interface.implements import visitImplements
from zope.interface import Interface
from zope.interface.interfaces import BadImplements

class I1(Interface): pass
class I2(Interface): pass
class I3(Interface): pass

class Test(unittest.TestCase):

    def testSimpleImplements(self):
        data=[]
        visitImplements(I1, None, data.append)
        self.assertEqual(data, [I1])

    def testSimpleBadImplements(self):
        data=[]
        self.assertRaises(BadImplements,
                          visitImplements, unittest, None, data.append)
        
    def testComplexImplements(self):
        data=[]
        visitImplements((I1, (I2, I3)), None, data.append)
        data = map(lambda i: i.__name__, data)
        self.assertEqual(data, ['I1', 'I2', 'I3'])

    def testComplexBadImplements(self):
        data=[]
        self.assertRaises(BadImplements,
                          visitImplements, (I1, (I2, unittest)),
                          None, data.append)


def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/interface/tests/unitfixtures.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.
# 
##############################################################################
from zope.interface import Interface
from zope.interface.element import Attribute

class mytest(Interface):
    pass

class C:
    def m1(self, a, b):
        "return 1"
        return 1

    def m2(self, a, b):
        "return 2"
        return 2

# testInstancesOfClassImplements




#  YAGNI IC=Interface.impliedInterface(C)
class IC(Interface):
    def m1(a, b):
        "return 1"

    def m2(a, b):
        "return 2"
    


C.__implements__=IC

class I1(Interface):
    def ma():
        "blah"

class I2(I1): pass

class I3(Interface): pass

class I4(Interface): pass

class A(I1.deferred()):
    __implements__=I1

class B:
    __implements__=I2, I3

class D(A, B): pass

class E(A, B):
    __implements__ = A.__implements__, C.__implements__


class FooInterface(Interface):
    """ This is an Abstract Base Class """

    foobar = Attribute("fuzzed over beyond all recognition")

    def aMethod(foo, bar, bingo):
        """ This is aMethod """

    def anotherMethod(foo=6, bar="where you get sloshed", bingo=(1,3,)):
        """ This is anotherMethod """

    def wammy(zip, *argues):
        """ yadda yadda """

    def useless(**keywords):
        """ useless code is fun! """

class Foo:
    """ A concrete class """

    __implements__ = FooInterface,

    foobar = "yeah"

    def aMethod(self, foo, bar, bingo):
        """ This is aMethod """
        return "barf!"

    def anotherMethod(self, foo=6, bar="where you get sloshed", bingo=(1,3,)):
        """ This is anotherMethod """
        return "barf!"

    def wammy(self, zip, *argues):
        """ yadda yadda """
        return "barf!"

    def useless(self, **keywords):
        """ useless code is fun! """
        return "barf!"

foo_instance = Foo()

class Blah:
    pass

new = Interface.__class__
FunInterface = new('FunInterface')
BarInterface = new('BarInterface', [FunInterface])
BobInterface = new('BobInterface')
BazInterface = new('BazInterface', [BobInterface, BarInterface])