|
| 1 | +import unittest |
| 2 | + |
| 3 | +from coalib.core.ProjectBear import ProjectBear |
| 4 | +from coalib.core.Core import run |
| 5 | +from coalib.settings.Section import Section |
| 6 | + |
| 7 | + |
| 8 | +class TestProjectBear(ProjectBear): |
| 9 | + |
| 10 | + def analyze(self, files): |
| 11 | + yield '\n'.join(filename + ':' + str(files[filename]) |
| 12 | + for filename in sorted(files)) |
| 13 | + |
| 14 | + |
| 15 | +class TestProjectBearWithParameters(ProjectBear): |
| 16 | + |
| 17 | + def analyze(self, files, prefix: str='---'): |
| 18 | + yield '\n'.join(prefix + filename + ':' + str(files[filename]) |
| 19 | + for filename in sorted(files)) |
| 20 | + |
| 21 | + |
| 22 | +class ProjectBearTest(unittest.TestCase): |
| 23 | + |
| 24 | + def assertResultsEqual(self, bear_type, expected, |
| 25 | + section=None, file_dict=None): |
| 26 | + """ |
| 27 | + Asserts whether the expected results do match the output of the bear. |
| 28 | +
|
| 29 | + Asserts for the results out-of-order. |
| 30 | +
|
| 31 | + :param bear_type: |
| 32 | + The bear class to check. |
| 33 | + :param expected: |
| 34 | + A sequence of expected results. |
| 35 | + :param section: |
| 36 | + A section for the bear to use. By default uses a new section with |
| 37 | + name ``test-section``. |
| 38 | + :param file_dict: |
| 39 | + A file-dictionary for the bear to use. By default uses an empty |
| 40 | + dictionary. |
| 41 | + """ |
| 42 | + if section is None: |
| 43 | + section = Section('test-section') |
| 44 | + if file_dict is None: |
| 45 | + file_dict = {} |
| 46 | + |
| 47 | + uut = bear_type(section, file_dict) |
| 48 | + |
| 49 | + results = [] |
| 50 | + |
| 51 | + def on_result(result): |
| 52 | + results.append(result) |
| 53 | + |
| 54 | + run({uut}, on_result) |
| 55 | + |
| 56 | + self.assertEqual(sorted(expected), sorted(results)) |
| 57 | + |
| 58 | + def test_bear_without_parameters(self): |
| 59 | + self.assertResultsEqual( |
| 60 | + TestProjectBear, |
| 61 | + file_dict={}, |
| 62 | + expected=['']) |
| 63 | + self.assertResultsEqual( |
| 64 | + TestProjectBear, |
| 65 | + file_dict={'fileX': []}, |
| 66 | + expected=['fileX:[]']) |
| 67 | + self.assertResultsEqual( |
| 68 | + TestProjectBear, |
| 69 | + file_dict={'fileX': [], 'fileY': ['hello']}, |
| 70 | + expected=["fileX:[]\nfileY:['hello']"],) |
| 71 | + self.assertResultsEqual( |
| 72 | + TestProjectBear, |
| 73 | + file_dict={'fileX': [], 'fileY': ['hello'], 'fileZ': ['x\ny']}, |
| 74 | + expected=["fileX:[]\nfileY:['hello']\nfileZ:['x\\ny']"]) |
| 75 | + |
| 76 | + def test_bear_with_parameters_but_keep_defaults(self): |
| 77 | + self.assertResultsEqual( |
| 78 | + TestProjectBearWithParameters, |
| 79 | + file_dict={}, |
| 80 | + expected=['']) |
| 81 | + self.assertResultsEqual( |
| 82 | + TestProjectBearWithParameters, |
| 83 | + file_dict={'fileX': []}, |
| 84 | + expected=['---fileX:[]']) |
| 85 | + self.assertResultsEqual( |
| 86 | + TestProjectBearWithParameters, |
| 87 | + file_dict={'fileX': [], 'fileY': ['hello']}, |
| 88 | + expected=["---fileX:[]\n---fileY:['hello']"], ) |
| 89 | + self.assertResultsEqual( |
| 90 | + TestProjectBearWithParameters, |
| 91 | + file_dict={'fileX': [], 'fileY': ['hello'], 'fileZ': ['x\ny']}, |
| 92 | + expected=["---fileX:[]\n---fileY:['hello']\n---fileZ:['x\\ny']"]) |
| 93 | + |
| 94 | + def test_bear_with_parameters(self): |
| 95 | + section = Section('test-section') |
| 96 | + section['prefix'] = '___' |
| 97 | + |
| 98 | + self.assertResultsEqual( |
| 99 | + TestProjectBearWithParameters, |
| 100 | + section=section, |
| 101 | + file_dict={}, |
| 102 | + expected=['']) |
| 103 | + self.assertResultsEqual( |
| 104 | + TestProjectBearWithParameters, |
| 105 | + section=section, |
| 106 | + file_dict={'fileX': []}, |
| 107 | + expected=['___fileX:[]']) |
| 108 | + self.assertResultsEqual( |
| 109 | + TestProjectBearWithParameters, |
| 110 | + section=section, |
| 111 | + file_dict={'fileX': [], 'fileY': ['hello']}, |
| 112 | + expected=["___fileX:[]\n___fileY:['hello']"], ) |
| 113 | + self.assertResultsEqual( |
| 114 | + TestProjectBearWithParameters, |
| 115 | + section=section, |
| 116 | + file_dict={'fileX': [], 'fileY': ['hello'], 'fileZ': ['x\ny']}, |
| 117 | + expected=["___fileX:[]\n___fileY:['hello']\n___fileZ:['x\\ny']"]) |
0 commit comments