diff --git a/sphinx_automodapi/tests/test_automodsumm.py b/sphinx_automodapi/tests/test_automodsumm.py index fe14e9f..a04a8d7 100644 --- a/sphinx_automodapi/tests/test_automodsumm.py +++ b/sphinx_automodapi/tests/test_automodsumm.py @@ -49,10 +49,17 @@ def teardown_function(func): def write_api_files_to_tmpdir(tmpdir): - apidir = tmpdir.mkdir('api') - with open(apidir.join('sphinx_automodapi.tests.example_module.mixed.add.rst').strpath, 'w') as f: + apidir = tmpdir.mkdir("api") + with open( + apidir.join("sphinx_automodapi.tests.example_module.mixed.add.rst").strpath, "w" + ) as f: f.write(ADD_RST) - with open(apidir.join('sphinx_automodapi.tests.example_module.mixed.MixedSpam.rst').strpath, 'w') as f: + with open( + apidir.join( + "sphinx_automodapi.tests.example_module.mixed.MixedSpam.rst" + ).strpath, + "w", + ) as f: f.write(MIXEDSPAM_RST) @@ -77,26 +84,24 @@ def write_api_files_to_tmpdir(tmpdir): def test_ams_to_asmry(tmpdir): - - with open(tmpdir.join('index.rst').strpath, 'w') as f: - f.write(ams_to_asmry_str.format(options='')) + with open(tmpdir.join("index.rst").strpath, "w") as f: + f.write(ams_to_asmry_str.format(options="")) write_api_files_to_tmpdir(tmpdir) run_sphinx_in_tmpdir(tmpdir) - with open(tmpdir.join('index.rst.automodsumm').strpath) as f: + with open(tmpdir.join("index.rst.automodsumm").strpath) as f: result = f.read() assert result == ams_to_asmry_expected def test_too_many_options(tmpdir, capsys): + ops = ["", ":classes-only:", ":functions-only:"] + ostr = "\n ".join(ops) - ops = ['', ':classes-only:', ':functions-only:'] - ostr = '\n '.join(ops) - - with open(tmpdir.join('index.rst').strpath, 'w') as f: + with open(tmpdir.join("index.rst").strpath, "w") as f: f.write(ams_to_asmry_str.format(options=ostr)) write_api_files_to_tmpdir(tmpdir) @@ -104,8 +109,10 @@ def test_too_many_options(tmpdir, capsys): run_sphinx_in_tmpdir(tmpdir, expect_error=True) stdout, stderr = capsys.readouterr() - assert ("[automodsumm] Defined more than one of functions-only, " - "classes-only, and variables-only. Skipping this directive." in stderr) + assert ( + "[automodsumm] Defined more than one of functions-only, " + "classes-only, and variables-only. Skipping this directive." in stderr + ) ORDEREDDICT_RST = """ @@ -121,39 +128,59 @@ def test_too_many_options(tmpdir, capsys): """.strip() -@pytest.mark.parametrize('options,expect', [ - ('', ['add', 'MixedSpam']), - (':allowed-package-names: sphinx_automodapi', ['add', 'MixedSpam']), - (':allowed-package-names: collections', ['OrderedDict']), - (':allowed-package-names: sphinx_automodapi,collections', - ['add', 'MixedSpam', 'OrderedDict']), -]) +@pytest.mark.parametrize( + "options,expect", + [ + ("", ["add", "MixedSpam"]), + (":allowed-package-names: sphinx_automodapi", ["add", "MixedSpam"]), + (":allowed-package-names: collections", ["OrderedDict"]), + ( + ":allowed-package-names: sphinx_automodapi,collections", + ["add", "MixedSpam", "OrderedDict"], + ), + ], +) def test_am_allowed_package_names(options, expect, tmpdir): """ Test that allowed_package_names is interpreted correctly. """ + def mixed2noall(s): - return s.replace('example_module.mixed', 'example_module.noall') + return s.replace("example_module.mixed", "example_module.noall") am_str = ams_to_asmry_str - with open(tmpdir.join('index.rst').strpath, 'w') as f: - f.write(mixed2noall(am_str).format(options=(' '+options if options else ''))) - - apidir = tmpdir.mkdir('api') - with open(apidir.join('sphinx_automodapi.tests.example_module.noall.add.rst').strpath, 'w') as f: + with open(tmpdir.join("index.rst").strpath, "w") as f: + f.write( + mixed2noall(am_str).format(options=(" " + options if options else "")) + ) + + apidir = tmpdir.mkdir("api") + with open( + apidir.join("sphinx_automodapi.tests.example_module.noall.add.rst").strpath, "w" + ) as f: f.write(mixed2noall(ADD_RST)) - with open(apidir.join('sphinx_automodapi.tests.example_module.noall.MixedSpam.rst').strpath, 'w') as f: + with open( + apidir.join( + "sphinx_automodapi.tests.example_module.noall.MixedSpam.rst" + ).strpath, + "w", + ) as f: f.write(mixed2noall(MIXEDSPAM_RST)) - with open(apidir.join('sphinx_automodapi.tests.example_module.noall.OrderedDict.rst').strpath, 'w') as f: + with open( + apidir.join( + "sphinx_automodapi.tests.example_module.noall.OrderedDict.rst" + ).strpath, + "w", + ) as f: f.write(ORDEREDDICT_RST) run_sphinx_in_tmpdir(tmpdir) - with open(tmpdir.join('index.rst.automodsumm').strpath) as f: + with open(tmpdir.join("index.rst.automodsumm").strpath) as f: result = f.read() for x in expect: - assert ' '+x in result + assert " " + x in result PILOT_RST = """ @@ -187,17 +214,52 @@ def mixed2noall(s): def test_ams_cython(tmpdir, cython_testpackage): # noqa - - with open(tmpdir.join('index.rst').strpath, 'w') as f: + with open(tmpdir.join("index.rst").strpath, "w") as f: f.write(ams_cython_str) - apidir = tmpdir.mkdir('api') - with open(apidir.join('apyhtest_eva.unit02.pilot.rst').strpath, 'w') as f: + apidir = tmpdir.mkdir("api") + with open(apidir.join("apyhtest_eva.unit02.pilot.rst").strpath, "w") as f: f.write(PILOT_RST) run_sphinx_in_tmpdir(tmpdir) - with open(tmpdir.join('index.rst.automodsumm').strpath) as f: + with open(tmpdir.join("index.rst.automodsumm").strpath) as f: result = f.read() assert result == ams_cython_expected + + +# ============================================================================= + +sorted_str = """ +Before + +.. automodsumm:: sphinx_automodapi.tests.example_module.classes + :sort: + +And After +""" + +sorted_expected = """\ +.. currentmodule:: sphinx_automodapi.tests.example_module.classes + +.. autosummary:: + + Egg + Spam + +""" + + +def test_sort(tmpdir): + with open(tmpdir.join("index.rst").strpath, "w") as f: + f.write(sorted_str) + + write_api_files_to_tmpdir(tmpdir) + + run_sphinx_in_tmpdir(tmpdir) + + with open(tmpdir.join("index.rst.automodsumm").strpath) as f: + result = f.read() + + assert result == sorted_expected