3

Y'ícZ4ã@sBddlmZddlZddlmZmZmZmZmZGdd„dƒZ	dS)é)ÚcontextmanagerNé)Ú
ParserElementÚParseExceptionÚKeywordÚ__diag__Ú
__compat__c@sfeZdZdZGdd„dƒZGdd„dƒZedeej	e
ej	e
eeej	eej	eed	œd
d„ƒZdS)
Úpyparsing_testzB
    namespace class for classes useful in writing unit tests
    c@s@eZdZdZdd„Zdd„Zdd„Zdd	„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|_dS)N)Ú
_save_context)Úself©rú6/tmp/pip-build-gk9425m9/pyparsing/pyparsing/testing.pyÚ__init__-sz/pyparsing_test.reset_pyparsing_context.__init__cCsžtj|jd<tj|jd<tj|jd<tj|jd<tj|jd<tjrRtjj	|jd<n
d|jd<tj
|jd<tj|jd<d	d
„tj
Dƒ|jd<dtji|jd
<|S)NÚdefault_whitespaceÚdefault_keyword_charsÚliteral_string_classÚverbose_stacktraceÚpackrat_enabledÚpackrat_cache_sizeÚ
packrat_parseÚrecursion_enabledcSsi|]}tt|ƒ|“qSr)Úgetattrr)Ú.0Únamerrr
ú
<dictcomp>Fsz?pyparsing_test.reset_pyparsing_context.save.<locals>.<dictcomp>rÚcollect_all_And_tokensr)rÚDEFAULT_WHITE_CHARSr
rÚDEFAULT_KEYWORD_CHARSÚ_literalStringClassrÚ_packratEnabledÚ
packrat_cacheÚsizeÚ_parseÚ_left_recursion_enabledrZ
_all_namesrr)rrrr
Úsave0s
z+pyparsing_test.reset_pyparsing_context.savecCsÂtj|jdkr tj|jdƒ|jdt_|jdt_tj|jdƒx.|jdjƒD]\}}|rjt	j
nt	j|ƒqXWdt_|jdrštj
|jdƒn|jd	t_|jd
t_|jdt_|S)NrrrrrFrrrrr)rrr
Zset_default_whitespace_charsrrrÚinlineLiteralsUsingÚitemsrÚenableÚdisablerZenable_packratr"r#rr)rrÚvaluerrr
ÚrestorePs$
z.pyparsing_test.reset_pyparsing_context.restorecCst|ƒƒ}|jj|jƒ|S)N)Útyper
Úupdate)rÚretrrr
Úcopyqs
z+pyparsing_test.reset_pyparsing_context.copycCs|jƒS)N)r$)rrrr
Ú	__enter__vsz0pyparsing_test.reset_pyparsing_context.__enter__cGs|jƒdS)N)r*)rÚargsrrr
Ú__exit__ysz/pyparsing_test.reset_pyparsing_context.__exit__N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__rr$r*r.r/r1rrrr
Úreset_pyparsing_contexts !r6c@sJeZdZdZddd„Zddd„Zddd	„Zdd
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<|dk	r|j||jƒ|d|dk	r8|j||jƒ|ddS)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``.
            N)Úmsg)ZassertEqualÚas_listZas_dict)rÚresultÚ
