Commits

Georg Brandl committed 482eae1

Add tests for new literalinclude options, and fix an off-by-one bug.

Comments (0)

Files changed (5)

sphinx/directives/code.py

                 'Object named %r not found in include file %r' %
                 (objectname, arguments[0]), line=lineno)]
         else:
-            lines = lines[tags[objectname][1] - 1 : tags[objectname][2]]
+            lines = lines[tags[objectname][1] - 1 : tags[objectname][2] - 1]
 
     linespec = options.get('lines')
     if linespec is not None:

tests/root/includes.txt

 .. include:: wrongenc.inc
    :encoding: latin-1
 
+Literalinclude options
+======================
+
+.. highlight:: text
+
+.. cssclass:: inc-pyobj1
+.. literalinclude:: literal.inc
+   :pyobject: Foo
+
+.. cssclass:: inc-pyobj2
+.. literalinclude:: literal.inc
+   :pyobject: Bar.baz
+
+.. cssclass:: inc-lines
+.. literalinclude:: literal.inc
+   :lines: 6-7,9
+
+.. cssclass:: inc-startend
+.. literalinclude:: literal.inc
+   :start-after: coding: utf-8
+   :end-before: class Foo
+
 
 Testing downloadable files
 ==========================

tests/root/literal.inc

 # -*- coding: utf-8 -*-
 
 foo = u"Including Unicode characters: üöä"
+
+class Foo:
+    pass
+
+class Bar:
+    def baz():
+        pass
+
+def bar(): pass
 """
 
 import os
+import re
 import sys
 import difflib
 import htmlentitydefs
 WARNING: %(root)s/images.txt:23: Nonlocal image URI found: http://www.python.org/logo.png
 WARNING: %(root)s/includes.txt:: (WARNING/2) Encoding 'utf-8' used for reading included \
 file u'wrongenc.inc' seems to be wrong, try giving an :encoding: option
-WARNING: %(root)s/includes.txt:34: Download file not readable: nonexisting.png
+WARNING: %(root)s/includes.txt:56: Download file not readable: nonexisting.png
 """
 
 HTML_WARNINGS = ENV_WARNINGS + """\
         ".//pre": u'Max Strauß',
         ".//a[@href='_downloads/img.png']": '',
         ".//a[@href='_downloads/img1.png']": '',
+        ".//div[@class='inc-pyobj1 highlight-text']/div/pre":
+            r'^class Foo:\n    pass\n\s*$',
+        ".//div[@class='inc-pyobj2 highlight-text']/div/pre":
+            r'^    def baz\(\):\n        pass\n\s*$',
+        ".//div[@class='inc-lines highlight-text']/div/pre":
+            r'^class Foo:\n    pass\nclass Bar:\n$',
+        ".//div[@class='inc-startend highlight-text']/div/pre":
+            ur'^foo = u"Including Unicode characters: üöä"\n$',
     },
     'autodoc.html': {
         ".//dt[@id='test_autodoc.Class']": '',
-        ".//dt[@id='test_autodoc.function']/em": '**kwds',
-        ".//dd": 'Return spam.',
+        ".//dt[@id='test_autodoc.function']/em": r'\*\*kwds',
+        ".//dd": r'Return spam\.',
     },
     'markup.html': {
         ".//meta[@name='author'][@content='Me']": '',
     },
     'contents.html': {
         ".//meta[@name='hc'][@content='hcval']": '',
-        ".//td[@class='label']": '[Ref1]',
+        ".//td[@class='label']": r'\[Ref1\]',
         ".//li[@class='toctree-l1']/a": 'Testing various markup',
         ".//li[@class='toctree-l2']/a": 'Admonitions',
         ".//title": 'Sphinx <Tests>',
         parser = NslessParser()
         parser.entity.update(htmlentitydefs.entitydefs)
         etree = ET.parse(os.path.join(app.outdir, fname), parser)
-        for path, text in paths.iteritems():
+        for path, check in paths.iteritems():
             nodes = list(etree.findall(path))
             assert nodes != []
-            if not text:
+            if callable(check):
+                check(nodes)
+            elif not check:
                 # only check for node presence
                 continue
-            for node in nodes:
-                if node.text and text in node.text:
-                    break
             else:
-                assert False, ('%r not found in any node matching '
-                               'path %s in %s' % (text, path, fname))
+                rex = re.compile(check)
+                for node in nodes:
+                    if node.text and rex.search(node.text):
+                        break
+                else:
+                    assert False, ('%r not found in any node matching '
+                                   'path %s in %s: %r' % (check, path, fname,
+                                   [node.text for node in nodes]))
 
 
 @with_app(buildername='latex', warning=latex_warnfile)
 from docutils.parsers import rst
 
 from sphinx import addnodes
+from sphinx.util import texescape
 from sphinx.writers.html import HTMLWriter, SmartyPantsHTMLTranslator
 from sphinx.writers.latex import LaTeXWriter, LaTeXTranslator
 
 def setup_module():
     global app, settings, parser
+    texescape.init()  # otherwise done by the latex builder
     app = TestApp(cleanenv=True)
     optparser = frontend.OptionParser(components=(rst.Parser, HTMLWriter, LaTeXWriter))
     settings = optparser.get_default_values()