<s> """<STR_LIT>""" <EOL> import re <EOL> import sys <EOL> import unittest <EOL> from doctest import DocTestFinder , DocTestRunner , TestResults <EOL> if sys . version [ <NUM_LIT:0> ] == '<STR_LIT:2>' : <EOL> from . _paramtestpy2 import ParametricTestCase <EOL> else : <EOL> from . _paramtestpy3 import ParametricTestCase <EOL> def count_failures ( runner ) : <EOL> """<STR_LIT>""" <EOL> return [ TestResults ( f , t ) for f , t in list ( runner . _name2ft . values ( ) ) if f > <NUM_LIT:0> ] <EOL> class IPython2PythonConverter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . rps1 = re . compile ( r'<STR_LIT>' ) <EOL> self . rps2 = re . compile ( r'<STR_LIT>' ) <EOL> self . rout = re . compile ( r'<STR_LIT>' ) <EOL> self . pyps1 = '<STR_LIT>' <EOL> self . pyps2 = '<STR_LIT>' <EOL> self . rpyps1 = re . compile ( '<STR_LIT>' % self . pyps1 ) <EOL> self . rpyps2 = re . compile ( '<STR_LIT>' % self . pyps2 ) <EOL> def __call__ ( self , ds ) : <EOL> """<STR_LIT>""" <EOL> from . import globalipapp <EOL> pyps1 = '<STR_LIT>' <EOL> pyps2 = '<STR_LIT>' <EOL> pyout = '<STR_LIT>' <EOL> dnew = ds <EOL> dnew = self . rps1 . sub ( pyps1 , dnew ) <EOL> dnew = self . rps2 . sub ( pyps2 , dnew ) <EOL> dnew = self . rout . sub ( pyout , dnew ) <EOL> ip = globalipapp . get_ipython ( ) <EOL> out = [ ] <EOL> newline = out . append <EOL> for line in dnew . splitlines ( ) : <EOL> mps1 = self . rpyps1 . match ( line ) <EOL> if mps1 is not None : <EOL> prompt , text = mps1 . groups ( ) <EOL> newline ( prompt + ip . prefilter ( text , False ) ) <EOL> continue <EOL> mps2 = self . rpyps2 . match ( line ) <EOL> if mps2 is not None : <EOL> prompt , text = mps2 . groups ( ) <EOL> newline ( prompt + ip . prefilter ( text , True ) ) <EOL> continue <EOL> newline ( line ) <EOL> newline ( '<STR_LIT>' ) <EOL> return '<STR_LIT:\n>' . join ( out ) <EOL> class Doc2UnitTester ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> self . verbose = verbose <EOL> self . finder = DocTestFinder ( verbose = verbose , recurse = False ) <EOL> def __call__ ( self , func ) : <EOL> """<STR_LIT>""" <EOL> d2u = self <EOL> if func . __doc__ is not None : <EOL> func . __doc__ = ip2py ( func . __doc__ ) <EOL> class Tester ( unittest . TestCase ) : <EOL> def test ( self ) : <EOL> runner = DocTestRunner ( verbose = d2u . verbose ) <EOL> list ( map ( runner . run , d2u . finder . find ( func , func . __name__ ) ) ) <EOL> failed = count_failures ( runner ) <EOL> if failed : <EOL> if len ( failed ) > <NUM_LIT:1> : <EOL> err = "<STR_LIT>" % failed <EOL> raise ValueError ( err ) <EOL> self . fail ( '<STR_LIT>' % str ( failed [ <NUM_LIT:0> ] ) ) <EOL> Tester . __name__ = func . __name__ <EOL> return Tester <EOL> def ipdocstring ( func ) : <EOL> """<STR_LIT>""" <EOL> if func . __doc__ is not None : <EOL> func . __doc__ = ip2py ( func . __doc__ ) <EOL> return func <EOL> ipdoctest = Doc2UnitTester ( ) <EOL> ip2py = IPython2PythonConverter ( ) </s>
