Skip to content

Commit

Permalink
Merge pull request #1 from zopefoundation/patrick-module-dir
Browse files Browse the repository at this point in the history
Make dir() work as expected on zope.testing.module.FakeModule instances.
  • Loading branch information
tseaver committed May 2, 2013
2 parents 1d10073 + 96315f9 commit fa76496
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/zope/testing/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

import sys

class FakeModule:
class FakeModule(object):
def __init__(self, dict):
self.__dict = dict
def __getattr__(self, name):
try:
return self.__dict[name]
except KeyError:
raise AttributeError(name)
def __dir__(self):
return self.__dict.keys()

def setUp(test, name='__main__'):
dict = test.globs
Expand Down
12 changes: 10 additions & 2 deletions src/zope/testing/module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ desirable. Let's demonstrate the behavior::
>>> class Foo(object):
... pass

>>> 'builtin' in Foo.__module__
>>> 'builtin' in Foo.__module__
True

By using ``zope.testing.module.setUp`` this can be
Expand Down Expand Up @@ -39,6 +39,14 @@ else, in this case the default, ``__main__``::
>>> Foo.__module__
'__main__'

Calling ``dir`` on the module will yield the expected attributes,
including the ``FakeTest`` class we added above.

>>> import __main__
>>> attrs = dir(__main__)
>>> "FakeTest" in attrs
True

Let's tear this down again::

>>> from zope.testing.module import tearDown
Expand All @@ -48,7 +56,7 @@ We should now be back to the original situation::

>>> class Foo(object):
... pass
>>> 'builtin' in Foo.__module__
>>> 'builtin' in Foo.__module__
True

Importing
Expand Down

0 comments on commit fa76496

Please sign in to comment.