Skip to content

Commit

Permalink
Handle pass-through assertion methods that are just forwarding parame…
Browse files Browse the repository at this point in the history
…ters

This deals with the "deprecated assertion names" like assertEquals forwarding to assertEqual, and possibly
others like them.

Closes #85
  • Loading branch information
leifwalsh committed Jul 7, 2018
1 parent 4078af4 commit 229aa52
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Changelog
=========

* :bug:`85` Fixed marbles handling of deprecated assertEquals and similar methods
* :release:`0.9.5 <2018-06-24>`
* :support:`80` Added support for ``pandas<0.24``
* :bug:`58` Fixed test failure on OSX
Expand Down
14 changes: 12 additions & 2 deletions marbles/core/marbles/core/marbles.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,19 @@ def _find_msg_argument(signature):
The index of the ``msg`` param, the default value for it,
and the number of non-``msg`` positional parameters we expect.
'''
names = signature.parameters.keys()
names = list(signature.parameters.keys())
if len(names) == 2:
param_kinds = [signature.parameters[name].kind for name in names]
if (param_kinds[0] == inspect.Parameter.VAR_POSITIONAL
and param_kinds[1] == inspect.Parameter.VAR_KEYWORD):
# This is likely an assertion wrapper like
# assertEquals(*args, **kwargs), in which case we should
# just forward the arguments along and catch them in the
# wrapped assertion method (see
# https://github.com/twosigma/marbles/issue/85).
return sys.maxsize, None, len(names)
try:
msg_idx = list(names).index('msg')
msg_idx = names.index('msg')
default_msg = signature.parameters['msg'].default
except ValueError: # 'msg' is not in list
# It's likely that this is a custom assertion that's just
Expand Down
29 changes: 28 additions & 1 deletion marbles/core/tests/test_marbles.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ def test_success(self):
def test_failure(self):
self.assertTrue(False, note='some note')

def test_deprecated_assertEquals_success(self):
x = 1
y = 1
self.assertEquals(x, y)

def test_deprecated_assertEquals_failure(self):
x = 1
y = 2
self.assertEquals(x, y)

def test_fail_without_msg_without_note(self):
self.fail()

Expand Down Expand Up @@ -345,6 +355,23 @@ def test_annotated_assertion_error_raised(self):
with self.assertRaises(ContextualAssertionError):
self.case.test_failure()

def test_deprecated_assertEquals_success(self):
'''Does the deprecated assertEquals method still work?'''
if self._use_annotated_test_case:
with self.assertRaises(AnnotationError):
self.case.test_deprecated_assertEquals_success()
else:
self.case.test_deprecated_assertEquals_success()

def test_deprecated_assertEquals_failure(self):
'''Does the deprecated assertEquals method work on failure?'''
if self._use_annotated_test_case:
with self.assertRaises(AnnotationError):
self.case.test_deprecated_assertEquals_failure()
else:
with self.assertRaises(ContextualAssertionError):
self.case.test_deprecated_assertEquals_failure()

def test_fail_handles_note_properly(self):
'''Does TestCase.fail() deal with note the right way?'''
if self._use_annotated_test_case:
Expand Down Expand Up @@ -515,7 +542,7 @@ def test_get_stack(self):
self.assertEqual(e.filename, os.path.abspath(__file__))
# This isn't great because I have to change it every time I
# add/remove imports but oh well
self.assertEqual(e.linenumber, 211)
self.assertEqual(e.linenumber, 221)

def test_assert_stmt_indicates_line(self):
'''Does e.assert_stmt indicate the line from the source code?'''
Expand Down

0 comments on commit 229aa52

Please sign in to comment.