bg1xddlZddlZddlZddlZddlZddlmZmZddlm Z  ddl Z dZ n #e $rdZ YnwxYwdZ dZdZd Zd Zd Z edeeeefZn#e$redeeeefZYnwxYweGd d eZdZdZdZddZddZ ddej!fdZ"dZ#dZ$dZ%dZ&dZ'dZ(ddZ)dS)N)Enumunique)wrapsTFz1.4.4z%valuesz %file_pathz %yaml_loaderz%unpackz %index_lenceZdZdZdZdZdS)TestNameFormata An enum to configure how ``mk_test_name()`` to compose a test name. Given the following example: .. code-block:: python @data("a", "b") def testSomething(self, value): ... if using just ``@ddt`` or together with ``DEFAULT``: * ``testSomething_1_a`` * ``testSomething_2_b`` if using ``INDEX_ONLY``: * ``testSomething_1`` * ``testSomething_2`` rN)__name__ __module__ __qualname____doc__DEFAULT INDEX_ONLYT/builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib/python3.11/site-packages/ddt.pyrr*s$*GJJJrrct|trdSt|ttfr"t t t |SdS)NTF) isinstance trivial_typeslisttupleallmap is_trivial)values rrrEsL%''+t ED%= ) )+3z5))*** 5rc2t|td|S)z2 Method decorator to add unpack feature. T)setattr UNPACK_ATTR)funcs runpackrMs  D+t$$$ Krc t|S)z Method decorator to add to your test methods. Should be added to methods of instances of ``unittest.TestCase``. )idata)valuess rdatar#Vs ==rc8ttttfd}|S)a Method decorator to add to your test methods. Should be added to methods of instances of ``unittest.TestCase``. :param iterable: iterable of the values to provide to the test function. :param index_len: an optional integer specifying the width to zero-pad the test identifier indices to. If not provided, this will add the fewest zeros necessary to make all identifiers the same length. Nc`t|tt|t|SN)r DATA_ATTR INDEX_LEN)r index_leniterables rwrapperzidata..wrapperps,i***i+++ r)rlenstr)r*r)r+s`` rr!r!`sW??CMM**++  Nrcfd}|S)a 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. ``yaml_loader`` can be used to customize yaml deserialization. The default is ``None``, which results in using the ``yaml.safe_load`` method. cdt|trt|t|Sr&)r FILE_ATTRYAML_LOADER_ATTR)rr yaml_loaders rr+zfile_data..wrappers5i'''  9 D*K 8 8 8 rr)rr2r+s`` r file_datar3xs), Nrcbd|dz|}|tjust|sd||S t |}n&#t $r|dd}YnwxYwd|||}tjdd|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 ``_``. We avoid doing str(value) if dealing with non-trivial values. The problem is possible different names with different runs, e.g. different order of dictionary keys (see PYTHONHASHSEED) or dealing with mock objects. Trivial scalar values are passed as is. A "trivial" value is a plain scalar, or a tuple or list consisting only of trivial values. The test name format is controlled by enum ``TestNameFormat`` as well. See the enum documentation for further details. z{0:0{1}}rz{0}_{1}asciibackslashreplacez {0}_{1}_{2}z \W|^(?=\d)_) formatrrrr-UnicodeEncodeErrorencoderesub)namerindexr)name_fmt test_names r mk_test_namerBs,   eai 3 3E>,,,Ju4E4E,e,,,:E  ::: W&899:$$T5%88I 6-i 0 00sA BBctfd}||_|_|||_n7jr0 jji|_n#t t f$rYnwxYw|S)zO This internal method decorator feeds the test data item to the test. c|gRiSr&r)selfargsrkwargss rr+zfeed_data..wrappers#tD*4***6***r)rr __wrapped__r r9 IndexErrorKeyError)rnew_nametest_data_docstringrFrGr+s` `` r feed_datarMs  4[[++++++[+GG&- <  "5$,"5t"Fv"F"F)       NsAA,+A,c Ht||t|||g|Ri|dS)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)rrM)clsrAtest_docstringrrFrGs radd_testrQsP CIdI~ rctjtj|}tjtj|}fd}tj|s0t|d}d}t||||dddS| d} | r7ts0t|d}d}t||||dddStj |dd 5} | rVt|tr,t!|t} t#j| | } n)t#j| } nt)j| } dddn #1swxYwYt+|||| dS) z= Process the parameter in the `file_data` decorator. cfd}|S)Nc(tzr&) ValueError)rF file_attrmessages rrz:process_file_data..create_error_func..funcsWy011 1rr)rWrrVs` rcreate_error_funcz,process_file_data..create_error_funcs' 2 2 2 2 2 2 rerrorzError!z%s does not existN)z.ymlz.yamlz(%s is a YAML file, please install PyYAMLrzutf-8)Loader)ospathabspathinspect getsourcefilejoindirnameexistsrBrQendswith _have_yamlcodecsopenhasattrr1getattryamlload safe_loadjson_add_tests_from_data) rOr>rrVcls_pathdata_file_pathrXrArP _is_yaml_filefr2r#s ` rprocess_file_datarssww4S99::HW\\"'//(";";YGGN 7>>. ) ) w// %i""#677 ? ? ?"++,=>>M Z  w// %     H I I       ^S' 2 2  a  t-.. )%d,<== y;777~a((9Q<rr#r)ielemkeyrrAs rrnrnsCD NN##IT?? = =4 dD ! ! @tDzC$T39==II d # # @E$T5!Y??I eT " " = S)Y > > > > > > S)Ye < < < < = =rc$t|d S)zNFinds out if the obj is a "primitive". It is somewhat hacky but it works. __dict__)rh)objs r _is_primitiver}"ssJ'' ''rc@t|s|jr|jSdS)zReturns a docstring based on the following resolution strategy: 1. Passed value is not a "primitive" and has a docstring, then use it. 2. In all other cases return None, i.e the test name is used. N)r}r )rrs r_get_test_data_docstringr(s*   EM}trc |dtjfd}tj|r ||n|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. Decorating with the keyword argument ``testNameFormat`` can control the format of the generated test names. For example: - ``@ddt(testNameFormat=TestNameFormat.DEFAULT)`` will be index and values. - ``@ddt(testNameFormat=TestNameFormat.INDEX_ONLY)`` will be index only. - ``@ddt`` is the same as DEFAULT. testNameFormatc t|jD]\\}}t|trt |t }tt |tD]\}}t|t |d||| }t||}t|trLt|tst|trt||||g|Rt||||fi|t|||||t||t|tr7t |t}t!||||t||^|S)Nr )rr{itemsrhr'rir(rurBrrrrrQdelattrr0rs) rOr>rr)rwvrArLrV fmt_test_names rr+zddt..wrapperVss|113344$ #$ #JD$tY''# ##D)44 %gdI&>&>??OODAq ,:q11!% !!I+C4*K*K't[11O%a//:a3F3F$ # ) 3 $  "# % # ) 3 $  #$ !i1DdANNNNT""""y)) ##D)44 !#tT9===T""" r)getrr r_isclass)argrGr+rs @rddtr3sZBJJ/1GHHM&&&&&T#?3// <773<<rs?  KKKJJJJJ    !  8T$ZZsE:>MM888T$ZZsE37MMM8T40<%&^=S1111D6   .0.0.0b===$((( M=M=M=M=M=M=s+55AA.-A.