Пример #1
0
    def test_works_with_no_header(self, mock_write):
        expected = [
            '"""\n',
            NOTICE + '\n',
            '"""\n',
            '\n',
            'import os\n',
        ]

        m = Module('test/examples/no_header.py')
        m.add_copyright()

        for actual, expected in zip(m.lines[:5], expected):
            self.assertEqual(actual, expected, expected + ' failed.')

        mock_write.assert_called()
Пример #2
0
    def test_loads_file_correctly(self):
        expected_lines = 11
        expected_line_10 = '[print(add_ten(i)) for i in range(100)]'

        m = Module('test/examples/simple_header.py')
        self.assertEqual(len(m.lines), expected_lines)
        self.assertIn(expected_line_10, m.lines[9])
Пример #3
0
    def test_works_with_multiple_docstrings_in_a_file(self, mock_write):
        expected = [
            '"""\n',
            'one_line_docstring.py: This file has a docstring.\n',
            NOTICE + '\n',
            '"""\n',
            '\n',
            'import os\n',
        ]

        m = Module('test/examples/multiple_docstrings.py')
        m.add_copyright()

        for actual, expected in zip(m.lines[:6], expected):
            self.assertEqual(actual, expected)

        mock_write.assert_called()
Пример #4
0
    def test_add_notice_inserts_lines_and_overwrites(self):
        notice_lines = ['copyright notice', '"""']
        ix = 2
        overwrite = True
        expected = [
            '"""\n',
            'simple_docstring.py: This file has a docstring.\n',
            'copyright notice\n',
            '"""\n',
            '\n',
            'import os\n',
        ]

        m = Module('test/examples/simple_docstring.py')
        m.add_notice(ix, notice_lines, overwrite)

        for actual, expected in zip(m.lines[:6], expected):
            self.assertEqual(actual, expected, expected + ' failed.')
Пример #5
0
    def test_add_notice_inserts_lines(self):
        notice_lines = ['# copyright notice']
        ix = 3
        overwite = False
        expected = [
            '# comment_header.py\n',
            '# This module has a simple comment header\n',
            '# which is wrong but sometimes exists.\n',
            '# copyright notice\n',
            '\n',
            'import os\n',
        ]

        m = Module('test/examples/comment.py')
        m.add_notice(ix, notice_lines, overwite)

        for actual, expected in zip(m.lines[:6], expected):
            self.assertEqual(actual, expected, expected + ' failed.')
Пример #6
0
    def test_identify_header_correctly(self):
        files = [
            ('test/examples/simple_header.py', 'comment'),
            ('test/examples/comment.py', 'comment'),
            ('test/examples/no_header.py', None),
            ('test/examples/docstring.py', 'docstring'),
        ]

        for file, expected in files:
            m = Module(file)
            self.assertEqual(m.header_type, expected, file + ' failed.')
Пример #7
0
    def test_identifies_lines_with_docstrings(self):
        lines = [
            ('# this has a comment', False),
            ('this = 2 # has a comment bus is code', False),
            ('"""', True),
            ("''' so is this '''", True),
            ('from something import something', False),
        ]

        for line, expected in lines:
            actual = Module.line_contains_docstring(line)
            self.assertEqual(actual, expected, line + ' failed.')
Пример #8
0
 def test_no_copyright_when_doesnt_exist(self):
     m = Module('test/examples/simple_header.py')
     self.assertFalse(m.has_copyright())
Пример #9
0
 def test_finds_copyright_when_exists(self):
     m = Module('test/examples/has_copyright.py')
     self.assertTrue(m.has_copyright())
Пример #10
0
    def test_ignores_file_with_copyright(self, mock_write):

        m = Module('test/examples/has_copyright.py')
        m.add_copyright()

        mock_write.assert_not_called()
Пример #11
0
 def test_detects_unimportable_module(self):
     m = Module('test/examples/bad_module.py')
     self.assertFalse(m.can_import())
Пример #12
0
 def test_detects_importable_module(self):
     m = Module('test/examples/simple_docstring.py')
     self.assertTrue(m.can_import())