PYTHON   19
PackageTest
Guest on 25th May 2023 01:41:31 PM


  1. import unittest
  2.  
  3. from package import parse_package, NotAPackageException
  4. from transform import make_document
  5.  
  6. # The following is to ensure that there are .pyc files in the package
  7. # - this is important for testing, since the Python compiler gets quite
  8. # unhappy if given a non-text file to play with (it doesn't like null bytes),
  9. # so we need to do something about that...
  10. import trivial_package
  11.  
  12. class PackageTest(unittest.TestCase):
  13.  
  14.     def testNoSuchDirectory(self):
  15.         """Not a package - no such directory.
  16.        """
  17.  
  18.         self.assertRaises(OSError,
  19.                           parse_package,
  20.                           "no_such_directory")
  21.  
  22.     def testNotADirectory(self):
  23.         """Not a package - file is not a directory.
  24.        """
  25.  
  26.         self.assertRaises(OSError,
  27.                           parse_package,
  28.                           "not_a_directory")
  29.  
  30.     def testNotAPackage(self):
  31.         """Not a package - directory is empty.
  32.        """
  33.  
  34.         self.assertRaises(NotAPackageException,
  35.                           parse_package,
  36.                           "not_a_package")
  37.  
  38.     def testPackage(self):
  39.         """A package containing subpackage(s)
  40.  
  41.        The directory is called "trivial_package" for historical reasons.
  42.        """
  43.  
  44.         wanted_result = """\
  45. <Package filename="trivial_package">
  46.    <Module filename="__init__.py">
  47.        <Docstring>
  48.            A simple docstring.
  49.    <Module filename="file1.py">
  50.        <Docstring>
  51.            This is the first example file. It *does* use reStructuredText.
  52.        <Attribute lineno="5" name="__docformat__">
  53.            <Expression lineno="5">
  54.                "reST"
  55.        <Import lineno="7">
  56.            os
  57.        <Class lineno="9" name="Fred">
  58.            <Docstring lineno="9">
  59.                An example class - it announces each instance as it is created.
  60.            <Method lineno="13" name="__init__">
  61.                <ParameterList lineno="13">
  62.                    <Parameter lineno="13" name="self">
  63.    <Module filename="file2.py">
  64.        <Docstring>
  65.            This module is *not* using reStructuredText for its docstrings.
  66.    <NotPython filename="not_python">
  67.    <Package filename="sub_package">
  68.        <Module filename="__init__.py">\n"""
  69.  
  70.         actual_result = str(parse_package("trivial_package"))
  71.  
  72.         if wanted_result != actual_result:
  73.             print "+++++++++++++++++++++++++ WANT"
  74.             print wanted_result
  75.             print "+++++++++++++++++++++++++ GOT"
  76.             print actual_result
  77.             print "+++++++++++++++++++++++++"
  78.  
  79.         self.assertEqual(actual_result,wanted_result)
  80.  
  81.     def testMakeDocument(self):
  82.         """
  83.        Turn our Package tree into a docutils Document.
  84.        """
  85.  
  86.         # I've split the wanted result string up into substrings so I can
  87.         # amend it more easily (or so I hope).
  88.         trivial_package = """\
  89. <document source="Package trivial_package">
  90.    <section class="package" id="package-trivial-package" name="package trivial_package">
  91.        <title>
  92.            Package trivial_package\n"""
  93.  
  94.         # The "xml:space" attribute is by observation, not prediction
  95.         module_init = """\
  96.        <section class="module" id="module-trivial-package-init" name="module trivial_package.__init__">
  97.            <title>
  98.                Module trivial_package.__init__
  99.            <literal_block class="docstring" xml:space="preserve">
  100.                A simple docstring.\n"""
  101.  
  102.         module_file1 = """\
  103.        <section class="module" id="module-trivial-package-file1" name="module trivial_package.file1">
  104.            <title>
  105.                Module trivial_package.file1
  106.            <literal_block class="docstring" xml:space="preserve">
  107.                This is the first example file. It *does* use reStructuredText.
  108.            <section class="class" id="class-trivial-package-file1-fred" name="class trivial_package.file1.fred">
  109.                <title>
  110.                    Class trivial_package.file1.Fred
  111.                <literal_block class="docstring" xml:space="preserve">
  112.                    An example class - it announces each instance as it is created.\n"""
  113.  
  114.         module_file2 = """\
  115.        <section class="module" id="module-trivial-package-file2" name="module trivial_package.file2">
  116.            <title>
  117.                Module trivial_package.file2
  118.            <literal_block class="docstring" xml:space="preserve">
  119.                This module is *not* using reStructuredText for its docstrings.\n"""
  120.  
  121.         non_python_file = """\
  122.        <section class="file" id="file-trivial-package-not-python" name="file trivial_package.not_python">
  123.            <title>
  124.                File trivial_package.not_python
  125.            <paragraph>
  126.                File
  127.                <literal>
  128.                    not_python
  129.                 is not a Python module.\n"""
  130.  
  131.         sub_package = """\
  132.        <section class="package" id="package-trivial-package-sub-package" name="package trivial_package.sub_package">
  133.            <title>
  134.                Package trivial_package.sub_package\n"""
  135.  
  136.         sub_module_init = """\
  137.            <section class="module" id="module-trivial-package-sub-package-init" name="module trivial_package.sub_package.__init__">
  138.                <title>
  139.                    Module trivial_package.sub_package.__init__\n"""
  140.  
  141.         wanted_result = (trivial_package + module_init + module_file1 +
  142.                          module_file2 + non_python_file + sub_package +
  143.                          sub_module_init)
  144.  
  145.         tree = parse_package("trivial_package")
  146.  
  147.         document = make_document(tree)
  148.  
  149.         actual_result = document.pformat()
  150.  
  151.         if wanted_result != actual_result:
  152.             print "+++++++++++++++++++++++++ WANT"
  153.             print wanted_result
  154.             print "+++++++++++++++++++++++++ GOT"
  155.             print actual_result
  156.             print "+++++++++++++++++++++++++"
  157.  
  158.         self.assertEqual(actual_result,wanted_result)
  159.  
  160.  
  161. if __name__ == "__main__":
  162.     unittest.main()

Raw Paste

Login or Register to edit or fork this paste. It's free.