Skip to content

Use bs4 for tests #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
SPHINX="${SPHINX}==${SPHINX_VERSION}";
JINJA2="${JINJA2}<3.1";
fi
pip install pytest pytest-cov codecov "${SPHINX}" "${JINJA2}" -e .
pip install pytest pytest-cov codecov "${SPHINX}" "${JINJA2}" beautifulsoup4 -e .
- name: Test with pytest
run: |
pytest --cov=autodocsumm --cov-report=xml tests
Expand Down
164 changes: 84 additions & 80 deletions tests/test_autodocsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
limitations under the License.
"""
import re
import bs4
import pytest
import sphinx

Expand All @@ -39,22 +40,34 @@ def get_html(app, fname):
return f.read()


def in_autosummary(what, html) -> bool:
soup = bs4.BeautifulSoup(html)
autosummaries = soup("table")
found = False
for tag in autosummaries:
if tag.find_all("span", string=what):
found = True
break
return found


class TestAutosummaryDocumenter:

def test_module(self, app):
app.build()
html = get_html(app, 'test_module.html')
assert '<span class="pre">TestClass</span>' in html
assert '<span class="pre">test_func</span>' in html
assert '<span class="pre">test_method</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert in_autosummary("TestClass", html)
assert in_autosummary("test_func", html)
assert in_autosummary("test_method", html)
assert in_autosummary("test_attr", html)

# test whether the right objects are included
assert '<span class="pre">class_caller</span>' in html
assert in_autosummary("class_caller", html)
assert 'Caller docstring for class attribute' in html

# test whether the data is shown correctly
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">small_data</span>' in html
assert in_autosummary("large_data", html)
assert in_autosummary("small_data", html)

try:
assert 'Should be included' in html
Expand Down Expand Up @@ -94,26 +107,17 @@ def test_module_no_nesting(self, app):
app.build()
html = get_html(app, 'test_module_no_nesting.html')

assert '<span class="pre">TestClass</span>' in html
assert '<span class="pre">test_func</span>' in html
assert in_autosummary("TestClass", html)
assert in_autosummary("test_func", html)

# test whether the data is shown correctly
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">small_data</span>' in html
assert in_autosummary("large_data", html)
assert in_autosummary("small_data", html)

# test that elements of TestClass are not autosummarized, since nesting is disabled.
try:
assert '<span class="pre">test_method</span>' not in html
assert '<span class="pre">test_attr</span>' not in html
except AssertionError: # sphinx>=3.5
found_methods = re.findall(
'<span class="pre">test_method</span>', html
)
assert len(found_methods) == 1
found_attrs = re.findall(
'<span class="pre">test_attr</span>', html
)
assert len(found_attrs) == 1
# test that elements of TestClass are not autosummarized,
# since nesting is disabled.
assert not in_autosummary("test_method", html)
assert not in_autosummary("test_attr", html)

# test the members are still displayed
assert re.search(
Expand All @@ -124,12 +128,12 @@ def test_module_no_nesting(self, app):
def test_module_summary_only(self, app):
app.build()
html = get_html(app, 'test_module_summary_only.html')
assert '<span class="pre">TestClass</span>' in html
assert '<span class="pre">test_func</span>' in html
assert in_autosummary("TestClass", html)
assert in_autosummary("test_func", html)

# test whether the data is shown correctly
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">small_data</span>' in html
assert in_autosummary("large_data", html)
assert in_autosummary("small_data", html)

assert not re.search(
r'<dt( class=".*")? id="dummy.Class_CallTest"( class=".*")*>',
Expand All @@ -139,18 +143,18 @@ def test_module_summary_only(self, app):
def test_module_with_title(self, app):
app.build()
html = get_html(app, 'test_module_title.html')
assert '<span class="pre">TestClass</span>' in html
assert '<span class="pre">test_func</span>' in html
assert '<span class="pre">test_method</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert in_autosummary("TestClass", html)
assert in_autosummary("test_func", html)
assert in_autosummary("test_method", html)
assert in_autosummary("test_attr", html)

# test whether the right objects are included
assert '<span class="pre">class_caller</span>' in html
assert in_autosummary("class_caller", html)
assert 'Caller docstring for class attribute' in html

# test whether the data is shown correctly
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">small_data</span>' in html
assert in_autosummary("large_data", html)
assert in_autosummary("small_data", html)
try:
assert 'Should be included' in html
except AssertionError: # sphinx>=3.5
Expand Down Expand Up @@ -188,12 +192,12 @@ def test_module_nosignatures(self, app):
app.build()

html = get_html(app, 'test_module_nosignatures.html')
assert '<span class="pre">TestClass</span>' in html
assert '<span class="pre">test_func</span>' in html
assert in_autosummary("TestClass", html)
assert in_autosummary("test_func", html)

# test whether the data is shown correctly
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">small_data</span>' in html
assert in_autosummary("large_data", html)
assert in_autosummary("small_data", html)

assert not re.search(
r'<dt( class=".*")? id="dummy.Class_CallTest"( class=".*")*>',
Expand All @@ -206,15 +210,15 @@ def test_class(self, app):
html = get_html(app, '/test_class.html')

if sphinx_version[:2] > [3, 1]:
assert '<span class="pre">instance_attribute</span>' in html
assert in_autosummary("instance_attribute", html)
elif sphinx_version[:2] < [3, 1]:
assert (
'<span class="pre">dummy.TestClass.instance_attribute</span>'
in html
)

assert '<span class="pre">test_method</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert in_autosummary("test_method", html)
assert in_autosummary("test_attr", html)

# test escaping of *
assert r'\*args' not in html
Expand All @@ -223,12 +227,12 @@ def test_class(self, app):
assert '**kwargs' in html

# test whether the right objects are included
assert '<span class="pre">class_caller</span>' in html
assert in_autosummary("class_caller", html)
assert 'Caller docstring for class attribute' in html

# test whether the data is shown correctly
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">small_data</span>' in html
assert in_autosummary("large_data", html)
assert in_autosummary("small_data", html)

assert 'Should be skipped' not in html
try:
Expand Down Expand Up @@ -261,15 +265,15 @@ def test_class_order(self, app):
html = get_html(app, '/test_class_order.html')

if sphinx_version[:2] > [3, 1]:
assert '<span class="pre">instance_attribute</span>' in html
assert in_autosummary("instance_attribute", html)
elif sphinx_version[:2] < [3, 1]:
assert (
'<span class="pre">dummy.TestClass.instance_attribute</span>'
in html
)

assert '<span class="pre">test_attr</span>' in html
assert '<span class="pre">large_data</span>' in html
assert in_autosummary("test_attr", html)
assert in_autosummary("large_data", html)

assert (
html.index('<span class="pre">test_attr</span>')
Expand All @@ -281,22 +285,22 @@ def test_class_summary_only(self, app):
html = get_html(app, '/test_class_summary_only.html')

if sphinx_version[:2] > [3, 1]:
assert '<span class="pre">instance_attribute</span>' in html
assert in_autosummary("instance_attribute", html)
elif sphinx_version[:2] < [3, 1]:
assert (
'<span class="pre">dummy.TestClass.instance_attribute</span>'
in html
)

assert '<span class="pre">test_method</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert in_autosummary("test_method", html)
assert in_autosummary("test_attr", html)

# test whether the right objects are included
assert '<span class="pre">class_caller</span>' in html
assert in_autosummary("class_caller", html)

# test whether the data is shown correctly
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">small_data</span>' in html
assert in_autosummary("large_data", html)
assert in_autosummary("small_data", html)

assert not re.search(
r'<dt( class=".*")? id="dummy.TestClass.small_data"( class=".*")*>',
Expand All @@ -308,22 +312,22 @@ def test_class_nosignatures(self, app):
html = get_html(app, '/test_class_nosignatures.html')

if sphinx_version[:2] > [3, 1]:
assert '<span class="pre">instance_attribute</span>' in html
assert in_autosummary("instance_attribute", html)
elif sphinx_version[:2] < [3, 1]:
assert (
'<span class="pre">dummy.TestClass.instance_attribute</span>'
in html
)

assert '<span class="pre">test_method</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert in_autosummary("test_method", html)
assert in_autosummary("test_attr", html)

# test whether the right objects are included
assert '<span class="pre">class_caller</span>' in html
assert in_autosummary("class_caller", html)

# test whether the data is shown correctly
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">small_data</span>' in html
assert in_autosummary("large_data", html)
assert in_autosummary("small_data", html)

assert not re.search(
r'<dt( class=".*")? id="dummy.TestClass.small_data"( class=".*")*>',
Expand All @@ -335,7 +339,7 @@ def test_class_nosignatures(self, app):
def test_inherited(self, app):
app.build()
html = get_html(app, '/test_inherited.html')
assert '<span class="pre">test_method</span>' in html
assert in_autosummary("test_method", html)

@pytest.mark.xfail
def test_warnings_depreciation(self, app):
Expand Down Expand Up @@ -376,8 +380,8 @@ def test_autoclasssumm(self, app):
assert "Class test for autosummary" not in html

# test if the methods and attributes are there in a table
assert '<span class="pre">test_method</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert in_autosummary("test_method", html)
assert in_autosummary("test_attr", html)

def test_autoclasssumm_no_titles(self, app):
"""Test building the autosummary of a class."""
Expand All @@ -389,8 +393,8 @@ def test_autoclasssumm_no_titles(self, app):
assert "Class test for autosummary" not in html

# test if the methods and attributes are there in a table
assert '<span class="pre">test_method</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert in_autosummary("test_method", html)
assert in_autosummary("test_attr", html)

assert "<strong>Methods</strong>" not in html

Expand All @@ -404,9 +408,9 @@ def test_autoclasssumm_some_sections(self, app):
assert "Class test for autosummary" not in html

# test if the methods and attributes are there in a table
assert '<span class="pre">test_method</span>' not in html
assert '<span class="pre">class_caller</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert not in_autosummary("test_method", html)
assert in_autosummary("class_caller", html)
assert in_autosummary("test_attr", html)

def test_autoclasssumm_nosignatures(self, app):
"""Test building the autosummary of a class without signatures."""
Expand All @@ -418,8 +422,8 @@ def test_autoclasssumm_nosignatures(self, app):
assert "Class test for autosummary" not in html

# test if the methods and attributes are there in a table
assert '<span class="pre">test_method</span>' in html
assert '<span class="pre">test_attr</span>' in html
assert in_autosummary("test_method", html)
assert in_autosummary("test_attr", html)

assert '()' not in html

Expand All @@ -433,9 +437,9 @@ def test_automodulesumm(self, app):
assert "Module for testing the autodocsumm" not in html

# test if the classes, data and functions are there in a table
assert '<span class="pre">Class_CallTest</span>' in html
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">test_func</span>' in html
assert in_autosummary("Class_CallTest", html)
assert in_autosummary("large_data", html)
assert in_autosummary("test_func", html)

def test_automodulesumm_some_sections(self, app):
"""Test building the autosummary of a module with some sections only."""
Expand All @@ -447,9 +451,9 @@ def test_automodulesumm_some_sections(self, app):
assert "Module for testing the autodocsumm" not in html

# test if the classes, data and functions are there in a table
assert '<span class="pre">Class_CallTest</span>' not in html
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">test_func</span>' in html
assert not in_autosummary("Class_CallTest", html)
assert in_autosummary("large_data", html)
assert in_autosummary("test_func", html)

def test_automodulesumm_nosignatures(self, app):
"""Test building the autosummary of a module without signatures."""
Expand All @@ -461,9 +465,9 @@ def test_automodulesumm_nosignatures(self, app):
assert "Module for testing the autodocsumm" not in html

# test if the classes, data and functions are there in a table
assert '<span class="pre">Class_CallTest</span>' in html
assert '<span class="pre">large_data</span>' in html
assert '<span class="pre">test_func</span>' in html
assert in_autosummary("Class_CallTest", html)
assert in_autosummary("large_data", html)
assert in_autosummary("test_func", html)

assert '()' not in html

Expand All @@ -472,4 +476,4 @@ def test_empty(self, app):

html = get_html(app, '/test_empty.html')

assert '<span class="pre">product</span>' not in html
assert not in_autosummary("product", html)