Skip to content

Commit

Permalink
Add basic doctest for format_number
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Bechberger committed Jul 16, 2019
1 parent 9a878ca commit 85d1d7e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ matrix:
install:
- pip install --upgrade pip
- pip3 install -e .
script: pytest
script: ./test.sh
- language: python
python: "3.7"
dist: xenial
install:
- pip install --upgrade pip
- pip3 install -e .
script:
- pytest
- TEMCI_TEST_CMD=1 pytest
- ./test.sh
- TEMCI_TEST_CMD=1 ./test.sh
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ Install temci via `pip` and run the tests via

.. code:: sh
pytest tests
./test.sh
The tests can be found in the `tests` folder and use the pytest framework.

Expand Down
6 changes: 6 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import py
from py.path import local


def pytest_ignore_collect(path: py.path.local):
return "misc" in str(path) or "/doc/" in str(path) or "library_init" in str(path) or "cli" in str(path)
4 changes: 3 additions & 1 deletion doc/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ Testing
The tests are located in the ``tests`` folder and roughly grouped by the temci subcommand they belong to.
New features should by covered by tests.

There is also support for doctests that can be added into the documentation.

The tests are using the pytest framework and can be executed by simply calling

.. code:: sh
pytest
./test.sh
It recommended to install the package ``pytest-clarity`` to improve the error output.
18 changes: 15 additions & 3 deletions temci/utils/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,34 @@ def init_settings(cls, new_settings: t.Dict[str, t.Union[int, bool]]):

def format_number(number: Number, deviation: float = 0.0,
parentheses: bool = True, explicit_deviation: bool = False,
is_deviation_absolute: bool = False,
is_deviation_absolute: bool = True,
min_decimal_places: int = 3,
max_decimal_places: t.Optional[int] = None,
omit_insignificant_decimal_places: bool = True,
scientific_notation: bool = False,
scientific_notation_steps: int = 3,
scientific_notation_decimal_places: int = None,
scientific_notation_si_prefixes: bool = True,
force_min_decimal_places: bool = False,
force_min_decimal_places: bool = True,
relative_to_deviation: bool = False,
sigmas: int = 2,
parentheses_mode: ParenthesesMode = ParenthesesMode.ORDER_OF_MAGNITUDE
) -> str:
"""
Format the passed number
>>> format_number(1.0, 0.5)
'1.(000)'
>>> format_number(1.56, 0.005)
'1.56(0)'
>>> format_number(1560, scientific_notation=True)
'1.560k'
>>> format_number(1560, scientific_notation_si_prefixes=False, scientific_notation=True)
'1.560e3'
:param number: formatted number
:param deviation: standard deviation associated with the number
:param parentheses: show parentheses around non significant digits?
Expand Down Expand Up @@ -208,7 +220,7 @@ def _format_number(number: Number, deviation: float,
min_decimal_places: int = 3,
max_decimal_places: t.Optional[int] = None,
omit_insignificant_decimal_places: bool = True,
force_min_decimal_places: bool = False,
force_min_decimal_places: bool = True,
relative_to_deviation: bool = False,
scientific_notation: bool = False,
scientific_notation_si_prefixes: bool = True,
Expand Down
5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/sh

cd "$(dirname "$0")"
pytest --doctest-modules --doctest-glob='*.rst'

4 changes: 4 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ def test_config_default_values():
"runs": 11
}
}).out


def test_format():
assert run_temci("format 1.001 0.05").out == "1.0(01)"

0 comments on commit 85d1d7e

Please sign in to comment.