test: verify linenothreshold usage

Adding a unit test to verify the use of the `linenothreshold` option
provided by the `highlight` directive [1]. The included document to test
will introduce two sets of `code-block` and `literalinclude` directives
where the first entry generates contents using line numbers and the
second entry generates contents not using line numbers, respectfully.

[1]: https://github.com/sphinx-doc/sphinx/blob/v1.8.4/sphinx/transforms/post_transforms/code.py#L85

Signed-off-by: James Knight <james.d.knight@live.com>
This commit is contained in:
James Knight 2019-02-16 21:44:32 -05:00
parent 0ded648c1a
commit 3858a62814
3 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,23 @@
Code Blocks and Literal Includes with Line Numbers via linenothreshold
======================================================================
.. highlight:: python
:linenothreshold: 5
.. code-block::
class Foo:
pass
class Bar:
def baz():
pass
.. code-block::
# comment
value = True
.. literalinclude:: literal.inc
.. literalinclude:: literal-short.inc

View File

@ -0,0 +1,3 @@
# Very small literal include (linenothreshold check)
value = True

View File

@ -565,3 +565,52 @@ def test_code_block_highlighted(app, status, warning):
assert codeblocks[1]['language'] == 'python2'
assert codeblocks[2]['language'] == 'python3'
assert codeblocks[3]['language'] == 'python2'
@pytest.mark.sphinx('html', testroot='directive-code')
def test_linenothreshold(app, status, warning):
app.builder.build(['linenothreshold'])
html = (app.outdir / 'linenothreshold.html').text()
lineos_head = '<td class="linenos"><div class="linenodiv"><pre>'
lineos_tail = '</pre></div></td>'
# code-block using linenothreshold
_, matched, html = html.partition(lineos_head +
'1\n'
'2\n'
'3\n'
'4\n'
'5\n'
'6' + lineos_tail)
assert matched
# code-block not using linenothreshold
html, matched, _ = html.partition(lineos_head +
'1\n'
'2' + lineos_tail)
assert not matched
# literal include using linenothreshold
_, matched, html = html.partition(lineos_head +
' 1\n'
' 2\n'
' 3\n'
' 4\n'
' 5\n'
' 6\n'
' 7\n'
' 8\n'
' 9\n'
'10\n'
'11\n'
'12\n'
'13' + lineos_tail)
assert matched
# literal include not using linenothreshold
html, matched, _ = html.partition(lineos_head +
'1\n'
'2\n'
'3' + lineos_tail)
assert not matched