o
    ò‘OjZ4  ã                   @   sB   d dl mZ d dlZddlmZmZmZmZmZ G dd„ dƒZ	dS )é    )ÚcontextmanagerNé   )ÚParserElementÚParseExceptionÚKeywordÚ__diag__Ú
__compat__c                   @   s€   e Zd ZdZG dd„ dƒZG dd„ dƒZe						dd	ed
ej	e
 dej	e
 dededej	e dej	e defdd„ƒZdS )Úpyparsing_testzB
    namespace class for classes useful in writing unit tests
    c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )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
        c                 C   s
   i | _ d S ©N)Ú_save_context©Úself© r   úc/var/www/html/pharmsmart-cdr-gen/venv/lib/python3.10/site-packages/pip/_vendor/pyparsing/testing.pyÚ__init__-   s   
z/pyparsing_test.reset_pyparsing_context.__init__c                 C   sž   t j| jd< tj| jd< t j| jd< t j| jd< t j| jd< t jr)t jj	| jd< nd | jd< t j
| jd< t j| jd< d	d
„ tjD ƒ| jd< dtji| jd< | S )NÚdefault_whitespaceÚdefault_keyword_charsÚliteral_string_classÚverbose_stacktraceÚpackrat_enabledÚpackrat_cache_sizeÚpackrat_parseÚrecursion_enabledc                 S   s   i | ]}|t t|ƒ“qS r   )Úgetattrr   )Ú.0Únamer   r   r   Ú
<dictcomp>F   s    ÿz?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_enabledr   Ú
_all_namesr   r   r   r   r   r   Úsave0   s2   þÿþÿ
þÿÿ
ÿz+pyparsing_test.reset_pyparsing_context.savec                 C   s¾   t j| jd krt  | jd ¡ | jd t _| jd t_t  | jd ¡ | jd  ¡ D ]\}}|r4t	j
nt	j|ƒ q+dt _| jd rKt  | jd ¡ n| jd	 t _| jd
 t _| jd t_| S )Nr   r   r   r   r   Fr   r   r   r   r   )r   r   r   Úset_default_whitespace_charsr   r   r   ÚinlineLiteralsUsingÚitemsr   ÚenableÚdisabler!   Úenable_packratr$   r%   r   r   )r   r   Úvaluer   r   r   ÚrestoreP   s.   ÿÿÿÿ
ÿz.pyparsing_test.reset_pyparsing_context.restorec                 C   s   t | ƒƒ }|j | j¡ |S r
   )Útyper   Úupdate)r   Úretr   r   r   Úcopyq   s   
z+pyparsing_test.reset_pyparsing_context.copyc                 C   s   |   ¡ S r
   )r'   r   r   r   r   Ú	__enter__v   s   z0pyparsing_test.reset_pyparsing_context.__enter__c                 G   s   |   ¡  d S r
   )r/   )r   Úargsr   r   r   Ú__exit__y   s   z/pyparsing_test.reset_pyparsing_context.__exit__N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r/   r3   r4   r6   r   r   r   r   Úreset_pyparsing_context   s     !r;   c                   @   sR   e Zd ZdZ	ddd„Z	ddd„Z	ddd	„Z	dd
d„Zee	dfdd„ƒZ
dS )z&pyparsing_test.TestParseResultsAssertszk
        A mixin class to add parse results assertion methods to normal unittest.TestCase classes.
        Nc                 C   s@   |dur| j || ¡ |d� |dur| j || ¡ |d� dS dS )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)ÚassertEqualÚas_listÚas_dict)r   ÚresultÚexpected_listÚexpected_dictr=   r   r   r   ÚassertParseResultsEquals�   s
   ÿz?pyparsing_test.TestParseResultsAsserts.assertParseResultsEqualsTc                 C   ó@   |j |dd�}|rt| ¡ ƒ nt| ¡ ƒ | j|||d� dS )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)rB   r=   N©Úparse_stringÚprintÚdumpr?   rD   )r   ÚexprÚtest_stringrB   r=   ÚverboserA   r   r   r   ÚassertParseAndCheckList�   ó
   z>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckListc                 C   rE   )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)rC   r=   NrG   )r   rK   rL   rC   r=   rM   rA   r   r   r   ÚassertParseAndCheckDict›   rO   z>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckDictc              	   C   s  |\}}|dur€dd„ t ||ƒD ƒ}|D ]k\}}}	tdd„ |	D ƒdƒ}
tdd„ |	D ƒdƒ}|durR| j||
p7|d�� t|tƒrB|‚W d  ƒ n1 sLw   Y  qtdd„ |	D ƒdƒ}td	d„ |	D ƒdƒ}||fd
krx| j||||
pt|d� qtd |¡ƒ q| j||dur‰|ndd� dS )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)]
            Nc                 S   s   g | ]\}}g |¢|‘R ‘qS r   r   )r   ÚrptÚexpectedr   r   r   Ú
<listcomp>¸   s    ÿÿzOpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<listcomp>c                 s   ó   � | ]
}t |tƒr|V  qd S r
   )Ú
