Ejemplo n.º 1
0
    def run_bear(self,
                 filename,
                 file,
                 max_line_length: int,
                 tab_width: int = SpacingHelper.DEFAULT_TAB_WIDTH):
        """
        Yields results for all lines longer than the given maximum line length.

        :param max_line_length: Maximum number of characters for a line.
        :param tab_width: Number of spaces to show for one tab.
        """
        results = []
        bearname = self.__class__.__name__
        spacing_helper = SpacingHelper.from_section(section=self.section)

        for line_number, line in enumerate(file):
            line = spacing_helper.replace_tabs_with_spaces(line)
            if len(line) > max_line_length + 1:
                results.append(
                    Result(origin=bearname,
                           message=_("Line is longer than allowed.") +
                           " ({actual} > {maximum})".format(
                               actual=len(line), maximum=max_line_length),
                           file=filename,
                           line_nr=line_number + 1))

        return results
Ejemplo n.º 2
0
    def run(self,
            filename,
            file,
            max_line_length: int,
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH):
        """
        Yields results for all lines longer than the given maximum line length.

        :param max_line_length: Maximum number of characters for a line.
        :param tab_width: Number of spaces to show for one tab.
        """
        results = []
        spacing_helper = SpacingHelper.from_section(section=self.section)

        for line_number, line in enumerate(file):
            line = spacing_helper.replace_tabs_with_spaces(line)
            if len(line) > max_line_length + 1:
                results.append(
                    Result(origin=self,
                           message=_("Line is longer than allowed.") +
                                   " ({actual} > {maximum})".format(
                                       actual=len(line),
                                       maximum=max_line_length),
                           file=filename,
                           line_nr=line_number + 1))

        return results
