a
    4ñ»jˆI  ã                   @   sÌ   d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZmZ ddlmZmZmZmZ dd	lmZ G d
d„ de	ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ ZdS )zf
Provides a formatter that provides an overview of available step definitions
(step implementations).
é    )Úabsolute_import)Ú
attrgetterN)Úzip)Ú	Formatter)ÚStepRegistryÚregistry)Úcompute_words_maxsizeÚindentÚmake_indentationÚtext)Úi18nc                       s^   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zddd„Z‡  ZS )ÚAbstractStepsFormatterzÓ
    Provides a formatter base class that provides the common functionality
    for formatter classes that operate on step definitions (implementations).

    .. note::
        Supports behave dry-run mode.
    )ÚgivenÚwhenZthenÚstepc                    s*   t t| ƒ ||¡ d | _d | _|j| _d S ©N)Úsuperr   Ú__init__Ústep_registryÚcurrent_featureZshow_sourceÚshows_location©ÚselfZstream_openerÚconfig©Ú	__class__© ú<E:\soccer\ml_env\Lib\site-packages\behave/formatter/steps.pyr      s    zAbstractStepsFormatter.__init__c                 C   s   d | _ d | _d S r   )r   r   ©r   r   r   r   Úreset%   s    zAbstractStepsFormatter.resetc                 C   sL   | j d u rtƒ | _ tjD ].}ttj| ƒ}|D ]
}||_q.|| j j|< qd S r   )r   r   r   ÚstepsÚtupleÚ	step_type)r   r"   Ústep_definitionsÚstep_definitionr   r   r   Údiscover_step_definitions)   s    

z0AbstractStepsFormatter.discover_step_definitionsc                 C   s   || _ | js|  ¡  d S r   )r   r   r%   ©r   Úfeaturer   r   r   r'   4   s    zAbstractStepsFormatter.featurec                 C   s
   d| _ dS )zCalled at end of a feature.N)r   r   r   r   r   Úeof:   s    zAbstractStepsFormatter.eofc                 C   s2   | j s|  ¡  | j r&|  ¡ | _|  ¡  |  ¡  dS )zCalled at end of test run.N)r   r%   ÚopenÚstreamÚreportZclose_streamr   r   r   r   Úclose>   s    
zAbstractStepsFormatter.closec                 C   s
   t ƒ ‚d S r   )ÚNotImplementedErrorr   r   r   r   r+   L   s    zAbstractStepsFormatter.reportNc                 C   s    |s
|j }|sJ ‚d||jf S )Nz	@%s('%s'))r"   Úpattern)r   r$   r"   r   r   r   Údescribe_step_definitionP   s    z/AbstractStepsFormatter.describe_step_definition)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
step_typesr   r   r%   r'   r(   r,   r+   r/   Ú__classcell__r   r   r   r   r      s   r   c                   @   s0   e Zd ZdZdZdZdZdZdd„ Zdd	„ Z	d
S )ÚStepsFormattera”  
    Provides formatter class that provides an overview
    which step definitions are available.

    EXAMPLE:
        $ behave --dry-run -f steps features/
        GIVEN STEP DEFINITIONS[21]:
          Given a new working directory
          Given I use the current directory as working directory
          Given a file named "{filename}" with
          ...
          Given a step passes
          Given a step fails

        WHEN STEP DEFINITIONS[14]:
          When I run "{command}"
          ...
          When a step passes
          When a step fails

        THEN STEP DEFINITIONS[45]:
          Then the command should fail with returncode="{result:int}"
          Then it should pass with
          Then it should fail with
          Then the command output should contain "{text}"
          ...
          Then a step passes
          Then a step fails

        GENERIC STEP DEFINITIONS[13]:
          * I remove the directory "{directory}"
          * a file named "{filename}" exists
          * a file named "{filename}" does not exist
          ...
          * a step passes
          * a step fails

    .. note::
        Supports behave dry-run mode.
    r    z.Shows step definitions (step implementations).Té(   c                 C   s   |   ¡  d S r   )Úreport_steps_by_typer   r   r   r   r+   ‰   s    zStepsFormatter.reportc                    s|  t | jƒt | jj ¡ ƒksJ ‚| jjp(d}tj| }| jD �]:}t	| jj| ƒ}|dkrj| 
