Ejemplo n.º 1
0
    def test_comment_contains_code_with_print(self):
        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#print'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#print(1)'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#print 1'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '#to print'))
Ejemplo n.º 2
0
    def test_comment_contains_code_with_encoding(self):
        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# coding=utf-8'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '#coding= utf-8'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# coding: utf-8'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# encoding: utf8'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '# codings=utf-8'))
Ejemplo n.º 3
0
    def test_filter_commented_out_code(self):
        self.assertEqual(
            """\
# This is a comment.

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""",
            ''.join(eradicate.Eradicator().filter_commented_out_code(
                """\
# print(5)
# This is a comment.
# x = 1

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
# try:
#     x = 1
# finally:
#     x = 0
""")))
Ejemplo n.º 4
0
    def test_commented_out_code_line_numbers_with_for_statement(self):
        self.assertEqual(
            [1, 2],
            list(eradicate.Eradicator().commented_out_code_line_numbers("""\
# for x in y:
#     oops = x.ham
""")))
Ejemplo n.º 5
0
 def test_extend_whitelist(self):
     eradicator = eradicate.Eradicator()
     eradicator.update_whitelist(["foo"], True)
     self.assertTrue(
         eradicator.WHITELIST_REGEX == re.compile(
             r'|'.join(list(eradicator.DEFAULT_WHITELIST) + ["foo"]), flags=re.IGNORECASE
         )
     )
Ejemplo n.º 6
0
    def test_commented_out_code_line_numbers_with_with_statement(self):
        self.assertEqual(
            [1, 2],
            list(eradicate.Eradicator().commented_out_code_line_numbers("""\
# with open('filename', 'w') as out_file:
#     json.dump(objects, out_file)
#
""")))
Ejemplo n.º 7
0
    def test_filter_commented_out_code_should_avoid_escaped_newlines(self):
        line = """\
if False: \\
# print(3)
    print(2)
    print(3)
"""
        self.assertEqual(
            line,
            ''.join(eradicate.Eradicator().filter_commented_out_code(line)))
Ejemplo n.º 8
0
    def test_filter_commented_out_code_without_aggressive(self):
        code = """\
# iterate through choices inside of parenthesis (separated by '|'):

# if the Optional takes a value, format is:
#    -s ARGS, --long ARGS
"""
        self.assertEqual(
            code,
            ''.join(eradicate.Eradicator().filter_commented_out_code(code,
                                                        aggressive=False)))
Ejemplo n.º 9
0
    def test_commented_out_code_line_numbers(self):
        self.assertEqual(
            [1, 3],
            list(eradicate.Eradicator().commented_out_code_line_numbers(
                """\
# print(5)
# This is a comment.
# x = 1

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""")))
Ejemplo n.º 10
0
    def test_filter_commented_out_code_with_annotation(self):
        self.assertEqual(
            '\n\n\n',
            ''.join(eradicate.Eradicator().filter_commented_out_code("""\
# class CommentedClass(object):
#     def __init__(self, prop: int) -> None:
#         self.property = prop

#     def __str__(self) -> str:
#         return self.__class__.__name__

#    def set_prop(self, prop: int):
#        self.prop = prop

#    def get_prop(self):
#        return self.prop
""")))
Ejemplo n.º 11
0
    def test_filter_commented_out_code_with_larger_example(self):
        self.assertEqual(
            """\
# This is a comment.

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""",
            ''.join(eradicate.Eradicator().filter_commented_out_code(
                """\
# print(5)
# This is a comment.
# x = 1

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""")))
Ejemplo n.º 12
0
 def test_update_whitelist(self):
     eradicator = eradicate.Eradicator()
     eradicator.update_whitelist(["foo"], False)
     self.assertTrue(eradicator.WHITELIST_REGEX == re.compile("foo", flags=re.IGNORECASE))
Ejemplo n.º 13
0
 def test_detect_encoding_with_bad_encoding(self):
     with temporary_file('# -*- coding: blah -*-\n') as filename:
         self.assertEqual('latin-1',
                          eradicate.Eradicator().detect_encoding(filename))
Ejemplo n.º 14
0
 def test_comment_contains_code_with_sentences(self):
     self.assertFalse(eradicate.Eradicator().comment_contains_code(
         '#code is good'))
Ejemplo n.º 15
0
    def test_comment_contains_code(self):
        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '#'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# This is a (real) comment.'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# 123'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# 123.1'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# 1, 2, 3'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            'x = 1  # x = 1'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# pylint: disable=redefined-outer-name'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# Issue #999: This is not code'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '# x = 1'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#from foo import eradicate'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#import eradicate'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#"key": value,'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#"key": "value",'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#"key": 1 + 1,'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            "#'key': 1 + 1,"))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#"key": {'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#}'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#} )]'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#},'))
Ejemplo n.º 16
0
    def test_comment_contains_code_with_default_whitelist(self):
        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# pylint: disable=A0123'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# pylint:disable=A0123'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# pylint: disable = A0123'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# pylint:disable = A0123'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# pyright: reportErrorName=true'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# noqa'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# NOQA'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# noqa: A123'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# noqa:A123'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# fmt: on'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# fmt: off'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# fmt:on'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# fmt:off'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# type: ignore'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# type:ignore'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# type: ignore[import]'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# type:ignore[import]'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# TODO: Do that'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# FIXME: Fix that'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# XXX: What ever'))
Ejemplo n.º 17
0
    def test_comment_contains_code_with_return(self):
        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#return x'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '#to return'))
Ejemplo n.º 18
0
    def test_comment_contains_code_with_multi_line(self):
        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#def foo():'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#else:'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#  else  :  '))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '# "foo %d" % \\'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#elif True:'))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#x = foo('))

        self.assertTrue(eradicate.Eradicator().comment_contains_code(
            '#except Exception:'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '# this is = to that :('))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '#else'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '#or else:'))

        self.assertFalse(eradicate.Eradicator().comment_contains_code(
            '#else True:'))