Skip to content
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

Add tests for docs and examples #68

Merged
merged 7 commits into from
Dec 29, 2020
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 docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ argument "`min`":

```{code-cell} python
---
tags: [raises-exception]
tags: [warns]
---
@magicgui(a_string={'min': 10})
def whoops(a_string: str = 'Hi there'):
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion magicgui/widgets/_concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ def _value_from_position(self, position):

def _position_from_value(self, value):
minv = math.log(self.min, self.base)
return (math.log(value, self.base) - minv) / self._scale + self._min_pos
pos = (math.log(value, self.base) - minv) / self._scale + self._min_pos
return int(pos)

@property
def base(self):
Expand Down
49 changes: 49 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import re
import runpy
from glob import glob
from pathlib import Path

import pytest

from magicgui import use_app


@pytest.mark.parametrize(
"fname",
[
f
for f in glob("docs/**/*.md", recursive=True)
if "_build" not in f and Path(f).read_text(encoding="utf-8").startswith("-")
],
)
def test_doc_code_cells(fname, globalns=globals()):
"""Make sure that all code cells in documentation perform as expected."""
text = Path(fname).read_text()
code_cells = re.findall(r"```{code-cell}[^\n]+\n(.*?)`{3}", text, re.S)
for cell in code_cells:
header = re.search(r"-{3}(.+?)-{3}", cell, re.S)
if header:
cell = cell.replace(header.group(), "")
if "warns" in header.group():
with pytest.warns(None):
exec(cell, globalns)
continue
if "raises-exception" in header.group():
with pytest.raises(Exception):
exec(cell, globalns)
continue
exec(cell, globalns)


@pytest.mark.parametrize(
"fname", [f for f in glob("examples/*.py") if "napari" not in f]
)
def test_examples(fname):
"""Make sure that all code cells in documentation perform as expected."""
app = use_app()
app.start_timer(0, app.quit)
if "OLD" in fname:
with pytest.warns(FutureWarning):
assert runpy.run_path(fname)
else:
assert runpy.run_path(fname)