o 6a!?@sdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z e ddZ Gd dde Z d d Zdd dZddZddZdadaegdZddiZddZddZGdddZdS)as tl;dr: all code code is licensed under simplified BSD, unless stated otherwise. Unless stated otherwise in the source files, all code is copyright 2010 David Wolever . 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 ``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 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)TestCaseparamz args kwargsc@s:eZdZdZddZed ddZeddZd d ZdS) ra 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 cOst|||SN)_param__new__clsargskwargsrF/usr/lib/python3/dist-packages/numpy/testing/_private/parameterized.pyr Bsz param.__new__NcCs|pd}|pi}||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"}) rrr rrrexplicitEs zparam.explicitc Csbt|tr|St|tfr|f}z||WSty0}zdt|vr$td||fd}~ww)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={}) zafter * must bez=Parameters must be tuples, but %r is not (hint: use '(%r, )')N) isinstancerstr TypeError)r r errrfrom_decoratorSs    zparam.from_decoratorcCsd|S)Nzparam(*%r, **%r)r)selfrrr__repr__kszparam.__repr__NN) __name__ __module__ __qualname____doc__r classmethodrrrrrrrr,s   cst|}|jdddgkrdnd}|j|d}tt|j}|jt||d}jt|d}|fddt||jpCgDdd|Dtt fd dj D}|rm| d |j ft |f|rz| d |jf|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, ))] Nrrcs"g|] \}}|j||fqSr)r get).0namedefault)prr sz6parameterized_argument_value_pairs..cSsh|]\}}|qSrr)r n_rrr sz5parameterized_argument_value_pairs..cs"g|] }|vr|j|fqSr)r )r r!r#seen_arg_namesrrr$s  z*%sz**%s)inspect getargspecr listziplenextenddefaultsdictsortedr appendvarargstuplekeywords)funcr#argspec arg_offset named_argsresultr4r6rr(r"parameterized_argument_value_pairsos$  r<@cCszt|}t|trzt|d}Wntyt|d}Ynwt||kr;|d|dd|t||dd}|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" zutf-8latin1Nz...)reprrbytesrUnicodeDecodeErrorr.)xr%x_reprrrr short_reprs   ,rEc Cs|jdurdSt||}dd|D}|jd\}}}d}|dr-d}|dd}dt|r4dp5dd |f} d|| |||gS) NcSs"g|] \}}|dt|qS)=)rE)r r%vrrrr$s"z$default_doc_func.. .z %s[with %s] , )rr<lstrip partitionendswithr.joinrstrip) r7numr#all_args_with_valuesdescsfirstnlrestsuffixr rrrdefault_doc_funcs    rZcCsP|j}d|f}t|jdkr$t|jdtfr$|dt|jd7}||S)Nz_%srr&)rr.r rr parameterized to_safe_name)r7rSr# base_name name_suffixrrrdefault_name_funcs   r_noseF)unittest unittest2r`nose2pytest_pytestrdcCs&|tvrtd|dtf|adS)Nz,Invalid test runner: %r (must be one of: %s)rM) _test_runnersrrQ_test_runner_override)r!rrrset_test_runners rhcCsrtdurtStdur7t}t|D]"}|d}|jddd}|tvr+t|}|t vr4|atSqdatS)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. NFrrrJ) rg_test_runner_guessr*stackreversed f_globalsrrO_test_runner_aliasesrf)rjrecordframemodulerrr detect_runners  rqc@sxeZdZdZdddZddZddZd d Zd d Ze d dZ e ddZ e dddZ e ddZ e ddZdS)r[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) NcCs|||_|p t|_dSr)input_as_callable get_inputrZdoc_func)rinputrtrrr__init__s zparameterized.__init__csDtdfdd __djf_S)Nc 3s|ot|}j}tjD]D\}}t|}|||\}}z#|dj_|dur4t|j||VW|durBt |j|_q|durPt |j|_wdS)Nr) typer enumerateparameterized_inputrrparam_as_nose_tuplesetattrrdelattr) test_selftest_cls original_docrSr r# unbound_func nose_tupler test_funcwrapperrrrs$     z'parameterized.__call__..wrapperz_parameterized_original_%sr)assert_not_in_testcase_subclassrrsryparameterized_funcr)rrrrr__call__s zparameterized.__call__csVtfdd}||||_|}|durt||}||f|j|jp'iffS)Ncs|ddi|dS)NrKr)r r7rr8sz3parameterized.param_as_nose_tuple..)rrtrrr r )rr}r7rSr# nose_funcrrrrrz7s  z!parameterized.param_as_nose_tuplecCs&|}tdd|DrtddS)Ncss|]}t|tVqdSr) issubclassr)r r rrr Dsz@parameterized.assert_not_in_testcase_subclass..zqWarning: '@parameterized' tests won't work inside subclasses of 'TestCase' - use '@parameterized.expand' instead.)$_terrible_magic_get_defining_classesany Exception)rparent_classesrrrrBsz-parameterized.assert_not_in_testcase_subclasscCst}t|dkr gS|d}|do|dd}|r#|ds%gS|d\}}}|d\}}}td|d|dj|djS)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 rzclass ()[]) r*rjr.strip startswithrOevalrlf_locals)rrjro code_contextr&parentsrrrrIs  z2parameterized._terrible_magic_get_defining_classescs,tr fddSfddS)Ncs Sr)check_input_valuesrr rurrr]s z1parameterized.input_as_callable..csSrrr) input_valuesrrr_s)callablerrr)r rurrrrZs  zparameterized.input_as_callablecCs t|ts t|}dd|DS)NcSsg|]}t|qSr)rr)r r#rrrr$jsz4parameterized.check_input_values..)rr,)r rrrrras z parameterized.check_input_valuesc spd|vrtjdtdds|dd|vr$tjdtdds$|dp'tp+td fdd }|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': ... >>> testcase_func_namez1testcase_func_name= is deprecated; use name_func=r?) stackleveltestcase_func_docz/testcase_func_doc= is deprecated; use doc_func=Nc srt}|d}|dj}}t|D]\}}|||}|||||<|||||_qd|_dS)NrrF)r*rjrrrrxparam_as_standalone_funcr__test__) finstancerjro frame_locals parametersrSr#r!r rtru name_funcrrparameterized_expand_wrappers    z:parameterized.expand..parameterized_expand_wrapperr)warningswarnDeprecationWarningrZr_)r rurrtlegacyrrrrexpandls  zparameterized.expandcsBtfdd}||_|_z|`W|Sty Y|Sw)Ncs|jijSr)r r )ar7r#rrstandalone_funcsz?parameterized.param_as_standalone_func..standalone_func)rrplace_as __wrapped__AttributeError)r r#r7r!rrrrrs z¶meterized.param_as_standalone_funccCsttdd|S)Nz[^a-zA-Z0-9_]+r&)rresub)r srrrr\szparameterized.to_safe_namerr)rrrrrvrrzrrrrrrrrr\rrrrr[s"     - r[)r=)rrr*r functoolsrtypesr collectionsrrarrrr<rErZr_rgrisetrfrmrhrqr[rrrrs,      C ;