a
    p=ic?                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ e	ddZG d	d deZd
d ZdddZdd Zdd Zdadaeg dZddiZdd Zdd ZG dd dZdS )an  
tl;dr: all code is licensed under simplified BSD, unless stated otherwise.

Unless stated otherwise in the source files, all code is copyright 2010 David
Wolever <david@wolever.net>. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of David Wolever.

    N)wraps)
MethodType)
namedtuple)TestCaseparamzargs kwargsc                   @   s:   e Zd ZdZdd ZedddZedd Zd	d
 ZdS )r   a   Represents a single parameter to a test case.

        For example::

            >>> p = param("foo", bar=16)
            >>> p
            param("foo", bar=16)
            >>> p.args
            ('foo', )
            >>> p.kwargs
            {'bar': 16}

        Intended to be used as an argument to ``@parameterized``::

            @parameterized([
                param("foo", bar=16),
            ])
            def test_stuff(foo, bar=16):
                pass
        c                 O   s   t | ||S N)_param__new__clsargskwargs r   u/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/numpy/testing/_private/parameterized.pyr	   B   s    zparam.__new__Nc                 C   s   |pd}|pi }| |i |S )a   Creates a ``param`` by explicitly specifying ``args`` and
            ``kwargs``::

                >>> param.explicit([1,2,3])
                param(*(1, 2, 3))
                >>> param.explicit(kwargs={"foo": 42})
                param(*(), **{"foo": "42"})
            r   r   r
   r   r   r   explicitE   s    
zparam.explicitc              
   C   sr   t |tr|S t |tfr |f}z
| | W S  tyl } z*dt|vrH td||f W Y d}~n
d}~0 0 dS )a(   Returns an instance of ``param()`` for ``@parameterized`` argument
            ``args``::

                >>> param.from_decorator((42, ))
                param(args=(42, ), kwargs={})
                >>> param.from_decorator("foo")
                param(args=("foo", ), kwargs={})
            zafter * must bez=Parameters must be tuples, but %r is not (hint: use '(%r, )')N)
isinstancer   str	TypeError)r   r   er   r   r   from_decoratorS   s    


zparam.from_decoratorc                 C   s   d|  S )Nzparam(*%r, **%r)r   )selfr   r   r   __repr__k   s    zparam.__repr__)NN)	__name__
__module____qualname____doc__r	   classmethodr   r   r   r   r   r   r   r   ,   s   
c                    s   t | }|jdd dgkr"dnd}|j|d }tt| j}|jt|| d } jt|d }| fddt||jpg D  dd |D tt	 fd	d j
D }|r|d
|jf t|f |r|d|jf |f |S )a   Return tuples of parameterized arguments and their values.

        This is useful if you are writing your own doc_func
        function and need to know the values for each parameter name::

            >>> def func(a, foo=None, bar=42, **kwargs): pass
            >>> p = param(1, foo=7, extra=99)
            >>> parameterized_argument_value_pairs(func, p)
            [("a", 1), ("foo", 7), ("bar", 42), ("**kwargs", {"extra": 99})]

        If the function's first argument is named ``self`` then it will be
        ignored::

            >>> def func(self, a): pass
            >>> p = param(1)
            >>> parameterized_argument_value_pairs(func, p)
            [("a", 1)]

        Additionally, empty ``*args`` or ``**kwargs`` will be ignored::

            >>> def func(foo, *args): pass
            >>> p = param(1)
            >>> parameterized_argument_value_pairs(func, p)
            [("foo", 1)]
            >>> p = param(1, 16)
            >>> parameterized_argument_value_pairs(func, p)
            [("foo", 1), ("*args", (16, ))]
    N   r   r   c                    s"   g | ]\}}| j ||fqS r   )r   get).0namedefault)pr   r   
