B
     \f                 @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ dZdZdZ	dZ
dd Zd	d
 Zdd Zdd ZdddZdd Zdd Zdd Zdd ZdS )    N)wrapsz1.0.0z%valuesz
%file_pathz%unpackc             C   s   t | td | S )z2
    Method decorator to add unpack feature.

    T)setattrUNPACK_ATTR)func r   0lib/python3.7/site-packages/numba/testing/ddt.pyunpack   s    r   c                 s    fdd}|S )z
    Method decorator to add to your test methods.

    Should be added to methods of instances of ``unittest.TestCase``.

    c                s   t | t  | S )N)r   	DATA_ATTR)r   )valuesr   r   wrapper*   s    zdata.<locals>.wrapperr   )r
   r   r   )r
   r   data#   s    r   c                s    fdd}|S )ac  
    Method decorator to add to your test methods.

    Should be added to methods of instances of ``unittest.TestCase``.

    ``value`` should be a path relative to the directory of the file
    containing the decorated ``unittest.TestCase``. The file
    should contain JSON encoded data, that can either be a list or a
    dict.

    In case of a list, each value in the list will correspond to one
    test case, and the value will be concatenated to the test method
    name.

    In case of a dict, keys will be used as suffixes to the name of the
    test case, and values will be fed as test data.

    c                s   t | t  | S )N)r   	FILE_ATTR)r   )valuer   r   r   C   s    zfile_data.<locals>.wrapperr   )r   r   r   )r   r   	file_data0   s    r   c               C   s0   t jdkrt jdk st jdko.t jjo.dtjkS )Ni i   i ZPYTHONHASHSEED)sys
hexversionflagshash_randomizationosenvironr   r   r   r   is_hash_randomizedI   s
    


r   c                s   t dttttfytf7 W n tk
r4   Y nX  fdd t rb |sbd| |d S yt|}W n  t	k
r   |
dd}Y nX d| |d |}td	d
|S )a  
    Generate a new name for a test case.

    It will take the original test name and append an ordinal index and a
    string representation of the value, and convert the result into a valid
    python identifier by replacing extraneous characters with ``_``.

    If hash randomization is enabled (a feature available since 2.7.3/3.2.3
    and enabled by default since 3.3) and a "non-trivial" value is passed
    this will omit the name argument by default. Set `PYTHONHASHSEED`
    to a fixed value before running tests in these cases to get the
    names back consistently or use the `__name__` attribute on data values.

    A "trivial" value is a plain scalar, or a tuple or list consisting
    only of trivial values.

    Nc                s.   t | rdS t | ttfr*tt | S dS )NTF)
isinstancelisttupleallmap)r   )
is_trivialtrivial_typesr   r   r   v   s
    
z mk_test_name.<locals>.is_trivialz{0}_{1}   asciibackslashreplacez{0}_{1}_{2}z
\W|^(?=\d)_)typeboolstrintfloatZunicode	NameErrorr   formatUnicodeEncodeErrorencoderesub)namer   index	test_namer   )r   r   r   mk_test_nameQ   s    	r0   c          	      sV   t  fdd}||_jrRyjj |_W n ttfk
rP   Y nX |S )zO
    This internal method decorator feeds the test data item to the test.

    c                s   | f S )Nr   )self)argsr   kwargsr   r   r      s    zfeed_data.<locals>.wrapper)r   __name____doc__r(   
IndexErrorKeyError)r   new_namer2   r3   r   r   )r2   r   r3   r   	feed_data   s    r9   c             O   s   t | |t||f|| dS )z~
    Add a test case to this class.

    The test will be based on an existing function but will give it a new
    name.

    N)r   r9   )clsr/   r   r2   r3   r   r   r   add_test   s    r;   c                s   t jt| }t jt j| } fdd}t j|dkr^t|d}t	| ||d nt
t| }x~t|D ]r\}	}
t|tr|
||
  }}t|||	}nt|tr|
}t|||	}t|trt	| ||f| qzt	| ||| qzW dS )z>
    Process the parameter in the `file_data` decorator.

    c                 s   t d  d S )Nz%s does not exist)
ValueError)r2   )	file_attrr   r   	_raise_ve   s    z$process_file_data.<locals>._raise_veFerrorN)r   pathabspathinspectZgetsourcefilejoindirnameexistsr0   r;   jsonloadsopenread	enumerater   dictr   )r:   r-   r   r=   Zcls_pathZdata_file_pathr>   r/   r   ielemkeyr   r   )r=   r   process_file_data   s"    



rO   c             C   s   xt | j D ]\}}t|trxtt|tD ]n\}}t|t|d||}t|trt	|t
slt	|t rt| ||f|  qt| ||f| q2t| ||| q2W t| | qt|trt|t}t| ||| t| | qW | S )a  
    Class decorator for subclasses of ``unittest.TestCase``.

    Apply this decorator to the test case class, and then
    decorate test methods with ``@data``.

    For each method decorated with ``@data``, this will effectively create as
    many methods as data items are passed as parameters to ``@data``.

    The names of the test methods follow the pattern
    ``original_test_name_{ordinal}_{data}``. ``ordinal`` is the position of the
    data argument, starting with 1.

    For data we use a string representation of the data value converted into a
    valid python identifier.  If ``data.__name__`` exists, we use that instead.

    For each method decorated with ``@file_data('test_data.json')``, the
    decorator will try to load the test_data.json file located relative
    to the python file containing the method that is decorated. It will,
    for each ``test_name`` key create as many methods in the list of values
    from the ``data`` key.

    r4   )r   __dict__itemshasattrr	   rJ   getattrr0   r   r   r   r;   delattrr   rO   )r:   r-   r   rL   vr/   r=   r   r   r   ddt   s    



rV   )r   )rB   rF   r   r+   r   	functoolsr   __version__r	   r   r   r   r   r   r   r0   r9   r;   rO   rV   r   r   r   r   <module>   s$   	
: