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_utils.py @ 745
History | View | Annotate | Download (3.29 KB)
1 |
# Copyright 2013 Google Inc.
|
---|---|
2 |
# This program is free software; you can redistribute it and/or modify it under
|
3 |
# the terms of the GNU General Public License as published by the Free Software
|
4 |
# Foundation; either version 2 of the License, or (at your option) any later
|
5 |
# version.
|
6 |
#
|
7 |
# This program is distributed in the hope that it will be useful, but WITHOUT
|
8 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
9 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
10 |
#
|
11 |
# You should have received a copy of the GNU General Public License along with
|
12 |
# this program; if not, write to the Free Software Foundation, Inc.,
|
13 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
14 |
import unittest |
15 |
import warnings |
16 |
|
17 |
import astroid |
18 |
|
19 |
from pylint import __pkginfo__ |
20 |
from pylint import utils |
21 |
from pylint import interfaces |
22 |
from pylint.checkers.utils import check_messages |
23 |
|
24 |
|
25 |
class PyLintASTWalkerTest(unittest.TestCase): |
26 |
class MockLinter(object): |
27 |
def __init__(self, msgs): |
28 |
self._msgs = msgs
|
29 |
|
30 |
def is_message_enabled(self, msgid): |
31 |
return self._msgs.get(msgid, True) |
32 |
|
33 |
class Checker(object): |
34 |
def __init__(self): |
35 |
self.called = set() |
36 |
|
37 |
@check_messages('first-message') |
38 |
def visit_module(self, module): |
39 |
self.called.add('module') |
40 |
|
41 |
@check_messages('second-message') |
42 |
def visit_call(self, module): |
43 |
raise NotImplementedError |
44 |
|
45 |
@check_messages('second-message', 'third-message') |
46 |
def visit_assignname(self, module): |
47 |
self.called.add('assname') |
48 |
|
49 |
@check_messages('second-message') |
50 |
def leave_assignname(self, module): |
51 |
raise NotImplementedError |
52 |
|
53 |
def test_check_messages(self): |
54 |
linter = self.MockLinter({'first-message': True, |
55 |
'second-message': False, |
56 |
'third-message': True}) |
57 |
walker = utils.PyLintASTWalker(linter) |
58 |
checker = self.Checker()
|
59 |
walker.add_checker(checker) |
60 |
walker.walk(astroid.parse("x = func()"))
|
61 |
self.assertEqual(set(['module', 'assname']), checker.called) |
62 |
|
63 |
def test_deprecated_methods(self): |
64 |
class Checker(object): |
65 |
def __init__(self): |
66 |
self.called = False |
67 |
|
68 |
@check_messages('first-message') |
69 |
def visit_assname(self, node): |
70 |
self.called = True |
71 |
|
72 |
linter = self.MockLinter({'first-message': True}) |
73 |
walker = utils.PyLintASTWalker(linter) |
74 |
checker = Checker() |
75 |
walker.add_checker(checker) |
76 |
with warnings.catch_warnings(record=True) as w: |
77 |
warnings.simplefilter('always')
|
78 |
walker.walk(astroid.parse("x = 1"))
|
79 |
|
80 |
if __pkginfo__.numversion < (2, 0): |
81 |
expected = ('Implemented method visit_assname instead of '
|
82 |
'visit_assignname. This will be supported until '
|
83 |
'Pylint 2.0.')
|
84 |
self.assertEqual(len(w), 1) |
85 |
self.assertIsInstance(w[0].message, PendingDeprecationWarning) |
86 |
self.assertEqual(str(w[0].message), expected) |
87 |
self.assertTrue(checker.called)
|
88 |
else:
|
89 |
self.assertNotEqual(len(w), 1) |
90 |
self.assertFalse(checker.called)
|
91 |
|
92 |
|
93 |
if __name__ == '__main__': |
94 |
unittest.main() |