<listcomp>   s   z6parameterized_argument_value_pairs.<locals>.<listcomp>c                 S   s   h | ]\}}|qS r   r   )r   n_r   r   r   	<setcomp>       z5parameterized_argument_value_pairs.<locals>.<setcomp>c                    s"   g | ]}|vr| j | fqS r   )r   )r   r    r"   Zseen_arg_namesr   r   r#      s   z*%sz**%s)inspect
getargspecr   listziplenextenddefaultsdictsortedr   appendvarargstuplekeywords)funcr"   ZargspecZ
arg_offsetZ
named_argsresultr3   r5   r   r(   r   "parameterized_argument_value_pairso   s$    

r8   @   c                 C   sz   t | }t|tr>zt|d}W n ty<   t|d}Y n0 t||krv|d|d  d |t||d  d  }|S )z A shortened repr of ``x`` which is guaranteed to be ``unicode``::

            >>> short_repr("foo")
            u"foo"
            >>> short_repr("123456789", n=4)
            u"12...89"
    zutf-8latin1N   z...)reprr   bytesr   UnicodeDecodeErrorr-   )xr$   Zx_reprr   r   r   
short_repr   s    	
,r@   c           
      C   s   | j d u rd S t| |}dd |D }| j  d\}}}d}|drZd}|d d }dt|rhdpjdd	|f }	d| |	|||gS )
Nc                 S   s"   g | ]\}}| d t | qS )=)r@   )r   r$   vr   r   r   r#      r'   z$default_doc_func.<locals>.<listcomp>
 .z%s[with %s] , )r   r8   lstrip	partitionendswithr-   joinrstrip)
r6   numr"   Zall_args_with_valuesZdescsfirstnlrestsuffixr   r   r   r   default_doc_func   s    


rS   c                 C   sP   | j }d|f }t|jdkrHt|jd tfrH|dt|jd  7 }|| S )Nz_%sr   r%   )r   r-   r   r   r   parameterizedto_safe_name)r6   rN   r"   	base_nameZname_suffixr   r   r   default_name_func   s
    
 rW   noseF)unittestZ	unittest2rX   Znose2pytestZ_pytestrZ   c                 C   s&   | t vrtd| dt f | ad S )Nz,Invalid test runner: %r (must be one of: %s)rH   )_test_runnersr   rL   _test_runner_override)r    r   r   r   set_test_runner   s    r]   c                  C   sp   t durt S tdu rlt } t| D ]B}|d }|jddd }|tv rVt| }|t	v r$|a qlq$datS )a
   Guess which test runner we're using by traversing the stack and looking
        for the first matching module. This *should* be reasonably safe, as
        it's done during test discovery where the test runner should be the
        stack frame immediately outside. NFr   r   rE   )
r\   _test_runner_guessr)   stackreversed	f_globalsr   rJ   _test_runner_aliasesr[   )r_   recordframemoduler   r   r   detect_runner   s    rf   c                   @   sx   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Ze	dd Z
e	dd Ze	dddZe	dd Ze	dd ZdS )rT   a=   Parameterize a test case::

            class TestInt:
                @parameterized([
                    ("A", 10),
                    ("F", 15),
                    param("10", 42, base=42)
                ])
                def test_int(self, input, expected, base=16):
                    actual = int(input, base=base)
                    assert_equal(actual, expected)

            @parameterized([
                (2, 3, 5)
                (3, 5, 8),
            ])
            def test_add(a, b, expected):
                assert_equal(a + b, expected)
        Nc                 C   s   |  || _|pt| _d S r   )input_as_callable	get_inputrS   doc_func)r   inputri   r   r   r   __init__  s    zparameterized.__init__c                    sD       td fdd	  __djf _S )Nc              
   3   s   | o
t | }j}tjD ]\}}t|} | ||\}}zF|d j_| d urft|j| |V  W | d urt	|j |_q| d urt	|j |_0 qd S )Nr   )
typer   	enumerateparameterized_inputr   r   param_as_nose_tuplesetattrr   delattr)	test_selfZtest_clsZoriginal_docrN   r   r"   unbound_funcZ
nose_tupler   	test_funcwrapperr   r   rv     s     
z'parameterized.__call__.<locals>.wrapperz_parameterized_original_%s)N)assert_not_in_testcase_subclassr   rh   rn   Zparameterized_funcr   )r   ru   r   rt   r   __call__  s    
zparameterized.__call__c                    sV   t   fdd}|  |||_|}|d ur:t||}||f|j |jpNi f fS )Nc                     s    | d d i | d S )NrF   r   )r   r6   r   r   <lambda>8  r'   z3parameterized.param_as_nose_tuple.<locals>.<lambda>)r   ri   r   r   r   r   )r   rr   r6   rN   r"   Z	nose_funcrs   r   ry   r   ro   7  s    
z!parameterized.param_as_nose_tuplec                 C   s&   |   }tdd |D r"tdd S )Nc                 s   s   | ]}t |tV  qd S r   )
issubclassr   )r   r   r   r   r   	<genexpr>D  r'   z@parameterized.assert_not_in_testcase_subclass.<locals>.<genexpr>zqWarning: '@parameterized' tests won't work inside subclasses of 'TestCase' - use '@parameterized.expand' instead.)$_terrible_magic_get_defining_classesany	Exception)r   Zparent_classesr   r   r   rw   B  s    z-parameterized.assert_not_in_testcase_subclassc                 C   s   t  }t|dkrg S |d }|d o6|d d  }|rF|dsJg S |d\}}}|d\}}}td| d |d j|d jS )a@   Returns the list of parent classes of the class currently being defined.
            Will likely only work if called from the ``parameterized`` decorator.
            This function is entirely @brandon_rhodes's fault, as he suggested
            the implementation: http://stackoverflow.com/a/8793684/71522
               r   zclass ()[])	r)   r_   r-   strip
