misc/cwfs/cwfs_test.py
changeset 0 b97547f5f1fa
child 1977 606923dff11b
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 from logilab.common.testlib import TestCase, unittest_main
       
     2 
       
     3 import cubicwebfs
       
     4 import sre
       
     5 
       
     6 def spec_parser(filename) :
       
     7     """
       
     8     extract tests from specification
       
     9     """
       
    10     sections = []
       
    11     buffer = ""
       
    12     in_section = False
       
    13     for line in file(filename) :
       
    14         if line.startswith('Test::'):
       
    15             in_section = True
       
    16             buffer = ""
       
    17         elif in_section :
       
    18             if line.startswith("  ") or not line.strip() :
       
    19                 buffer += line.lstrip()
       
    20             else :
       
    21                 sections.append(buffer)
       
    22                 in_section = False
       
    23     tests = []
       
    24     for section in sections :
       
    25         subsections = [t for t in section.strip().split('$ ls') if t]
       
    26         for subsection in subsections :
       
    27             path, results = subsection.splitlines()[0], subsection.splitlines()[1:]
       
    28             path = path.strip()
       
    29             items = set([i for i in sre.split('[\t\n]', '\n'.join(results)) if i])
       
    30             tests.append((path, items))
       
    31     return tests
       
    32 
       
    33 tests = spec_parser("cubicwebfs-spec.txt")
       
    34 
       
    35 class monTC(TestCase) :
       
    36     pass
       
    37 
       
    38 for index, (path, results) in enumerate(tests) :
       
    39     def f(self, p=path, r=results) :
       
    40         res = set(cubicwebfs.ls(p))
       
    41         self.assertEqual(r, res) #, 'en trop %s\nmanque %s' % (r-results,results-r))
       
    42     f.__doc__ = "%s %s"%(index,path)
       
    43     setattr(monTC,'test_%s'%index,f)
       
    44 
       
    45 if __name__ == '__main__':
       
    46     unittest_main()