diff options
author | FELD Boris <lothiraldan@gmail.com> | 2012-07-04 13:12:18 +0200 |
---|---|---|
committer | FELD Boris <lothiraldan@gmail.com> | 2012-07-04 13:12:18 +0200 |
commit | b28810c7694f99547287987d034d49f0929203cc (patch) | |
tree | 981e05927a0dfa211d27b4b58482b8ea06ac25df /test/test_func.py | |
parent | 8f46d8c7badf1762e464e7b14269911fc6e4adb6 (diff) | |
download | pylint-b28810c7694f99547287987d034d49f0929203cc.tar.gz |
Refactor test_func in order to extract common part for reutilization in pylint-brain
Diffstat (limited to 'test/test_func.py')
-rw-r--r-- | test/test_func.py | 202 |
1 files changed, 40 insertions, 162 deletions
diff --git a/test/test_func.py b/test/test_func.py index cdfc116..c71ee38 100644 --- a/test/test_func.py +++ b/test/test_func.py @@ -18,118 +18,35 @@ import unittest import sys import re -from os import linesep, getcwd -from os.path import exists, abspath, dirname, join -from logilab.common import testlib - -from utils import get_tests_info, fix_path, TestReporter +from os import getcwd +from os.path import abspath, dirname, join -from logilab.astng import MANAGER -from pylint.lint import PyLinter -from pylint import checkers +from logilab.common import testlib -test_reporter = TestReporter() -linter = PyLinter() -linter.set_reporter(test_reporter) -linter.config.persistent = 0 -checkers.initialize(linter) -linter.global_set_option('required-attributes', ('__revision__',)) +from pylint.testutils import (make_tests, LintTestUsingModule, LintTestUsingFile, + cb_test_gen, linter, test_reporter) PY26 = sys.version_info >= (2, 6) PY3K = sys.version_info >= (3, 0) - -if linesep != '\n': - LINE_RGX = re.compile(linesep) - def ulines(string): - return LINE_RGX.sub('\n', string) -else: - def ulines(string): - return string - -INFO_TEST_RGX = re.compile('^func_i\d\d\d\d$') - -MSG_DIR = join(dirname(abspath(__file__)), 'messages') +# Configure paths INPUT_DIR = join(dirname(abspath(__file__)), 'input') +MSG_DIR = join(dirname(abspath(__file__)), 'messages') -def exception_str(self, ex): - """function used to replace default __str__ method of exception instances""" - return 'in %s\n:: %s' % (ex.file, ', '.join(ex.args)) - -class LintTestUsingModule(testlib.TestCase): - DEFAULT_PACKAGE = 'input' - package = DEFAULT_PACKAGE - linter = linter - module = None - depends = None - - _TEST_TYPE = 'module' - - def shortDescription(self): - values = { 'mode' : self._TEST_TYPE, - 'input': self.module, - 'pkg': self.package, - 'cls': self.__class__.__name__} - - if self.package == self.DEFAULT_PACKAGE: - msg = '%(mode)s test of input file "%(input)s" (%(cls)s)' - else: - msg = '%(mode)s test of input file "%(input)s" in "%(pkg)s" (%(cls)s)' - return msg % values - - def test_functionality(self): - tocheck = [self.package+'.'+self.module] - if self.depends: - tocheck += [self.package+'.%s' % name.replace('.py', '') - for name, file in self.depends] - self._test(tocheck) - - def _test(self, tocheck): - if PY3K: # XXX remove this if block as soon as python bugs fixed - module = tocheck[0] - if module == 'input.func_unknown_encoding': - self.skipTest('FIXME: http://bugs.python.org/issue10588 ' - '(unexpected SyntaxError)') - elif 'func_w0613' in module: - self.skipTest('FIXME: http://bugs.python.org/issue10445 ' - '(no line number on function args)') - if INFO_TEST_RGX.match(self.module): - self.linter.enable('I') - else: - self.linter.disable('I') - try: - self.linter.check(tocheck) - except Exception, ex: - # need finalization to restore a correct state - self.linter.reporter.finalize() - ex.file = tocheck - print ex - ex.__str__ = exception_str - raise - got = self.linter.reporter.finalize() - self.assertMultiLineEqual(got, self._get_expected()) - - - def _get_expected(self): - if self.module.startswith('func_noerror_'): - expected = '' - else: - output = open(self.output) - expected = output.read().strip() + '\n' - output.close() - return expected - -class LintTestUsingFile(LintTestUsingModule): +# Classes - _TEST_TYPE = 'file' +class LintTestNonExistentModuleTC(LintTestUsingModule): + module = 'nonexistent' + _get_expected = lambda self: 'F: 1: No module named nonexistent\n' + tags = testlib.Tags(('generated','pylint_input_%s' % module)) +class LintTestNonExistentFileTC(LintTestUsingFile): + module = join(INPUT_DIR, 'nonexistent.py') + _get_expected = lambda self: 'F: 1: No module named %s\n' % self.module[len(getcwd())+1 :] + tags = testlib.Tags(('generated', 'pylint_input_%s' % module)) def test_functionality(self): - tocheck = [join(INPUT_DIR, self.module + '.py')] - if self.depends: - tocheck += [join(INPUT_DIR, name) for name, _file in self.depends] - self._test(tocheck) - + self._test([self.module]) class TestTests(testlib.TestCase): """check that all testable messages have been checked""" @@ -158,79 +75,40 @@ class TestTests(testlib.TestCase): else: self.assertEqual(todo, ['I0001']) -#bycat = {} -#for msgid in linter._messages.keys(): -# bycat[msgid[0]] = bycat.setdefault(msgid[0], 0) + 1 -#for cat, val in bycat.items(): -# print '%s: %s' % (cat, val) -#print 'total', sum(bycat.values()) -# -# on 2007/02/17: -# -# W: 48 -# E: 42 -# R: 15 -# C: 13 -# F: 7 -# I: 5 -# total 130 - -class LintTestNonExistentModuleTC(LintTestUsingModule): - module = 'nonexistent' - _get_expected = lambda self: 'F: 1: No module named nonexistent\n' - tags = testlib.Tags(('generated','pylint_input_%s' % module)) - -class LintTestNonExistentFileTC(LintTestUsingFile): - module = join(INPUT_DIR, 'nonexistent.py') - _get_expected = lambda self: 'F: 1: No module named %s\n' % self.module[len(getcwd())+1 :] - tags = testlib.Tags(('generated', 'pylint_input_%s' % module)) +class LintBuiltinModuleTest(LintTestUsingModule): + output = join(MSG_DIR, 'builtin_module.txt') + module = 'sys' def test_functionality(self): - self._test([self.module]) + self._test(['sys']) -def make_tests(filter_rgx): - """generate tests classes from test info +# Callbacks - return the list of generated test classes - """ - if filter_rgx: - is_to_run = re.compile(filter_rgx).search +base_cb_file = cb_test_gen(LintTestUsingFile) + +def cb_file(*args): + if MODULES_ONLY: + return None else: - is_to_run = lambda x: 1 - tests = [] - for module_file, messages_file in get_tests_info('func_', '.py'): - if not is_to_run(module_file): - continue - base = module_file.replace('func_', '').replace('.py', '') + return base_cb_file(*args) - dependencies = get_tests_info(base, '.py') +callbacks = [cb_test_gen(LintTestUsingModule), + cb_file] - class LintTestUsingModuleTC(LintTestUsingModule): - module = module_file.replace('.py', '') - output = messages_file - depends = dependencies or None - tags = testlib.Tags(('generated','pylint_input_%s' % module)) - tests.append(LintTestUsingModuleTC) +# Gen tests - if MODULES_ONLY: - continue +def gen_tests(filter_rgx): + tests = make_tests(INPUT_DIR, MSG_DIR, filter_rgx, callbacks) - class LintTestUsingFileTC(LintTestUsingFile): - module = module_file.replace('.py', '') - output = messages_file - depends = dependencies or None - tags = testlib.Tags(('generated', 'pylint_input_%s' % module)) - tests.append(LintTestUsingFileTC) + if filter_rgx: + is_to_run = re.compile(filter_rgx).search + else: + is_to_run = lambda x: 1 if is_to_run('nonexistent'): tests.append(LintTestNonExistentModuleTC) if not MODULES_ONLY: tests.append(LintTestNonExistentFileTC) - class LintBuiltinModuleTest(LintTestUsingModule): - output = join(MSG_DIR, 'builtin_module.txt') - module = 'sys' - def test_functionality(self): - self._test(['sys']) tests.append(LintBuiltinModuleTest) if not filter_rgx: @@ -239,12 +117,14 @@ def make_tests(filter_rgx): return tests +# Create suite + FILTER_RGX = None MODULES_ONLY = False def suite(): return testlib.TestSuite([unittest.makeSuite(test, suiteClass=testlib.TestSuite) - for test in make_tests(FILTER_RGX)]) + for test in gen_tests(FILTER_RGX)]) if __name__=='__main__': if '-m' in sys.argv: @@ -255,5 +135,3 @@ if __name__=='__main__': FILTER_RGX = sys.argv[1] del sys.argv[1] testlib.unittest_main(defaultTest='suite') - - |