ó
æNXc           @   sr  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m Z m Z d d l	 Td d l
 m Z m Z d „  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 k d ƒ Z e e j j d ƒ d ƒ Z e e j d k d ƒ Z e j d+ k o‹e j j d d ƒ d k Z  d Z! e e  e! ƒ Z" d „  Z# d „  Z$ e$ d ƒ Z% e$ d ƒ Z& e$ d  ƒ Z' e( e) d! ƒ Z* e( e j+ d" d# k d$ ƒ Z, d% „  Z- y e j. d& d' ƒ Z/ Wn e0 k
 r<e1 Z2 n Xe) Z2 e/ j3 ƒ  e e2 d( ƒ Z4 d) „  Z5 d* „  Z6 d S(,   s  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.

iÿÿÿÿN(   t	   decoratori   (   t	   ipdoctestt   ipdocstring(   t   *(   t   string_typest   whichc            s/   d t  j f ‡  f d †  ƒ  Y} ˆ  j | _ | S(   sD   Decorator to make a simple function into a normal test via unittest.t   Testerc              s   e  Z ‡  f d  †  Z RS(   c            s   ˆ  ƒ  d  S(   N(    (   t   self(   t   func(    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   test<   s    (   t   __name__t
   __module__R	   (    (   R   (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyR   ;   s   (   t   unittestt   TestCaseR
   (   R   R   (    (   R   sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   as_unittest9   s    c         C   s+   d d l  } t |  | j j | ƒ |  ƒ ƒ S(   s…  Apply a wrapper to a function for decoration.

    This mixes Michele Simionato's decorator tool with nose's make_decorator,
    to apply a wrapper in a decorator so that all nose attributes, as well as
    function signature and other properties, survive the decoration cleanly.
    This will ensure that wrapped functions can still be well introspected via
    IPython, for example.
    iÿÿÿÿN(   t
   nose.toolsR    t   toolst   make_decorator(   t   wrapperR   t   nose(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   apply_wrapperE   s    	c            s€   t  |  t ƒ r |  g ‰  n |  ‰  d „  } x ˆ  D] }  t | |  t ƒ q1 W‡  f d †  } | d k rs d |  } n  | | _ | S(   s  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           S   s   d  S(   N(   t   None(    (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   <lambda>‡   s    c            s%   x ˆ  D] } t  |  | t ƒ q W|  S(   N(   t   setattrt   True(   t   ft   label(   t   labels(    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   decorŒ   s    s   Labels a test as %r.N(   t
   isinstanceR   R   R   R   t   __doc__(   R   t   dst   tmpR   (    (   R   sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   make_label_decS   s    -		c            s   ‡ ‡  f d †  } | S(   sœ   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            s©   d d  l  ‰ t ˆ ƒ r! ˆ ‰ n ‡ f d †  ‰ d  d „ ‰ ‡  ‡ ‡ ‡ ‡ f d †  } ‡  ‡ ‡ ‡ ‡ f d †  } ˆ j j ˆ  ƒ r | } n | } ˆ j j ˆ  ƒ | ƒ S(   Niÿÿÿÿc              s   ˆ  S(   N(    (    (   t   skip_condition(    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyR   Á   s    c         S   s,   | d k r d } n | } d |  j | f S(   s;   Skip message with information about function being skipped.s#   Test skipped due to test condition.s   Skipping test: %s. %sN(   R   R
   (   R   t   msgt   out(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   get_msgÃ   s     	c             s5   ˆ ƒ  r$ ˆ j  ˆ ˆ  ˆ ƒ ƒ ‚ n ˆ  |  | Ž  Sd S(   s"   Skipper for normal test functions.N(   t   SkipTest(   t   argst   kwargs(   R   R%   R   R#   t   skip_val(    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   skipper_funcË   s    	c          ?   sG   ˆ ƒ  r$ ˆ j  ˆ ˆ  ˆ ƒ ƒ ‚ n x ˆ  |  | Ž  D] } | Vq4 Wd S(   s   Skipper for test generators.N(   R&   (   R'   R(   t   x(   R   R%   R   R#   R)   (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   skipper_genÒ   s    	(   R   t   callableR   t   utilt   isgeneratorR   R   (   R   R*   R,   t   skipper(   R"   R#   (   R   R%   R   R)   sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   skip_decorator¸   s    			(    (   R"   R#   R1   (    (   R#   R"   sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   skipifœ   s    *c         C   s   t  t |  ƒ S(   sY  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.
      (   R2   R   (   R#   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   skipæ   s    c            s:   t  ˆ  ƒ r ‡  f d †  } n ‡  f d †  } t | | ƒ S(   s0   The reverse from skipif, see skipif for details.c              s   ˆ  ƒ  S(   N(    (    (   t	   condition(    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyR   ü   s    c              s   ˆ  S(   N(    (    (   R4   (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyR   þ   s    (   R-   R2   (   R4   R#   R"   (    (   R4   sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   onlyifø   s    c         C   s4   y t  |  ƒ } t } Wn t k
 r/ t } n X| S(   s×   Can 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.
    (   t
   __import__t   Falset   ImportErrorR   (   t   modulet   modt   mod_not_avail(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   module_not_available  s    

c         C   s   d „  } | | _  |  | ƒ S(   sÏ   Return 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           S   s   d  S(   N(   R   (    (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyR     s    (   R
   (   t   dect   namet   dummy(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   decorated_dummy  s    		t   win32s$   This test does not run under Windowst   linuxs"   This test does not run under Linuxt   darwins!   This test does not run under OS Xs!   This test only runs under Windowss   This test only runs under Linuxs   This test only runs under OSXt   DISPLAYt    s.   Skipped under *nix when X11/XOrg not availablec         C   s   t  r t t |  ƒ Sd  S(   N(   t   _x11_skip_condR@   t   skip_if_no_x11R   (   R>   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   skip_file_no_x11:  s    c         C   s   t  t |  ƒ d |  ƒ S(   Ns   This test requires %s(   R2   R<   (   R:   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyR   @  s    t   numpyt
   matplotlibt   sympys   This test is known to faili    i   s'   This test is known to fail on Python 3.c         C   s   |  S(   N(    (   R   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyR   O  s    t   prefixu   tmpâ‚¬sC   This test is only applicable where we can use unicode in filenames.c          G   s4   x- |  D]% } t  | ƒ s t d j | ƒ ƒ Sq Wt S(   sN   
    Decorator to skip test when at least one of `commands` is not found.
    s1   This test runs only if command '{0}' is installed(   R   R3   t   formatt	   null_deco(   t   commandst   cmd(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   onlyif_cmds_exist_  s
    	c          G   s4   x |  D] } t  | ƒ r t Sq Wt d j |  ƒ ƒ S(   sL   
    Decorator to skip test unless at least one of `commands` is found.
    s;   This test runs only if one of the commands {0} is installed(   R   RN   R3   RM   (   RO   RP   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   onlyif_any_cmd_existsi  s
    	(   s   darwins   win32(7   R   t   syst   ost   tempfileR   R    t
   ipunittestR   R   t   IPython.external.decoratorst   IPython.utils.py3compatR   R   R   R   R   R!   R2   R3   R5   R<   R@   t   platformt
   skip_win32t
   startswitht
   skip_linuxt   skip_osxt   skip_if_not_win32t   skip_if_not_linuxt   skip_if_not_osxt   environt   getRF   t   _x11_skip_msgRG   RH   t   skip_withoutt   skipif_not_numpyt   skipif_not_matplotlibt   skipif_not_sympyt   knownfailureifR   t   skip_known_failuret   version_infot   known_failure_py3RN   t   NamedTemporaryFileR   t   UnicodeEncodeErrorR7   t   unicode_pathst   closet   onlyif_unicode_pathsRQ   RR   (    (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/testing/decorators.pyt   <module>   sb   
		IJ												

	
