[Checkins] SVN: zope.configuration/branches/tseaver-test_cleanup/src/zope/configuration/tests/test_config.py Convert doctests to unittests.
Tres Seaver
cvs-admin at zope.org
Tue May 8 01:18:40 UTC 2012
Log message for revision 125715:
Convert doctests to unittests.
Changed:
U zope.configuration/branches/tseaver-test_cleanup/src/zope/configuration/tests/test_config.py
-=-
Modified: zope.configuration/branches/tseaver-test_cleanup/src/zope/configuration/tests/test_config.py
===================================================================
--- zope.configuration/branches/tseaver-test_cleanup/src/zope/configuration/tests/test_config.py 2012-05-08 01:18:29 UTC (rev 125714)
+++ zope.configuration/branches/tseaver-test_cleanup/src/zope/configuration/tests/test_config.py 2012-05-08 01:18:36 UTC (rev 125715)
@@ -13,179 +13,150 @@
##############################################################################
"""Test configuration machinery.
"""
-
-import sys
import unittest
-import re
-from doctest import DocTestSuite
-from zope.testing import renormalizing
-from zope.configuration.config import metans, ConfigurationMachine
-from zope.configuration import config
-def test_keyword_handling():
- """
- >>> machine = ConfigurationMachine()
- >>> ns = "http://www.zope.org/testing"
- Register some test directives:
+class ConfigurationContextTests(unittest.TestCase):
- Start with a grouping directive that sets a package:
+ def _getTargetClass(self):
+ from zope.configuration.config import ConfigurationContext
+ return ConfigurationContext
+
+ def _makeOne(self, *args, **kw):
+ return self._getTargetClass()(*args, **kw)
- >>> machine((metans, "groupingDirective"),
- ... name="package", namespace=ns,
- ... schema="zope.configuration.tests.directives.IPackaged",
- ... handler="zope.configuration.tests.directives.Packaged",
- ... )
+ def assertRaises(self, excClass, callableObj, *args, **kwargs):
+ # Morph stdlib version to return the raised exception
+ try:
+ callableObj(*args, **kwargs)
+ except excClass as exc:
+ return exc
+ else:
+ if hasattr(excClass,'__name__'): excName = excClass.__name__
+ else: excName = str(excClass)
+ raise self.failureException, "%s not raised" % excName
- Now we can set the package:
+ def test_resolve_trailing_dot_in_resolve(self):
+ #Dotted names are no longer allowed to end in dots
+ c = self._makeOne()
+ self.assertRaises(ValueError, c.resolve, 'zope.')
- >>> machine.begin((ns, "package"),
- ... package="zope.configuration.tests.directives",
- ... )
+ def test_resolve_blank(self):
+ c = self._makeOne()
+ self.assertRaises(ValueError, c.resolve, ' ')
- Which makes it easier to define the other directives:
+ def test_bad_dotted_last_import(self):
+ # Import error caused by a bad last component in the dotted name.
+ from zope.configuration.exceptions import ConfigurationError
+ c = self._makeOne()
+ exc = self.assertRaises(ConfigurationError,
+ c.resolve, 'zope.configuration.tests.nosuch')
+ self.assertTrue('ImportError' in str(exc))
- >>> machine((metans, "directive"),
- ... namespace=ns, name="k",
- ... schema=".Ik", handler=".k")
+ def test_bad_dotted_import(self):
+ # Import error caused by a totally wrong dotted name.
+ from zope.configuration.exceptions import ConfigurationError
+ c = self._makeOne()
+ exc = self.assertRaises(ConfigurationError,
+ c.resolve, 'zope.configuration.nosuch.noreally')
+ self.assertTrue('ImportError' in str(exc))
+ def test_bad_sub_last_import(self):
+ #Import error caused by a bad sub import inside the referenced
+ #dotted name. Here we keep the standard traceback.
+ import sys
+ c = self._makeOne()
+ self.assertRaises(ImportError,
+ c.resolve, 'zope.configuration.tests.victim')
+ #Cleanup:
+ for name in ('zope.configuration.tests.victim',
+ 'zope.configuration.tests.bad'):
+ if name in sys.modules:
+ del sys.modules[name]
- >>> machine((ns, "k"), "yee ha", **{"for": u"f", "class": u"c", "x": u"x"})
+ def test_bad_sub_import(self):
+ #Import error caused by a bad sub import inside part of the referenced
+ #dotted name. Here we keep the standard traceback.
+ import sys
+ c = self._makeOne()
+ self.assertRaises(ImportError,
+ c.resolve, 'zope.configuration.tests.victim.nosuch')
+ #Cleanup:
+ for name in ('zope.configuration.tests.victim',
+ 'zope.configuration.tests.bad'):
+ if name in sys.modules:
+ del sys.modules[name]
- >>> from pprint import PrettyPrinter
- >>> pprint=PrettyPrinter(width=60).pprint
- >>> pprint(machine.actions)
- [{'args': ('f', 'c', 'x'),
- 'callable': f,
- 'discriminator': ('k', 'f'),
- 'includepath': (),
- 'info': 'yee ha',
- 'kw': {},
- 'order': 0}]
- """
+ def test_path_basepath_absolute(self):
+ #Path must always return an absolute path.
+ import os
+ class stub:
+ __file__ = os.path.join('relative', 'path')
+ c = self._makeOne()
+ c.package = stub()
+ self.assertTrue(os.path.isabs(c.path('y/z')))
-def test_basepath_absolute():
- """Path must always return an absolute path.
+ def test_path_basepath_uses_dunder_path(self):
+ #Determine package path using __path__ if __file__ isn't available.
+ # (i.e. namespace package installed with
+ #--single-version-externally-managed)
+ import os
+ class stub:
+ __path__ = [os.path.join('relative', 'path')]
+ c = self._makeOne()
+ c.package = stub()
+ os.path.isabs(c.path('y/z'))
- >>> import os
- >>> class stub:
- ... __file__ = os.path.join('relative', 'path')
- >>> c = config.ConfigurationContext()
- >>> c.package = stub()
+class ConfigurationMachineTests(unittest.TestCase):
- >>> os.path.isabs(c.path('y/z'))
- True
- """
+ def _getTargetClass(self):
+ from zope.configuration.config import ConfigurationMachine
+ return ConfigurationMachine
+
+ def _makeOne(self, *args, **kw):
+ return self._getTargetClass()(*args, **kw)
-def test_basepath_uses_dunder_path():
- """Determine package path using __path__ if __file__ isn't available.
- (i.e. namespace package installed with --single-version-externally-managed)
+ def test_keyword_handling(self):
+ from zope.configuration.config import metans
+ from zope.configuration.tests.directives import f
+ machine = self._makeOne()
+ ns = "http://www.zope.org/testing"
- >>> import os
- >>> class stub:
- ... __path__ = [os.path.join('relative', 'path')]
- >>> c = config.ConfigurationContext()
- >>> c.package = stub()
+ #Register some test directives, starting with a grouping directive
+ # that sets a package:
- >>> os.path.isabs(c.path('y/z'))
- True
- """
+ machine((metans, "groupingDirective"),
+ name="package", namespace=ns,
+ schema="zope.configuration.tests.directives.IPackaged",
+ handler="zope.configuration.tests.directives.Packaged",
+ )
-def test_trailing_dot_in_resolve():
- """Dotted names are no longer allowed to end in dots
+ # set the package:
+ machine.begin((ns, "package"),
+ package="zope.configuration.tests.directives",
+ )
- >>> c = config.ConfigurationContext()
+ #Which makes it easier to define the other directives:
+ machine((metans, "directive"),
+ namespace=ns, name="k",
+ schema=".Ik", handler=".k")
- >>> c.resolve('zope.')
- Traceback (most recent call last):
- ...
- ValueError: Trailing dots are no longer supported in dotted names
+ machine((ns, "k"), "yee ha",
+ **{"for": u"f", "class": u"c", "x": u"x"})
- >>> c.resolve(' ')
- Traceback (most recent call last):
- ...
- ValueError: The given name is blank
- """
+ self.assertEqual(len(machine.actions), 1)
+ self.assertEqual(machine.actions[0],
+ {'args': ('f', 'c', 'x'),
+ 'callable': f,
+ 'discriminator': ('k', 'f'),
+ 'includepath': (),
+ 'info': 'yee ha',
+ 'kw': {},
+ 'order': 0,
+ })
-def test_bad_dotted_last_import():
- """
- >>> c = config.ConfigurationContext()
-
- Import error caused by a bad last component in the dotted name.
-
- >>> c.resolve('zope.configuration.tests.nosuch')
- Traceback (most recent call last):
- ...
- ConfigurationError: ImportError: Module zope.configuration.tests""" \
- """ has no global nosuch
- """
-
-def test_bad_dotted_import():
- """
- >>> c = config.ConfigurationContext()
-
- Import error caused by a totally wrong dotted name.
-
- >>> c.resolve('zope.configuration.nosuch.noreally')
- Traceback (most recent call last):
- ...
- ConfigurationError: ImportError: Couldn't import""" \
- """ zope.configuration.nosuch, No module named nosuch
- """
-
-def test_bad_sub_last_import():
- """
- >>> c = config.ConfigurationContext()
-
- Import error caused by a bad sub import inside the referenced
- dotted name. Here we keep the standard traceback.
-
- >>> c.resolve('zope.configuration.tests.victim')
- Traceback (most recent call last):
- ...
- File "...bad.py", line 3 in ?
- import bad_to_the_bone
- ImportError: No module named bad_to_the_bone
-
- Cleanup:
-
- >>> for name in ('zope.configuration.tests.victim',
- ... 'zope.configuration.tests.bad'):
- ... if name in sys.modules:
- ... del sys.modules[name]
- """
-
-def test_bad_sub_import():
- """
- >>> c = config.ConfigurationContext()
-
- Import error caused by a bad sub import inside part of the referenced
- dotted name. Here we keep the standard traceback.
-
- >>> c.resolve('zope.configuration.tests.victim.nosuch')
- Traceback (most recent call last):
- ...
- File "...bad.py", line 3 in ?
- import bad_to_the_bone
- ImportError: No module named bad_to_the_bone
-
- Cleanup:
-
- >>> for name in ('zope.configuration.tests.victim',
- ... 'zope.configuration.tests.bad'):
- ... if name in sys.modules:
- ... del sys.modules[name]
- """
-
def test_suite():
- checker = renormalizing.RENormalizing([
- (re.compile(r"<type 'exceptions.(\w+)Error'>:"),
- r'exceptions.\1Error:'),
- ])
return unittest.TestSuite((
- DocTestSuite('zope.configuration.fields'),
- DocTestSuite('zope.configuration.config',checker=checker),
- DocTestSuite(),
+ unittest.makeSuite(ConfigurationContextTests),
+ unittest.makeSuite(ConfigurationMachineTests),
))
-
-if __name__ == '__main__': unittest.main()
More information about the checkins
mailing list