<s> """<STR_LIT>""" <EOL> import functools <EOL> import logging <EOL> import sys <EOL> import types <EOL> import unittest <EOL> from nose2 import exceptions , util <EOL> from nose2 . events import Plugin <EOL> from nose2 . compat import unittest as ut2 <EOL> log = logging . getLogger ( __name__ ) <EOL> __unittest = True <EOL> class Generators ( Plugin ) : <EOL> """<STR_LIT>""" <EOL> alwaysOn = True <EOL> configSection = '<STR_LIT>' <EOL> def registerInSubprocess ( self , event ) : <EOL> event . pluginClasses . append ( self . __class__ ) <EOL> def unpack ( self , generator ) : <EOL> for index , func_args in enumerate ( generator ) : <EOL> try : <EOL> func , args = func_args <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> yield index , ( func , args ) <EOL> except ValueError : <EOL> func , args = func_args [ <NUM_LIT:0> ] , func_args [ <NUM_LIT:1> : ] <EOL> yield index , ( func , args ) <EOL> def loadTestsFromTestCase ( self , event ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' , event . testCase ) <EOL> testCaseClass = event . testCase <EOL> for name in dir ( testCaseClass ) : <EOL> method = getattr ( testCaseClass , name ) <EOL> if ( name . startswith ( self . session . testMethodPrefix ) and <EOL> hasattr ( getattr ( testCaseClass , name ) , '<STR_LIT>' ) and <EOL> util . isgenerator ( method ) ) : <EOL> instance = testCaseClass ( name ) <EOL> event . extraTests . extend ( <EOL> self . _testsFromGenerator ( <EOL> event , name , method ( instance ) , testCaseClass ) <EOL> ) <EOL> def loadTestsFromTestClass ( self , event ) : <EOL> testCaseClass = event . testCase <EOL> for name in dir ( testCaseClass ) : <EOL> method = getattr ( testCaseClass , name ) <EOL> if ( name . startswith ( self . session . testMethodPrefix ) and <EOL> hasattr ( getattr ( testCaseClass , name ) , '<STR_LIT>' ) and <EOL> util . isgenerator ( method ) ) : <EOL> instance = testCaseClass ( ) <EOL> event . extraTests . extend ( <EOL> self . _testsFromGeneratorMethod ( <EOL> event , name , method , instance ) <EOL> ) <EOL> def getTestCaseNames ( self , event ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' , event . testCase ) <EOL> names = filter ( event . isTestMethod , dir ( event . testCase ) ) <EOL> klass = event . testCase <EOL> for name in names : <EOL> method = getattr ( klass , name ) <EOL> if util . isgenerator ( method ) : <EOL> event . excludedNames . append ( name ) <EOL> def getTestMethodNames ( self , event ) : <EOL> return self . getTestCaseNames ( event ) <EOL> def loadTestsFromName ( self , event ) : <EOL> """<STR_LIT>""" <EOL> original_name = name = event . name <EOL> module = event . module <EOL> try : <EOL> result = util . test_from_name ( name , module ) <EOL> except ( AttributeError , ImportError ) : <EOL> event . handled = True <EOL> return event . loader . failedLoadTests ( name , sys . exc_info ( ) ) <EOL> if result is None : <EOL> return <EOL> parent , obj , name , index = result <EOL> if not util . isgenerator ( obj ) : <EOL> return <EOL> if ( index is None and not <EOL> isinstance ( parent , type ) and not <EOL> isinstance ( obj , types . FunctionType ) ) : <EOL> log . debug ( "<STR_LIT>" , obj ) <EOL> return <EOL> if ( parent and <EOL> isinstance ( parent , type ) and <EOL> issubclass ( parent , unittest . TestCase ) ) : <EOL> instance = parent ( obj . __name__ ) <EOL> tests = list ( <EOL> self . _testsFromGenerator ( <EOL> event , obj . __name__ , obj ( instance ) , parent ) <EOL> ) <EOL> elif ( parent and <EOL> isinstance ( parent , type ) ) : <EOL> method = obj <EOL> instance = parent ( ) <EOL> tests = list ( <EOL> self . _testsFromGeneratorMethod ( event , name , method , instance ) <EOL> ) <EOL> else : <EOL> tests = list ( self . _testsFromGeneratorFunc ( event , obj ) ) <EOL> if index is not None : <EOL> try : <EOL> tests = [ tests [ index - <NUM_LIT:1> ] ] <EOL> except IndexError : <EOL> raise exceptions . TestNotFoundError ( original_name ) <EOL> suite = event . loader . suiteClass ( ) <EOL> suite . addTests ( tests ) <EOL> event . handled = True <EOL> return suite <EOL> def loadTestsFromModule ( self , event ) : <EOL> """<STR_LIT>""" <EOL> module = event . module <EOL> def is_test ( obj ) : <EOL> return ( obj . __name__ . startswith ( self . session . testMethodPrefix ) <EOL> and util . isgenerator ( obj ) ) <EOL> tests = [ ] <EOL> for name in dir ( module ) : <EOL> obj = getattr ( module , name ) <EOL> if isinstance ( obj , types . FunctionType ) and is_test ( obj ) : <EOL> tests . extend ( self . _testsFromGeneratorFunc ( event , obj ) ) <EOL> event . extraTests . extend ( tests ) <EOL> def _testsFromGenerator ( self , event , name , generator , testCaseClass ) : <EOL> try : <EOL> for index , ( func , args ) in self . unpack ( generator ) : <EOL> method_name = util . name_from_args ( name , index , args ) <EOL> setattr ( testCaseClass , method_name , None ) <EOL> instance = testCaseClass ( method_name ) <EOL> delattr ( testCaseClass , method_name ) <EOL> def method ( func = func , args = args ) : <EOL> return func ( * args ) <EOL> method = functools . update_wrapper ( method , func ) <EOL> setattr ( instance , method_name , method ) <EOL> yield instance <EOL> except Exception as e : <EOL> test_name = '<STR_LIT>' % ( testCaseClass . __module__ , <EOL> testCaseClass . __name__ , <EOL> name ) <EOL> yield event . loader . failedLoadTests ( test_name , e ) <EOL> def _testsFromGeneratorFunc ( self , event , obj ) : <EOL> extras = list ( obj ( ) ) <EOL> name = '<STR_LIT>' % ( obj . __module__ , obj . __name__ ) <EOL> args = { } <EOL> setUp = getattr ( obj , '<STR_LIT>' , None ) <EOL> tearDown = getattr ( obj , '<STR_LIT>' , None ) <EOL> if setUp is not None : <EOL> args [ '<STR_LIT>' ] = setUp <EOL> if tearDown is not None : <EOL> args [ '<STR_LIT>' ] = tearDown <EOL> def createTest ( name ) : <EOL> return util . transplant_class ( <EOL> GeneratorFunctionCase , obj . __module__ ) ( name , ** args ) <EOL> for test in self . _testsFromGenerator ( event , name , extras , createTest ) : <EOL> yield test <EOL> def _testsFromGeneratorMethod ( self , event , name , method , instance ) : <EOL> extras = list ( method ( instance ) ) <EOL> name = "<STR_LIT>" % ( instance . __class__ . __module__ , <EOL> instance . __class__ . __name__ , <EOL> method . __name__ ) <EOL> args = { } <EOL> setUp = getattr ( instance , '<STR_LIT>' , None ) <EOL> tearDown = getattr ( instance , '<STR_LIT>' , None ) <EOL> if setUp is not None : <EOL> args [ '<STR_LIT>' ] = setUp <EOL> if tearDown is not None : <EOL> args [ '<STR_LIT>' ] = tearDown <EOL> def createTest ( name ) : <EOL> return util . transplant_class ( <EOL> GeneratorMethodCase ( instance . __class__ ) , <EOL> instance . __class__ . __module__ ) ( name , ** args ) <EOL> for test in self . _testsFromGenerator ( event , name , extras , createTest ) : <EOL> yield test <EOL> class GeneratorFunctionCase ( ut2 . FunctionTestCase ) : <EOL> def __init__ ( self , name , ** args ) : <EOL> self . _funcName = name <EOL> ut2 . FunctionTestCase . __init__ ( self , None , ** args ) <EOL> _testFunc = property ( lambda self : getattr ( self , self . _funcName ) , <EOL> lambda self , func : None ) <EOL> def __repr__ ( self ) : <EOL> return self . _funcName <EOL> id = __str__ = __repr__ <EOL> def GeneratorMethodCase ( cls ) : <EOL> class _GeneratorMethodCase ( GeneratorFunctionCase ) : <EOL> @ classmethod <EOL> def setUpClass ( klass ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> cls . setUpClass ( ) <EOL> @ classmethod <EOL> def tearDownClass ( klass ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> cls . tearDownClass ( ) <EOL> return _GeneratorMethodCase </s>
