From d47a7587f9813f2366e2077be051116291bf930e Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 21 Sep 2014 17:17:02 +0200 Subject: Complete test suite overhaul. * rename a few test modules to make the names more consistent * do not copy/use Sphinx from build/ (unnecessary without 2to3) * use a temporary dir for *all* test projects, the source tree will stay pristine that way (default is tests/build) * speed up tests by ~3x by splitting up test projects and avoiding rebuilds --- tests/test_directive_code.py | 287 +++++++++++++++++-------------------------- 1 file changed, 114 insertions(+), 173 deletions(-) (limited to 'tests/test_directive_code.py') diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index 4dbdff881..295e201b1 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -1,173 +1,114 @@ -# -*- coding: utf-8 -*- -""" - test_directive_code - ~~~~~~~~~~~~~~~~~~~ - - Test the code-block directive. - - :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS. - :license: BSD, see LICENSE for details. -""" - -import re -from xml.etree import ElementTree - -from util import with_app, test_roots - - -def teardown_module(): - (test_roots / 'test-directive-code' / '_build').rmtree(True) - - -@with_app(buildername='xml', - srcdir=(test_roots / 'test-directive-code'), - _copy_to_temp=True) -def test_code_block(app): - app.builder.build('index') - et = ElementTree.parse(app.outdir / 'index.xml') - secs = et.findall('./section/section') - code_block = secs[0].findall('literal_block') - assert len(code_block) > 0 - actual = code_block[0].text - expect = ( - " def ruby?\n" + - " false\n" + - " end" - ) - assert actual == expect - - -@with_app(buildername='xml', - srcdir=(test_roots / 'test-directive-code'), - _copy_to_temp=True) -def test_code_block_dedent(app): - outdir = app.outdir - - def get_dedent_actual(dedent): - dedent_text = (app.srcdir / 'dedent.rst').text(encoding='utf-8') - dedent_text = re.sub( - r':dedent: \d', ':dedent: %d' % dedent, dedent_text) - (app.srcdir / 'dedent.rst').write_text(dedent_text, encoding='utf-8') - - # use another output dir to force rebuild - app.outdir = outdir / str(dedent) - app._init_env(freshenv=True) - app._init_builder(app.builder.name) - app.builder.build(['dedent'], method='specific') - - et = ElementTree.parse(app.outdir / 'dedent.xml') - secs = et.findall('./section/section') - code_block = secs[0].findall('literal_block') - - assert len(code_block) > 0 - actual = code_block[0].text - return actual - - for i in range(5): # 0-4 - actual = get_dedent_actual(i) - indent = " " * (4 - i) - expect = ( - indent + "def ruby?\n" + - indent + " false\n" + - indent + "end" - ) - assert (i, actual) == (i, expect) - - actual = get_dedent_actual(1000) - assert actual == '\n\n' - - -@with_app(buildername='html', - srcdir=(test_roots / 'test-directive-code'), - _copy_to_temp=True) -def test_code_block_caption_html(app): - app.builder.build('index') - html = (app.outdir / 'caption.html').text() - caption = '
caption-test.rb
' - assert caption in html - - -@with_app(buildername='latex', - srcdir=(test_roots / 'test-directive-code'), - _copy_to_temp=True) -def test_code_block_caption_latex(app): - app.builder.build('index') - latex = (app.outdir / 'Python.tex').text() - caption = ('{\\colorbox[rgb]{0.9,0.9,0.9}{\\makebox[\\textwidth][l]' - '{\\small\\texttt{caption-test.rb}}}}') - assert caption in latex - - -@with_app(buildername='xml', - srcdir=(test_roots / 'test-directive-code'), - _copy_to_temp=True) -def test_literal_include(app): - app.builder.build('index') - et = ElementTree.parse(app.outdir / 'index.xml') - secs = et.findall('./section/section') - literal_include = secs[1].findall('literal_block') - literal_src = (app.srcdir / 'literal.inc').text(encoding='utf-8') - assert len(literal_include) > 0 - actual = literal_include[0].text - assert actual == literal_src - - -@with_app(buildername='xml', - srcdir=(test_roots / 'test-directive-code'), - _copy_to_temp=True) -def test_literal_include_dedent(app): - outdir = app.outdir - literal_src = (app.srcdir / 'literal.inc').text(encoding='utf-8') - literal_lines = [l[4:] for l in literal_src.split('\n')[9:11]] - - def get_dedent_actual(dedent): - dedent_text = (app.srcdir / 'dedent.rst').text(encoding='utf-8') - dedent_text = re.sub( - r':dedent: \d', ':dedent: %d' % dedent, dedent_text) - (app.srcdir / 'dedent.rst').write_text(dedent_text, encoding='utf-8') - - # use another output dir to force rebuild - app.outdir = outdir / str(dedent) - app._init_env(freshenv=True) - app._init_builder(app.builder.name) - app.builder.build(['dedent']) - - et = ElementTree.parse(app.outdir / 'dedent.xml') - secs = et.findall('./section/section') - literal_include = secs[1].findall('literal_block') - - assert len(literal_include) > 0 - actual = literal_include[0].text - return actual - - - for i in range(5): # 0-4 - actual = get_dedent_actual(i) - indent = " " * (4 - i) - expect = '\n'.join(indent + l for l in literal_lines) + '\n' - assert (i, actual) == (i, expect) - - - actual = get_dedent_actual(1000) - assert actual == '\n\n' - - -@with_app(buildername='html', - srcdir=(test_roots / 'test-directive-code'), - _copy_to_temp=True) -def test_literalinclude_caption_html(app): - app.builder.build('index') - html = (app.outdir / 'caption.html').text() - caption = '
caption-test.py
' - assert caption in html - - -@with_app(buildername='latex', - srcdir=(test_roots / 'test-directive-code'), - _copy_to_temp=True) -def test_literalinclude_caption_latex(app): - app.builder.build('index') - latex = (app.outdir / 'Python.tex').text() - caption = ('{\\colorbox[rgb]{0.9,0.9,0.9}{\\makebox[\\textwidth][l]' - '{\\small\\texttt{caption-test.py}}}}') - assert caption in latex +# -*- coding: utf-8 -*- +""" + test_directive_code + ~~~~~~~~~~~~~~~~~~~ + + Test the code-block directive. + + :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +import re +from xml.etree import ElementTree + +from util import with_app + + +@with_app('xml', testroot='directive-code') +def test_code_block(app, status, warning): + app.builder.build('index') + et = ElementTree.parse(app.outdir / 'index.xml') + secs = et.findall('./section/section') + code_block = secs[0].findall('literal_block') + assert len(code_block) > 0 + actual = code_block[0].text + expect = ( + " def ruby?\n" + + " false\n" + + " end" + ) + assert actual == expect + + +@with_app('xml', testroot='directive-code') +def test_code_block_dedent(app, status, warning): + app.builder.build(['dedent_code']) + et = ElementTree.parse(app.outdir / 'dedent_code.xml') + blocks = et.findall('./section/section/literal_block') + + for i in range(5): # 0-4 + actual = blocks[i].text + indent = " " * (4 - i) + expect = ( + indent + "def ruby?\n" + + indent + " false\n" + + indent + "end" + ) + assert (i, actual) == (i, expect) + + assert blocks[5].text == '\n\n' # dedent: 1000 + + +@with_app('html', testroot='directive-code') +def test_code_block_caption_html(app, status, warning): + app.builder.build(['caption']) + html = (app.outdir / 'caption.html').text() + caption = '
caption-test.rb
' + assert caption in html + + +@with_app('latex', testroot='directive-code') +def test_code_block_caption_latex(app, status, warning): + app.builder.build_all() + latex = (app.outdir / 'Python.tex').text() + caption = ('{\\colorbox[rgb]{0.9,0.9,0.9}{\\makebox[\\textwidth][l]' + '{\\small\\texttt{caption-test.rb}}}}') + assert caption in latex + + +@with_app('xml', testroot='directive-code') +def test_literal_include(app, status, warning): + app.builder.build(['index']) + et = ElementTree.parse(app.outdir / 'index.xml') + secs = et.findall('./section/section') + literal_include = secs[1].findall('literal_block') + literal_src = (app.srcdir / 'literal.inc').text(encoding='utf-8') + assert len(literal_include) > 0 + actual = literal_include[0].text + assert actual == literal_src + + +@with_app('xml', testroot='directive-code') +def test_literal_include_dedent(app, status, warning): + literal_src = (app.srcdir / 'literal.inc').text(encoding='utf-8') + literal_lines = [l[4:] for l in literal_src.split('\n')[9:11]] + + app.builder.build(['dedent']) + et = ElementTree.parse(app.outdir / 'dedent.xml') + blocks = et.findall('./section/section/literal_block') + + for i in range(5): # 0-4 + actual = blocks[i].text + indent = ' ' * (4 - i) + expect = '\n'.join(indent + l for l in literal_lines) + '\n' + assert (i, actual) == (i, expect) + + assert blocks[5].text == '\n\n' # dedent: 1000 + + +@with_app('html', testroot='directive-code') +def test_literalinclude_caption_html(app, status, warning): + app.builder.build('index') + html = (app.outdir / 'caption.html').text() + caption = '
caption-test.py
' + assert caption in html + + +@with_app('latex', testroot='directive-code') +def test_literalinclude_caption_latex(app, status, warning): + app.builder.build('index') + latex = (app.outdir / 'Python.tex').text() + caption = ('{\\colorbox[rgb]{0.9,0.9,0.9}{\\makebox[\\textwidth][l]' + '{\\small\\texttt{caption-test.py}}}}') + assert caption in latex -- cgit v1.2.1