| jjd ¡ |spq:| ¡ }|dkrŠd‰ d}n6|| }|d dkr¸t|ƒdks®J ‚|d ‰ n|d ‰ ‡ fdd„|D ƒ}| j�rt|ƒ}|| jk rò| j}d	t|ƒ d
 }	nd}	d}
| j |
|t|ƒf ¡ t||ƒD ]:\}}| j�rV| j |	||jf ¡ n| j |	| ¡ �q.| j d¡ q:dS )zMShow an overview of the existing step implementations per step type.
        Úenr   Ú*ZGENERICr   é   c                    s   g | ]}d ˆ |j f ‘qS )ú%s %s)r.   ©Ú.0r   ©Zstep_keywordr   r   Ú
<listcomp>©   s   ÿz7StepsFormatter.report_steps_by_type.<locals>.<listcomp>ú  %-ús  # %s
z  %s
z%s STEP DEFINITIONS[%s]:
Ú
N)Úsetr4   r   r    Úkeysr   Úlangr   Ú	languagesÚlistÚextendÚupperÚlenr   r   Úmin_location_columnÚ_textr*   Úwriter   Úlocation)r   ÚlanguageZlanguage_keywordsr"   r    Zstep_type_nameÚkeywordsÚ
steps_textÚmax_sizeÚschemaÚmessager   Ú	step_textr   r?   r   r8   Œ   sD    


ÿ
z#StepsFormatter.report_steps_by_typeN)
r0   r1   r2   r3   ÚnameÚdescriptionr   rL   r+   r8   r   r   r   r   r6   Z   s   (r6   c                   @   sD   e Zd ZdZdZdZdZdZdZe	dƒZ
dd„ Zdd	„ Zd
d„ ZdS )ÚStepsDocFormattera=  
    Provides formatter class that shows the documentation of all registered
    step definitions. The primary purpose is to provide help for a test writer.

    EXAMPLE:
        $ behave --dry-run -f steps.doc features/
        @given('a file named "{filename}" with')
          Function: step_a_file_named_filename_with()
          Location: behave4cmd0/command_steps.py:50
            Creates a textual file with the content provided as docstring.

        @when('I run "{command}"')
          Function: step_i_run_command()
          Location: behave4cmd0/command_steps.py:80
            Run a command as subprocess, collect its output and returncode.

        @step('a file named "{filename}" exists')
          Function: step_file_named_filename_exists()
          Location: behave4cmd0/command_steps.py:305
            Verifies that a file with this filename exists.

            .. code-block:: gherkin

                Given a file named "abc.txt" exists
                 When a file named "abc.txt" exists
        ...

    .. note::
        Supports behave dry-run mode.
    z	steps.docz)Shows documentation for step definitions.Té   c                 C   s   |   ¡  | j d¡ d S ©NrC   )Úreport_step_definition_docsr*   rN   r   r   r   r   r+   è   s    zStepsDocFormatter.reportc                 C   sh   g }| j D ].}| jj| D ]}|jd us,J ‚| |¡ qq
| jrPt|tdƒd�}|D ]}|  |¡ qTd S )NrO   ©Úkey)	r4   r   r    r"   ÚappendÚordered_by_locationÚsortedr   Úwrite_step_definition)r   r#   r"   r$   r   r   r   r\   ì   s    
ÿz-StepsDocFormatter.report_step_definition_docsc                 C   s    |   |¡}| j d| ¡ t |j¡}|jj}| jrL|dvrL| j d| ¡ | jrd| j d|j	 ¡ |r�| 
¡ }| j t|| jƒ¡ | j d¡ | j d¡ d S )Nz%s
)r   Úimplz  Function: %s()
z  Location: %s
rC   )r/   r*   rN   ÚinspectÚgetdocÚfuncr0   Úshows_function_namer   rO   Ústripr	   Ú
doc_prefix)r   r$   Zstep_definition_textÚdocÚ	func_namer   r   r   rb   û   s    
z'StepsDocFormatter.write_step_definitionN)r0   r1   r2   r3   rW   rX   r   rg   r`   r
   ri   r+   r\   rb   r   r   r   r   rY   Á   s   rY   c                   @   s6   e Zd ZdZdZdZdZdZdZe	dƒZ
