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_spelling.py @ 745

History | View | Annotate | Download (3.43 KB)

1
# Copyright 2014 Michal Nowikowski.
2
#
3
# This program is free software; you can redistribute it and/or modify it under
4
# the terms of the GNU General Public License as published by the Free Software
5
# Foundation; either version 2 of the License, or (at your option) any later
6
# version.
7
#
8
# This program is distributed in the hope that it will be useful, but WITHOUT
9
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11
#
12
# You should have received a copy of the GNU General Public License along with
13
# this program; if not, write to the Free Software Foundation, Inc.,
14
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15
"""Unittest for the spelling checker."""
16

    
17
import unittest
18

    
19
from astroid import test_utils
20

    
21
from pylint.checkers import spelling
22
from pylint.testutils import (
23
    CheckerTestCase, Message, set_config, tokenize_str,
24
)
25

    
26
# try to create enchant dictionary
27
try:
28
    import enchant
29
except ImportError:
30
    enchant = None
31

    
32
spell_dict = None
33
if enchant is not None:
34
    try:
35
        enchant.Dict("en_US")
36
        spell_dict = "en_US"
37
    except enchant.DictNotFoundError:
38
        pass
39

    
40

    
41
class SpellingCheckerTest(CheckerTestCase):
42
    CHECKER_CLASS = spelling.SpellingChecker
43

    
44
    @unittest.skipIf(spell_dict is None,
45
                     "missing python-enchant package or missing "
46
                     "spelling dictionaries")
47
    @set_config(spelling_dict=spell_dict)
48
    def test_check_bad_coment(self):
49
        with self.assertAddsMessages(
50
            Message('wrong-spelling-in-comment', line=1,
51
                    args=('coment', '# bad coment',
52
                          '      ^^^^^^',
53
                          "comet' or 'comment' or 'moment' or 'foment"))):
54
            self.checker.process_tokens(tokenize_str("# bad coment"))
55

    
56
    @unittest.skipIf(spell_dict is None,
57
                     "missing python-enchant package or missing "
58
                     "spelling dictionaries")
59
    @set_config(spelling_dict=spell_dict)
60
    def test_check_bad_docstring(self):
61
        stmt = test_utils.extract_node(
62
            'def fff():\n   """bad coment"""\n   pass')
63
        with self.assertAddsMessages(
64
            Message('wrong-spelling-in-docstring', line=2,
65
                    args=('coment', 'bad coment',
66
                          '    ^^^^^^',
67
                          "comet' or 'comment' or 'moment' or 'foment"))):
68
            self.checker.visit_functiondef(stmt)
69

    
70
        stmt = test_utils.extract_node(
71
            'class Abc(object):\n   """bad coment"""\n   pass')
72
        with self.assertAddsMessages(
73
            Message('wrong-spelling-in-docstring', line=2,
74
                    args=('coment', 'bad coment',
75
                          '    ^^^^^^',
76
                          "comet' or 'comment' or 'moment' or 'foment"))):
77
            self.checker.visit_classdef(stmt)
78

    
79
    @unittest.skipIf(spell_dict is None,
80
                     "missing python-enchant package or missing "
81
                     "spelling dictionaries")
82
    @set_config(spelling_dict=spell_dict)
83
    def test_invalid_docstring_characters(self):
84
        stmt = test_utils.extract_node(
85
            'def fff():\n   """test\\x00"""\n   pass')
86
        with self.assertAddsMessages(
87
            Message('invalid-characters-in-docstring', line=2,
88
                    args=('test\x00',))):
89
            self.checker.visit_functiondef(stmt)
90

    
91

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