<s> try : <EOL> import unittest2 as unittest <EOL> except ImportError : <EOL> import unittest <EOL> from functools import wraps <EOL> import collections <EOL> def add_metaclass ( metaclass ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( cls ) : <EOL> orig_vars = cls . __dict__ . copy ( ) <EOL> orig_vars . pop ( '<STR_LIT>' , None ) <EOL> orig_vars . pop ( '<STR_LIT>' , None ) <EOL> slots = orig_vars . get ( '<STR_LIT>' ) <EOL> if slots is not None : <EOL> if isinstance ( slots , str ) : <EOL> slots = [ slots ] <EOL> for slots_var in slots : <EOL> orig_vars . pop ( slots_var ) <EOL> return metaclass ( cls . __name__ , cls . __bases__ , orig_vars ) <EOL> return wrapper <EOL> def augment_method_docstring ( method , new_class_dict , classname , <EOL> param_names , param_values , new_method ) : <EOL> param_assignments_str = '<STR_LIT>' . join ( <EOL> [ '<STR_LIT>' % ( k , v ) for ( k , v ) in zip ( param_names , param_values ) ] ) <EOL> extra_doc = "<STR_LIT>" % ( <EOL> method . __name__ , new_class_dict . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> classname , param_assignments_str ) <EOL> try : <EOL> new_method . __doc__ = extra_doc + new_method . __doc__ <EOL> except TypeError : <EOL> new_method . __doc__ = extra_doc <EOL> class ParameterizedTestCaseMetaClass ( type ) : <EOL> method_counter = { } <EOL> def __new__ ( meta , classname , bases , class_dict ) : <EOL> new_class_dict = { } <EOL> for attr_name , attr_value in list ( class_dict . items ( ) ) : <EOL> if isinstance ( attr_value , collections . Callable ) and hasattr ( attr_value , '<STR_LIT>' ) : <EOL> method = attr_value <EOL> param_names = attr_value . param_names <EOL> data = attr_value . data <EOL> func_name_format = attr_value . func_name_format <EOL> meta . process_method ( <EOL> classname , method , param_names , data , new_class_dict , <EOL> func_name_format ) <EOL> else : <EOL> new_class_dict [ attr_name ] = attr_value <EOL> return type . __new__ ( meta , classname , bases , new_class_dict ) <EOL> @ classmethod <EOL> def process_method ( <EOL> cls , classname , method , param_names , data , new_class_dict , <EOL> func_name_format ) : <EOL> method_counter = cls . method_counter <EOL> for param_values in data : <EOL> new_method = cls . new_method ( method , param_values ) <EOL> method_counter [ method . __name__ ] = method_counter . get ( method . __name__ , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> case_data = dict ( list ( zip ( param_names , param_values ) ) ) <EOL> case_data [ '<STR_LIT>' ] = method . __name__ <EOL> case_data [ '<STR_LIT>' ] = method_counter [ method . __name__ ] <EOL> new_method . __name__ = func_name_format . format ( ** case_data ) <EOL> augment_method_docstring ( <EOL> method , new_class_dict , classname , <EOL> param_names , param_values , new_method ) <EOL> new_class_dict [ new_method . __name__ ] = new_method <EOL> @ classmethod <EOL> def new_method ( cls , method , param_values ) : <EOL> @ wraps ( method ) <EOL> def new_method ( self ) : <EOL> return method ( self , * param_values ) <EOL> return new_method <EOL> @ add_metaclass ( ParameterizedTestCaseMetaClass ) <EOL> class ParameterizedTestMixin ( object ) : <EOL> @ classmethod <EOL> def parameterize ( cls , param_names , data , <EOL> func_name_format = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def newfunc ( * arg , ** kwargs ) : <EOL> return func ( * arg , ** kwargs ) <EOL> newfunc . param_names = param_names <EOL> newfunc . data = data <EOL> newfunc . func_name_format = func_name_format <EOL> return newfunc <EOL> return decorator <EOL> @ add_metaclass ( ParameterizedTestCaseMetaClass ) <EOL> class ParameterizedTestCase ( unittest . TestCase , ParameterizedTestMixin ) : <EOL> pass </s>