d	dd„ZdS )
ÚStepsCatalogFormatteraÝ  
    Provides formatter class that shows the documentation of all registered
    step definitions. The primary purpose is to provide help for a test writer.

    In order to ease work for non-programmer testers, the technical details of
    the steps (i.e. function name, source location) are ommited and the
    steps are shown as they would apprear in a feature file (no noisy '@',
    or '(', etc.).

    Also, the output is sorted by step type (Given, When, Then)

    Generic step definitions are listed with all three step types.

    EXAMPLE:
        $ behave --dry-run -f steps.catalog features/
        Given a file named "{filename}" with
            Creates a textual file with the content provided as docstring.

        When I run "{command}"
            Run a command as subprocess, collect its output and returncode.

        Given a file named "{filename}" exists
         When a file named "{filename}" exists
         Then a file named "{filename}" exists
            Verifies that a file with this filename exists.

            .. code-block:: gherkin

                Given a file named "abc.txt" exists
                 When a file named "abc.txt" exists
        ...

    .. note::
        Supports behave dry-run mode.
    zsteps.catalogz7Shows non-technical documentation for step definitions.FrZ   Nc                 C   sr   |s
|j }|sJ ‚g }|dkrP| jd d… D ] }d| ¡ |jf }| |¡ q,n| d| ¡ |jf ¡ d |¡S )Nr   éÿÿÿÿz%5s %sr<   rC   )r"   r4   Útitler.   r_   Újoin)r   r$   r"   ÚdescZ
step_type1r   r   r   r   r/   :  s    z.StepsCatalogFormatter.describe_step_definition)N)r0   r1   r2   r3   rW   rX   r   rg   r`   r
   ri   r/   r   r   r   r   rl     s   #rl   c                       s”   e Zd ZdZdZdZedƒZdZ‡ fdd„Z	‡ fdd	„Z
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚStepsUsageFormatterzØ
    Provides formatter class that shows how step definitions are used by steps.

    EXAMPLE:
        $ behave --dry-run -f steps.usage features/
        ...

    .. note::
        Supports behave dry-run mode.
    zsteps.usagez-Shows how step definitions are used by steps.rZ   r7   c                    s"   t t| ƒ ||¡ i | _g | _d S r   )r   rq   r   Ústep_usage_databaseÚundefined_stepsr   r   r   r   r   \  s    zStepsUsageFormatter.__init__c                    s   t t| ƒ ¡  i | _g | _d S r   )r   rq   r   rr   rs   r   r   r   r   r   a  s    zStepsUsageFormatter.resetc                 C   sB   |j }|s>| jsJ ‚| jj ¡ D ]\}}||v r |  S q d}|S )Nr   )r"   r   r    Úitems)r   r$   r"   Úvaluesr   r   r   Ú!get_step_type_for_step_definitiong  s    

z5StepsUsageFormatter.get_step_type_for_step_definitionc                 C   s@   t ƒ }| jj ¡ D ]\}}| |¡ qt | j ¡ ƒ}|| }|S r   )rD   r   r    rt   Úupdaterr   rE   )r   r#   r"   ru   Zused_step_definitionsÚunused_step_definitionsr   r   r   Úselect_unused_step_definitionst  s    z2StepsUsageFormatter.select_unused_step_definitionsc                 C   sJ   | j  |d ¡}|d u r2|jd us$J ‚g  }| j |< t||ƒsF| |¡ d S r   )rr   Úgetr"   Ústeps_containr_   )r   r$   r   Zmatching_stepsr   r   r   Úupdate_usage_database|  s    
z)StepsUsageFormatter.update_usage_databasec                 C   s:   | j  |¡}|r|  ||¡ nt| j|ƒs6| j |¡ d S r   )r   Zfind_step_definitionr|   r{   rs   r_   )r   r   r$   r   r   r   Úupdate_usage_database_for_step…  s
    z2StepsUsageFormatter.update_usage_database_for_stepc                 C   sF   |j r|j jD ]}|  |¡ q| ¡ D ]}|jD ]}|  |¡ q0q&d S r   )Ú
backgroundr    r}   Zwalk_scenarios)r   r'   r   Zscenarior   r   r   Ú!update_usage_database_for_feature�  s    
z5StepsUsageFormatter.update_usage_database_for_featurec                    s   t t| ƒ |¡ |  |¡ d S r   )r   rq   r'   r   r&   r   r   r   r'   œ  s    zStepsUsageFormatter.featurec                 C   s(   |   ¡  |  ¡  |  ¡  | j d¡ d S r[   )Úreport_used_step_definitionsÚreport_unused_step_definitionsÚreport_undefined_stepsr*   rN   r   r   r   r   r+   ¡  s    zStepsUsageFormatter.reportc                 C   sÖ   dd„ }| j  ¡ }t||d�}|D ]®\}}|  |¡}dd„ |D ƒ}| |¡ t|ƒ}|| jk rd| j}dt|ƒ d }| j 	|||j
f ¡ dt|ƒ d }t||ƒD ]\}	}
| j 	||
|	j
f ¡ q¤| j 	d¡ q"d S )	Nc                 S   s
   | d j S )Nr   ©rO   ©Úxr   r   r   Ú<lambda>«  ó    zBStepsUsageFormatter.report_used_step_definitions.<locals>.<lambda>r]   c                 S   s   g | ]}d |j |jf ‘qS ©z  %s %s©ÚkeywordrW   r=   r   r   r   r@   ±  s   ÿzDStepsUsageFormatter.report_used_step_definitions.<locals>.<listcomp>ú%-rB   rC   )rr   rt   ra   r/   r_   r   rL   rM   r*   rN   rO   r   )r   Úget_locationZstep_definition_itemsr$   r    Zstepdef_textrR   rS   rT   r   rV   r   r   r   r€   ¨  s$    

ÿ

z0StepsUsageFormatter.report_used_step_definitionsc           	         s¨   ˆ   ¡ }|sd S dd„ }t||d�}‡ fdd„|D ƒ}t|ƒ}|ˆ jd k rVˆ jd }dt|ƒ d }ˆ j d	t|ƒ ¡ t||ƒD ]\}}ˆ j |||j	f ¡ q„d S )
Nc                 S   s   | j S r   rƒ   r„   r   r   r   r†   Æ  r‡   zDStepsUsageFormatter.report_unused_step_definitions.<locals>.<lambda>r]   c                    s   g | ]}ˆ   |¡‘qS r   )r/   )r>   r$   r   r   r   r@   È  s   ÿzFStepsUsageFormatter.report_unused_step_definitions.<locals>.<listcomp>é   rA   rB   zUNUSED STEP DEFINITIONS[%d]:
)
ry   ra   r   rL   rM   r*   rN   rK   r   rO   )	r   rx   rŒ   r#   Z
step_textsrS   rT   r$   rV   r   r   r   r�   ¿  s    
ÿ
z2StepsUsageFormatter.report_unused_step_definitionsc                 C   s”   | j s
d S t| j tdƒd�}dd„ |D ƒ}t|ƒ}|| jk rB| j}| j dt|ƒ ¡ dt|ƒ d }t	||ƒD ]\}}| j |||j
f ¡ qpd S )NrO   r]   c                 S   s   g | ]}d |j |jf ‘qS rˆ   r‰   r=   r   r   r   r@   Ý  s   ÿz>StepsUsageFormatter.report_undefined_steps.<locals>.<listcomp>z
UNDEFINED STEPS[%d]:
r‹   rB   )rs   ra   r   r   rL   r*   rN   rK   rM   r   rO   )r   rs   rR   rS   rT   r   rV   r   r   r   r‚   Õ  s    ÿÿ
z*StepsUsageFormatter.report_undefined_steps)r0   r1   r2   r3   rW   rX   r
   ri   rL   r   r   rv   ry   r|   r}   r   r'   r+   r€   r�   r‚   r5   r   r   r   r   rq   L  s"   
	
rq   c                 C   s(   | D ]}||kr|j |j kr dS qdS )NTFrƒ   )r    r   Z
other_stepr   r   r   r{   ë  s    r{   )r3   Ú
__future__r   Úoperatorr   rd   Z	six.movesr   Zbehave.formatter.baser   Zbehave.step_registryr   r   Zbehave.textutilr   r	   r
   r   rM   Zbehaver   r   r6   rY   rl   rq   r{   r   r   r   r   Ú<module>   s   EgM>  