diff options
-rw-r--r-- | test/unittest_testlib.py | 39 | ||||
-rw-r--r-- | testlib.py | 32 |
2 files changed, 70 insertions, 1 deletions
diff --git a/test/unittest_testlib.py b/test/unittest_testlib.py index 32417c2..03050ea 100644 --- a/test/unittest_testlib.py +++ b/test/unittest_testlib.py @@ -23,7 +23,7 @@ from unittest import TestSuite 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 +from logilab.common.testlib import with_tempdir, tag class MockTestCase(TestCase): def __init__(self): @@ -653,6 +653,43 @@ class DecoratorTC(TestCase): self.assertListEquals(list(os.walk(tempdir)), [(tempdir,[],[])]) +class TagTC(TestCase): + + def setUp(self): + @tag('testing', 'bob') + def bob(a, b, c): + return (a + b) * c + + self.func = bob + + def test_tag_decorator(self): + bob = self.func + + self.assertEquals(bob(2, 3, 7), 35) + self.assertTrue(hasattr(bob, 'tags')) + self.assertSetEquals(bob.tags, set(['testing','bob'])) + + + def test_tags_class(self): + tags = self.func.tags + + self.assertTrue(tags['testing']) + self.assertFalse(tags['Not inside']) + + def test_tags_match(self): + tags = self.func.tags + + self.assertTrue(tags.match('testing')) + self.assertFalse(tags.match('other')) + + self.assertFalse(tags.match('testing and coin')) + self.assertTrue(tags.match('testing or other')) + + self.assertTrue(tags.match('not other')) + + self.assertTrue(tags.match('not other or (testing and bibi)')) + self.assertTrue(tags.match('other or (testing and bob)')) + if __name__ == '__main__': unittest_main() @@ -1696,3 +1696,35 @@ def enable_dbc(*args): class AttrObject: # XXX cf mock_object def __init__(self, **kwargs): self.__dict__.update(kwargs) + + +#class Tagged(object): +# def __init__(self, tags, callable): +# self.__tag = tags +# self.__callable = callable +# +# def __getttr__(self, attr): +# return getattr(self, attr) +# +# def __setattr__(self, attr, value): +# return setattr(self.__callable, attr, value) + +def tag(*args): + """descriptor adding tag to a function""" + def desc(func): + assert not hasattr(func, 'tags') + setattr(func, 'tags', Tags(args)) + return func + return desc + + + +class Tags(set): + + def __getitem__(self, key): + return key in self + + + + def match(self, exp): + return eval(exp, {}, self) |