Skip to content

Commit

Permalink
WIP test
Browse files Browse the repository at this point in the history
Signed-off-by: nstarman <[email protected]>
  • Loading branch information
nstarman committed Jan 17, 2024
1 parent 8705d7a commit d259d02
Showing 1 changed file with 97 additions and 35 deletions.
132 changes: 97 additions & 35 deletions sphinx_automodapi/tests/test_automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -77,35 +84,35 @@ 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)

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 = """
Expand All @@ -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 = """
Expand Down Expand Up @@ -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

0 comments on commit d259d02

Please sign in to comment.