
    X*                        d Z ddlZddlZddlZddlZddlmZmZ d Zd&dZ	d&dZ
d&dZd	 Zd
 Zd Z e
ej                  dk(  d      Z e
ej                  j#                  d      d      Z e
ej                  dk(  d      Z e
ej                  dk7  d      Z e
ej                  j#                  d       d      Z e
ej                  dk7  d      Zej                  dvxr ej.                  j1                  dd      dk(  ZdZ e
ee      Zd Zd Z ed      Z ed      Z ed      Z d Z!	  ejD                  d       Z#d!Z$e#jK                           ee$d#      Z'd$ Z(d% Z)y# e&$ r d"Z$Y w xY w)'a  Decorators for labeling test objects.

Decorators that merely return a modified version of the original function
object are straightforward.  Decorators that return a new function object need
to use nose.tools.make_decorator(original_function)(decorator) in returning the
decorator, in order to preserve metadata such as function name, setup and
teardown functions and so on - see nose.tools for more information.

This module provides a set of useful decorators meant to be ready to use in
your own tests.  See the bottom of the file for the ready-made ones, and if you
find yourself writing a new one that may be of generic use, add it here.

Included decorators:


Lightweight testing that remains unittest-compatible.

- An @as_unittest decorator can be used to tag any normal parameter-less
  function as a unittest TestCase.  Then, both nose and normal unittest will
  recognize it as such.  This will make it easier to migrate away from Nose if
  we ever need/want to while maintaining very lightweight tests.

NOTE: This file contains IPython-specific decorators. Using the machinery in
IPython.external.decorators, we import either numpy.testing.decorators if numpy is
available, OR use equivalent code in IPython.external._decorators, which
we've copied verbatim from numpy.

    N   )string_typeswhichc                 `      G  fddt         j                        } j                  |_        |S )zDDecorator to make a simple function into a normal test via unittest.c                       e Zd Z fdZy)as_unittest.<locals>.Testerc                              y N )selffuncs    E/usr/lib/python3/dist-packages/ipython_genutils/testing/decorators.pytestz as_unittest.<locals>.Tester.test2   s    F    N)__name__
__module____qualname__r   )r   s   r   Testerr   1   s    	r   r   )unittestTestCaser   )r   r   s   ` r   as_unittestr   /   s'    ""  mmFOMr   c                     t        | t              r| gn| d }D ]  } t        || d        fd}|d| z  }||_        |S )a  Factory function to create a decorator that applies one or more labels.

    Parameters
    ----------
      label : string or sequence
      One or more labels that will be applied by the decorator to the functions
    it decorates.  Labels are attributes of the decorated function with their
    value set to True.

      ds : string
      An optional docstring for the resulting decorator.  If not given, a
      default docstring is auto-generated.

    Returns
    -------
      A decorator.

    Examples
    --------

    A simple labeling decorator:

    >>> slow = make_label_dec('slow')
    >>> slow.__doc__
    "Labels a test as 'slow'."
    
    And one that uses multiple labels and a custom docstring:
    
    >>> rare = make_label_dec(['slow','hard'],
    ... "Mix labels 'slow' and 'hard' for rare tests.")
    >>> rare.__doc__
    "Mix labels 'slow' and 'hard' for rare tests."

    Now, let's test using this one:
    >>> @rare
    ... def f(): pass
    ...
    >>>
    >>> f.slow
    True
    >>> f.hard
    True
    c                       y r
   r   r   r   r   <lambda>z make_label_dec.<locals>.<lambda>q       r   Tc                 0    D ]  }t        | |d        | S )NT)setattr)flabellabelss     r   decorzmake_label_dec.<locals>.decorv   s#     	"EAeD!	"r   zLabels a test as %r.)
