From e8caf6ae4263e36c492acadd02ae3fbaa501b14b Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 31 Mar 2019 12:42:43 +0900 Subject: [PATCH] refactor: test_environment_indexentries To test IndexEntriesCollector also, new testcases parses reST text and builds indexentries. --- tests/test_environment_indexentries.py | 187 +++++++++++-------------- 1 file changed, 84 insertions(+), 103 deletions(-) diff --git a/tests/test_environment_indexentries.py b/tests/test_environment_indexentries.py index 62e4ffb79..5b7cc36ec 100644 --- a/tests/test_environment_indexentries.py +++ b/tests/test_environment_indexentries.py @@ -8,135 +8,116 @@ :license: BSD, see LICENSE for details. """ -from collections import namedtuple -from unittest import mock +import pytest -from sphinx import locale from sphinx.environment.adapters.indexentries import IndexEntries - -Environment = namedtuple('Environment', 'indexentries') - -dummy_builder = mock.Mock() -dummy_builder.get_relative_uri.return_value = '' +from sphinx.testing import restructuredtext -def test_create_single_index(): - # type, value, tid, main, index_key - env = Environment({ - 'index': [ - ('single', 'docutils', 'id1', '', None), - ('single', 'Python', 'id2', '', None), - ('single', 'pip; install', 'id3', '', None), - ('single', 'pip; upgrade', 'id4', '', None), - ('single', 'Sphinx', 'id5', '', None), - ('single', 'Ель', 'id6', '', None), - ('single', 'ёлка', 'id7', '', None), - ('single', '‏תירבע‎', 'id8', '', None), - ('single', '9-symbol', 'id9', '', None), - ('single', '&-symbol', 'id10', '', None), - ], - }) - index = IndexEntries(env).create_index(dummy_builder) +@pytest.mark.sphinx('dummy') +def test_create_single_index(app): + app.env.indexentries.clear() + text = (".. index:: docutils\n" + ".. index:: Python\n" + ".. index:: pip; install\n" + ".. index:: pip; upgrade\n" + ".. index:: Sphinx\n" + ".. index:: Ель\n" + ".. index:: ёлка\n" + ".. index:: ‏תירבע‎\n" + ".. index:: 9-symbol\n" + ".. index:: &-symbol\n") + restructuredtext.parse(app, text) + index = IndexEntries(app.env).create_index(app.builder) assert len(index) == 6 - assert index[0] == ('Symbols', [('&-symbol', [[('', '#id10')], [], None]), - ('9-symbol', [[('', '#id9')], [], None])]) - assert index[1] == ('D', [('docutils', [[('', '#id1')], [], None])]) - assert index[2] == ('P', [('pip', [[], [('install', [('', '#id3')]), - ('upgrade', [('', '#id4')])], None]), - ('Python', [[('', '#id2')], [], None])]) - assert index[3] == ('S', [('Sphinx', [[('', '#id5')], [], None])]) - assert index[4] == ('Е', [('ёлка', [[('', '#id7')], [], None]), - ('Ель', [[('', '#id6')], [], None])]) - assert index[5] == ('ת', [('‏תירבע‎', [[('', '#id8')], [], None])]) + assert index[0] == ('Symbols', [('&-symbol', [[('', '#index-9')], [], None]), + ('9-symbol', [[('', '#index-8')], [], None])]) + assert index[1] == ('D', [('docutils', [[('', '#index-0')], [], None])]) + assert index[2] == ('P', [('pip', [[], [('install', [('', '#index-2')]), + ('upgrade', [('', '#index-3')])], None]), + ('Python', [[('', '#index-1')], [], None])]) + assert index[3] == ('S', [('Sphinx', [[('', '#index-4')], [], None])]) + assert index[4] == ('Е', [('ёлка', [[('', '#index-6')], [], None]), + ('Ель', [[('', '#index-5')], [], None])]) + assert index[5] == ('ת', [('‏תירבע‎', [[('', '#index-7')], [], None])]) -def test_create_pair_index(): - # type, value, tid, main, index_key - env = Environment({ - 'index': [ - ('pair', 'docutils; reStructuredText', 'id1', '', None), - ('pair', 'Python; interpreter', 'id2', '', None), - ('pair', 'Sphinx; documentation tool', 'id3', '', None), - ], - }) - index = IndexEntries(env).create_index(dummy_builder) +@pytest.mark.sphinx('dummy') +def test_create_pair_index(app): + app.env.indexentries.clear() + text = (".. index:: pair: docutils; reStructuredText\n" + ".. index:: pair: Python; interpreter\n" + ".. index:: pair: Sphinx; documentation tool\n") + restructuredtext.parse(app, text) + index = IndexEntries(app.env).create_index(app.builder) assert len(index) == 5 assert index[0] == ('D', - [('documentation tool', [[], [('Sphinx', [('', '#id3')])], None]), - ('docutils', [[], [('reStructuredText', [('', '#id1')])], None])]) - assert index[1] == ('I', [('interpreter', [[], [('Python', [('', '#id2')])], None])]) - assert index[2] == ('P', [('Python', [[], [('interpreter', [('', '#id2')])], None])]) + [('documentation tool', [[], [('Sphinx', [('', '#index-2')])], None]), + ('docutils', [[], [('reStructuredText', [('', '#index-0')])], None])]) + assert index[1] == ('I', [('interpreter', [[], [('Python', [('', '#index-1')])], None])]) + assert index[2] == ('P', [('Python', [[], [('interpreter', [('', '#index-1')])], None])]) assert index[3] == ('R', - [('reStructuredText', [[], [('docutils', [('', '#id1')])], None])]) + [('reStructuredText', [[], [('docutils', [('', '#index-0')])], None])]) assert index[4] == ('S', - [('Sphinx', [[], [('documentation tool', [('', '#id3')])], None])]) + [('Sphinx', [[], [('documentation tool', [('', '#index-2')])], None])]) -def test_create_triple_index(): - # type, value, tid, main, index_key - env = Environment({ - 'index': [ - ('triple', 'foo; bar; baz', 'id1', '', None), - ('triple', 'Python; Sphinx; reST', 'id2', '', None), - ], - }) - index = IndexEntries(env).create_index(dummy_builder) +@pytest.mark.sphinx('dummy') +def test_create_triple_index(app): + app.env.indexentries.clear() + text = (".. index:: triple: foo; bar; baz\n" + ".. index:: triple: Python; Sphinx; reST\n") + restructuredtext.parse(app, text) + index = IndexEntries(app.env).create_index(app.builder) assert len(index) == 5 - assert index[0] == ('B', [('bar', [[], [('baz, foo', [('', '#id1')])], None]), - ('baz', [[], [('foo bar', [('', '#id1')])], None])]) - assert index[1] == ('F', [('foo', [[], [('bar baz', [('', '#id1')])], None])]) - assert index[2] == ('P', [('Python', [[], [('Sphinx reST', [('', '#id2')])], None])]) - assert index[3] == ('R', [('reST', [[], [('Python Sphinx', [('', '#id2')])], None])]) - assert index[4] == ('S', [('Sphinx', [[], [('reST, Python', [('', '#id2')])], None])]) + assert index[0] == ('B', [('bar', [[], [('baz, foo', [('', '#index-0')])], None]), + ('baz', [[], [('foo bar', [('', '#index-0')])], None])]) + assert index[1] == ('F', [('foo', [[], [('bar baz', [('', '#index-0')])], None])]) + assert index[2] == ('P', [('Python', [[], [('Sphinx reST', [('', '#index-1')])], None])]) + assert index[3] == ('R', [('reST', [[], [('Python Sphinx', [('', '#index-1')])], None])]) + assert index[4] == ('S', [('Sphinx', [[], [('reST, Python', [('', '#index-1')])], None])]) -def test_create_see_index(): - locale.init([], None) - - # type, value, tid, main, index_key - env = Environment({ - 'index': [ - ('see', 'docutils; reStructuredText', 'id1', '', None), - ('see', 'Python; interpreter', 'id2', '', None), - ('see', 'Sphinx; documentation tool', 'id3', '', None), - ], - }) - index = IndexEntries(env).create_index(dummy_builder) +@pytest.mark.sphinx('dummy') +def test_create_see_index(app): + app.env.indexentries.clear() + text = (".. index:: see: docutils; reStructuredText\n" + ".. index:: see: Python; interpreter\n" + ".. index:: see: Sphinx; documentation tool\n") + restructuredtext.parse(app, text) + index = IndexEntries(app.env).create_index(app.builder) assert len(index) == 3 assert index[0] == ('D', [('docutils', [[], [('see reStructuredText', [])], None])]) assert index[1] == ('P', [('Python', [[], [('see interpreter', [])], None])]) assert index[2] == ('S', [('Sphinx', [[], [('see documentation tool', [])], None])]) -def test_create_seealso_index(): - locale.init([], None) - - # type, value, tid, main, index_key - env = Environment({ - 'index': [ - ('seealso', 'docutils; reStructuredText', 'id1', '', None), - ('seealso', 'Python; interpreter', 'id2', '', None), - ('seealso', 'Sphinx; documentation tool', 'id3', '', None), - ], - }) - index = IndexEntries(env).create_index(dummy_builder) +@pytest.mark.sphinx('dummy') +def test_create_seealso_index(app): + app.env.indexentries.clear() + text = (".. index:: seealso: docutils; reStructuredText\n" + ".. index:: seealso: Python; interpreter\n" + ".. index:: seealso: Sphinx; documentation tool\n") + restructuredtext.parse(app, text) + index = IndexEntries(app.env).create_index(app.builder) assert len(index) == 3 assert index[0] == ('D', [('docutils', [[], [('see also reStructuredText', [])], None])]) assert index[1] == ('P', [('Python', [[], [('see also interpreter', [])], None])]) assert index[2] == ('S', [('Sphinx', [[], [('see also documentation tool', [])], None])]) -def test_create_index_by_key(): - # type, value, tid, main, index_key - env = Environment({ - 'index': [ - ('single', 'docutils', 'id1', '', None), - ('single', 'Python', 'id2', '', None), - ('single', 'スフィンクス', 'id3', '', 'ス'), - ], - }) - index = IndexEntries(env).create_index(dummy_builder) +@pytest.mark.sphinx('dummy') +def test_create_index_by_key(app): + app.env.indexentries.clear() + # At present, only glossary directive is able to create index key + text = (".. glossary::\n" + "\n" + " docutils\n" + " Python\n" + " スフィンクス : ス\n") + restructuredtext.parse(app, text) + index = IndexEntries(app.env).create_index(app.builder) assert len(index) == 3 - assert index[0] == ('D', [('docutils', [[('', '#id1')], [], None])]) - assert index[1] == ('P', [('Python', [[('', '#id2')], [], None])]) - assert index[2] == ('ス', [('スフィンクス', [[('', '#id3')], [], 'ス'])]) + assert index[0] == ('D', [('docutils', [[('main', '#term-docutils')], [], None])]) + assert index[1] == ('P', [('Python', [[('main', '#term-python')], [], None])]) + assert index[2] == ('ス', [('スフィンクス', [[('main', '#term')], [], 'ス'])])