Ejemplo n.º 3
0
class SpacingHelperTest(unittest.TestCase):

    def setUp(self):
        self.uut = SpacingHelper()

    def test_needed_settings(self):
        self.assertEqual(list(self.uut.get_optional_settings()), ['tab_width'])
        self.assertEqual(list(self.uut.get_non_optional_settings()), [])

    def test_construction(self):
        section = Section('test section')
        self.assertRaises(TypeError, SpacingHelper, 'no integer')
        self.assertRaises(TypeError, self.uut.from_section, 5)

        self.assertEqual(self.uut.tab_width,
                         self.uut.from_section(section).tab_width)

        # This is assumed in some tests. If you want to change this value, be
        # sure to change the tests too
        self.assertEqual(self.uut.DEFAULT_TAB_WIDTH, 4)
        self.assertEqual(self.uut.tab_width, self.uut.DEFAULT_TAB_WIDTH)

    def test_get_indentation(self):
        self.assertRaises(TypeError, self.uut.get_indentation, 5)

        self.assertEqual(self.uut.get_indentation('no indentation'), 0)
        self.assertEqual(self.uut.get_indentation(' indentation'), 1)
        self.assertEqual(self.uut.get_indentation('  indentation'), 2)
        self.assertEqual(self.uut.get_indentation('\tindentation'),
                         self.uut.DEFAULT_TAB_WIDTH)

        # Having a space before the tab shouldn't make any difference
        self.assertEqual(self.uut.get_indentation(' \tindentation'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t indentation'),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation('\t indentation'),
                         self.uut.DEFAULT_TAB_WIDTH+1)

        # same tests but with indentation only
        self.assertEqual(self.uut.get_indentation('\t'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t '),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation('\t '),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation('\t\t'),
                         self.uut.DEFAULT_TAB_WIDTH*2)

    def test_replace_tabs_with_spaces(self):
        self.assertRaises(TypeError, self.uut.replace_tabs_with_spaces, 5)

        self.assertEqual(self.uut.replace_tabs_with_spaces(''), '')
        self.assertEqual(self.uut.replace_tabs_with_spaces(' '), ' ')
        self.assertEqual(self.uut.replace_tabs_with_spaces('\t'),
                         ' '*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('\t\t'),
                         ' '*self.uut.DEFAULT_TAB_WIDTH*2)
        self.assertEqual(self.uut.replace_tabs_with_spaces(' \t'),
                         ' '*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('  \t'),
                         ' '*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('d \t '),
                         'd' + ' '*self.uut.DEFAULT_TAB_WIDTH)

    def test_replace_spaces_with_tabs(self):
        self.assertRaises(TypeError, self.uut.replace_spaces_with_tabs, 5)

        self.assertEqual(self.uut.replace_spaces_with_tabs(''), '')
        self.assertEqual(self.uut.replace_spaces_with_tabs(' '), ' ')
        self.assertEqual(self.uut.replace_spaces_with_tabs('    '), '\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   \t'), '\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd  '),
                         '   dd  ')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd d '),
                         '   dd d ')  # One space shouldnt be replaced
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd   '),
                         '   dd\t')
        self.assertEqual(
            self.uut.replace_spaces_with_tabs(' \t   a_text   another'),
            '\t   a_text\tanother')
        self.assertEqual(self.uut.replace_spaces_with_tabs('123\t'), '123\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('d  d'), 'd  d')
Ejemplo n.º 4
0
class SpacingHelperTest(unittest.TestCase):
    def setUp(self):
        self.uut = SpacingHelper()

    def test_needed_settings(self):
        self.assertEqual(list(self.uut.get_optional_settings()), ['tab_width'])
        self.assertEqual(list(self.uut.get_non_optional_settings()), [])

    def test_construction(self):
        section = Section('test section')
        self.assertRaises(TypeError, SpacingHelper, 'no integer')
        self.assertRaises(TypeError, self.uut.from_section, 5)

        self.assertEqual(self.uut.tab_width,
                         self.uut.from_section(section).tab_width)

        # This is assumed in some tests. If you want to change this value, be
        # sure to change the tests too
        self.assertEqual(self.uut.DEFAULT_TAB_WIDTH, 4)
        self.assertEqual(self.uut.tab_width, self.uut.DEFAULT_TAB_WIDTH)

    def test_get_indentation(self):
        self.assertRaises(TypeError, self.uut.get_indentation, 5)

        self.assertEqual(self.uut.get_indentation('no indentation'), 0)
        self.assertEqual(self.uut.get_indentation(' indentation'), 1)
        self.assertEqual(self.uut.get_indentation('  indentation'), 2)
        self.assertEqual(self.uut.get_indentation('\tindentation'),
                         self.uut.DEFAULT_TAB_WIDTH)

        # Having a space before the tab shouldn't make any difference
        self.assertEqual(self.uut.get_indentation(' \tindentation'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t indentation'),
                         self.uut.DEFAULT_TAB_WIDTH + 1)
        self.assertEqual(self.uut.get_indentation('\t indentation'),
                         self.uut.DEFAULT_TAB_WIDTH + 1)

        # same tests but with indentation only
        self.assertEqual(self.uut.get_indentation('\t'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t '),
                         self.uut.DEFAULT_TAB_WIDTH + 1)
        self.assertEqual(self.uut.get_indentation('\t '),
                         self.uut.DEFAULT_TAB_WIDTH + 1)
        self.assertEqual(self.uut.get_indentation('\t\t'),
                         self.uut.DEFAULT_TAB_WIDTH * 2)

    def test_replace_tabs_with_spaces(self):
        self.assertRaises(TypeError, self.uut.replace_tabs_with_spaces, 5)

        self.assertEqual(self.uut.replace_tabs_with_spaces(''), '')
        self.assertEqual(self.uut.replace_tabs_with_spaces(' '), ' ')
        self.assertEqual(self.uut.replace_tabs_with_spaces('\t'),
                         ' ' * self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('\t\t'),
                         ' ' * self.uut.DEFAULT_TAB_WIDTH * 2)
        self.assertEqual(self.uut.replace_tabs_with_spaces(' \t'),
                         ' ' * self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('  \t'),
                         ' ' * self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('d \t '),
                         'd' + ' ' * self.uut.DEFAULT_TAB_WIDTH)

    def test_replace_spaces_with_tabs(self):
        self.assertRaises(TypeError, self.uut.replace_spaces_with_tabs, 5)

        self.assertEqual(self.uut.replace_spaces_with_tabs(''), '')
        self.assertEqual(self.uut.replace_spaces_with_tabs(' '), ' ')
        self.assertEqual(self.uut.replace_spaces_with_tabs('    '), '\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   \t'), '\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd  '),
                         '   dd  ')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd d '),
                         '   dd d ')  # One space shouldnt be replaced
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd   '),
                         '   dd\t')
        self.assertEqual(
            self.uut.replace_spaces_with_tabs(' \t   a_text   another'),
            '\t   a_text\tanother')
        self.assertEqual(self.uut.replace_spaces_with_tabs('123\t'), '123\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('d  d'), 'd  d')
Ejemplo n.º 5
0
class SpacingHelperTest(unittest.TestCase):
    def setUp(self):
        self.uut = SpacingHelper()

    def test_needed_settings(self):
        self.assertEqual(list(self.uut.get_optional_settings()), ["tab_width"])
        self.assertEqual(list(self.uut.get_non_optional_settings()), [])

    def test_construction(self):
        section = Section("test section")
        self.assertRaises(TypeError, SpacingHelper, "no integer")
        self.assertRaises(TypeError, self.uut.from_section, 5)

        self.assertEqual(self.uut.tab_width,
                         self.uut.from_section(section).tab_width)

        section.append(Setting("tab_width", "invalid"))
        # Setting won't be converted since it's not possible, SpacingHelper
        # will then complain with TypeError
        self.assertRaises(TypeError, self.uut.from_section, section)

        # This is assumed in some tests. If you want to change this value, be
        # sure to change the tests too
        self.assertEqual(self.uut.DEFAULT_TAB_WIDTH, 4)
        self.assertEqual(self.uut.tab_width, self.uut.DEFAULT_TAB_WIDTH)

    def test_get_indentation(self):
        self.assertRaises(TypeError, self.uut.get_indentation, 5)

        self.assertEqual(self.uut.get_indentation("no indentation"), 0)
        self.assertEqual(self.uut.get_indentation(" indentation"), 1)
        self.assertEqual(self.uut.get_indentation("  indentation"), 2)
        self.assertEqual(self.uut.get_indentation("\tindentation"),
                         self.uut.DEFAULT_TAB_WIDTH)

        # Having a space before the tab shouldn't make any difference
        self.assertEqual(self.uut.get_indentation(" \tindentation"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t indentation"),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation("\t indentation"),
                         self.uut.DEFAULT_TAB_WIDTH+1)

        # same tests but with indentation only
        self.assertEqual(self.uut.get_indentation("\t"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t "),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation("\t "),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation("\t\t"),
                         self.uut.DEFAULT_TAB_WIDTH*2)

    def test_replace_tabs_with_spaces(self):
        self.assertRaises(TypeError, self.uut.replace_tabs_with_spaces, 5)

        self.assertEqual(self.uut.replace_tabs_with_spaces(""), "")
        self.assertEqual(self.uut.replace_tabs_with_spaces(" "), " ")
        self.assertEqual(self.uut.replace_tabs_with_spaces("\t"),
                         " "*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("\t\t"),
                         " "*self.uut.DEFAULT_TAB_WIDTH*2)
        self.assertEqual(self.uut.replace_tabs_with_spaces(" \t"),
                         " "*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("  \t"),
                         " "*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("d \t "),
                         "d" + " "*self.uut.DEFAULT_TAB_WIDTH)

    def test_replace_spaces_with_tabs(self):
        self.assertRaises(TypeError, self.uut.replace_spaces_with_tabs, 5)

        self.assertEqual(self.uut.replace_spaces_with_tabs(""), "")
        self.assertEqual(self.uut.replace_spaces_with_tabs(" "), " ")
        self.assertEqual(self.uut.replace_spaces_with_tabs("    "), "\t")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   \t"), "\t")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd  "),
                         "   dd  ")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd d "),
                         "   dd d ")  # One space shouldnt be replaced
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd   "),
                         "   dd\t")
        self.assertEqual(
            self.uut.replace_spaces_with_tabs(" \t   a_text   another"),
            "\t   a_text\tanother")
        self.assertEqual(self.uut.replace_spaces_with_tabs("d  d"), "d  d")
