Skip to content

Commit

Permalink
Add support for using places and decimal as a parameter for assert_al…
Browse files Browse the repository at this point in the history
…most_equal
  • Loading branch information
massich committed Dec 1, 2017
1 parent 9471ea7 commit cedca1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 8 additions & 1 deletion nose2pytest/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ def _transform_dest(self, assert_arg_test_node: PyNode, results: {str: PyNode})
wrapped_delta_val = wrap_parens_for_comparison(delta_val)
dest3.replace(wrapped_delta_val)

elif delta.children[0] == PyLeaf(token.NAME, 'places'):
delta_val = delta.children[2]
delta_val.prefix = "1e-"
wrapped_delta_val = wrap_parens_for_comparison(delta_val)
dest3.replace(wrapped_delta_val)

elif delta.children[0] == PyLeaf(token.NAME, 'msg'):
delta_val = results['msg'].children[2]
delta_val.prefix = ""
Expand Down Expand Up @@ -573,7 +579,6 @@ def get_fixers(self):

return pre_fixers, post_fixers


def setup():
# from nose import tools as nosetools
# import inspect
Expand All @@ -589,6 +594,8 @@ def setup():
help='disable overwriting of original files')
parser.add_argument('-v', dest='verbose', action='store_true',
help='verbose output (list files changed, etc)')
parser.add_argument('--version', action='version',
version='%(prog)s {0}'.format(__version__))

return parser.parse_args()

Expand Down
15 changes: 8 additions & 7 deletions tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,14 @@ def test_not_almost_equal(self):
'assert_not_almost_equals(123.456, 124, delta=0.6)',
'assert 123.456 != pytest.approx(124, abs=0.6)')

def test_ignore_places(self):
statement_in = 'assert_almost_equal(123.456, 123.5, 2)'
check_transformation(statement_in, statement_in)

statement_in = 'assert_almost_equal(123.456, 123.5, places=2)'
check_transformation(statement_in, statement_in)

def test_places(self):
check_transformation('assert_almost_equal(123.456, 124, places=1)',
'assert 123.456 == pytest.approx(124, abs=1e-1)')
check_transformation('assert_almost_equal(123.456, 124, places=7)',
'assert 123.456 == pytest.approx(124, abs=1e-7)')
check_passes(refac,
'assert_almost_equal(123.456, 123.450, places=1)',
'assert 123.456 == pytest.approx(123.450, abs=1e-1)')

class TestAssertTools:

Expand Down

0 comments on commit cedca1d

Please sign in to comment.