
    &
d4                     H    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      Z	y)    )contextmanagerN   )ParserElementParseExceptionKeyword__diag__
__compat__c                       e Zd ZdZ G d d      Z G 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       Zy)pyparsing_testzB
    namespace class for classes useful in writing unit tests
    c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	&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                     i | _         y N)_save_contextselfs    3/usr/lib/python3/dist-packages/pyparsing/testing.py__init__z/pyparsing_test.reset_pyparsing_context.__init__-   s
    !#D    c                    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<   t        j                  D ci c]  }|t        t        |       c}| j                  d	<   d
t        j                   i| j                  d<   | S c c}w )Ndefault_whitespacedefault_keyword_charsliteral_string_classverbose_stacktracepackrat_enabledpackrat_cache_sizepackrat_parserecursion_enabledr   collect_all_And_tokensr	   )r   DEFAULT_WHITE_CHARSr   r   DEFAULT_KEYWORD_CHARS_literalStringClassr   _packratEnabledpackrat_cachesize_parse_left_recursion_enabledr   
_all_namesgetattrr	   r   )r   names     r   savez+pyparsing_test.reset_pyparsing_context.save0   sT   7D7X7XD34:A:W:WD67 11 & 8E7W7WD344A4Q4QD01,, "//44 ""( <@""#782?2F2FD/ 55 #
 ;C:M:M.26gh--.Dz*
 )**K*K0D|, K.s   %E.c                    t         j                  | j                  d   k7  r"t        j                  | j                  d          | j                  d   t         _        | j                  d   t
        _        t        j                  | j                  d          | j                  d   j                         D ]-  \  }} |rt        j                  nt        j                  |       / dt         _        | j                  d   r#t        j                  | 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*   values      r   restorez.pyparsing_test.reset_pyparsing_context.restoreP   s@    11%%&:;< ::&&';< 04/A/ABV/WM,,0,>,>?V,WG)--""#9:  $11*=CCE Ge?Ex/?/?FG -2M)!!"34,,T-?-?@T-UV'+'9'9/'J$484F4F#5M1 150B0B<0PJ-Kr   c                 p     t        |              }|j                  j                  | j                         |S r   )typer   update)r   rets     r   copyz+pyparsing_test.reset_pyparsing_context.copyq   s.    $t*,C$$T%7%78Jr   c                 "    | j                         S r   )r+   r   s    r   	__enter__z0pyparsing_test.reset_pyparsing_context.__enter__v   s    99;r   c                 $    | j                          y r   )r4   )r   argss     r   __exit__z/pyparsing_test.reset_pyparsing_context.__exit__y   s    LLNr   N)
__name__
__module____qualname____doc__r   r+   r4   r9   r;   r>    r   r   reset_pyparsing_contextr      s'    	0	$	@	B	
		r   rD   c                   N    e Zd ZdZ	 ddZ	 d	dZ	 d	dZ	 d
dZee	dfd       Z
y)&pyparsing_test.TestParseResultsAssertszk
        A mixin class to add parse results assertion methods to normal unittest.TestCase classes.
        Nc                     |"| j                  ||j                         |       |#| j                  ||j                         |       yy)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_dictrI   s        r   assertParseResultsEqualsz?pyparsing_test.TestParseResultsAsserts.assertParseResultsEquals   sM     (  0@c J(  0@c J )r   c                     |j                  |d      }|rt        |j                                nt        |j                                | j	                  |||       y)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)rN   rI   Nparse_stringprintdumprK   rP   )r   exprtest_stringrN   rI   verboserM   s          r   assertParseAndCheckListz>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckList   sO     &&{d&CFfkkm$fnn&'))&SV)Wr   c                     |j                  |d      }|rt        |j                                nt        |j                                | j	                  |||       y)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)rO   rI   NrS   )r   rW   rX   rO   rI   rY   rM   s          r   assertParseAndCheckDictz>pyparsing_test.TestParseResultsAsserts.assertParseAndCheckDict   sO     &&{T&BFfkkm$fnn&'))&SV)Wr   c                 .   |\  }}|t        ||      D cg c]  \  }}g || }}}|D ]  \  }	}
}t        d |D        d      }t        d |D        d      }|4| j                  ||xs |      5  t        |
t              r|
	 ddd       ct        d |D        d      }t        d |D        d      }||fdk7  r| j                  |
|||xs |       t        d	|	        | j                  |||       yd
       yc c}}w # 1 sw Y   xY w)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              3   B   K   | ]  }t        |t              s|  y wr   )
