Skip to content

Commit

Permalink
LocalBearTestHelper: Add assertObjectsEqual
Browse files Browse the repository at this point in the history
Add ``assertObjectsEqual(..)`` method that compares individual
fields of the Result object and yields better, easy to understand
messages in case of an attribute mismatch.

Closes coala#4302
  • Loading branch information
yash-nisar committed Feb 2, 2018
1 parent 1d9eb29 commit 14642d8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
38 changes: 26 additions & 12 deletions coalib/testing/LocalBearTestHelper.py
Expand Up @@ -8,6 +8,7 @@
from coalib.bearlib.abstractions.LinterClass import LinterClass
from coalib.testing.BearTestHelper import generate_skip_decorator
from coalib.bears.LocalBear import LocalBear
from coala_utils.Comparable import Comparable
from coala_utils.ContextManagers import prepare_file
from coalib.settings.Section import Section
from coalib.settings.Setting import Setting
Expand Down Expand Up @@ -95,6 +96,28 @@ class LocalBearTestHelper(unittest.TestCase):
If you miss some methods, get in contact with us, we'll be happy to help!
"""

def assertComparableObjectsEqual(self, observed_result, expected_result):
if len(observed_result) == len(expected_result):
messages = ''
for observed, expected in zip(observed_result, expected_result):
if (isinstance(observed, Comparable)
and isinstance(expected, Comparable)) and (
type(observed) is type(expected)):
for attribute in type(observed).__compare_fields__:
try:
self.assertEqual(
getattr(observed, attribute),
getattr(expected, attribute),
msg='{} mismatch.'.format(attribute))
except AssertionError as ex:
messages += (str(ex) + '\n\n')
else:
self.assertEqual(observed_result, expected_result)
if messages:
raise AssertionError(messages)
else:
self.assertEqual(observed_result, expected_result)

def check_validity(self,
local_bear,
lines,
Expand Down Expand Up @@ -212,26 +235,17 @@ def check_results(self,
list,
msg='The given results are not a list.')

if results in [[], ()]:
msg = ("The local bear '{}' yields a result although it "
"shouldn't.".format(local_bear.__class__.__name__))
check_order = True
else:
msg = ("The local bear '{}' doesn't yield the right results."
.format(local_bear.__class__.__name__))
if check_order:
msg += ' Or the order may be wrong.'

bear_output = get_results(local_bear, lines,
filename=filename,
force_linebreaks=force_linebreaks,
create_tempfile=create_tempfile,
tempfile_kwargs=tempfile_kwargs,
settings=settings)
if not check_order:
self.assertEqual(sorted(bear_output), sorted(results), msg=msg)
self.assertComparableObjectsEqual(
sorted(bear_output), sorted(results))
else:
self.assertEqual(bear_output, results, msg=msg)
self.assertComparableObjectsEqual(bear_output, results)

return bear_output

Expand Down
53 changes: 53 additions & 0 deletions tests/testing/LocalBearTestHelperTest.py
Expand Up @@ -7,6 +7,10 @@
TestDepBearCDependsB,
TestDepBearDependsAAndAA)
from coalib.bearlib.abstractions.Linter import linter
from tests.test_bears.LineCountTestBear import LineCountTestBear
from coala_utils.ContextManagers import prepare_file
from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.settings.Section import Section
from coalib.settings.Setting import Setting
from coalib.testing.LocalBearTestHelper import verify_local_bear, execute_bear
Expand Down Expand Up @@ -39,6 +43,55 @@ def test_require_order(self):
self.check_results(self.uut, ['a', 'b'], ['b', 'a'],
check_order=True)

def test_result_inequality(self):
with self.assertRaises(AssertionError):
self.check_results(self.uut, ['a', 'b'], ['a', 'b', None],
check_order=True)

def test_good_assertComparableObjectsEqual(self):
self.uut = LineCountTestBear(Section('name'), Queue())
file_content = 'a\nb\nc'
with prepare_file(file_content.splitlines(), filename=None,
create_tempfile=True) as (file, fname):
self.check_results(self.uut,
file_content.splitlines(),
[Result.from_values(
origin='LineCountTestBear',
message='This file has 3 lines.',
severity=RESULT_SEVERITY.INFO,
file=fname)],
filename=fname,
create_tempfile=False)

def test_bad_assertComparableObjectsEqual(self):
with self.assertRaises(AssertionError) as cm:
self.uut = LineCountTestBear(Section('name'), Queue())
file_content = 'a\nb\nc'
with prepare_file(file_content.splitlines(), filename=None,
create_tempfile=True) as (file, fname):
self.check_results(self.uut,
file_content.splitlines(),
[Result.from_values(
origin='LineCountTestBea',
message='This file has 2 lines.',
severity=RESULT_SEVERITY.INFO,
file=fname)],
filename=fname,
create_tempfile=False)
self.assertEqual('\'LineCountTestBear\' != \'LineCountTestBea\'\n'
'- LineCountTestBear\n'
'? -\n'
'+ LineCountTestBea\n'
' : origin mismatch.\n\n'
'\'This file has 3 lines.\' != \'This file has 2 '
'lines.\'\n'
'- This file has 3 lines.\n'
'? ^\n'
'+ This file has 2 lines.\n'
'? ^\n'
' : message_base mismatch.\n\n',
str(cm.exception))


class LocalBearTestCheckLineResultCountTest(Helper):

Expand Down

0 comments on commit 14642d8

Please sign in to comment.