o 7]hL4@sFddlmZddlmZddlmZmZmZmZm Z GdddZ dS))contextmanager)Optional) ParserElementParseExceptionKeyword__diag__ __compat__c@sxeZdZdZGdddZGdddZe      dd ed ee d ee d e d edeedeedefddZ dS)pyparsing_testzB namespace class for classes useful in writing unit tests c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)z&pyparsing_test.reset_pyparsing_contexta Context manager to be used when writing unit tests that modify pyparsing config values: - packrat parsing - bounded recursion parsing - default whitespace characters. - default keyword characters - literal string auto-conversion class - __diag__ settings Example:: with reset_pyparsing_context(): # test that literals used to construct a grammar are automatically suppressed ParserElement.inlineLiteralsUsing(Suppress) term = Word(alphas) | Word(nums) group = Group('(' + term[...] + ')') # assert that the '()' characters are not included in the parsed tokens self.assertParseAndCheckList(group, "(abc 123 def)", ['abc', '123', 'def']) # after exiting context manager, literals are converted to Literal expressions again cCs i|_dSN) _save_contextselfr?/usr/lib/python3/dist-packages/pip/_vendor/pyparsing/testing.py__init__-s z/pyparsing_test.reset_pyparsing_context.__init__cCstj|jd<tj|jd<tj|jd<tj|jd<tj|jd<tjr)tjj |jd<nd|jd<tj |jd<tj |jd<d d t j D|jd <d tji|jd <|S)Ndefault_whitespacedefault_keyword_charsliteral_string_classverbose_stacktracepackrat_enabledpackrat_cache_size packrat_parserecursion_enabledcSsi|]}|tt|qSr)getattrr).0namerrr Fs z?pyparsing_test.reset_pyparsing_context.save..rcollect_all_And_tokensr )rDEFAULT_WHITE_CHARSr rDEFAULT_KEYWORD_CHARS_literalStringClassr_packratEnabled packrat_cachesize_parse_left_recursion_enabledr _all_namesr rr rrrsave0s2        z+pyparsing_test.reset_pyparsing_context.savecCstj|jdkrt|jd|jdt_|jdt_t|jd|jdD]\}}|r4t j nt j |q+dt_ |jdrKt |jdn|jd t_|jd t_|jd t_|S) NrrrrrFrrrrr )rrr set_default_whitespace_charsrrr inlineLiteralsUsingitemsrenabledisabler"enable_packratr%r&r r)rrvaluerrrrestorePs,     z.pyparsing_test.reset_pyparsing_context.restorecCst|}|j|j|Sr )typer update)rretrrrcopyqs z+pyparsing_test.reset_pyparsing_context.copycCs|Sr )r(r rrr __enter__vsz0pyparsing_test.reset_pyparsing_context.__enter__cGs |dSr )r0)rargsrrr__exit__ys z/pyparsing_test.reset_pyparsing_context.__exit__N) __name__ __module__ __qualname____doc__rr(r0r4r5r7rrrrreset_pyparsing_contexts ! r<c@sReZdZdZ dddZ dddZ ddd Z dd d Zee dfd d Z dS)z&pyparsing_test.TestParseResultsAssertszk A mixin class to add parse results assertion methods to normal unittest.TestCase classes. NcCs@|dur|j|||d|dur|j|||ddSdS)z Unit test assertion to compare a :class:`ParseResults` object with an optional ``expected_list``, and compare any defined results names with an optional ``expected_dict``. Nmsg) assertEqualas_listas_dict)rresult expected_list expected_dictr>rrrassertParseResultsEqualss z?pyparsing_test.TestParseResultsAsserts.assertParseResultsEqualsTcC@|j|dd}|rt|nt||j|||ddS)z Convenience wrapper assert to test a parser element and input string, and assert that the resulting ``ParseResults.asList()`` is equal to the ``expected_list``. T) parse_all)rCr>N parse_stringprintdumpr@rE)rexpr test_stringrCr>verboserBrrrassertParseAndCheckList  z>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckListcCrF)z Convenience wrapper assert to test a parser element and input string, and assert that the resulting ``ParseResults.asDict()`` is equal to the ``expected_dict``. T)parseAll)rDr>NrH)rrLrMrDr>rNrBrrrassertParseAndCheckDictrPz>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckDictc Cs|\}}|durddt||D}|D]k\}}} tdd| Dd} tdd| Dd} | durR|j| | p7|dt|trB|Wdn1sLwYqtdd| Dd} td d| Dd} | | fd krx|j|| | | pt|d qtd |q|j||dur|nd ddS)ah Unit test assertion to evaluate output of ``ParserElement.runTests()``. If a list of list-dict tuples is given as the ``expected_parse_results`` argument, then these are zipped with the report tuples returned by ``runTests`` and evaluated using ``assertParseResultsEquals``. Finally, asserts that the overall ``runTests()`` success value is ``True``. :param run_tests_report: tuple(bool, [tuple(str, ParseResults or Exception)]) returned from runTests :param expected_parse_results (optional): [tuple(str, list, dict, Exception)] NcSsg|] \}}g||RqSrr)rrptexpectedrrr s zOpyparsing_test.TestParseResultsAsserts.assertRunTestResults..cs|] }t|tr|VqdSr ) isinstancestrrexprrr zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults..css(|]}t|trt|tr|VqdSr )rWr1 issubclass ExceptionrYrrrr[s )expected_exceptionr>csrVr )rWlistrYrrrr[r\csrVr )rWdictrYrrrr[r\NN)rCrDr>zno validation for {!r}zfailed runTestsr=) zipnext assertRaisesrWr^rErJformat assertTrue)rrun_tests_reportexpected_parse_resultsr>run_test_successrun_test_resultsmergedrMrBrTfail_msgr_rCrDrrrassertRunTestResultssR    z;pyparsing_test.TestParseResultsAsserts.assertRunTestResultsccs<|j||d dVWddS1swYdS)Nr=)re)rexc_typer>rrrassertRaisesParseExceptions"zApyparsing_test.TestParseResultsAsserts.assertRaisesParseException)NNN)NTrb) r8r9r:r;rErOrRrnrrrprrrrTestParseResultsAsserts|s    ?rqNT|s start_lineend_line expand_tabseol_mark mark_spaces mark_controlreturncs|r|}durAdkr(tddttddtddDd d iB}d ntfd dttdd d gD}||}|dura|dkra|dkr[tddd}||}n|d|}|durgd}|durot|}t |t|}t t d||}dkr| |d|}ndd| d|d|D}|sd Stt|t dd|D} dd} | dkr| d ddtt | ddDd} nd } | | d ddt| d Dd} | d| d d} | | d fddt||d DdS)!u  Helpful method for debugging a parser - prints a string with line and column numbers. (Line and column numbers are 1-based.) :param s: tuple(bool, str - string to be printed with line and column numbers :param start_line: int - (optional) starting line number in s to print (default=1) :param end_line: int - (optional) ending line number in s to print (default=len(s)) :param expand_tabs: bool - (optional) expand tabs to spaces, to match the pyparsing default :param eol_mark: str - (optional) string to mark the end of lines, helps visualize trailing spaces (default="|") :param mark_spaces: str - (optional) special character to display in place of spaces :param mark_control: str - (optional) convert non-printing control characters to a placeholding character; valid values: - "unicode" - replaces control chars with Unicode symbols, such as "␍" and "␊" - any single character string - replace control characters with given string - None (default) - string is displayed as-is :return: str - input string with leading line numbers and column number headers NunicodecSsi|]\}}||qSrr)rcurrrrsz4pyparsing_test.with_line_numbers..r!i$i3$i!$csi|]}|qSrr)rr|)ryrrrs  i $i#$) rrcSsg|]}|dqS)␊rrlinerrrrU'sz4pyparsing_test.with_line_numbers..rcss|]}t|VqdSr )lenrrrrr[,sz3pyparsing_test.with_line_numbers..ccss$|] }dd|ddVqdS)z{}{}zc rdNrfrirrrr[1s  r css"|] }d|ddVqdS)z {}r Nrrrrrr[<s  r 1234567890c3s$|] \}}d||VqdS)z {:{}d}:{}{}Nr)rrr)rw lineno_widthrrr[Fs  )start) expandtabsrX maketransrcranger` translatereplacerminmax splitlinessplitjoin enumerate)rsrtrurvrwrxrytbls_lines max_line_lenleadheader0header1header2r)rwrryrwith_line_numberss            z pyparsing_test.with_line_numbers)NNTrrNN) r8r9r:r;r<rq staticmethodrXrintboolrrrrrr s8hqr N) contextlibrtypingrcorerrrrr r rrrrs