U
    ¸~Hj­R  ã                   @   sN  d Z ddlZddlmZ ddlmZmZ ddlmZ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d�Zdd„ ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ de
jƒZG dd„ de
jƒZG dd„ deƒZdd„ ZG dd„ de
jƒZG dd „ d e
jƒZ G d!d"„ d"e
jƒZ!G d#d$„ d$eƒZ"d%Z#d&Z$dS )'z*
Test cases for twisted.python._shellcomp
é    N)ÚBytesIO)ÚListÚOptional)Ú
_shellcompÚreflectÚusage)ÚCompleteFilesÚCompleteListÚ	CompleterÚCompletions)Úunittestc                   @   s   e Zd ZdZdd„ ZdS )ÚZshScriptTestMetaz*
    Metaclass of ZshScriptTestMixin.
    c                 C   sH   dd„ }d|kr8|d D ]\}}|||ƒ}||d| < qt  | |||¡S )Nc                    s   ‡ ‡fdd„}|S )Nc                    s   t | ˆ ˆƒS )N)Útest_genZshFunction©Úself©ÚcmdNameÚoptionsFQPN© úF/tmp/pip-unpacked-wheel-ewod1tgb/twisted/python/test/test_shellcomp.pyÚrunTest   s    z<ZshScriptTestMeta.__new__.<locals>.makeTest.<locals>.runTestr   )r   r   r   r   r   r   ÚmakeTest   s    z+ZshScriptTestMeta.__new__.<locals>.makeTestZgenerateForZtest_genZshFunction_)ÚtypeÚ__new__)ÚclsÚnameÚbasesÚattrsr   r   r   Útestr   r   r   r      s    
zZshScriptTestMeta.__new__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )ÚZshScriptTestMixina§  
    Integration test helper to show that C{usage.Options} classes can have zsh
    completion functions generated for them without raising errors.

    In your subclasses set a class variable like so::

      #            | cmd name | Fully Qualified Python Name of Options class |
      #
      generateFor = [('conch',  'twisted.conch.scripts.conch.ClientOptions'),
                     ('twistd', 'twisted.scripts.twistd.ServerOptions'),
                     ]

    Each package that contains Twisted scripts should contain one TestCase
    subclass which also inherits from this mixin, and contains a C{generateFor}
    list appropriate for the scripts in that package.
    N)r   r    r!   r"   r   r   r   r   r#   (   s   r#   )Ú	metaclassc           
      C   s¶  t ƒ }|  tjd|¡ zt |¡ƒ }W n2 tk
rX } zt d|f ¡‚W 5 d}~X Y nX z| 	dddg¡ W nB t
k
rž } zt d|f¡‚W 5 d}~X Y n tk
r°   Y nX |  d¡ | d	¡ |  d
t| d
¡ƒ¡ | d	¡ | ¡  t|dƒ�rª|jD ]¨\}}}}	z| 	|dddg¡ W nF t
k
�rT } zt d|f¡‚W 5 d}~X Y n  tk
�rh   Y nX |  d¡ | d	¡ |  d
t| d
¡ƒ¡ | d	¡ | ¡  �q |  ¡  dS )aI  
    Generate completion functions for given twisted command - no errors
    should be raised

    @type cmdName: C{str}
    @param cmdName: The name of the command-line utility e.g. 'twistd'

    @type optionsFQPN: C{str}
    @param optionsFQPN: The Fully Qualified Python Name of the C{Options}
        class to be tested.
    Ú_shellCompFilez0Couldn't import or instantiate Options class: %sNÚ ú--_shell-completionúzsh:2z&ImportError calling parseOptions(): %szSystemExit not raisedr   é   ÚsubCommandszzsh:3z4ImportError calling parseOptions() on subcommand: %s)r   Úpatchr   ÚOptionsr   ZnamedAnyÚ	Exceptionr   ZSkipTestÚparseOptionsÚImportErrorÚ
SystemExitZfailÚseekÚassertEqualÚlenÚreadÚtruncateÚhasattrr*   ZflushWarnings)
r   r   r   Ú
outputFileÚoÚeÚcmdÚshortÚparserÚdocr   r   r   r   ;   sH    ÿ


 ÿ


r   c                   @   sx   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d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚZshTestsz'
    Tests for zsh completion code
    c                 C   s¤   t ƒ }t |dtƒ ¡}tjj ¡ }| t jj¡ |  	|j|¡ |  	|j
