示例#1
0
 def test_for_loop_starred_no_error(self):
     tree = ast.parse(
         'for index, *other in enumerate([(1, "a"), (2, "b")]):\n'
         '        continue\n', )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#2
0
 def test_function_nothing(self):
     tree = ast.parse(
         'def integer(): pass\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#3
0
 def test_async_function(self):
     tree = ast.parse(
         'async def int(): pass\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#4
0
 def test_more_nested(self):
     tree = ast.parse('class Bla(object):\n'
                      '    def method(self):\n'
                      '        int = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['B001'])
示例#5
0
 def test_for_loop_multiple_variables(self):
     tree = ast.parse(
         'for (index, format) in enumerate([1,2,3,]):\n'
         '        continue\n', )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#6
0
 def test_list_comprehension_multiple_as_list(self):
     tree = ast.parse(
         'a = [(int, a) for [int, a] in enumerate(range(3,9))]\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#7
0
 def test_for_loop_nested_tuple(self):
     tree = ast.parse(
         'for index, (format, list) in enumerate([(1, "a"), (2, "b")]):\n'
         '        continue\n', )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 2)
示例#8
0
 def test_list_comprehension(self):
     tree = ast.parse(
         'a = [int for int in range(3,9)]\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#9
0
 def test_with_statement_unpack(self):
     tree = ast.parse(
         'with open("bla.txt") as (dir, bla):\n'
         '    continue\n', )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#10
0
 def test_class_nothing(self):
     tree = ast.parse(
         'class integer(object): pass\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#11
0
 def test_import_as(self):
     tree = ast.parse(
         'import zope.component.getSite as int\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#12
0
 def test_import_from_as_nothing(self):
     tree = ast.parse(
         'from zope.component import getSite as something_else\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#13
0
 def test_with_statement_unpack_star(self):
     tree = ast.parse(
         'with open("bla.txt") as (bla, *int):\n'
         '    continue\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#14
0
 def test_report_all_arguments(self):
     tree = ast.parse('def bla(zip, object=4):\n    b = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(
         len(ret),
         2
     )
示例#15
0
 def test_for_loop_starred_no_error(self):
     tree = ast.parse(
         'for index, *other in enumerate([(1, "a"), (2, "b")]):\n'
         '        continue\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#16
0
 def test_for_loop_nested_tuple(self):
     tree = ast.parse(
         'for index, (format, list) in enumerate([(1, "a"), (2, "b")]):\n'
         '        continue\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 2)
示例#17
0
 def test_for_loop_list(self):
     tree = ast.parse(
         'for [index, format] in enumerate([1,2,3,]):\n'
         '        continue\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#18
0
 def test_for_loop_variable(self):
     tree = ast.parse(
         'for format in (1, 2, 3):\n'
         '        continue\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#19
0
 def test_ignore_whitelisted_names(self):
     tree = ast.parse(
         'class MyClass(object):\n'
         '    __name__ = 4\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#20
0
 def test_method(self):
     tree = ast.parse(
         'class bla(object):\n'
         '    def int(): pass\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#21
0
 def test_method_without_arguments(self):
     tree = ast.parse('def bla():\n    b = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(
         len(ret),
         0
     )
示例#22
0
 def test_nested(self):
     tree = ast.parse(
         'def bla():\n'
         '    filter = 4'
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 1)
示例#23
0
 def test_method_only_normal_keyword_arguments(self):
     tree = ast.parse('def bla(k=4):\n    b = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(
         len(ret),
         0
     )
示例#24
0
 def test_with_statement_multiple(self):
     tree = ast.parse(
         'with open("bla.txt") as dir, open("bla.txt") as int:\n'
         '    continue\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 2)
示例#25
0
 def test_no_error(self):
     tree = ast.parse('def bla(first):\n    b = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(
         len(ret),
         0
     )
示例#26
0
 def test_nested(self):
     tree = ast.parse(
         'def bla():\n'
         '    filter = 4',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['A001'])
示例#27
0
 def test_line_number(self):
     tree = ast.parse(
         'a = 2\n'
         'open = 4',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(ret[0][0], 2)
示例#28
0
 def test_report_all_variables_within_a_line(self):
     tree = ast.parse('def bla():\n    object = 4; zip = 3')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(
         len(ret),
         2
     )
示例#29
0
 def test_line_number(self):
     tree = ast.parse(
         'a = 2\n'
         'open = 4',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(ret[0][0], 2)
示例#30
0
 def test_nested(self):
     tree = ast.parse(
         'def bla():\n'
         '    filter = 4',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['A001'])
示例#31
0
 def test_async_for_nothing(self):
     tree = ast.parse(
         'async def bla():\n'
         '    async for x in range(4):\n'
         '        pass\n', )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#32
0
 def test_async_with_nothing(self):
     tree = ast.parse(
         'async def bla():\n'
         '    async with open("bla.txt") as x:\n'
         '        pass\n', )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#33
0
 def test_offset(self):
     tree = ast.parse(
         'def bla():\n'
         '    zip = 4',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(ret[0][1], 4)
示例#34
0
 def test_ignore_whitelisted_names(self):
     tree = ast.parse(
         'class MyClass(object):\n'
         '    __name__ = 4\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#35
0
 def test_offset(self):
     tree = ast.parse(
         'def bla():\n'
         '    zip = 4',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(ret[0][1], 4)
示例#36
0
 def test_stdin(self, stdin_get_value):
     code = u'max = 4'
     stdin_get_value.return_value = code
     checker = BuiltinsChecker('', 'stdin')
     ret = [c for c in checker.run()]
     self.assertEqual(
         len(ret),
         1,
     )
示例#37
0
 def test_async_with_nothing(self):
     tree = ast.parse(
         'async def bla():\n'
         '    async with open("bla.txt") as x:\n'
         '        pass\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#38
0
 def test_more_nested(self):
     tree = ast.parse(
         'class Bla(object):\n'
         '    def method(self):\n'
         '        int = 4',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['A001'])
示例#39
0
 def test_exception_no_error(self):
     tree = ast.parse(
         'try:\n'
         '    a = 2\n'
         'except Exception:\n'
         '    print("ooops")\n', )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#40
0
 def test_method_error_code(self):
     tree = ast.parse(
         'class bla(object):\n'
         '    def int(): pass\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     error_code = ret[0][2][:4]
     self.assertEqual(error_code, 'A003')
示例#41
0
 def test_builtin_works_on_many_examples(source_code):
     try:
         source = source_code.encode('utf-8-sig')
     except UnicodeEncodeError:
         reject()
         raise
     tree = ast.parse(source)
     checker = BuiltinsChecker(tree, '/home/script.py')
     assert isinstance([c for c in checker.run()], list)
示例#42
0
 def test_async_for_nothing(self):
     tree = ast.parse(
         'async def bla():\n'
         '    async for x in range(4):\n'
         '        pass\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#43
0
 def test_stdin(self, stdin_get_value):
     code = u'max = 4'
     stdin_get_value.return_value = code
     checker = BuiltinsChecker('', 'stdin')
     ret = [c for c in checker.run()]
     self.assertEqual(
         len(ret),
         1,
     )
示例#44
0
 def test_exception_no_error(self):
     tree = ast.parse(
         'try:\n'
         '    a = 2\n'
         'except Exception:\n'
         '    print("ooops")\n',
     )
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)
示例#45
0
 def test_assign_message(self):
     tree = ast.parse('def bla():\n    object = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['A001'])
示例#46
0
 def test_assignment_list(self):
     tree = ast.parse('[bla, int] = range(4)')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['A001'])
示例#47
0
 def test_builtin_top_level(self):
     tree = ast.parse('max = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['A001'])
示例#48
0
 def test_class_attribute_message(self):
     tree = ast.parse('class TestClass():\n    object = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['A003'])
示例#49
0
 def test_keyword_argument_message(self):
     tree = ast.parse('def bla(dict=3):\n    b = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assert_codes(ret, ['A002'])
示例#50
0
 def test_no_error(self):
     tree = ast.parse('def bla(first):\n    b = 4')
     checker = BuiltinsChecker(tree, '/home/script.py')
     ret = [c for c in checker.run()]
     self.assertEqual(len(ret), 0)