Commits

Anonymous committed f89104e Merge

Merged test-doctests

Comments (0)

Files changed (3)

nose2/plugins/doctests.py

         self.extensions = self.config.as_list('extensions', ['.txt', '.rst'])
 
     def handleFile(self, event):
+        """Implement hook."""
         loader_ = event.loader
         path = event.path
         _root, ext = os.path.splitext(path)
         if ext in self.extensions:
             suite = doctest.DocFileTest(path, module_relative=False)
             event.extraTests.append(suite)
+            # XXX: Return from here, since we know now this isn't a module from
+            # which to load doctests?
         elif not loader.VALID_MODULE_NAME.match(os.path.basename(path)):
             return
+
         name = loader_._get_name_from_path(path)
         try:
             module = loader_._get_module_from_name(name)

nose2/tests/_common.py

 """Common functionality."""
+import unittest2.loader
 import os.path
 import tempfile
 import shutil
 import sys
 import subprocess
-import unittest2
 
 HERE = os.path.dirname(__file__)
 ROOT = os.path.join(HERE, '..', '..')
 
 class TestCase(unittest2.TestCase):
     """TestCase extension.
-    
+
     If the class variable _RUN_IN_TEMP is True (default: False), tests will be
     performed in a temporary directory, which is deleted afterwards.
     """
 
     def setUp(self):
         super(TestCase, self).setUp()
-        
+
         if self._RUN_IN_TEMP:
             self._orig_dir = os.getcwd()
             work_dir = self._work_dir = tempfile.mkdtemp()
     def message(self, msg, verbosity=(1, 2)):
         self._fake_messages.append((msg, verbosity))
 
+
+class FakeHandleFileEvent(_FakeEventBase):
+    """Fake HandleFileEvent."""
+    def __init__(self, name):
+        super(FakeHandleFileEvent, self).__init__()
+
+        self.loader = unittest2.loader.TestLoader()
+        self.name = name
+        self.path = os.path.split(name)[1]
+        self.extraTests = []
+
+
 class FakeStartTestEvent(_FakeEventBase):
     """Fake StartTestEvent."""
     def __init__(self, test):
         import time
         self.startTime = time.time()
 
+
 class FakeLoadFromNameEvent(_FakeEventBase):
     """Fake LoadFromNameEvent."""
     def __init__(self, name):
         super(FakeLoadFromNameEvent, self).__init__()
         self.name = name
 
+
 class FakeLoadFromNamesEvent(_FakeEventBase):
     """Fake LoadFromNamesEvent."""
     def __init__(self, names):

nose2/tests/test_doctests.py

+"""Test doctests plugin."""
+import doctest
+import unittest
+import os.path
+
+from ..plugins import doctests
+
+from ._common import TestCase, FakeHandleFileEvent
+
+
+class UnitTestDocTestLoader(TestCase):
+    """Test class DocTestLoader."""
+    tags = ['unit']
+
+    _RUN_IN_TEMP = True
+
+    def test___init__(self):
+        """Test the __init__ method."""
+        plug = self._create()
+
+        self.assertEqual(plug.extensions, ['.txt', '.rst'])
+
+
+    def test_handle_file(self):
+        """Test method handleFile."""
+        # Create doctest files of supported types
+        doc_test = """\
+>>> 2 == 2
+True
+"""
+        txt_event = self._handle_file('docs.txt', doc_test)
+        rst_event = self._handle_file('docs.rst', doc_test)
+        # Excercise loading of doctests from Python code
+        py_event = self._handle_file('docs.py', """\
+\"\"\"
+>>> 2 == 2
+True
+\"\"\"
+""")
+
+
+        for event, ext in [(txt_event, 'txt'), (rst_event, 'rst')]:
+            test, = event.extraTests
+            self.assertTrue(isinstance(test, doctest.DocFileCase))
+            self.assertEqual(repr(test), "docs.%s" % ext)
+
+        testsuite, = py_event.extraTests
+        test, = list(testsuite)
+        self.assertEqual(repr(test), 'docs ()')
+
+
+    def test_handle_file_python_without_doctests(self):
+        """Test calling handleFile for a Python module without doctests."""
+        event = self._handle_file("mod.py", """\
+def func():
+    pass
+""")
+
+        self.assertEqual(event.extraTests, [])
+
+
+    def _handle_file(self, fpath, content):
+        """Have plugin handle a file with certain content.
+
+        The file is created, then a plugin is instantiated and its handleFile
+        method is called for the file.
+        """
+        plug = self._create()
+        fh = open(fpath, "wb")
+        try:
+            fh.write(content)
+        finally:
+            fh.close()
+
+        event = FakeHandleFileEvent(fh.name)
+        plug.handleFile(event)
+        return event
+
+
+    def _create(self):
+        """Create a DocTestLoader instance."""
+        plug = doctests.DocTestLoader()
+        return plug