ttjj
ƒ¡ |  	|jtjj¡ tjj ¡ }| t jj¡ |  	|j|¡ |  	|jtjj¡ dS )zi
        Are `compData' attributes you can place on Options classes
        picked up correctly?
        ÚaceN)ÚFighterAceExtendedOptionsr   ÚZshArgumentsGeneratorr   ÚFighterAceOptionsÚcompDataÚdescriptionsÚcopyÚupdater2   ÚmultiUseÚsetÚmutuallyExclusiveÚ
optActionsÚextraActions)r   ÚoptsÚagrD   rJ   r   r   r   Útest_accumulateMetadata„   s     ÿz ZshTests.test_accumulateMetadatac                 C   s�   G dd„ dt ƒ}|ƒ }t |dtƒ ¡}dddddd	d
hddddddd	d
hdddddd	d
hdddddd	d
hdddddddhdœ}|  |j|¡ dS )z²
        Exercise a corner-case of ZshArgumentsGenerator.makeExcludesDict()
        where the long option name already exists in the `excludes` dict being
        built.
        c                   @   s.   e Zd ZdddggZedddddggd�ZdS )	zGZshTests.test_mutuallyExclusiveCornerCase.<locals>.OddFighterAceOptionsÚanatraNz0Select the Anatra DS as your dogfighter aircraftÚfokkerÚalbatrosÚspadÚbristol©rI   )r   r    r!   ÚoptFlagsr   rC   r   r   r   r   ÚOddFighterAceOptions¢   s
   ÿÿrV   r?   rO   ÚbrS   ÚfrP   ÚsrR   ÚarQ   )rQ   rO   rS   rP   rR   N)r@   r   rA   r   r2   Zexcludes)r   rV   rL   rM   Úexpectedr   r   r   Ú test_mutuallyExclusiveCornerCase›   s    ûz)ZshTests.test_mutuallyExclusiveCornerCasec                 C   sR   t ƒ }t |dtƒ ¡}|  d|j¡ |  d|j¡ |  d|j¡ |  d|j¡ dS )z¥
        We pick up options that are only defined by having an
        appropriately named method on your Options class,
        e.g. def opt_foo(self, foo)
        r?   ZnocrashÚ
difficultyN)r@   r   rA   r   ZassertInZflagNameToDefinitionZallOptionsNameToDefinitionZparamNameToDefinition)r   rL   rM   r   r   r   Ú test_accumulateAdditionalOptionsº   s    z)ZshTests.test_accumulateAdditionalOptionsc                 C   sT   G dd„ dt ƒ}|  ttj|ƒ dtƒ ¡ G dd„ dt ƒ}|  ttj|ƒ dtƒ ¡ dS )z\
        Using a parameter/flag name that doesn't exist
        will raise an error
        c                   @   s   e Zd Zeddid�ZdS )z0ZshTests.test_verifyZshNames.<locals>.TmpOptionsZdetaillN)rJ   ©r   r    r!   r   rC   r   r   r   r   Ú
TmpOptionsÏ   s   r`   r?   c                   @   s   e Zd Zedgd�ZdS )z1ZshTests.test_verifyZshNames.<locals>.TmpOptions2)ZfooÚbarrT   Nr_   r   r   r   r   ÚTmpOptions2×   s   rb   N)r@   ÚassertRaisesÚ
ValueErrorr   rA   r   )r   r`   rb   r   r   r   Útest_verifyZshNamesÉ   s         ÿûzZshTests.test_verifyZshNamesc                 C   sT   t ƒ }|  tjd|¡ |  tdddddg¡ tƒ }|  t|j¡ |  	t
| ¡ ¡ dS )zt
        Generate a completion function, and test the textual output
        against a known correct output
        r%   ÚargvÚsillyr&   r'   r(   N)r   r+   r   r,   ÚsysÚSimpleProgOptionsrc   r0   r.   r2   ÚtestOutput1Úgetvalue©r   r7   rL   r   r   r   Útest_zshCodeä   s    zZshTests.test_zshCodec                 C   sT   t ƒ }|  tjd|¡ |  tdddddg¡ tƒ }|  t|j¡ |  	t
| ¡ ¡ dS )z…
        Generate a completion function with subcommands,
        and test the textual output against a known correct output
        r%   rf   Zsilly2r&   r'   r(   N)r   r+   r   r,   rh   ÚSimpleProgWithSubcommandsrc   r0   r.   r2   ÚtestOutput2rk   rl   r   r   r   Útest_zshCodeWithSubsð   s    zZshTests.test_zshCodeWithSubsc              
   C   s\   t ƒ }|  tjd|¡ tƒ }|  t|jddddddg¡ | d¡ |  	d	t
| d	¡ƒ¡ d
S )zx
        Completion still happens even if a command-line is given
        that would normally throw UsageError.
        r%   ú--fokkerÚserverú--unknown-optionz--unknown-option2r'   úzsh:5r   r)   N©r   r+   r   r,   rB   rc   r0   r.   r1   r2   r3   r4   rl   r   r   r   Útest_incompleteCommandLineü   s     úý
z#ZshTests.test_incompleteCommandLinec              
   C   sn   t ƒ }|  tjd|¡ tƒ }|  t|jddddddg¡ | d¡ |  	d	t
| d	¡ƒ¡ | d¡ | ¡  d
S )zý
        Completion still happens even if a command-line is given
        that would normally throw UsageError.

        The existence of --unknown-option prior to the subcommand
        will break subcommand detection... but we complete anyway
        r%   rq   rs   rr   ú--list-serverr'   rt   r   r)   N)r   r+   r   r,   rB   rc   r0   r.   r1   r2   r3   r4   r5   rl   r   r   r   Ú test_incompleteCommandLine_case2  s$    úý

z)ZshTests.test_incompleteCommandLine_case2c              	   C   sZ   t ƒ }|  tjd|¡ tƒ }|  t|jdddddg¡ | d¡ |  	dt
| d¡ƒ¡ d	S )
zà
        Completion still happens even if a command-line is given
        that would normally throw UsageError.

        Break subcommand detection in a different way by providing
        an invalid subcommand name.
        r%   rq   zunknown-subcommandrw   r'   zzsh:4r   r)   Nru   rl   r   r   r   Ú test_incompleteCommandLine_case34  s    ûý
z)ZshTests.test_incompleteCommandLine_case3c                 C   sV   t ƒ }|  tjd|¡ tƒ }|  t|jdddg¡ | d¡ |  	dt
| d¡ƒ¡ dS )z�
        Ensure the optimization which skips building the subcommand list
        under certain conditions isn't broken.
        r%   z--albar'   r(   r   r)   Nru   rl   r   r   r   Útest_skipSubcommandListO  s      ÿ
z ZshTests.test_skipSubcommandListc                 C   s.   t ƒ }t |dd¡}| d¡}|  |d¡ dS )zY
        Test corner case fetching an option description from a method docstring
        r?   Nrg   )rB   r   rA   ÚgetDescriptionr2   )r   rL   ÚargGenÚdescrr   r   r   Útest_poorlyDescribedOptMethod_  s    
z&ZshTests.test_poorlyDescribedOptMethodc                 C   sF   G dd„ dt jƒ}tƒ }|ƒ }|  |d|¡ |  t|jdddg¡ dS )zu
        A C{Completer} with repeat=True may only be used as the
        last item in the extraActions list.
        c                   @   s(   e Zd Zejejdd�e ¡ gd�ZdS )z2ZshTests.test_brokenActions.<locals>.BrokenActionsT)Úrepeat)rK   N)r   r    r!   r   r   r
   rC   r   r   r   r   ÚBrokenActionsr  s   ÿr€   r%   r&   r'   r(   N)r   r,   r   r+   rc   rd   r.   )r   r€   r7   rL   r   r   r   Útest_brokenActionsl  s      ÿzZshTests.test_brokenActionsc                 C   sN   G dd„ dt jƒ}|ƒ }t |dd¡}|  | d¡d¡ |  | d¡d¡ dS )	z~
        opt_* methods on Options classes should not override the
        data provided in optFlags or optParameters.
        c                   @   s6   e Zd ZdddggZddddggZdd	„ Zd
d„ ZdS )z5ZshTests.test_optMethodsDontOverride.<locals>.OptionsÚflagrX   úA flagÚparamÚpNúA paramc                 S   s   dS ©zjunk descriptionNr   r   r   r   r   Úopt_flagˆ  s    z>ZshTests.test_optMethodsDontOverride.<locals>.Options.opt_flagc                 S   s   dS r‡   r   )r   r„   r   r   r   Ú	opt_param‹  s    z?ZshTests.test_optMethodsDontOverride.<locals>.Options.opt_param)r   r    r!   rU   ÚoptParametersrˆ   r‰   r   r   r   r   r,   „  s   r,   r?   Nr‚   rƒ   r„   r†   )r   r,   r   rA   r2   r{   )r   r,   rL   r|   r   r   r   Útest_optMethodsDontOverride~  s
    
z$ZshTests.test_optMethodsDontOverrideN)r   r    r!   r"   rN   r\   r^   re   rm   rp   rv   rx   ry   rz   r~   r�   r‹   r   r   r   r   r>      s   r>   c                   @   s   e Zd Zdd„ ZdS )ÚEscapeTestsc                 C   s2   t j}d}|  ||ƒd¡ d}|  ||ƒd¡ dS )z5
        Verify _shellcomp.escape() function
        ú$z'$'zA--'$"\`--Bz"A--'\$\"\\\`--B"N)r   Úescaper2   )r   Úescr   r   r   r   Útest_escape–  s
    zEscapeTests.test_escapeN)r   r    r!   r�   r   r   r   r   rŒ   •  s   rŒ   c                   @   s   e Zd ZdZdd„ ZdS )ÚCompleterNotImplementedTestszª
    Test that using an unknown shell constant with SubcommandAction
    raises NotImplementedError

    The other Completer() subclasses are tested in test_usage.py
    c                 C   s   t  ¡ }|  t|jdd¡ dS )zM
        Using an unknown shellType should raise NotImplementedError
        NZbad_shell_type)r   ZSubcommandActionrc   ÚNotImplementedErrorZ
_shellCode)r   Úactionr   r   r   Útest_unknownShell«  s       ÿz.CompleterNotImplementedTests.test_unknownShellN)r   r    r!   r"   r”   r   r   r   r   r‘   £  s   r‘   c                   @   s*   e Zd ZdZdddggZddddggZdS )ÚFighterAceServerOptionsz4
    Options for FighterAce 'server' subcommand
    zlist-serverNz3List this server with the online FighterAce networkzpackets-per-secondz+Number of update packets to send per secondZ20)r   r    r!   r"   rU   rŠ   r   r   r   r   r•   ¶  s   ÿüÿr•   c                	   @   sÚ   e Zd ZU dZdddgdddgdd	d
gdddgdddgdddgdddggZeeee   ed< ddddgddddggZ	eeee   ed < d!de
d"ggZed#d$d%œdgddddggded&gƒied'd(�gd)�Zd*d+„ ZdS ),rB   zB
    Command-line options for an imaginary `Fighter Ace` game
    rP   rX   z2Select the Fokker Dr.I as your dogfighter aircraftrQ   rZ   z5Select the Albatros D-III as your dogfighter aircraftrR   rY   z1Select the SPAD S.VII as your dogfighter aircraftrS   rW   z4Select the Bristol Scout as your dogfighter aircraftÚphysicsr…   z$Enable secret Twisted physics engineZjamÚjz6Enable a small chance that your machine guns will jam!ÚverboseÚvú1Verbose logging (may be specified more than once)rU   z
pilot-nameNzWhat's your name, Ace?zManfred von RichthofenÚdetailÚdz*Select the level of rendering detail (1-5)Ú3rŠ   rr   zStart FighterAce game-server.zTwisted-PhysicszRendering detail level)r–   r›   Z12345zsaved game file to load©r}   ©rD   rG   rI   rJ   rK   c                 C   s   dS )ú Nr   r   r   r   r   Ú	opt_sillyè  s    zFighterAceOptions.opt_silly)r   r    r!   r"   rU   r   r   ÚstrÚ__annotations__rŠ   r•   r*   r   r	   r   rC   r¡   r   r   r   r   rB   È  s,   
ù

þ
ÿ
ûrB   c                   @   sT   e Zd ZdZdddggZddddggZeddided	d
�id�Zdd„ Z	dd„ Z
dS )r@   zÃ
    Extend the options and zsh metadata provided by FighterAceOptions.
    _shellcomp must accumulate options and metadata from all classes in the
    hiearchy so this is important to test.
    z	no-stallsNz+Turn off the ability to stall your aircraftzreality-levelz)Select the level of physics reality (1-5)Ú5zCan't stall your planezPhysics reality levelrž   )rD   rJ   c                 C   s   dS )z8
        Select that you can't crash your plane
        Nr   r   r   r   r   Úopt_nocrashþ  s    z%FighterAceExtendedOptions.opt_nocrashc                 C   s   dS )z+
        How tough are you? (1-10)
        Nr   )r   r]   r   r   r   Úopt_difficulty  s    z(FighterAceExtendedOptions.opt_difficulty)r   r    r!   r"   rU   rŠ   r   r
   rC   r¥   r¦   r   r   r   r   r@   í  s   
ÿþr@   c                   C   s   t dddgdd�S )NÚ1Ú2r�   zAccuracy'`?rž   )r	   r   r   r   r   Ú_accuracyAction	  s    r©   c                
   @   sŒ   e Zd ZdZdddgdddgdd	d
