diff options
author | Fabrice Douchant <Fabrice.Douchant@logilab.fr> | 2008-10-21 09:46:10 +0200 |
---|---|---|
committer | Fabrice Douchant <Fabrice.Douchant@logilab.fr> | 2008-10-21 09:46:10 +0200 |
commit | b8bac257593f24fd87bd6a86bceb6e7c0b38873d (patch) | |
tree | b700c3327d674e628c02d1f7ce2941a65255ff8e | |
parent | ff2e4a52357ca16b14d99105a077645c5f478008 (diff) | |
download | logilab-common-b8bac257593f24fd87bd6a86bceb6e7c0b38873d.tar.gz |
moving decorators require_version and require_module to testlib.py. Makes more sens.
-rw-r--r-- | test/unittest_testlib.py | 69 | ||||
-rw-r--r-- | testlib.py | 39 |
2 files changed, 108 insertions, 0 deletions
diff --git a/test/unittest_testlib.py b/test/unittest_testlib.py index d71fbfc..93f4490 100644 --- a/test/unittest_testlib.py +++ b/test/unittest_testlib.py @@ -24,6 +24,7 @@ from logilab.common.testlib import TestCase, SkipAwareTextTestRunner from logilab.common.testlib import mock_object, NonStrictTestLoader, create_files from logilab.common.testlib import capture_stdout, unittest_main, InnerTest from logilab.common.testlib import with_tempdir, tag +from logilab.common.testlib import require_version, require_module class MockTestCase(TestCase): def __init__(self): @@ -706,6 +707,74 @@ class TagTC(TestCase): self.assertTrue(tags.match('not other or (testing and bibi)')) self.assertTrue(tags.match('other or (testing and bob)')) +class DecoratorsTC(TestCase): + def test_require_version_good(self): + """ should return the same function + """ + def func() : + pass + sys.version_info = (2, 5, 5, 'final', 4) + current = sys.version_info[:3] + compare = ('2.4', '2.5', '2.5.4', '2.5.5') + for version in compare: + decorator = require_version(version) + self.assertEquals(func, decorator(func), '%s =< %s : function \ + return by the decorator should be the same.' % (version, + '.'.join([str(element) for element in current]))) + + def test_require_version_bad(self): + """ should return a different function : skipping test + """ + def func() : + pass + sys.version_info = (2, 5, 5, 'final', 4) + current = sys.version_info[:3] + compare = ('2.5.6', '2.6', '2.6.5') + for version in compare: + decorator = require_version(version) + self.assertNotEquals(func, decorator(func), '%s >= %s : function \ + return by the decorator should NOT be the same.' + % ('.'.join([str(element) for element in current]), version)) + + def test_require_version_exception(self): + """ should throw a ValueError exception + """ + def func() : + pass + compare = ('2.5.a', '2.a', 'azerty') + for version in compare: + decorator = require_version(version) + self.assertRaises(ValueError, decorator, func) + + def test_require_module_good(self): + """ should return the same function + """ + def func() : + pass + module = 'sys' + decorator = require_module(module) + self.assertEquals(func, decorator(func), 'module %s exists : function \ + return by the decorator should be the same.' % module) + + def test_require_module_bad(self): + """ should return a different function : skipping test + """ + def func() : + pass + modules = ('bla', 'blo', 'bli') + for module in modules: + try: + __import__(module) + pass + except ImportError: + decorator = require_module(module) + self.assertNotEquals(func, decorator(func), 'module %s does \ + not exist : function return by the decorator should \ + NOT be the same.' % module) + return + print 'all modules in %s exist. Could not test %s' % (', '.join(modules), + sys._getframe().f_code.co_name) + if __name__ == '__main__': unittest_main() @@ -1784,3 +1784,42 @@ class Tags(set): def match(self, exp): return eval(exp, {}, self) + +def require_version(version): + """ Compare version of python interpretor to the given one. Skip the test + if older. + """ + def check_require_version(f): + version_elements = version.split('.') + try: + compare = tuple([int(v) for v in version_elements]) + except ValueError: + raise ValueError('%s is not a correct version : should be X.Y[.Z].' % version) + current = sys.version_info[:3] + #print 'comp', current, compare + if current < compare: + #print 'version too old' + def new_f(self, *args, **kwargs): + self.skip('Need at least %s version of python. Current version is %s.' % (version, '.'.join([str(element) for element in current]))) + new_f.__name__ = f.__name__ + return new_f + else: + #print 'version young enough' + return f + return check_require_version + +def require_module(module): + """ Check if the given module is loaded. Skip the test if not. + """ + def check_require_module(f): + try: + __import__(module) + #print module, 'imported' + return f + except ImportError: + #print module, 'can not be imported' + def new_f(self, *args, **kwargs): + self.skip('%s can not be imported.' % module) + new_f.__name__ = f.__name__ + return new_f + return check_require_module |