diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index e3867bd9f..df4b940e0 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -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'): diff --git a/examples/forward_refs.py b/examples/napari_forward_refs.py similarity index 100% rename from examples/forward_refs.py rename to examples/napari_forward_refs.py diff --git a/magicgui/widgets/_concrete.py b/magicgui/widgets/_concrete.py index e26b6152e..1c27aa41b 100644 --- a/magicgui/widgets/_concrete.py +++ b/magicgui/widgets/_concrete.py @@ -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): diff --git a/tests/test_docs.py b/tests/test_docs.py new file mode 100644 index 000000000..e9ffa6b2c --- /dev/null +++ b/tests/test_docs.py @@ -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)