pre-commit-hooks/tests/check_builtin_literals_test.py

152 lines
3.6 KiB
Python
Raw Normal View History

from __future__ import annotations
import ast
import pytest
2019-02-12 11:56:15 +08:00
from pre_commit_hooks.check_builtin_literals import Call
from pre_commit_hooks.check_builtin_literals import main
2019-02-12 11:56:15 +08:00
from pre_commit_hooks.check_builtin_literals import Visitor
BUILTIN_CONSTRUCTORS = '''\
2020-02-06 03:10:42 +08:00
import builtins
c1 = complex()
d1 = dict()
f1 = float()
i1 = int()
l1 = list()
s1 = str()
t1 = tuple()
c2 = builtins.complex()
d2 = builtins.dict()
f2 = builtins.float()
i2 = builtins.int()
l2 = builtins.list()
s2 = builtins.str()
t2 = builtins.tuple()
'''
BUILTIN_LITERALS = '''\
c1 = 0j
d1 = {}
f1 = 0.0
i1 = 0
l1 = []
s1 = ''
t1 = ()
'''
@pytest.fixture
def visitor():
2019-02-12 11:56:15 +08:00
return Visitor()
@pytest.mark.parametrize(
('expression', 'calls'),
[
2018-05-18 08:14:25 +08:00
# see #285
('x[0]()', []),
# complex
2019-02-12 11:57:37 +08:00
('0j', []),
('complex()', [Call('complex', 1, 0)]),
('complex(0, 0)', []),
("complex('0+0j')", []),
('builtins.complex()', []),
# float
2019-02-12 11:57:37 +08:00
('0.0', []),
('float()', [Call('float', 1, 0)]),
("float('0.0')", []),
('builtins.float()', []),
# int
2019-02-12 11:57:37 +08:00
('0', []),
('int()', [Call('int', 1, 0)]),
("int('0')", []),
('builtins.int()', []),
# list
2019-02-12 11:57:37 +08:00
('[]', []),
('list()', [Call('list', 1, 0)]),
("list('abc')", []),
("list([c for c in 'abc'])", []),
("list(c for c in 'abc')", []),
('builtins.list()', []),
# str
("''", []),
2019-02-12 11:57:37 +08:00
('str()', [Call('str', 1, 0)]),
("str('0')", []),
('builtins.str()', []),
# tuple
2019-02-12 11:57:37 +08:00
('()', []),
('tuple()', [Call('tuple', 1, 0)]),
("tuple('abc')", []),
("tuple([c for c in 'abc'])", []),
("tuple(c for c in 'abc')", []),
('builtins.tuple()', []),
],
)
def test_non_dict_exprs(visitor, expression, calls):
visitor.visit(ast.parse(expression))
assert visitor.builtin_type_calls == calls
@pytest.mark.parametrize(
('expression', 'calls'),
[
2019-02-12 11:57:37 +08:00
('{}', []),
('dict()', [Call('dict', 1, 0)]),
('dict(a=1, b=2, c=3)', []),
("dict(**{'a': 1, 'b': 2, 'c': 3})", []),
("dict([(k, v) for k, v in [('a', 1), ('b', 2), ('c', 3)]])", []),
("dict((k, v) for k, v in [('a', 1), ('b', 2), ('c', 3)])", []),
('builtins.dict()', []),
],
)
def test_dict_allow_kwargs_exprs(visitor, expression, calls):
visitor.visit(ast.parse(expression))
assert visitor.builtin_type_calls == calls
@pytest.mark.parametrize(
('expression', 'calls'),
[
2019-02-12 11:57:37 +08:00
('dict()', [Call('dict', 1, 0)]),
('dict(a=1, b=2, c=3)', [Call('dict', 1, 0)]),
2019-02-12 11:56:15 +08:00
("dict(**{'a': 1, 'b': 2, 'c': 3})", [Call('dict', 1, 0)]),
('builtins.dict()', []),
],
)
def test_dict_no_allow_kwargs_exprs(expression, calls):
2019-02-12 11:56:15 +08:00
visitor = Visitor(allow_dict_kwargs=False)
visitor.visit(ast.parse(expression))
assert visitor.builtin_type_calls == calls
def test_ignore_constructors():
2020-02-04 00:41:48 +08:00
visitor = Visitor(
ignore=('complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'),
)
visitor.visit(ast.parse(BUILTIN_CONSTRUCTORS))
assert visitor.builtin_type_calls == []
def test_failing_file(tmpdir):
f = tmpdir.join('f.py')
f.write(BUILTIN_CONSTRUCTORS)
2020-05-21 00:07:45 +08:00
rc = main([str(f)])
assert rc == 1
def test_passing_file(tmpdir):
f = tmpdir.join('f.py')
f.write(BUILTIN_LITERALS)
2020-05-21 00:07:45 +08:00
rc = main([str(f)])
assert rc == 0
def test_failing_file_ignore_all(tmpdir):
f = tmpdir.join('f.py')
f.write(BUILTIN_CONSTRUCTORS)
2020-05-21 00:07:45 +08:00
rc = main(['--ignore=complex,dict,float,int,list,str,tuple', str(f)])
assert rc == 0