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

This commit is contained in:
Georg Brandl
2009-01-03 12:29:42 +01:00
parent 9fc2437773
commit 4d083fabbe
5 changed files with 59 additions and 12 deletions

View File

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

View File

@@ -15,6 +15,28 @@ Test file and literal inclusion
.. include:: wrongenc.inc .. include:: wrongenc.inc
:encoding: latin-1 :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 Testing downloadable files
========================== ==========================

View File

@@ -2,3 +2,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
foo = u"Including Unicode characters: üöä" foo = u"Including Unicode characters: üöä"
class Foo:
pass
class Bar:
def baz():
pass
def bar(): pass

View File

@@ -10,6 +10,7 @@
""" """
import os import os
import re
import sys import sys
import difflib import difflib
import htmlentitydefs import htmlentitydefs
@@ -32,7 +33,7 @@ WARNING: %(root)s/images.txt:9: Image file not readable: foo.png
WARNING: %(root)s/images.txt:23: Nonlocal image URI found: http://www.python.org/logo.png 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 \ 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 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 + """\ HTML_WARNINGS = ENV_WARNINGS + """\
@@ -61,11 +62,19 @@ HTML_XPATH = {
".//pre": u'Max Strauß', ".//pre": u'Max Strauß',
".//a[@href='_downloads/img.png']": '', ".//a[@href='_downloads/img.png']": '',
".//a[@href='_downloads/img1.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': { 'autodoc.html': {
".//dt[@id='test_autodoc.Class']": '', ".//dt[@id='test_autodoc.Class']": '',
".//dt[@id='test_autodoc.function']/em": '**kwds', ".//dt[@id='test_autodoc.function']/em": r'\*\*kwds',
".//dd": 'Return spam.', ".//dd": r'Return spam\.',
}, },
'markup.html': { 'markup.html': {
".//meta[@name='author'][@content='Me']": '', ".//meta[@name='author'][@content='Me']": '',
@@ -81,7 +90,7 @@ HTML_XPATH = {
}, },
'contents.html': { 'contents.html': {
".//meta[@name='hc'][@content='hcval']": '', ".//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-l1']/a": 'Testing various markup',
".//li[@class='toctree-l2']/a": 'Admonitions', ".//li[@class='toctree-l2']/a": 'Admonitions',
".//title": 'Sphinx <Tests>', ".//title": 'Sphinx <Tests>',
@@ -117,18 +126,23 @@ def test_html(app):
parser = NslessParser() parser = NslessParser()
parser.entity.update(htmlentitydefs.entitydefs) parser.entity.update(htmlentitydefs.entitydefs)
etree = ET.parse(os.path.join(app.outdir, fname), parser) 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)) nodes = list(etree.findall(path))
assert nodes != [] assert nodes != []
if not text: if callable(check):
check(nodes)
elif not check:
# only check for node presence # only check for node presence
continue continue
for node in nodes:
if node.text and text in node.text:
break
else: else:
assert False, ('%r not found in any node matching ' rex = re.compile(check)
'path %s in %s' % (text, path, fname)) 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) @with_app(buildername='latex', warning=latex_warnfile)

View File

@@ -17,11 +17,13 @@ from docutils import frontend, utils, nodes
from docutils.parsers import rst from docutils.parsers import rst
from sphinx import addnodes from sphinx import addnodes
from sphinx.util import texescape
from sphinx.writers.html import HTMLWriter, SmartyPantsHTMLTranslator from sphinx.writers.html import HTMLWriter, SmartyPantsHTMLTranslator
from sphinx.writers.latex import LaTeXWriter, LaTeXTranslator from sphinx.writers.latex import LaTeXWriter, LaTeXTranslator
def setup_module(): def setup_module():
global app, settings, parser global app, settings, parser
texescape.init() # otherwise done by the latex builder
app = TestApp(cleanenv=True) app = TestApp(cleanenv=True)
optparser = frontend.OptionParser(components=(rst.Parser, HTMLWriter, LaTeXWriter)) optparser = frontend.OptionParser(components=(rst.Parser, HTMLWriter, LaTeXWriter))
settings = optparser.get_default_values() settings = optparser.get_default_values()