diff --git a/CHANGES.rst b/CHANGES.rst index 59f2e62..554ba9d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,8 @@ 0.12.1 (unreleased) =================== +- Allow floating point comparison in Python dictionary. [#186] + 0.12.0 (2022-02-25) =================== diff --git a/README.rst b/README.rst index ef57e0c..6263734 100644 --- a/README.rst +++ b/README.rst @@ -144,6 +144,11 @@ To address this issue, the ``pytest-doctestplus`` plugin provides support for a >>> 1.0 / 3.0 # doctest: +FLOAT_CMP 0.333333333333333311 +.. code-block:: python + + >>> {'a': 1 / 3., 'b': 2 / 3.} # doctest: +FLOAT_CMP + {'a': 0.333333, 'b': 0.666666} + When this flag is used, the expected and actual outputs are both parsed to find any floating point values in the strings. Those are then converted to actual Python `float` objects and compared numerically. This means that small diff --git a/pytest_doctestplus/output_checker.py b/pytest_doctestplus/output_checker.py index 511503f..ca6345f 100644 --- a/pytest_doctestplus/output_checker.py +++ b/pytest_doctestplus/output_checker.py @@ -70,7 +70,7 @@ def __init__(self): want_floats = got_floats + r'(\.{3})?' front_sep = r'\s|[*+-,<=(\[]' - back_sep = front_sep + r'|[>j)\]]' + back_sep = front_sep + r'|[>j)\]}]' fbeg = r'^{}(?={}|$)'.format(got_floats, back_sep) fmidend = r'(?<={}){}(?={}|$)'.format(front_sep, got_floats, back_sep) diff --git a/tests/test_doctestplus.py b/tests/test_doctestplus.py index 571a955..5924808 100644 --- a/tests/test_doctestplus.py +++ b/tests/test_doctestplus.py @@ -112,6 +112,29 @@ def g(): reprec.assertoutcome(failed=0, passed=1) +def test_float_cmp_dict(testdir): + testdir.makeini( + """ + [pytest] + doctest_optionflags = ELLIPSIS + doctestplus = enabled + """ + ) + p = testdir.makepyfile( + """ + def g(): + ''' + >>> x = {'a': 1/3., 'b': 2/3.} + >>> x # doctest: +FLOAT_CMP + {'a': 0.333333, 'b': 0.666666} + ''' + pass + """ + ) + reprec = testdir.inline_run(p, "--doctest-plus") + reprec.assertoutcome(failed=0, passed=1) + + def test_float_cmp_global(testdir): testdir.makeini(""" [pytest]