isinstanceÚstr©r   Úexpr   r   r   Ú	<genexpr>Á   ó   € zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>c                 s   s(   � | ]}t |tƒrt|tƒr|V  qd S r
   )rV   r0   Ú
issubclassÚ	ExceptionrX   r   r   r   rZ   Ä   s   € ýý
ÿ)Úexpected_exceptionr=   c                 s   rU   r
   )rV   ÚlistrX   r   r   r   rZ   Ó   r[   c                 s   rU   r
   )rV   ÚdictrX   r   r   r   rZ   Ö   r[   ©NN)rB   rC   r=   zno validation for {!r}zfailed runTestsr<   )	ÚzipÚnextÚassertRaisesrV   r]   rD   rI   ÚformatÚ
assertTrue)r   Úrun_tests_reportÚexpected_parse_resultsr=   Úrun_test_successÚrun_test_resultsÚmergedrL   rA   rS   Úfail_msgr^   rB   rC   r   r   r   ÚassertRunTestResults©   sR   þÿþúÿ
ÿý€ÿÿü
ÿz;pyparsing_test.TestParseResultsAsserts.assertRunTestResultsc                 c   s<   � | j ||d�� d V  W d   ƒ d S 1 sw   Y  d S )Nr<   )rd   )r   Úexc_typer=   r   r   r   ÚassertRaisesParseExceptionè   s   €"ÿzApyparsing_test.TestParseResultsAsserts.assertRaisesParseException)NNN)NTra   )r7   r8   r9   r:   rD   rN   rQ   rm   r   r   ro   r   r   r   r   ÚTestParseResultsAsserts|   s    
ÿ
ÿ
ÿ
ÿ?rp   NTú|ÚsÚ
start_lineÚend_lineÚexpand_tabsÚeol_markÚmark_spacesÚmark_controlÚreturnc                    s  |r|   ¡ } ˆdurAˆdkr(t dd„ ttddƒtddƒƒD ƒd	d
iB ¡}d‰ nt ‡fdd„ttddƒƒd	g D ƒ¡}|  |¡} |dura|dkra|dkr[t dddœ¡}|  |¡} n|  d|¡} |du rgd}|du rot| ƒ}t	|t| ƒƒ}t	t
d|ƒ|ƒ}ˆdkr�|  ¡ |d |… }ndd„ |  d¡|d |… D ƒ}|s¡dS tt|ƒƒ‰t
dd„ |D ƒƒ}	dˆd  }
|	dkrÐ|
d dd„ tt
|	d dƒƒD ƒ¡ d }nd}||
 d dd„ t|	 d  ƒD ƒ¡ d }|
d|	 d    d }|| d ‡ ‡fdd„t||d �D ƒ¡ d S )!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Úunicodec                 S   s   i | ]\}}||“qS r   r   )r   ÚcÚur   r   r   r     s    z4pyparsing_test.with_line_numbers.<locals>.<dictcomp>r   é!   i $  i3$  é   i!$  Ú c                    s   i | ]}|ˆ “qS r   r   )r   r{   )rx   r   r   r     s    é    ú i	$  i#$  )é	   r€   r   c                 S   s   g | ]}|d  ‘qS )õ   â�Šr   ©r   Úliner   r   r   rT   '  s    z4pyparsing_test.with_line_numbers.<locals>.<listcomp>rƒ   c                 s   s   � | ]}t |ƒV  qd S r
   )Úlenr„   r   r   r   rZ   ,  s   € z3pyparsing_test.with_line_numbers.<locals>.<genexpr>éc   c                 s   s$   � | ]}d   d|d d ¡V  qdS )z{}{}zc                                                                                                   r   éd   N©re   ©r   Úir   r   r   rZ   1  s
   € ÿ
ÿrˆ   Ú
c                 s   s"   � | ]}d   |d d ¡V  qdS )z         {}r   é
   Nr‰   rŠ   r   r   r   rZ   <  s
   € ÿ
ÿr�   Ú
1234567890c                 3   s$   � | ]\}}d   |ˆ|ˆ ¡V  qdS )z{:{}d}:{}{}Nr‰   )r   r‹   r…   )rv   Úlineno_widthr   r   rZ   F  s
   € ÿ
ÿ)Ústart)Ú
expandtabsrW   Ú	maketransrb   Úranger_   Ú	translateÚreplacer†   ÚminÚmaxÚ
splitlinesÚsplitÚjoinÚ	enumerate)rr   rs   rt   ru   rv   rw   rx   ÚtblÚs_linesÚmax_line_lenÚleadÚheader0Úheader1Úheader2r   )rv   r�   rx   r   Úwith_line_numbersí   s€   ÿÿ ÿ
 
þÿûÿ	ÿ
þþúÿ	ÿ
þþúÿz pyparsing_test.with_line_numbers)NNTrq   NN)r7   r8   r9   r:   r;   rp   ÚstaticmethodrW   ÚtypingÚOptionalÚintÚboolr£   r   r   r   r   r	      s8    hqùÿþýüûúùør	   )
Ú
contextlibr   r¥   Úcorer   r   r   r   r   r	   r   r   r   r   Ú<module>   s   	