o
    DDi]                     @   s~   d dl Z d dlZd dlZd dlm  mZ d dlm  mZ	 d dl
m  mZ d dlmZmZ d dlmZ G dd deZdS )    N)clean_generated_codeformat_file)UnitTestc                   @   sN   e Zd ZdedededdfddZddd	Zdd
dZdddZdddZdS )TestCodegenCleanold_codenew_codemodule_namereturnNc                 C   s^   ||kr-t j|jdd|jddddd}d|}| | dd d	 d
|   d S d S )NT)keependsr   r   )fromfiletofile z needs new codegen, see z+`python -m libcst.codegen.generate --help` zBfor instructions, or run `python -m libcst.codegen.generate all`. zDiff:
)difflibunified_diff
splitlinesjoinfail)selfr   r   r   diffdiff_str r   `/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/codegen/tests/test_codegen_clean.pyassert_code_matches   s$   


z$TestCodegenClean.assert_code_matchesc                 C     t dtj}tjtjtjtd}t	|d}|
| W d   n1 s,w   Y  zt| W n	 ty@   Y nw t	|d}| }W d   n1 sUw   Y  t| t	tjtjtjtdd}| }W d   n1 sw   Y  | ||d dS )z
        Verifies that codegen of visitor functions would not result in a
        changed file. If this test fails, please run 'python -m libcst.codegen.generate all'
        to generate new files.
        
zvisitor_codegen.deleteme.pywNrz../../_typed_visitor.pyzlibcst._typed_visitor)r   r   visitor_codegengenerated_codeospathdirnameabspath__file__openwriter   	Exceptionreadremover   r   r   new_filefpr   r   r   r   $test_codegen_clean_visitor_functions'   4   


	z5TestCodegenClean.test_codegen_clean_visitor_functionsc                 C   r   )z
        Verifies that codegen of matcher classes would not result in a
        changed file. If this test fails, please run 'python -m libcst.codegen.generate all'
        to generate new files.
        r   zmatcher_codegen.deleteme.pyr   Nr   z../../matchers/__init__.pyzlibcst.matchers.__init__)r   r   matcher_codegenr   r   r    r!   r"   r#   r$   r%   r   r&   r'   r(   r   r)   r   r   r   "test_codegen_clean_matcher_classesG   r-   z3TestCodegenClean.test_codegen_clean_matcher_classesc                 C   r   )z
        Verifies that codegen of return types would not result in a
        changed file. If this test fails, please run 'python -m libcst.codegen.generate all'
        to generate new files.
        r   ztype_codegen.deleteme.pyr   Nr   z../../matchers/_return_types.pyzlibcst.matchers._return_types)r   r   type_codegenr   r   r    r!   r"   r#   r$   r%   r   r&   r'   r(   r   r)   r   r   r   test_codegen_clean_return_typesg   s6   



z0TestCodegenClean.test_codegen_clean_return_typesc                    sr   ddl ddlm  dtdtddf fdd}|d	d
 |dd |dd |dd |dd
 |dd
 dS )z
        Verifies that NormalizeUnions correctly converts binary operations with |
        into Union types, with special handling for Optional cases.
        r   N)NormalizeUnions
input_codeexpected_coder	   c                    s    | } |}|  }t|jsJ dt| jdd|}jdd|}||d| d|  d S )NzExpected BaseExpression, got r   )bodyz	Expected z, got )parse_expressionvisit
isinstanceBaseExpressiontypeModulecode_for_nodeassertEqual)r3   r4   	input_cstexpected_cstresultresult_codeexpected_code_strr2   cstr   r   r   assert_transforms_to   s   

zDTestCodegenClean.test_normalize_unions.<locals>.assert_transforms_tozfoo | bar | bazztyping.Union[foo, bar, baz]z
foo | Noneztyping.Optional[foo]z
None | foozfoo | bar | Noneztyping.Union[foo, bar, None]z)typing.Union[foo, typing.Union[bar, baz]]zfoo | typing.Union[bar, baz])libcst"libcst.codegen.gen_matcher_classesr2   str)r   rE   r   rC   r   test_normalize_unions   s   



z&TestCodegenClean.test_normalize_unions)r	   N)	__name__
__module____qualname__rH   r   r,   r/   r1   rI   r   r   r   r   r      s    


 
 !r   )r   r   os.pathrG   codegengen_matcher_classesr.   libcst.codegen.gen_type_mappinggen_type_mappingr0   $libcst.codegen.gen_visitor_functionsgen_visitor_functionsr   libcst.codegen.generater   r   libcst.testing.utilsr   r   r   r   r   r   <module>   s   