o
    DDi("                     @   s   d Z ddlZddlZddlZddlmZ ddlZddlmZ ddl	Z	ej
jddeddejjejjgd	 ej
d ej
jd
ej
ddejjejjfd G dd dejZedkrhej
d
 e  dS dS )z
Fuzz-tests for libCST, by Zac Hatfield-Dodds (zac@hypothesis.works)

For Hypothesis documentation, see https://hypothesis.readthedocs.io/
For my Python code generator, see https://pypi.org/project/hypothesmith/
    N)	timedelta)from_grammarzsettings-for-unit-testsTi  )seconds)name
print_blobdeadlinesuppress_health_checkzsettings-for-fuzzingi ʚ;)r   parentmax_examplesphasesc                   @   s&  e Zd ZdZeeej	ddde
jedddded	d
fddZeeej	ddde
jeddejdded	d
fddZeeej	ddde
jedddd dded	d
fddZdededed	d
fddZededed	d
fddZeded	d
fddZd
S )FuzzTestz0Fuzz-tests based on Hypothesis and Hypothesmith.
HYPOTHESISFzHypothesis not requested
file_input)startsource_coder   returnNc                 C   s4   | j |dd | | t|}| ||j dS )a  The `from_grammar()` strategy generates strings from Python's grammar.

        This has a bunch of weird edge cases because "strings accepted by Cpython"
        isn't actually the same set as "strings accepted by the grammar", and for
        a few other weird reasons you can ask Zac about if you're interested.

        Valid values for ``start`` are ``"single_input"``, ``"file_input"``, or
        ``"eval_input"``; respectively a single interactive statement, a module or
        sequence of commands read from a file, and input for the eval() function.

        .. warning::
            DO NOT EXECUTE CODE GENERATED BY THE `from_grammar()` STRATEGY.

            It could do literally anything that running Python code is able to do,
            including changing, deleting, or uploading important data.  Arbitrary
            code can be useful, but "arbitrary code execution" can be very, very bad.
        execmodeN)reject_invalid_codereject_unsupported_codelibcstparse_moduleassertEqualcodeselfr   tree r   O/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/tests/test_fuzz.py&test_parsing_compilable_module_strings2   s   

z/FuzzTest.test_parsing_compilable_module_strings
eval_inputc                 C   sd   | j |dd | | zt|}| j|tg |dd W dS  tjy1   t	  Y dS w )zMuch like statements, but for expressions this time.

        We change the start production of the grammar, the compile mode,
        and the libCST parse function, but codegen is as for statements.
        evalr   N)
r   r   r   parse_expressionverify_identical_astsModulecode_for_nodeParserSyntaxError
hypothesisrejectr   r   r   r    *test_parsing_compilable_expression_stringsP   s   

	z3FuzzTest.test_parsing_compilable_expression_stringssingle_inputc                 C   s   |  ddd S )N
 )replace)sr   r   r    <lambda>x   s    zFuzzTest.<lambda>c                 C   sB   | j |dd | | t|}| j|tg |dd dS )zJust like the above, but for statements.

        We change the start production of the grammar, the compile mode,
        the libCST parse function, and the codegen method.
        singler   N)r   r   r   parse_statementr%   r&   r'   r   r   r   r    )test_parsing_compilable_statement_stringso   s   


z2FuzzTest.test_parsing_compilable_statement_stringsoriginal_coderendered_coder   c              	   C   s<   |dv sJ |  ttj||dttj||d d S )N>   r#   r   r2   r   )r   astdumpparse)r   r5   r6   r   r   r   r    r%      s
   zFuzzTest.verify_identical_astsc                 C   s   |dv sJ t |  z	t| d| W dS  tyF   ttjtjtjd}z|| | }d|}J | tyA   t 	  Y nw J dw )zGInput validation helper shared by modules, statements, and expressions.>   r#   r   r2   z<string>)r#   r   r2   z2libCST parsed a string rejected by compile() into FunreachableN)
r)   notecompile	Exceptiondictr   r$   r   r3   r*   )r   r   parserr   msgr   r   r    r      s&   

zFuzzTest.reject_invalid_codec                 C   s   d| v r
t   dS dS )a$  
        There are a few edge cases in Python that are too obscure and too hard to
        support reasonably. If we encounter code that is generated by Hypothesis
        which contains such features, we should reject it so we don't get failures
        that we aren't going to fix.
        N)r)   r*   r   r   r   r    r      s   z FuzzTest.reject_unsupported_code)__name__
__module____qualname____doc__unittest
skipUnlessboolosenvirongetr)   givenr   strr!   mapstripr+   r4   r%   staticmethodr   r   r   r   r   r    r   /   sD    

	r   __main__)rE   r7   rI   rF   datetimer   r)   hypothesmithr   r   settingsregister_profileHealthChecktoo_slowfilter_too_muchload_profileget_profilePhasegenerateshrinkTestCaser   rB   mainr   r   r   r    <module>   s:   	
 