daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Feb 19th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import inspect
  2.  
  3. def labelable_yielded_case(case):
  4.  
  5.     argspec = inspect.getargspec(case)
  6.     if argspec.defaults is not None:
  7.         defaults_list = [''] * (len(argspec.args) - len(argspec.defaults)) + argspec.defaults
  8.     else:
  9.         defaults_list = [''] * len(argspec.args)
  10.     argument_defaults_list = zip(argspec.args, defaults_list)
  11.     case_wrappers = []
  12.  
  13.     def add_description(wrapper_id, argument_dict):
  14.  
  15.         case_wrappers[wrapper_id].description = case.__doc__.format(**argument_dict)
  16.  
  17.     def case_factory(*factory_args, **factory_kwargs):
  18.  
  19.         def case_wrapper_wrapper():
  20.  
  21.             wrapper_id = len(case_wrappers)
  22.  
  23.             def case_wrapper(*args, **kwargs):
  24.  
  25.                 args = factory_args + args
  26.                 argument_list = []
  27.                 for argument in argument_defaults_list:
  28.                     argument_list.append(list(argument))
  29.                 for index, value in enumerate(args):
  30.                     argument_list[index][1] = value
  31.                 argument_dict = dict(argument_list)
  32.                 argument_dict.update(factory_kwargs)
  33.                 argument_dict.update(kwargs)
  34.                 add_description(wrapper_id, argument_dict)
  35.                 return case(*args, **kwargs)
  36.  
  37.             case_wrappers.append(case_wrapper)
  38.             case_wrapper.__name__ = case.__name__
  39.             return case_wrapper
  40.  
  41.         return case_wrapper_wrapper()
  42.  
  43.     return case_factory
  44.  
  45.  
  46. class TestTest(object):
  47.  
  48.     def __init__(self):
  49.  
  50.         self.data = None
  51.  
  52.     def setUp(self):
  53.  
  54.         print 'setup', self
  55.         self.data = (1,2,3)
  56.  
  57.     def test_all(self):
  58.  
  59.         for index, value in enumerate((1,2,3)):
  60.             yield self.validate_equality(), index, value
  61.  
  62.     def test_all_again(self):
  63.  
  64.         for index, value in enumerate((1,2,3)):
  65.             yield self.validate_equality_again, index, value
  66.  
  67.     @labelable_yielded_case
  68.     def validate_equality(self, index, value):
  69.         '''element {index} equals {value}'''
  70.  
  71.         print 'test', self
  72.         assert self.data[index] == value, 'expected %d got %d' % (value, self.data[index])
  73.  
  74.     def validate_equality_again(self, index, value):
  75.  
  76.         print 'test', self
  77.         assert self.data[index] == value, 'expected %d got %d' % (value, self.data[index])
  78.  
  79.     validate_equality_again.description = 'again'
  80.    
  81. import inspect
  82.  
  83. def labelable_yielded_case(case):
  84.  
  85.     argspec = inspect.getargspec(case)
  86.     if argspec.defaults is not None:
  87.         defaults_list = [''] * (len(argspec.args) - len(argspec.defaults)) + argspec.defaults
  88.     else:
  89.         defaults_list = [''] * len(argspec.args)
  90.     argument_defaults_list = zip(argspec.args, defaults_list)
  91.     case_wrappers = []
  92.  
  93.     def add_description(wrapper_id, argument_dict):
  94.  
  95.         case_wrappers[wrapper_id].description = case.__doc__.format(**argument_dict)
  96.  
  97.     def case_factory(*factory_args, **factory_kwargs):
  98.  
  99.         def case_wrapper_wrapper():
  100.  
  101.             wrapper_id = len(case_wrappers)
  102.  
  103.             def case_wrapper(*args, **kwargs):
  104.  
  105.                 argument_list = []
  106.                 for argument in argument_defaults_list:
  107.                     argument_list.append(list(argument))
  108.                 for index, value in enumerate(args):
  109.                     argument_list[index][1] = value
  110.                 argument_dict = dict(argument_list)
  111.                 argument_dict.update(kwargs)
  112.                 add_description(wrapper_id, argument_dict)
  113.                 return case(**argument_dict)
  114.  
  115.             case_wrappers.append(case_wrapper)
  116.             case_name = case.__name__ + str(wrapper_id)
  117.             case_wrapper.__name__ = case_name
  118.             if factory_args:
  119.                 setattr(factory_args[0].__class__, case_name, case_wrapper)
  120.                 return getattr(factory_args[0].__class__, case_name)
  121.             else:
  122.                 return case_wrapper
  123.  
  124.         return case_wrapper_wrapper()
  125.  
  126.     return case_factory
  127.  
  128.  
  129. class TestTest(object):
  130.  
  131.     def __init__(self):
  132.  
  133.         self.data = None
  134.  
  135.     def setUp(self):
  136.  
  137.         self.data = (1,2,3)
  138.  
  139.     def test_all(self):
  140.  
  141.         for index, value in enumerate((1,2,3)):
  142.             yield self.validate_equality(), index, value
  143.  
  144.     @labelable_yielded_case
  145.     def validate_equality(self, index, value):
  146.         '''element {index} equals {value}'''
  147.  
  148.         assert self.data[index] == value, 'expected %d got %d' % (value, self.data[index])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top