summaryrefslogtreecommitdiff
path: root/pylint/test/extensions/test_elseif_used.py
blob: 72dc9f45df1e5ee7b26aa21af700b54812e243b1 (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
"""Tests for the pylint checker in :mod:`pylint.extensions.check_elif
"""

import os
import os.path as osp
import unittest

from pylint import checkers
from pylint.lint import PyLinter
from pylint.reporters import BaseReporter
from pylint.utils import register_plugins


class TestReporter(BaseReporter):

    def handle_message(self, msg):
        self.messages.append(msg)

    def on_set_current_module(self, module, filepath):
        self.messages = []


class CheckElseIfUsedTC(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls._linter = PyLinter()
        cls._linter.set_reporter(TestReporter())
        checkers.initialize(cls._linter)
        plugins_path = osp.join(osp.dirname(osp.abspath(__file__)), os.pardir,
                                os.pardir, 'extensions')
        register_plugins(cls._linter, plugins_path)

    def test_elseif_message(self):
        elif_test = osp.join(osp.dirname(osp.abspath(__file__)), 'data',
                             'elif.py')
        self._linter.check([elif_test])
        msgs = self._linter.reporter.messages
        self.assertEqual(len(msgs), 2)
        for msg in msgs:
            self.assertEqual(msg.symbol, 'else-if-used')
            self.assertEqual(msg.msg,
                             'Consider using "elif" instead of "else if"')
        self.assertEqual(msgs[0].line, 9)
        self.assertEqual(msgs[1].line, 21)


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