isinstancer   r   __doc__)r   dstmpr!   r    s       @r   make_label_decr&   =   s`    Z %& C  E$  
z#e+EMLr   c                       fd}|S )a   Make function raise SkipTest exception if skip_condition is true

    Parameters
    ----------

    skip_condition : bool or callable
      Flag to determine whether to skip test. If the condition is a
      callable, it is used at runtime to dynamically make the decision. This
      is useful for tests that may require costly imports, to delay the cost
      until the test suite is actually executed.
    msg : string
      Message to give on raising a SkipTest exception.

    Returns
    -------
    decorator : function
      Decorator, which, when applied to a function, causes SkipTest
      to be raised when the skip_condition was True, and the function
      to be called normally otherwise.

    Notes
    -----
    You will see from the code that we had to further decorate the
    decorator with the nose.tools.make_decorator function in order to
    transmit function name, and various other metadata.
    c                      dd l t              rnfddd fd} fd}j                  j                         r|}n|} j                  j                         |      S )Nr   c                       S r
   r   )skip_conditions   r   r   z0skipif.<locals>.skip_decorator.<locals>.<lambda>   s     r   c                 4    |d}n|}d| j                   d|S )z;Skip message with information about function being skipped.z#Test skipped due to test condition.zSkipping test: z. r   )r   msgouts      r   get_msgz/skipif.<locals>.skip_decorator.<locals>.get_msg   s!    {"GC#.2mmC@@r   c                  R            rj                                | i |S )z"Skipper for normal test functions.SkipTest)argskwargsr   r/   r-   noseskip_vals     r   skipper_funcz4skipif.<locals>.skip_decorator.<locals>.skipper_func   s-    zmmGAcN33$)&))r   c               ?   n   K           rj                                | i |D ]  }|  yw)zSkipper for test generators.Nr1   )r3   r4   xr   r/   r-   r5   r6   s      r   skipper_genz3skipif.<locals>.skip_decorator.<locals>.skipper_gen   s?     zmmGAcN33D+F+ AGs   25r
   )r5   callableutilisgeneratortoolsmake_decorator)	r   r7   r:   skipperr/   r5   r6   r-   r*   s	   `   @@@r   skip_decoratorzskipif.<locals>.skip_decorator   sp     	 N#%H.H	A	* 	*	 	 99  #!G"G+tzz((+G44r   r   )r*   r-   rA   s   `` r   skipifrB      s    8(5T r   c                     t        d|       S )aY  Decorator factory - mark a test function for skipping from test suite.

    Parameters
    ----------
      msg : string
        Optional message to be added.

    Returns
    -------
       decorator : function
         Decorator, which, when applied to a function, causes SkipTest
         to be raised, with the optional message added.
      T)rB   )r-   s    r   skiprD      s     $sr   c                 H     t               r fd}n fd}t        ||      S )z0The reverse from skipif, see skipif for details.c                                S r
   r   	conditions   r   r   zonlyif.<locals>.<lambda>   s    ik/ r   c                  
      S r
   r   rG   s   r   r   zonlyif.<locals>.<lambda>   s	    i- r   )r;   rB   )rH   r-   r*   s   `  r   onlyifrJ      s$     	1/.#&&r   c                       fd}|S )zCan module be imported?  Returns true if module does NOT import.

    This is used to make a decorator to skip tests that require module to be
    available, but delay the 'import numpy' to test execution time.
    c                  <    	 t              } y# t        $ r Y yw xY w)NFT)
__import__ImportError)modmodules    r   rH   z'module_not_available.<locals>.condition   s'    	V$C 		s    	r   )rP   rH   s   ` r   module_not_availablerQ      s     r   c                 &    d }||_          | |      S )zReturn a dummy function decorated with dec, with the given name.
    
    Examples
    --------
    import IPython.testing.decorators as dec
    setup = dec.decorated_dummy(dec.skip_if_no_x11, __name__)
    c                       y r
   r   r   r   r   r   z!decorated_dummy.<locals>.<lambda>  r   r   r,   )decnamedummys      r   decorated_dummyrW      s     EENu:r   win32z$This test does not run under Windowslinuxz"This test does not run under Linuxdarwinz!This test does not run under OS Xz!This test only runs under WindowszThis test only runs under LinuxzThis test only runs under OSX)rZ   rX   DISPLAY z.Skipped under *nix when X11/XOrg not availablec                 2    t         rt        t        |       S d S r
   )_x11_skip_condrW   skip_if_no_x11)rU   s    r   skip_file_no_x11r`   $  s    4B?>40LLr   c                 2    t        t        |       d| z        S )NzThis test requires %s)rB   rQ   )rO   s    r   r   r   *  s    6"6s";=TWZ=Z[ r   numpy
matplotlibsympyc                     | S r
   r   )r   s    r   r   r   4  s    a r   u   tmp€)prefixTFzCThis test is only applicable where we can use unicode in filenames.c                  j    | D ])  }t        |      rt        dj                  |            c S  t        S )zN
    Decorator to skip test when at least one of `commands` is not found.
    z1This test runs only if command '{0}' is installed)r   rD   format	null_decocommandscmds     r   onlyif_cmds_existrm   D  s=      4Sz ''-vc{4 44 r   c                  j    | D ]  }t        |      st        c S  t        dj                  |             S )zL
    Decorator to skip test unless at least one of `commands` is found.
    z;This test runs only if one of the commands {0} is installed)r   ri   rD   rh   rj   s     r   onlyif_any_cmd_existsro   N  s>      :  %vh/1 1r   r
   )*r#   sysostempfiler   	py3compatr   r   r   r&   rB   rD   rJ   rQ   rW   platform
skip_win32
startswith
skip_linuxskip_osxskip_if_not_win32skip_if_not_linuxskip_if_not_osxenvirongetr^   _x11_skip_msgr_   r`   skip_withoutskipif_not_numpyskipif_not_matplotlibskipif_not_sympyri   NamedTemporaryFiler   unicode_pathscloseUnicodeEncodeErroronlyif_unicode_pathsrm   ro   r   r   r   <module>r      s  @  	   ,CRFT$'
  CLLG+:<
CLL++G48:
#,,(*+NO 3<<72>@ s||66w??<> 18: ,,&99 6**..B/25 @6M \( $\2 (  	###95A MGGIm /M O 1)  Ms   ,E! !E+*E+