Skip to content

Commit

Permalink
Backport grok_component() in doctest fix to Grok 0.11 branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
faassen committed Nov 20, 2007
1 parent 8fe4185 commit 8fb1487
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.txt
Expand Up @@ -17,6 +17,10 @@ Bug fixes
``grok.grok_component`` to their correct equivalents in
``grok.testing``.

* ``grok.testing.grok_component()`` could not be used in a pure
doctest. This needed a bugfix in Martian (since 0.9.2). Add a test
that demonstrates this problem.

0.11 (2007-11-08)
=================

Expand Down
41 changes: 41 additions & 0 deletions src/grok/ftests/grok_component.txt
@@ -0,0 +1,41 @@
Test grok_component() in an ordinary doctest.

We already have tests for grok_component(), but these were placed
inside a module. We will now test grok_component() in a pure doctest
context. This used to demonstrate an error in martian when dealing
with the __builtin__ module (fixed in martian 0.9.2).

grok.testing.grok_component() can be used to grok individual
components within a doctest, such as adapters. It sets up just enough
context for some grokking to work, though more complicated grokkers
which need module context (such as view grokkers) might not work.

This defines the object we want to provide an adapter for::

>>> class Bar(object):
... pass

This is the interface that we want to adapt to::

>>> from zope.interface import Interface
>>> class IFoo(Interface):
... pass

This is the adapter itself::

>>> import grok
>>> class MyAdapter(grok.Adapter):
... grok.provides(IFoo)
... grok.context(Bar)

Now we will register the adapter using grok_component()::

>>> from grok.testing import grok_component
>>> grok_component('MyAdapter', MyAdapter)
True

The adapter should now be available::

>>> adapted = IFoo(Bar())
>>> isinstance(adapted, MyAdapter)
True
7 changes: 7 additions & 0 deletions src/grok/ftests/test_grok_functional.py
Expand Up @@ -72,6 +72,13 @@ def test_suite():
for name in ['view', 'staticdir', 'xmlrpc', 'traversal', 'form', 'url',
'security', 'utility', 'catalog', 'admin', 'site', 'rest']:
suite.addTest(suiteFromPackage(name))

# this test cannot follow the normal testing pattern, as the
# bug it tests for is only exposed in the context of a doctest
grok_component = doctest.DocFileSuite('grok_component.txt')
grok_component.layer = GrokFunctionalLayer
suite.addTest(grok_component)

return suite

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion versions.cfg
@@ -1,7 +1,7 @@
[versions]
ClientForm = 0.2.7
docutils = 0.4
martian = 0.9.1
martian = 0.9.2
mechanize = 0.1.7b
Pygments = 0.8.1
pytz = 2007g
Expand Down

0 comments on commit 8fb1487

Please sign in to comment.