Skip to content

Commit

Permalink
Merge pull request #186 from hamogu/dicts
Browse files Browse the repository at this point in the history
Allow float comparison in dicts
  • Loading branch information
pllim authored Sep 26, 2022
2 parents 64d38f2 + 270bb79 commit 6fbe8cf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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

0 comments on commit 6fbe8cf

Please sign in to comment.