startswithrJ   evalra   f_locals)r   r_   rd   code_contextr%   parentsr   r   r   r}   I  s    z2parameterized._terrible_magic_get_defining_classesc                    s,   t r fddS  fddS )Nc                      s      S r   )check_input_valuesr   r   rj   r   r   rz   ]  r'   z1parameterized.input_as_callable.<locals>.<lambda>c                      s    S r   r   r   )input_valuesr   r   rz   _  r'   )callabler   r   r   )r   rj   r   r   rg   Z  s    
zparameterized.input_as_callablec                 C   s    t |tst|}dd |D S )Nc                 S   s   g | ]}t |qS r   )r   r   )r   r"   r   r   r   r#   j  r'   z4parameterized.check_input_values.<locals>.<listcomp>)r   r+   )r   r   r   r   r   r   a  s    
z parameterized.check_input_valuesc                    sp   d|v r$t jdtdd s$|d d|v rHt jdtdd sH|d pNtpVtd
 fdd		}|S )am   A "brute force" method of parameterizing test cases. Creates new
            test cases and injects them into the namespace that the wrapped
            function is being defined in. Useful for parameterizing tests in
            subclasses of 'UnitTest', where Nose test generators don't work.

            >>> @parameterized.expand([("foo", 1, 2)])
            ... def test_add1(name, input, expected):
            ...     actual = add1(input)
            ...     assert_equal(actual, expected)
            ...
            >>> locals()
            ... 'test_add1_foo_0': <function ...> ...
            >>>
            Ztestcase_func_namez1testcase_func_name= is deprecated; use name_func=r;   )
stacklevelZtestcase_func_docz/testcase_func_doc= is deprecated; use doc_func=Nc           	         sr   t  }|d }|d j}  }t|D ]8\}}| ||} || |||< | |||| _q.d| _d S )Nr   r   F)r)   r_   r   rg   rm   param_as_standalone_funcr   Z__test__)	finstancer_   rd   Zframe_locals
parametersrN   r"   r    r   ri   rj   	name_funcr   r   parameterized_expand_wrapper  s    
z:parameterized.expand.<locals>.parameterized_expand_wrapper)N)warningswarnDeprecationWarningrS   rW   )r   rj   r   ri   legacyr   r   r   r   expandl  s     zparameterized.expandc                    sB   t   fdd}||_ |_z|`W n ty<   Y n0 |S )Nc                     s    | j  i jS r   )r   r   )ar6   r"   r   r   standalone_func  s    z?parameterized.param_as_standalone_func.<locals>.standalone_func)r   r   Zplace_as__wrapped__AttributeError)r   r"   r6   r    r   r   r   r   r     s    z&parameterized.param_as_standalone_funcc                 C   s   t tdd|S )Nz[^a-zA-Z0-9_]+r%   )r   resub)r   sr   r   r   rU     s    zparameterized.to_safe_name)N)NN)r   r   r   r   rk   rx   ro   rw   r}   r   rg   r   r   r   rU   r   r   r   r   rT      s    



-
rT   )r9   )r   r   r)   r   	functoolsr   typesr   collectionsr   rY   r   r   r   r8   r@   rS   rW   r\   r^   setr[   rb   r]   rf   rT   r   r   r   r   <module>   s*    
C;
		