Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
massich committed Aug 24, 2017
1 parent 304d875 commit 4aef66a
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions nose2pytest/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,58 @@ def _transform_dest(self, assert_arg_test_node: PyNode, results: {str: PyNode})

return True

class FixAssertAlmostEq_v3(FixAssertBase):
"""
Fixer class for any 3-argument assertion function (assert_func(a, b, c)). It supports optional fourth arg
as the assertion message, ie assert_func(a, b, c, msg) -> assert a op b op c, msg.
"""

PATTERN = PATTERN_ALMOST_ARGS

# The args node paths are the same for every assert function: the first tuple is for
# arg a, the second for arg b, the third for arg c (delta).
DEFAULT_ARG_PATHS = ((0, 1, 1, 0), (0, 1, 1, 2), 2)

conversions = dict(
assert_almost_equal='a == pytest.approx(b, abs=delta)',
assert_almost_equals='a == pytest.approx(b, abs=delta)',
assert_not_almost_equal='a != pytest.approx(b, abs=delta)',
assert_not_almost_equals='a != pytest.approx(b, abs=delta)',
)

@override(FixAssertBase)
def _transform_dest(self, assert_arg_test_node: PyNode, results: {str: PyNode}) -> bool:
delta = results["delta"].clone()
if not delta.children:
return False

aaa = results["aaa"].clone()
bbb = results["bbb"].clone()

dest1 = self._get_node(assert_arg_test_node, self._arg_paths[0])
new_aaa = wrap_parens_for_addsub(aaa)
dest1.replace(new_aaa)
adjust_prefix_first_arg(new_aaa, results["aaa"].prefix)

dest2 = self._get_node(assert_arg_test_node, self._arg_paths[1])
dest2.replace(wrap_parens_for_addsub(bbb))

dest3 = self._get_node(assert_arg_test_node, self._arg_paths[2])
if delta.children[0] == PyLeaf(token.NAME, 'delta'):
delta_val = delta.children[2]
delta_val.prefix = " "
dest3.replace(wrap_parens_for_comparison(delta_val))

elif delta.children[0] == PyLeaf(token.NAME, 'msg'):
delta_val = results['msg'].children[2]
delta_val.prefix = " "
dest3.replace(wrap_parens_for_comparison(delta_val))
results['msg'] = delta

else:
return False

return True

# ------------ Main portion of script -------------------------------

Expand Down

0 comments on commit 4aef66a

Please sign in to comment.