File: test_make_whitelist.py

package info (click to toggle)
vulture 2.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: python: 3,254; makefile: 12
file content (80 lines) | stat: -rw-r--r-- 1,551 bytes parent folder | download | duplicates (3)
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
75
76
77
78
79
80
import pytest

from . import check, v

assert v  # silence pyflakes


@pytest.fixture
def check_whitelist(v):
    def examine(code, results_before, results_after):
        v.scan(code)
        check(v.get_unused_code(), results_before)
        for item in v.get_unused_code():
            v.scan(item.get_whitelist_string())
        check(v.get_unused_code(), results_after)

    return examine


def test_unused_function(check_whitelist):
    code = """\
def func():
    pass
"""
    check_whitelist(code, ["func"], [])


def test_unused_class(check_whitelist):
    code = """\
class Foo:
    def __init__(self):
        pass
"""
    check_whitelist(code, ["Foo"], [])


def test_unused_variables(check_whitelist):
    code = """\
foo = 'unused'
bar = 'variable'
"""
    check_whitelist(code, ["foo", "bar"], [])


def test_unused_import(check_whitelist):
    code = """\
import xyz
import foo as bar
from abc import iou
from lorem import ipsum as dolor
"""
    check_whitelist(code, ["xyz", "bar", "iou", "dolor"], [])


def test_unused_attribute(check_whitelist):
    code = """\
class Foo:
    def bar(self):
        self.foobar = 'unused attr'
"""
    check_whitelist(code, ["Foo", "bar", "foobar"], [])


def test_unused_property(check_whitelist):
    code = """\
class Foo:
    @property
    def bar(self):
        pass
"""
    check_whitelist(code, ["Foo", "bar"], [])


def test_unreachable_code(check_whitelist):
    code = """\
def foo():
    return "Foo Bar"
    print("Hello")
"""
    check_whitelist(code, ["foo", "return"], ["return"])