Skip to content

Commit

Permalink
Format multiline locals by indenting all lines uniformly #92 (#93)
Browse files Browse the repository at this point in the history
* Format multiline locals by indenting all lines uniformly #92

* Add test for multiline reprs

This also includes a fix to test_assert_stmt_surrounding_lines. In
484d14a#diff-a48123d72539171bb8301baf301217bbR522
I made an erroneous fix that modified the expected number of lines of
context instead of modifying the expected line related to the
assertion. I really don't like the brittleness of this aspect of our
test suite.
  • Loading branch information
leifwalsh authored and thejunglejane committed Sep 23, 2018
1 parent 400b0d5 commit 5d04973
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 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
=========

* :feature:`92` Improve indentation of multiline locals
* :support:`90` Added support for python 3.7
* :support:`88` Document how to install with conda
* :release:`0.9.5 <2018-06-24>`
Expand Down
11 changes: 10 additions & 1 deletion marbles/core/marbles/core/marbles.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,18 @@ def formattedMsg(self): # mimic unittest's name for standardMsg
standardMsg=self.standardMsg, assert_stmt=self.assert_stmt,
note=self.note, locals=local_string, filename=self.filename)

@classmethod
def _format_local(cls, name, value):
value_str = repr(value)
if '\n' in value_str:
value_str = textwrap.indent(value_str, '\t\t')
return '\t{0} =\n{1}'.format(name, value_str)
else:
return '\t{0} = {1}'.format(name, value_str)

@classmethod
def _format_locals(cls, locals_):
return '\n'.join('\t{0}={1}'.format(k, v) for k, v in locals_.items())
return '\n'.join(cls._format_local(k, v) for k, v in locals_.items())

@staticmethod
def _find_assert_stmt(filename, linenumber, leading=1, following=2,
Expand Down
27 changes: 24 additions & 3 deletions marbles/core/tests/test_marbles.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ def test_locals(self):
foo = 'bar' # noqa: F841
self.assertTrue(False, note='some note about {foo!r}')

def test_multiline_locals(self):
class Record(object):
def __init__(self, **kwargs):
self.kwargs = kwargs

def __repr__(self):
return 'Record(\n{}\n)'.format('\n'.join(
' {key} = {value!r},'.format(key=key, value=value)
for key, value in self.kwargs.items()
))
foo = Record(a=1, b=2) # noqa: F841
self.assertTrue(False, note='some note')

def test_string_equality(self):
s = ''
self.assertEqual(s, s, note='some note')
Expand Down Expand Up @@ -480,6 +493,14 @@ def test_verify_annotation_locals(self):
e = ar.exception
self.assertEqual(e.note.strip(), "some note about 'bar'")

def test_multiline_locals_indentation(self):
'''Are locals with multiline reprs indented correctly?'''
with self.assertRaises(ContextualAssertionError) as ar:
self.case.test_multiline_locals()
e = ar.exception
self.assertIn("\n\t\t a = 1,\n",
e._format_locals(e.public_test_locals))

def test_assert_raises_without_msg(self):
'''Do we capture annotations properly for assertRaises?'''
with self.assertRaises(ContextualAssertionError) as ar:
Expand Down Expand Up @@ -538,16 +559,16 @@ def test_assert_stmt_indicates_line(self):

def test_assert_stmt_surrounding_lines(self):
'''Does _find_assert_stmt read surrounding lines from the file?'''
test_linenumber = 75
test_linenumber = 83
test_filename = os.path.abspath(__file__)
with self.assertRaises(ContextualAssertionError) as ar:
self.case.test_failure()
e = ar.exception
lines = e._find_assert_stmt(test_filename, test_linenumber)[0]
self.assertEqual(len(lines), 7)
self.assertEqual(len(lines), 3)
more_lines = e._find_assert_stmt(
test_filename, test_linenumber, 2, 5)[0]
self.assertEqual(len(more_lines), 11)
self.assertEqual(len(more_lines), 7)

def test_note_wrapping(self):
'''Do we wrap the note properly?'''
Expand Down

0 comments on commit 5d04973

Please sign in to comment.