1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
import ast
from vulture import utils
from . import v
assert v # Silence pyflakes
def check_condition(code, result):
condition = ast.parse(code, mode="eval").body
if result:
assert utils.condition_is_always_true(condition)
else:
assert utils.condition_is_always_false(condition)
def test_false():
check_condition("False", False)
check_condition("None", False)
check_condition("0", False)
def test_empty():
check_condition("''", False)
check_condition("[]", False)
check_condition("{}", False)
def test_true():
check_condition("True", True)
check_condition("2", True)
check_condition("'s'", True)
check_condition("['foo', 'bar']", True)
check_condition("{'a': 1, 'b': 2}", True)
def test_complex_conditions():
conditions = [
("foo and False", True, False),
("foo or False", False, False),
("foo and True", False, False),
("foo or True", False, True),
("False and foo", True, False),
("False and 1", True, False),
("not False", False, True),
("not True", True, False),
("not foo", False, False),
("foo and (False or [])", True, False),
('(foo and bar) or {"a": 1}', False, True),
]
for condition, always_false, always_true in conditions:
condition = ast.parse(condition, mode="eval").body
assert not (always_false and always_true)
assert utils.condition_is_always_false(condition) == always_false
assert utils.condition_is_always_true(condition) == always_true
def test_errors():
conditions = [
"foo",
'__name__ == "__main__"',
"chr(-1)",
'getattr(True, "foo")',
'hasattr(str, "foo")',
"isinstance(True, True)",
"globals()",
"locals()",
"().__class__",
]
for condition in conditions:
condition = ast.parse(condition, mode="eval").body
assert not utils.condition_is_always_false(condition)
assert not utils.condition_is_always_true(condition)
|