isinstancestr.0exps     r   	<genexpr>zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>   s     IJsC4HI   c              3   `   K   | ]&  }t        |t              rt        |t              r| ( y wr   )r`   r6   
issubclass	Exceptionrb   s     r   re   zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>   s+       #)#t4C9S  s   ,.)expected_exceptionrI   c              3   B   K   | ]  }t        |t              s|  y wr   )r`   listrb   s     r   re   zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>        NS
38MSNrf   c              3   B   K   | ]  }t        |t              s|  y wr   )r`   dictrb   s     r   re   zNpyparsing_test.TestParseResultsAsserts.assertRunTestResults.<locals>.<genexpr>   rm   rf   NN)rN   rO   rI   zno validation for zfailed runTestsrH   )zipnextassertRaisesr`   ri   rP   rU   
assertTrue)r   run_tests_reportexpected_parse_resultsrI   run_test_successrun_test_resultsrptexpectedmergedrX   rM   fail_msgrj   rN   rO   s                  r   assertRunTestResultsz;pyparsing_test.TestParseResultsAsserts.assertRunTestResults   s    2B..%1 *--=?U)V%X %c$8$  6< %H1K  $II4 H *.'/
 *& *5!../AxSV /  -  *&)<&,  =- - )-NHNPT) )-NHNPT) *=9\I 99 &.;.;$,O	 :  "$6{o"FGK%HP OO S_c  BS  Y(- -s   D3DD	c              #   d   K   | j                  ||      5  d  d d d        y # 1 sw Y   y xY ww)NrH   )rs   )r   exc_typerI   s      r   assertRaisesParseExceptionzApyparsing_test.TestParseResultsAsserts.assertRaisesParseException   s0     ""8"5   s   0$	0-0)NNN)NTrp   )r?   r@   rA   rB   rP   rZ   r]   r}   r   r   r   rC   r   r   TestParseResultsAssertsrF   |   sT    	
 GK
	K GK	X GK	X FJ=	~ 
6D$ 	 
	r   r   Ns
start_lineend_lineexpand_tabseol_markmark_spacesmark_controlreturnc           
         |r| j                         } |t        j                  t        |      }|dk(  rPt	        t        dd      t        dd            D ci c]  \  }}||
 }	}}d|	d<   t        j                  |	      }
dnFt        |      }t        j                  t        t        dd	            dgz   D ci c]  }|| c}      }
| j                  |
      } |F|d
k7  rA|dk(  r*t        j                  ddd      }
| j                  |
      } n| j                  d
|      } |d}|t        |       }t        |t        |             }t        t        d|      |      }|dk7  r| j                         |dz
  | }n'| j                  d      |dz
  | D cg c]  }|dz   	 }}|syt        t        |            t        d |D              }d
dz   z  }|dk\  r5|dj!                  d t        t        |dz  d            D              z   dz   }nd}||z   dj!                  d t        | dz         D              z   dz   }|d| dz   z  z   dz   }||z   dj!                  fdt#        ||      D              z   dz   S c c}}w c c}w c c}w )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
        unicoder   !   i $  i3$  i!$           i	$  i#$  )	   r   r   u   ␊c              3   2   K   | ]  }t        |        y wr   )len)rc   lines     r   re   z3pyparsing_test.with_line_numbers.<locals>.<genexpr>/  s     93t99   c   c              3   4   K   | ]  }d  |dz   dz     yw)zc                                                                                                   r   d   NrC   rc   is     r   re   z3pyparsing_test.with_line_numbers.<locals>.<genexpr>4  s)        j!a%30s   r   
c              3   2   K   | ]  }d |dz   dz     yw)z	         r   
   NrC   r   s     r   re   z3pyparsing_test.with_line_numbers.<locals>.<genexpr>?  s     XQ	1q5B,0Xr   r   
1234567890c              3   >   K   | ]  \  }}| d d|    yw)d:NrC   )rc   r   r   r   lineno_widths      r   re   z3pyparsing_test.with_line_numbers.<locals>.<genexpr>F  s6      At l^1$%QtfXJ7s   )start)
expandtabstypingcastra   rq   range	maketransordrl   	translatereplacer   minmax
splitlinessplitjoin	enumerate)r   r   r   r   r   r   r   cutranstable_maptblord_mark_controls_linesr   max_line_lenleadheader0header1header2r   s       `              @r   with_line_numbersz pyparsing_test.with_line_numbers   s   8 A#!;;sL9Ly(%(q"uVV7L%M"!QAqD" " '-s#mmN3#&|#4 mm26uQ|2Du2LMQQ((M C A"{c'9i'mmF$;<KK$IIc;/J1vHxQ(Q
+X6
9$llnZ!^h?G01zA~PX0YZte|ZGZ3x=)999lQ&'2'' "3|s':A#>? 
   GggX%<-SUBU@V:WXXY  	 L=B+>)?@@4Gii (
C  	
k" N& [s   I?;
JJ
)NNT|NN)r?   r@   rA   rB   rD   r   staticmethodra   r   Optionalintboolr   rC   r   r   r   r      s    f fPo ob  ,0)- ,0-1]
]
OOC(]
 //#&]
 	]

 ]
 __S)]
 ooc*]
 
]
 ]
r   r   )

contextlibr   r   corer   r   r   r   r	   r   rC   r   r   <module>r      s#    &  |
 |
r   