123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656 |
- ##############################################################################
- #
- # Copyright (c) 2001, 2002 Zope Foundation and Contributors.
- # All Rights Reserved.
- #
- # This software is subject to the provisions of the Zope Public License,
- # Version 2.1 (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.
- #
- ##############################################################################
- """ zope.interface.verify unit tests
- """
- import unittest
- # pylint:disable=inherit-non-class,no-method-argument,no-self-argument
- class Test_verifyClass(unittest.TestCase):
- verifier = None
- def setUp(self):
- self.verifier = self._get_FUT()
- @classmethod
- def _get_FUT(cls):
- from zope.interface.verify import verifyClass
- return verifyClass
- def _adjust_object_before_verify(self, x):
- return x
- def _callFUT(self, iface, klass, **kwargs):
- return self.verifier(iface,
- self._adjust_object_before_verify(klass),
- **kwargs)
- def test_class_doesnt_implement(self):
- from zope.interface import Interface
- from zope.interface.exceptions import DoesNotImplement
- class ICurrent(Interface):
- pass
- class Current:
- pass
- self.assertRaises(DoesNotImplement, self._callFUT, ICurrent, Current)
- def test_class_doesnt_implement_but_classImplements_later(self):
- from zope.interface import Interface
- from zope.interface import classImplements
- class ICurrent(Interface):
- pass
- class Current:
- pass
- classImplements(Current, ICurrent)
- self._callFUT(ICurrent, Current)
- def test_class_doesnt_have_required_method_simple(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenImplementation
- class ICurrent(Interface):
- def method():
- """docstring"""
- @implementer(ICurrent)
- class Current:
- pass
- self.assertRaises(BrokenImplementation,
- self._callFUT, ICurrent, Current)
- def test_class_has_required_method_simple(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method():
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_class_doesnt_have_required_method_derived(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenImplementation
- class IBase(Interface):
- def method():
- """docstring"""
- class IDerived(IBase):
- pass
- @implementer(IDerived)
- class Current:
- pass
- self.assertRaises(BrokenImplementation,
- self._callFUT, IDerived, Current)
- def test_class_has_required_method_derived(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class IBase(Interface):
- def method():
- """docstring"""
- class IDerived(IBase):
- pass
- @implementer(IDerived)
- class Current:
- def method(self):
- raise NotImplementedError()
- self._callFUT(IDerived, Current)
- def test_method_takes_wrong_arg_names_but_OK(self):
- # We no longer require names to match.
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, b):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_method_takes_not_enough_args(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self):
- raise NotImplementedError()
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_method_doesnt_take_required_starargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method(*args):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self):
- raise NotImplementedError()
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_method_doesnt_take_required_only_kwargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method(**kw):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self):
- raise NotImplementedError()
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_method_takes_extra_arg(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, a, b):
- raise NotImplementedError()
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_method_takes_extra_arg_with_default(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, a, b=None):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_method_takes_only_positional_args(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, *args):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_method_takes_only_kwargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, **kw):
- raise NotImplementedError()
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_method_takes_extra_starargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, a, *args):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_method_takes_extra_starargs_and_kwargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, a, *args, **kw):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_method_doesnt_take_required_positional_and_starargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method(a, *args):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, a):
- raise NotImplementedError()
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_method_takes_required_positional_and_starargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method(a, *args):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, a, *args):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_method_takes_only_starargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method(a, *args):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, *args):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_method_takes_required_kwargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- def method(**kwargs):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, **kw):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_method_takes_positional_plus_required_starargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method(*args):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, a, *args):
- raise NotImplementedError()
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_method_doesnt_take_required_kwargs(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method(**kwargs):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- def method(self, a):
- raise NotImplementedError()
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_class_has_method_for_iface_attr(self):
- from zope.interface import Attribute
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- attr = Attribute("The foo Attribute")
- @implementer(ICurrent)
- class Current:
- def attr(self):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_class_has_nonmethod_for_method(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenMethodImplementation
- class ICurrent(Interface):
- def method():
- """docstring"""
- @implementer(ICurrent)
- class Current:
- method = 1
- self.assertRaises(BrokenMethodImplementation,
- self._callFUT, ICurrent, Current)
- def test_class_has_attribute_for_attribute(self):
- from zope.interface import Attribute
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- attr = Attribute("The foo Attribute")
- @implementer(ICurrent)
- class Current:
- attr = 1
- self._callFUT(ICurrent, Current)
- def test_class_misses_attribute_for_attribute(self):
- # This check *passes* for verifyClass
- from zope.interface import Attribute
- from zope.interface import Interface
- from zope.interface import implementer
- class ICurrent(Interface):
- attr = Attribute("The foo Attribute")
- @implementer(ICurrent)
- class Current:
- pass
- self._callFUT(ICurrent, Current)
- def test_w_callable_non_func_method(self):
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.interface import Method
- class QuasiMethod(Method):
- def __call__(self, *args, **kw):
- raise NotImplementedError()
- class QuasiCallable:
- def __call__(self, *args, **kw):
- raise NotImplementedError()
- class ICurrent(Interface):
- attr = QuasiMethod('This is callable')
- @implementer(ICurrent)
- class Current:
- attr = QuasiCallable()
- self._callFUT(ICurrent, Current)
- def test_w_decorated_method(self):
- from zope.interface import Interface
- from zope.interface import implementer
- def decorator(func):
- # this is, in fact, zope.proxy.non_overridable
- return property(lambda self: func.__get__(self))
- class ICurrent(Interface):
- def method(a):
- """docstring"""
- @implementer(ICurrent)
- class Current:
- @decorator
- def method(self, a):
- raise NotImplementedError()
- self._callFUT(ICurrent, Current)
- def test_dict_IFullMapping(self):
- # A dict should be an IFullMapping, but this exposes two
- # issues. First, on CPython, methods of builtin types are
- # "method_descriptor" objects, and are harder to introspect.
- # Second, on PyPy, the signatures can be just plain wrong,
- # specifying as required arguments that are actually optional.
- # See https://github.com/zopefoundation/zope.interface/issues/118
- from zope.interface.common.mapping import IFullMapping
- self._callFUT(IFullMapping, dict, tentative=True)
- def test_list_ISequence(self):
- # As for test_dict_IFullMapping
- from zope.interface.common.sequence import ISequence
- self._callFUT(ISequence, list, tentative=True)
- def test_tuple_IReadSequence(self):
- # As for test_dict_IFullMapping
- from zope.interface.common.sequence import IReadSequence
- self._callFUT(IReadSequence, tuple, tentative=True)
- def test_multiple_invalid(self):
- from zope.interface import Interface
- from zope.interface import classImplements
- from zope.interface.exceptions import BrokenImplementation
- from zope.interface.exceptions import DoesNotImplement
- from zope.interface.exceptions import MultipleInvalid
- class ISeveralMethods(Interface):
- def meth1(arg1):
- "Method 1"
- def meth2(arg1):
- "Method 2"
- class SeveralMethods:
- pass
- with self.assertRaises(MultipleInvalid) as exc:
- self._callFUT(ISeveralMethods, SeveralMethods)
- ex = exc.exception
- self.assertEqual(3, len(ex.exceptions))
- self.assertIsInstance(ex.exceptions[0], DoesNotImplement)
- self.assertIsInstance(ex.exceptions[1], BrokenImplementation)
- self.assertIsInstance(ex.exceptions[2], BrokenImplementation)
- # If everything else is correct, only the single error is raised
- # without the wrapper.
- classImplements(SeveralMethods, ISeveralMethods)
- SeveralMethods.meth1 = lambda self, arg1: "Hi"
- with self.assertRaises(BrokenImplementation):
- self._callFUT(ISeveralMethods, SeveralMethods)
- class Test_verifyObject(Test_verifyClass):
- @classmethod
- def _get_FUT(cls):
- from zope.interface.verify import verifyObject
- return verifyObject
- def _adjust_object_before_verify(self, target):
- if isinstance(target, (type, type(OldSkool))):
- target = target()
- return target
- def test_class_misses_attribute_for_attribute(self):
- # This check *fails* for verifyObject
- from zope.interface import Attribute
- from zope.interface import Interface
- from zope.interface import implementer
- from zope.interface.exceptions import BrokenImplementation
- class ICurrent(Interface):
- attr = Attribute("The foo Attribute")
- @implementer(ICurrent)
- class Current:
- pass
- self.assertRaises(BrokenImplementation,
- self._callFUT, ICurrent, Current)
- def test_module_hit(self):
- from . import dummy
- from .idummy import IDummyModule
- self._callFUT(IDummyModule, dummy)
- def test_module_miss(self):
- from zope.interface import Interface
- from zope.interface.exceptions import DoesNotImplement
- from . import dummy
- # same name, different object
- class IDummyModule(Interface):
- pass
- self.assertRaises(DoesNotImplement,
- self._callFUT, IDummyModule, dummy)
- def test_staticmethod_hit_on_class(self):
- from zope.interface import Interface
- from zope.interface import provider
- from zope.interface.verify import verifyObject
- class IFoo(Interface):
- def bar(a, b):
- "The bar method"
- @provider(IFoo)
- class Foo:
- @staticmethod
- def bar(a, b):
- raise AssertionError("We're never actually called")
- # Don't use self._callFUT, we don't want to instantiate the
- # class.
- verifyObject(IFoo, Foo)
- class OldSkool:
- pass
|