expected_listÚ
expected_dictr7rrr
ÚassertParseResultsEqualssz?pyparsing_test.TestParseResultsAsserts.assertParseResultsEqualsTcCs@|j|dd}|r t|jƒƒnt|jƒƒ|j|||ddS)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)Z	parse_all)r:r7N)Úparse_stringÚprintÚdumpr8r<)rÚexprÚtest_stringr:r7Úverboser9rrr
ÚassertParseAndCheckLists
z>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckListcCs@|j|dd}|r t|jƒƒnt|jƒƒ|j|||ddS)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)r;r7N)r=r>r?r8r<)rr@rAr;r7rBr9rrr
ÚassertParseAndCheckDict›s
z>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckDictcCs|\}}|dk	rðdd„t||ƒDƒ}xÊ|D]Â\}}}	tdd„|	Dƒdƒ}
tdd„|	Dƒdƒ}|dk	r’|j||
pp|dt|tƒr†|‚WdQRXq*tdd„|	Dƒdƒ}td	d„|	Dƒdƒ}
||
fdkrÞ|j|||
|
pÖ|d
q*tdj|ƒƒq*W|j||dk	r|ndd
dS)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|]\}}||f˜‘qSrr)rZrptÚexpectedrrr
ú
<listcomp>¹szOpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<listcomp>css|]}t|tƒr|VqdS)N)Ú
isinstanceÚstr)rÚexprrr
ú	<genexpr>ÁszNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>css&|]}t|tƒrt|tƒr|VqdS)N)rHr+Ú
issubclassÚ	Exception)rrJrrr
rKÅs)Úexpected_exceptionr7css|]}t|tƒr|VqdS)N)rHÚlist)rrJrrr
rKÓscss|]}t|tƒr|VqdS)N)rHÚdict)rrJrrr
rKÖs)r:r;r7zno validation for {!r}zfailed runTests)r7)NN)	ÚzipÚnextÚassertRaisesrHrMr<r>ÚformatZ
assertTrue)rZrun_tests_reportZexpected_parse_resultsr7Zrun_test_successZrun_test_resultsZmergedrAr9rFZfail_msgrNr:r;rrr
ÚassertRunTestResults©s:
z;pyparsing_test.TestParseResultsAsserts.assertRunTestResultsc	cs$|j||ddVWdQRXdS)N)r7)rS)rÚexc_typer7rrr
ÚassertRaisesParseExceptionèszApyparsing_test.TestParseResultsAsserts.assertRaisesParseException)NNN)NT)NT)NN)r2r3r4r5r<rCrErUrrrWrrrr
ÚTestParseResultsAsserts|s



>rXNTú|)ÚsÚ
start_lineÚend_lineÚexpand_tabsÚeol_markÚmark_spacesÚmark_controlÚreturncs|r|jƒ}ˆdk	r‚ˆdkrPtjdd„ttddƒtddƒƒDƒd	d
iBƒ}d‰n(tj‡fdd„ttdd
ƒƒd	gDƒƒ}|j|ƒ}|dk	rÂ|dkrÂ|dkr¶tjdddœƒ}|j|ƒ}n|jd|ƒ}|dkrÎd}|dkrÞt|ƒ}t	|t|ƒƒ}t	t
d|ƒ|ƒ}ˆdkr|jƒ|d|…}n dd„|jdƒ|d|…Dƒ}|sFdStt|ƒƒ‰t
dd„|Dƒƒ}	dˆd}
|	dkr¦|
dj
dd„tt
|	ddƒƒDƒƒd}nd}||
dj
dd„t|	dƒDƒƒd}|
d|	dd}
||
dj
‡‡fdd„t||d DƒƒdS)!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
        NÚunicodecSsi|]\}}||“qSrr)rÚcÚurrr
rsz4pyparsing_test.with_line_numbers.<locals>.<dictcomp>ré!i$i3$éi!$Úcsi|]
}ˆ|“qSrr)rrc)r`rr
rsé ú i	$i#$)é	rhrcSsg|]}|d‘qS)u␊r)rÚlinerrr
rG'sz4pyparsing_test.with_line_numbers.<locals>.<listcomp>u␊css|]}t|ƒVqdS)N)Úlen)rrkrrr
rK,sz3pyparsing_test.with_line_numbers.<locals>.<genexpr>éccss&|]}djdd|ddƒVqdS)z{}{}rirmrédN)rT)rÚirrr
rK2srnÚ
css |]}dj|ddƒVqdS)z         {}ré
N)rT)rrorrr
rK=srqZ
1234567890c3s"|]\}}dj|ˆ|ˆƒVqdS)z{:{}d}:{}{}N)rT)rrork)r^Úlineno_widthrr
rKGs)Ústart)Ú
expandtabsrIÚ	maketransrQÚrangerOÚ	translateÚreplacerlÚminÚmaxÚ
splitlinesÚsplitÚjoinÚ	enumerate)rZr[r\r]r^r_r`ZtblZs_linesZmax_line_lenZleadZheader0Zheader1Zheader2r)r^rrr`r
Úwith_line_numbersísH$

 
,,z pyparsing_test.with_line_numbers)NNTrYNN)
r2r3r4r5r6rXÚstaticmethodrIÚtypingÚOptionalÚintÚboolrrrrr
r	shq(r	)
Ú
contextlibrrÚcorerrrrrr	rrrr
Ú<module>s