Ejemplo n.º 6
0
class SpacingHelperTestCase(unittest.TestCase):
    def setUp(self):
        self.uut = SpacingHelper()

    def test_needed_settings(self):
        self.assertEqual(list(self.uut.get_optional_settings()), ["tab_width"])
        self.assertEqual(list(self.uut.get_non_optional_settings()), [])

    def test_construction(self):
        section = Section("test section")
        self.assertRaises(TypeError, SpacingHelper, "no integer")
        self.assertRaises(TypeError, self.uut.from_section, 5)

        self.assertEqual(self.uut.tab_width,
                         self.uut.from_section(section).tab_width)

        section.append(Setting("tab_width", "invalid"))
        # Setting won't be converted since it's not possible, SpacingHelper
        # will then complain with TypeError
        self.assertRaises(TypeError, self.uut.from_section, section)

        # This is assumed in some tests. If you want to change this value, be
        # sure to change the tests too
        self.assertEqual(self.uut.DEFAULT_TAB_WIDTH, 4)
        self.assertEqual(self.uut.tab_width, self.uut.DEFAULT_TAB_WIDTH)

    def test_get_indentation(self):
        self.assertRaises(TypeError, self.uut.get_indentation, 5)

        self.assertEqual(self.uut.get_indentation("no indentation"), 0)
        self.assertEqual(self.uut.get_indentation(" indentation"), 1)
        self.assertEqual(self.uut.get_indentation("  indentation"), 2)
        self.assertEqual(self.uut.get_indentation("\tindentation"),
                         self.uut.DEFAULT_TAB_WIDTH)

        # Having a space before the tab shouldn't make any difference
        self.assertEqual(self.uut.get_indentation(" \tindentation"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t indentation"),
                         self.uut.DEFAULT_TAB_WIDTH + 1)
        self.assertEqual(self.uut.get_indentation("\t indentation"),
                         self.uut.DEFAULT_TAB_WIDTH + 1)

        # same tests but with indentation only
        self.assertEqual(self.uut.get_indentation("\t"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t "),
                         self.uut.DEFAULT_TAB_WIDTH + 1)
        self.assertEqual(self.uut.get_indentation("\t "),
                         self.uut.DEFAULT_TAB_WIDTH + 1)
        self.assertEqual(self.uut.get_indentation("\t\t"),
                         self.uut.DEFAULT_TAB_WIDTH * 2)

    def test_replace_tabs_with_spaces(self):
        self.assertRaises(TypeError, self.uut.replace_tabs_with_spaces, 5)

        self.assertEqual(self.uut.replace_tabs_with_spaces(""), "")
        self.assertEqual(self.uut.replace_tabs_with_spaces(" "), " ")
        self.assertEqual(self.uut.replace_tabs_with_spaces("\t"),
                         " " * self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("\t\t"),
                         " " * self.uut.DEFAULT_TAB_WIDTH * 2)
        self.assertEqual(self.uut.replace_tabs_with_spaces(" \t"),
                         " " * self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("  \t"),
                         " " * self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("d \t "),
                         "d" + " " * self.uut.DEFAULT_TAB_WIDTH)

    def test_replace_spaces_with_tabs(self):
        self.assertRaises(TypeError, self.uut.replace_spaces_with_tabs, 5)

        self.assertEqual(self.uut.replace_spaces_with_tabs(""), "")
        self.assertEqual(self.uut.replace_spaces_with_tabs(" "), " ")
        self.assertEqual(self.uut.replace_spaces_with_tabs("    "), "\t")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   \t"), "\t")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd  "),
                         "   dd  ")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd d "),
                         "   dd d ")  # One space shouldnt be replaced
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd   "),
                         "   dd\t")
        self.assertEqual(
            self.uut.replace_spaces_with_tabs(" \t   a_text   another"),
            "\t   a_text\tanother")
        self.assertEqual(self.uut.replace_spaces_with_tabs("d  d"), "d  d")