Skip to content

Commit

Permalink
Make FakeModule instances behave like a real module when dir() is cal…
Browse files Browse the repository at this point in the history
…led on them
  • Loading branch information
kilink committed May 2, 2013
1 parent 1d10073 commit 14bea90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/zope/testing/module.py
Expand Up @@ -24,6 +24,8 @@ def __getattr__(self, name):
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
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 14bea90

Please sign in to comment.