ggZddddgddddggZedddœdgddggedddddgdd�edœe	dd�gd�Z
dd„ ZdS ) ri   z>
    Command-line options for a `Silly` imaginary program
    ÚcolorÚczTurn on color outputZgrayÚgzTurn on gray-scale outputr˜   r™   rš   ÚoptimizationNr¤   z&Select the level of optimization (1-5)ÚaccuracyrZ   r�   z"Select the level of accuracy (1-3)zColor onzOptimization level)rª   r­   r§   r¨   Ú4zOptimization?rž   )r­   r®   zoutput filerŸ   c                 C   s   dS )zM
        usage.Options does not recognize single-letter opt_ methods
        Nr   r   r   r   r   Úopt_X+  s    zSimpleProgOptions.opt_X)r   r    r!   r"   rU   rŠ   r   r	   r©   r   rC   r°   r   r   r   r   ri     s*   ý

þ ÿü
öri   c                   @   s   e Zd ZdddggZdS )ÚSimpleProgSub1úsub-optrY   zSub Opt OneN©r   r    r!   rU   r   r   r   r   r±   1  s   r±   c                   @   s   e Zd ZdddggZdS )ÚSimpleProgSub2r²   rY   zSub Opt TwoNr³   r   r   r   r   r´   5  s   r´   c                   @   sH   e Zd ZdgddggZdgddgddd	ggZd
dedgddedggZdS )rn   zsome-optionzother-optionr8   z
some-paramzother-paramr…   zanother-paramÚPzYet Another ParamZsub1NzSub Command 1Zsub2zSub Command 2)r   r    r!   rU   rŠ   r±   r´   r*   r   r   r   r   rn   9  s   ý

