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

Allow float comparison in dicts #186

Merged
merged 3 commits into from
Sep 26, 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: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
0.12.1 (unreleased)
===================

- Allow floating point comparison in Python dictionary. [#186]

0.12.0 (2022-02-25)
===================

Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pytest_doctestplus/output_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_doctestplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down