Format: application/x-python-code
⬇ Download File (5.3 KB)
   1  o
   2  
   3  ��hd�@s|dZddlZddlZddlZddlmZmZmZddlm	Z	dd�Z
   4  Gdd�de�ZGd	d
   5  �d
   6  e�Z
   7  dd�Ze
   8  �Ze�ZdS)
   9  a�Experimental code for cleaner support of IPython syntax with unittest.
  10  
  11  In IPython up until 0.10, we've used very hacked up nose machinery for running
  12  tests with IPython special syntax, and this has proved to be extremely slow.
  13  This module provides decorators to try a different approach, stemming from a
  14  conversation Brian and I (FP) had about this problem Sept/09.
  15  
  16  The goal is to be able to easily write simple functions that can be seen by
  17  unittest as tests, and ultimately for these to support doctests with full
  18  IPython syntax.  Nose already offers this based on naming conventions and our
  19  hackish plugins, but we are seeking to move away from nose dependencies if
  20  possible.
  21  
  22  This module follows a different approach, based on decorators.
  23  
  24  - A decorator called @ipdoctest can mark any function as having a docstring
  25    that should be viewed as a doctest, but after syntax conversion.
  26  
  27  Authors
  28  -------
  29  
  30  - Fernando Perez <Fernando.Perez@berkeley.edu>
  31  �N)�
  32  DocTestFinder�
  33  DocTestRunner�TestResults)�InteractiveShellcCs2tjdkrdd�|j��D�Sdd�|j��D�S)zmCount number of failures in a doctest runner.
  34  
  35      Code modeled after the summarize() method in doctest.
  36      )��
  37  cSs"g|]
  38  \}}|dkrt||��qS�r�r)�.0�f�t�r
  39  �Pc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/testing/ipunittest.py�
  40  <listcomp>6s"z"count_failures.<locals>.<listcomp>cSs$g|]\}}}|dkrt||��qSrr	)r
  41  ZfailureZtry_�skipr
  42  r
  43  rr8s
  44  �)�sys�version_infoZ_name2ft�valuesZ_stats)�runnerr
  45  r
  46  r�count_failures0s
  47  
  48  �rc@s eZdZdZdd�Zdd�ZdS)�IPython2PythonConverterz�Convert IPython 'syntax' to valid Python.
  49  
  50      Eventually this code may grow to be the full IPython syntax conversion
  51      implementation, but for now it only does prompt conversion.cCsXt�d�|_t�d�|_t�d�|_d|_d|_t�d|j�|_t�d|j�|_dS)Nz
  52  In\ \[\d+\]: z\ \ \ \.\.\.+: zOut\[\d+\]: \s*?\n?�>>> �... z(\s*%s)(.*)$)	�re�compile�rps1�rps2�rout�pyps1�pyps2�rpyps1�rpyps2)�selfr
  53  r
  54  r�__init__Esz IPython2PythonConverter.__init__cCs�ddlm}d}d}d}|}|j�||�}|j�||�}|j�||�}t��}g}|j}	|�	�D]<}
  55  |j
  56  �|
  57  �}|durM|��\}}
  58  |	||�
  59  |
  60  d��q0|j�|
  61  �}|durh|��\}}
  62  |	||�
  63  |
  64  d��q0|	|
  65  �q0|	d�d	�|�S)
  66  z3Convert IPython prompts to python ones in a string.�)�globalipapprr�NFT�
  67  )r&r%r�subrrr�instance�append�
  68  splitlinesr �match�groupsZ	prefilterr!�join)r"Zdsr%rrZpyoutZdnew�ip�out�newline�lineZmps1�prompt�textZmps2r
  69  r
  70  r�__call__Ns2
  71  
  72  z IPython2PythonConverter.__call__N��__name__�
  73  __module__�__qualname__�__doc__r#r5r
  74  r
  75  r
  76  rr?s	rc@s"eZdZdZddd�Zdd�ZdS)	�Doc2UnitTesterz�Class whose instances act as a decorator for docstring testing.
  77  
  78      In practice we're only likely to need one instance ever, made below (though
  79      no attempt is made at turning it into a singleton, there is no need for
  80      that).
  81      FcCs||_t|dd�|_dS)z�New decorator.
  82  
  83          Parameters
  84          ----------
  85  
  86          verbose : boolean, optional (False)
  87            Passed to the doctest finder and runner to control verbosity.
  88          F)�verbose�recurseN)r<r�finder)r"r<r
  89  r
  90  rr#|s	zDoc2UnitTester.__init__cs>|��jdur
  91  t�j��_G��fdd�dtj�}�j|_|S)z�Use as a decorator: doctest a function's docstring as a unittest.
  92          
  93          This version runs normal doctests, but the idea is to make it later run
  94          ipython syntax instead.NcseZdZ��fdd�ZdS)z'Doc2UnitTester.__call__.<locals>.Testercsrt�jd�}�j���j�D]}|�|�qt|�}|r7t|�dkr*d|}t|��|�	dt
  95  |d��dSdS)N)r<r$z"Invalid number of test results: %szfailed doctests: %sr)rr<r>�findr7�runr�len�
  96  ValueErrorZfail�str)r"rZthe_test�failed�err�Zd2u�funcr
  97  r�test�s�z,Doc2UnitTester.__call__.<locals>.Tester.testN)r7r8r9rHr
  98  rFr
  99  r�Tester�srI)r:�ip2py�unittestZTestCaser7)r"rGrIr
 100  rFrr5�s	
 101  zDoc2UnitTester.__call__N)Fr6r
 102  r
 103  r
 104  rr;us
 105  
 106  r;cCs|jdurt|j�|_|S)z-Change the function docstring via ip2py.
 107      N)r:rJ)rGr
 108  r
 109  r�ipdocstring�s
 110  rL)r:rrrK�doctestrrrZ!IPython.terminal.interactiveshellrr�objectrr;rLZ	ipdoctestrJr
 111  r
 112  r
 113  r�<module>s%6;	
 114