þrn   sº  #compdef silly

_arguments -s -A "-*" \
':output file (*):_files -g "*"' \
"(--accuracy)-a[Select the level of accuracy (1-3)]:Accuracy'\`?:(1 2 3)" \
"(-a)--accuracy=[Select the level of accuracy (1-3)]:Accuracy'\`?:(1 2 3)" \
'(--color --gray -g)-c[Color on]' \
'(--gray -c -g)--color[Color on]' \
'(--color --gray -c)-g[Turn on gray-scale output]' \
'(--color -c -g)--gray[Turn on gray-scale output]' \
'--help[Display this help and exit.]' \
'--optimization=[Optimization level]:Optimization?:(1 2 3 4 5)' \
'*-v[Verbose logging (may be specified more than once)]' \
'*--verbose[Verbose logging (may be specified more than once)]' \
'--version[Display Twisted version and exit.]' \
&& return 0
s×  #compdef silly2

_arguments -s -A "-*" \
'*::subcmd:->subcmd' \
':output file (*):_files -g "*"' \
"(--accuracy)-a[Select the level of accuracy (1-3)]:Accuracy'\`?:(1 2 3)" \
"(-a)--accuracy=[Select the level of accuracy (1-3)]:Accuracy'\`?:(1 2 3)" \
'(--another-param)-P[another-param]:another-param:_files' \
'(-P)--another-param=[another-param]:another-param:_files' \
'(--color --gray -g)-c[Color on]' \
'(--gray -c -g)--color[Color on]' \
'(--color --gray -c)-g[Turn on gray-scale output]' \
'(--color -c -g)--gray[Turn on gray-scale output]' \
'--help[Display this help and exit.]' \
'--optimization=[Optimization level]:Optimization?:(1 2 3 4 5)' \
'(--other-option)-o[other-option]' \
'(-o)--other-option[other-option]' \
'(--other-param)-p[other-param]:other-param:_files' \
'(-p)--other-param=[other-param]:other-param:_files' \
'--some-option[some-option]' \
'--some-param=[some-param]:some-param:_files' \
'*-v[Verbose logging (may be specified more than once)]' \
'*--verbose[Verbose logging (may be specified more than once)]' \
'--version[Display Twisted version and exit.]' \
&& return 0
local _zsh_subcmds_array
_zsh_subcmds_array=(
"sub1:Sub Command 1"
"sub2:Sub Command 2"
)

_describe "sub-command" _zsh_subcmds_array
)%r"   rh   Úior   Útypingr   r   Ztwisted.pythonr   r   r   Ztwisted.python.usager   r	   r
   r   Ztwisted.trialr   r   r   r#   r   ZTestCaser>   rŒ   r‘   r,   r•   rB   r@   r©   ri   r±   r´   rn   rj   ro   r   r   r   r   Ú<module>   s0   D  %#