From 5a11e2ad0828b7c763d0c81211f686a85bc0324c Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Thu, 21 May 2009 16:01:58 +0000 Subject: - Class objects may now be used as view callables (both via ZCML and via use of the ``bfg_view`` decorator in Python 2.6 as a class decorator). The calling semantics when using a class as a view callable is similar to that of using a class as a Zope "browser view": the class' ``__init__`` must accept two positional parameters (conventionally named ``context``, and ``request``). The resulting instance must be callable (it must have a ``__call__`` method). When called, the instance should return a response. For example:: from webob import Response class MyView(object): def __init__(self, context, request): self.context = context self.request = request def __call__(self): return Response('hello from %s!' % self.context) See the "Views" chapter in the documentation and the ``repoze.bfg.view`` API documentation for more information. --- repoze/bfg/tests/test_view.py | 54 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) (limited to 'repoze/bfg/tests/test_view.py') diff --git a/repoze/bfg/tests/test_view.py b/repoze/bfg/tests/test_view.py index a96051226..08beffeaa 100644 --- a/repoze/bfg/tests/test_view.py +++ b/repoze/bfg/tests/test_view.py @@ -446,13 +446,60 @@ class TestBFGViewDecorator(unittest.TestCase): self.assertEqual(decorator.for_, None) self.assertEqual(decorator.permission, 'foo') - def test_call(self): + def test_call_function(self): + from repoze.bfg.interfaces import IRequest + from zope.interface import Interface + decorator = self._makeOne() + def foo(): + """ docstring """ + wrapped = decorator(foo) + self.failUnless(wrapped is foo) + self.assertEqual(wrapped.__is_bfg_view__, True) + self.assertEqual(wrapped.__permission__, None) + self.assertEqual(wrapped.__for__, Interface) + self.assertEqual(wrapped.__request_type__, IRequest) + + def test_call_oldstyle_class(self): + import inspect from repoze.bfg.interfaces import IRequest from zope.interface import Interface decorator = self._makeOne() class foo: """ docstring """ + def __init__(self, context, request): + self.context = context + self.request = request + def __call__(self): + return self + wrapped = decorator(foo) + self.failIf(wrapped is foo) + self.failUnless(inspect.isfunction(wrapped)) + self.assertEqual(wrapped.__is_bfg_view__, True) + self.assertEqual(wrapped.__permission__, None) + self.assertEqual(wrapped.__for__, Interface) + self.assertEqual(wrapped.__request_type__, IRequest) + self.assertEqual(wrapped.__module__, foo.__module__) + self.assertEqual(wrapped.__name__, foo.__name__) + self.assertEqual(wrapped.__doc__, foo.__doc__) + result = wrapped(None, None) + self.assertEqual(result.context, None) + self.assertEqual(result.request, None) + + def test_call_newstyle_class(self): + import inspect + from repoze.bfg.interfaces import IRequest + from zope.interface import Interface + decorator = self._makeOne() + class foo(object): + """ docstring """ + def __init__(self, context, request): + self.context = context + self.request = request + def __call__(self): + return self wrapped = decorator(foo) + self.failIf(wrapped is foo) + self.failUnless(inspect.isfunction(wrapped)) self.assertEqual(wrapped.__is_bfg_view__, True) self.assertEqual(wrapped.__permission__, None) self.assertEqual(wrapped.__for__, Interface) @@ -460,8 +507,9 @@ class TestBFGViewDecorator(unittest.TestCase): self.assertEqual(wrapped.__module__, foo.__module__) self.assertEqual(wrapped.__name__, foo.__name__) self.assertEqual(wrapped.__doc__, foo.__doc__) - for k, v in foo.__dict__.items(): - self.assertEqual(v, wrapped.__dict__[k]) + result = wrapped(None, None) + self.assertEqual(result.context, None) + self.assertEqual(result.request, None) class DummyContext: pass -- cgit v1.2.3