Add else-if-used rule functional test

authorLaura Médioni <laura.medioni@logilab.fr>
changeset2871c4d70844
branchdefault
phasepublic
hiddenno
parent revision#aeafc897ae82 fix elif-used rule
child revision#fd86093b48c1 Refactor things through the imports checker
files modified by this revision
pylint/test/extensions/data/elif.py
pylint/test/extensions/test_elseif_used.py
# HG changeset patch
# User Laura Médioni <laura.medioni@logilab.fr>
# Date 1448890984 -3600
# Mon Nov 30 14:43:04 2015 +0100
# Node ID 2871c4d7084478b5db02f384e4e5167641c698f2
# Parent aeafc897ae82bc33288e000b5b322d123e4e52d8
Add else-if-used rule functional test

diff --git a/pylint/test/extensions/data/elif.py b/pylint/test/extensions/data/elif.py
@@ -0,0 +1,26 @@
1 +"""Checks use of "else if" triggers a refactor message"""
2 +
3 +def my_function():
4 +    """docstring"""
5 +    myint = 2
6 +    if myint > 5:
7 +        pass
8 +    else:
9 +        if myint <= 5:
10 +            pass
11 +        else:
12 +            myint = 3
13 +            if myint > 2:
14 +                if myint > 3:
15 +                    pass
16 +                elif myint == 3:
17 +                    pass
18 +                elif myint < 3:
19 +                    pass
20 +                else:
21 +                    if myint:
22 +                        pass
23 +            else:
24 +                if myint:
25 +                    pass
26 +                myint = 4
diff --git a/pylint/test/extensions/test_elseif_used.py b/pylint/test/extensions/test_elseif_used.py
@@ -0,0 +1,49 @@
27 +"""Tests for the pylint checker in :mod:`pylint.extensions.check_elif
28 +"""
29 +
30 +import os
31 +import os.path as osp
32 +import unittest
33 +
34 +from pylint import checkers
35 +from pylint.lint import PyLinter
36 +from pylint.reporters import BaseReporter
37 +from pylint.utils import register_plugins
38 +
39 +
40 +class TestReporter(BaseReporter):
41 +
42 +    def handle_message(self, msg):
43 +        self.messages.append(msg)
44 +
45 +    def on_set_current_module(self, module, filepath):
46 +        self.messages = []
47 +
48 +
49 +class CheckElseIfUsedTC(unittest.TestCase):
50 +
51 +    @classmethod
52 +    def setUpClass(cls):
53 +        cls._linter = PyLinter()
54 +        cls._linter.set_reporter(TestReporter())
55 +        checkers.initialize(cls._linter)
56 +        plugins_path = osp.join(osp.dirname(osp.abspath(__file__)), os.pardir,
57 +                                os.pardir, 'extensions')
58 +        register_plugins(cls._linter, plugins_path)
59 +
60 +    def test_elseif_message(self):
61 +        elif_test = osp.join(osp.dirname(osp.abspath(__file__)), 'data',
62 +                             'elif.py')
63 +        self._linter.check([elif_test])
64 +        msgs = self._linter.reporter.messages
65 +        self.assertEqual(len(msgs), 2)
66 +        for msg in msgs:
67 +            self.assertEqual(msg.symbol, 'else-if-used')
68 +            self.assertEqual(msg.msg,
69 +                             'Consider using "elif" instead of "else if"')
70 +        self.assertEqual(msgs[0].line, 9)
71 +        self.assertEqual(msgs[1].line, 21)
72 +
73 +
74 +if __name__ == '__main__':
75 +    unittest.main()