summaryrefslogtreecommitdiff
path: root/pylint/test/unittest_checkers_utils.py
blob: 78555bbcb6589e8181c268a7db35dc36b5520bdb (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright (c) 2010 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
# Copyright (c) 2013-2017 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2013-2014 Google, Inc.
# Copyright (c) 2014 Arun Persaud <arun@nubati.net>
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
# Copyright (c) 2018 Caio Carrara <ccarrara@redhat.com>

# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING

"""Tests for the pylint.checkers.utils module."""

import astroid

from pylint.checkers import utils
import pytest


@pytest.mark.parametrize("name,expected", [
    ('min', True),
    ('__builtins__', True),
    ('__path__', False),
    ('__file__', False),
    ('whatever', False),
    ('mybuiltin', False),
])
def testIsBuiltin(name, expected):
    assert utils.is_builtin(name) == expected


@pytest.mark.parametrize("fn,kw", [
    ('foo(3)', {'keyword': 'bar'}),
    ('foo(one=a, two=b, three=c)', {'position': 1}),
])
def testGetArgumentFromCallError(fn, kw):
    with pytest.raises(utils.NoSuchArgumentError):
        node = astroid.extract_node(fn)
        utils.get_argument_from_call(node, **kw)


@pytest.mark.parametrize("fn,kw", [
    ('foo(bar=3)', {'keyword': 'bar'}),
    ('foo(a, b, c)', {'position': 1}),
])
def testGetArgumentFromCallExists(fn, kw):
    node = astroid.extract_node(fn)
    assert utils.get_argument_from_call(node, **kw) is not None


def testGetArgumentFromCall():
    node = astroid.extract_node('foo(a, not_this_one=1, this_one=2)')
    arg = utils.get_argument_from_call(node, position=2, keyword='this_one')
    assert 2 == arg.value

    node = astroid.extract_node('foo(a)')
    with pytest.raises(utils.NoSuchArgumentError):
        utils.get_argument_from_call(node, position=1)
    with pytest.raises(ValueError):
        utils.get_argument_from_call(node, None, None)
    name = utils.get_argument_from_call(node, position=0)
    assert name.name == 'a'


def test_error_of_type():
    nodes = astroid.extract_node("""
    try: pass
    except AttributeError: #@
         pass
    try: pass
    except Exception: #@
         pass
    except: #@
         pass
    """)
    assert utils.error_of_type(nodes[0], AttributeError)
    assert utils.error_of_type(nodes[0], (AttributeError, ))
    assert not utils.error_of_type(nodes[0], Exception)
    assert utils.error_of_type(nodes[1], Exception)
    assert utils.error_of_type(nodes[2], ImportError)


def test_node_ignores_exception():
    nodes = astroid.extract_node("""
    try:
        1/0 #@
    except ZeroDivisionError:
        pass
    try:
        1/0 #@
    except Exception:
        pass
    try:
        2/0 #@
    except:
        pass
    try:
        1/0 #@
    except ValueError:
        pass
    """)
    assert utils.node_ignores_exception(nodes[0], ZeroDivisionError)
    assert not utils.node_ignores_exception(nodes[1], ZeroDivisionError)
    assert utils.node_ignores_exception(nodes[2], ZeroDivisionError)
    assert not utils.node_ignores_exception(nodes[3], ZeroDivisionError)


def test_is_subclass_of_node_b_derived_from_node_a():
    nodes = astroid.extract_node("""
    class Superclass: #@
        pass

    class Subclass(Superclass): #@
        pass
    """)
    assert utils.is_subclass_of(nodes[1], nodes[0])


def test_is_subclass_of_node_b_not_derived_from_node_a():
    nodes = astroid.extract_node("""
    class OneClass: #@
        pass

    class AnotherClass: #@
        pass
    """)
    assert not utils.is_subclass_of(nodes[1], nodes[0])


def test_is_subclass_of_not_classdefs():
    node = astroid.extract_node("""
    class OneClass: #@
        pass
    """)
    assert not utils.is_subclass_of(None, node)
    assert not utils.is_subclass_of(node, None)
    assert not utils.is_subclass_of(None, None)