Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / test / unittest_checker_variables.py @ 745

History | View | Annotate | Download (3.29 KB)

1
"""Unit tests for the variables checker."""
2
import sys
3
import os
4
import unittest
5

    
6
import astroid
7
from astroid import test_utils
8

    
9
from pylint.checkers import variables
10
from pylint.testutils import CheckerTestCase, linter, set_config, Message
11

    
12
class VariablesCheckerTC(CheckerTestCase):
13

    
14
    CHECKER_CLASS = variables.VariablesChecker
15

    
16
    def test_bitbucket_issue_78(self):
17
        """ Issue 78 report a false positive for unused-module """
18
        module = astroid.parse("""
19
        from sys import path
20
        path += ['stuff']
21
        def func():
22
            other = 1
23
            return len(other)
24
        """)
25
        with self.assertNoMessages():
26
            self.walk(module)
27

    
28
    @set_config(ignored_modules=('argparse',))
29
    def test_no_name_in_module_skipped(self):
30
        """Make sure that 'from ... import ...' does not emit a
31
        'no-name-in-module' with a module that is configured
32
        to be ignored.
33
        """
34

    
35
        node = test_utils.extract_node("""
36
        from argparse import THIS_does_not_EXIST
37
        """)
38
        with self.assertNoMessages():
39
            self.checker.visit_importfrom(node)
40

    
41
    @set_config(callbacks=('callback_', '_callback'))
42
    def test_custom_callback_string(self):
43
        """ Test the --calbacks option works. """
44
        def cleanup():
45
            self.checker._to_consume = _to_consume
46
        _to_consume = self.checker._to_consume
47
        self.checker._to_consume = []
48
        self.addCleanup(cleanup)
49

    
50
        node = test_utils.extract_node("""
51
        def callback_one(abc):
52
             ''' should not emit unused-argument. '''
53
        """)
54
        with self.assertNoMessages():
55
            self.checker.visit_functiondef(node)
56
            self.checker.leave_functiondef(node)
57

    
58
        node = test_utils.extract_node("""
59
        def two_callback(abc, defg):
60
             ''' should not emit unused-argument. '''
61
        """)
62
        with self.assertNoMessages():
63
            self.checker.visit_functiondef(node)
64
            self.checker.leave_functiondef(node)
65

    
66
        node = test_utils.extract_node("""
67
        def normal_func(abc):
68
             ''' should emit unused-argument. '''
69
        """)
70
        with self.assertAddsMessages(
71
                Message('unused-argument', node=node['abc'], args='abc')):
72
            self.checker.visit_functiondef(node)
73
            self.checker.leave_functiondef(node)
74

    
75
        node = test_utils.extract_node("""
76
        def cb_func(abc):
77
             ''' Previous callbacks are overriden. '''
78
        """)
79
        with self.assertAddsMessages(
80
                Message('unused-argument', node=node['abc'], args='abc')):
81
            self.checker.visit_functiondef(node)
82
            self.checker.leave_functiondef(node)
83

    
84

    
85
class MissingSubmoduleTest(CheckerTestCase):
86
    CHECKER_CLASS = variables.VariablesChecker
87

    
88
    def test_package_all(self):
89
        regr_data = os.path.join(os.path.dirname(os.path.abspath(__file__)),
90
                                 'regrtest_data')
91
        sys.path.insert(0, regr_data)
92
        try:
93
            linter.check(os.path.join(regr_data, 'package_all'))
94
            got = linter.reporter.finalize().strip()
95
            self.assertEqual(got, "E:  3: Undefined variable name "
96
                                  "'missing' in __all__")
97
        finally:
98
            sys.path.pop(0)
99

    
100
if __name__ == '__main__':
101
    unittest.main()