o
    b[                     @   sd  d Z ddlmZ ddlm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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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jZG d$d% d%ejZG d&d' d'ejZd(S ))zK
Tests for L{twisted.python.usage}, a command line option parsing library.
    )usage)unittestc                   @   sH   e Zd Zg dg dg dg dgZg dddggZdd	 Zd
d ZdS )WellBehaved)longwdefaultzand a docstring)anothernzno docstring)longonlyNnoshort)	shortlessNexceptzthis one got docstring)aflagfzO

                 flagallicious docstringness for this here

                 floutoc                 C   s   d| j d< d S )NPONY!myflagoptsself r   9/usr/lib/python3/dist-packages/twisted/test/test_usage.py
opt_myflag!   s   zWellBehaved.opt_myflagc                 C   s   | d| j d< d S )Nz WITH A PONY!myparamr   r   valuer   r   r   opt_myparam$   s   zWellBehaved.opt_myparamN)__name__
__module____qualname__optParametersoptFlagsr   r   r   r   r   r   r      s    	r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ParseCorrectnessTestszZ
    Test L{usage.Options.parseOptions} for correct values under
    good conditions.
    c                 C   s$   d  | _t | _| j| j dS )zL
        Instantiate and parseOptions a well-behaved Options class.
        A--long Alpha -n Beta --shortless Gamma -f --myflag --myparam TofuN)splitniceArgVr   niceparseOptionsr   r   r   r   setUp.   s
   zParseCorrectnessTests.setUpc                 C   sT   |  | jjd d |  | jjd d |  | jjd d |  | jjd d d	S )
z1
        Parameters have correct values.
        r   Alphar   Betar
   r   r   GammaNassertEqualr(   r   r   r   r   r   test_checkParameters;   s   z*ParseCorrectnessTests.test_checkParametersc                 C   ,   |  | jjd d |  | jjd d dS )z,
        Flags have correct values.
        r      r   r   Nr.   r   r   r   r   test_checkFlagsD      z%ParseCorrectnessTests.test_checkFlagsc                 C   r1   )zB
        Custom flags and parameters have correct values.
        r   r   r   zTofu WITH A PONY!Nr.   r   r   r   r   test_checkCustomsK   r4   z'ParseCorrectnessTests.test_checkCustomsN)r   r    r!   __doc__r*   r0   r3   r5   r   r   r   r   r$   (   s    	r$   c                   @   sL   e Zd Zddddegddddegdddd	egd
dddeggZdd ZeZdS )TypedOptionsfoointN  zFoo intfoofloatQ@z	Foo floateggintzEgg int without defaulteggfloatzEgg float without defaultc                 C   s
   || _ dS )zg
        This option has an underscore in its name to exercise the _ to -
        translation.
        N)underscoreValuer   r   r   r   opt_under_score[   s   
zTypedOptions.opt_under_score)r   r    r!   intfloatr"   r?   opt_ur   r   r   r   r7   S   s    r7   c                   @   s@   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S )
TypedTestszK
    Test L{usage.Options.parseOptions} for options with forced types.
    c                 C   s   t  | _d S N)r7   r   r   r   r   r   r*   j   s   zTypedTests.setUpc                 C   s   g }| j | | | j jd d | | j jd t | | j jd d | | j jd t | | j jd  | | j jd  dS )z,
        Default values are parsed.
        r8   r9   r:   r;   r<   r=   N)r   r)   r/   r   assertIsInstancer@   rA   assertIsNoner   argVr   r   r   test_defaultValuesm   s   zTypedTests.test_defaultValuesc                 C   s   d  }| j| | | jjd d | | jjd t | | jjd d | | jjd t | | jjd d | | jjd t | | jjd d	 | | jjd t d
S )z2
        int and float values are parsed.
        z8--fooint 912 --foofloat -823.1 --eggint 32 --eggfloat 21r8   i  r:   g̸r<       r=   g      5@N)r&   r   r)   r/   r   rE   r@   rA   rG   r   r   r   test_parsingValuesz   s   zTypedTests.test_parsingValuesc                 C   $   | j ddg | | j jd dS )zw
        A dash in an option name is translated to an underscore before being
        dispatched to a handler.
        z--under-scorefooNr   r)   r/   r>   r   r   r   r   test_underscoreOption   s   z TypedTests.test_underscoreOptionc                 C   rL   )zE
        An option name with a dash in it can have an alias.
        z-ubarNrN   r   r   r   r   test_underscoreOptionAlias   s   z%TypedTests.test_underscoreOptionAliasc                 C   s    d  }| tj| jj| dS )z7
        Passing wrong values raises an error.
        z--fooint eggN)r&   assertRaisesr   
UsageErrorr)   rG   r   r   r   test_invalidValues   s   zTypedTests.test_invalidValuesN)
r   r    r!   r6   r*   rI   rK   rO   rQ   rT   r   r   r   r   rC   e   s    rC   c                   @   s   e Zd Zg dgZdS )WrongTypedOptions)barwrongNNzBar with wrong coerceheN)r   r    r!   r"   r   r   r   r   rU      s    rU   c                   @   s8   e Zd Zdd Zdd Zddddegdddd	eggZdS )
WeirdCallableOptionsc                 C      t d)NOuch)RuntimeErrorr   r   r   r   _bar      zWeirdCallableOptions._barc                 C   rY   )NYay)
ValueErrorr\   r   r   r   _foo   r^   zWeirdCallableOptions._foorV   NzBar with strange callablefoowrongzFoo with strange callable)r   r    r!   r]   ra   r"   r   r   r   r   rX      s    rX   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	WrongTypedTestszF
    Test L{usage.Options.parseOptions} for wrong coerce options.
    c                 C   s"   t  }d }| t|j| dS )z2
        Using a non-callable type fails.
        z--barwrong eggN)rU   r&   rR   	TypeErrorr)   r   usrH   r   r   r   test_nonCallable   s   z WrongTypedTests.test_nonCallablec                 C   s   t  }g }|| dS )zP
        The coerce functions are not called if no values are provided.
        N)rX   r)   rf   r   r   r   test_notCalledInDefault   s   z'WrongTypedTests.test_notCalledInDefaultc                 C   sR   t  }d }| tj|j|}| t|d t  }d }| t|j| dS )zI
        Errors raised by coerce functions are handled properly.
        z--foowrong blahz&Parameter type enforcement failed: Yayz--barwrong blahN)	rX   r&   rR   r   rS   r)   r/   strr[   )r   rg   rH   er   r   r   test_weirdCallable   s   z"WrongTypedTests.test_weirdCallableN)r   r    r!   r6   rh   ri   rl   r   r   r   r   rd      s
    rd   c                   @      e Zd Zdd ZdS )OutputTestsc                 C   s.   t  }| tj|jdg}| t|d dS )zF
        Error output case adjustment does not mangle options
        z-Zzoption -Z not recognizedN)r   rR   r   rS   r)   r/   rj   )r   optrk   r   r   r   test_uppercasing   s   zOutputTests.test_uppercasingN)r   r    r!   rp   r   r   r   r   rn          rn   c                   @   s   e Zd ZdgZdgZdS )InquisitionOptions)expectrk   )torture-devicetcomfy-chairzset preferred torture deviceN)r   r    r!   r#   r"   r   r   r   r   rr      s
    rr   c                   @   s   e Zd ZddgZdS )HolyQuestOptions)	horsebackhzuse a horse)	for-grailgN)r   r    r!   r#   r   r   r   r   rw      s    rw   c                   @   s*   e Zd ZdgZddedfddedfgZdS )	SubCommandOptions)europian-swallowNz$set default swallow type to EuropianinquisitioninquestzPerform an inquisition	holyquestquestzEmbark upon a holy questN)r   r    r!   r#   rr   rw   subCommandsr   r   r   r   r|      s    

r|   c                   @   sP   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S )SubCommandTestszJ
    Test L{usage.Options.parseOptions} for options with subcommands.
    c                 C   sd   t  }|ddg | |d  | |jd | |jt | |jd  | |jd d dS )z-
        A subcommand is recognized.
        --europian-swallowr~   r}   rs   rt   rv   N)	r|   r)   
assertTruer/   
subCommandrE   
subOptionsrr   assertFalser   r   r   r   r   test_simpleSubcommand      z%SubCommandTests.test_simpleSubcommandc                 C   d   t  }|g d | |d  | |jd | |jt | |jd  | |jd d dS )zA
        Flags and options of a subcommand are assigned.
        )r~   --expect--torture-device=featherr}   r~   rs   rt   featherN	r|   r)   r   r/   r   rE   r   rr   r   r   r   r   r   "test_subcommandWithFlagsAndOptions  r   z2SubCommandTests.test_subcommandWithFlagsAndOptionsc                 C   r   )zG
        Flags and options of a subcommand alias are assigned.
        )r   r   r   r}   r~   rs   rt   r   Nr   r   r   r   r   'test_subcommandAliasWithFlagsAndOptions  r   z7SubCommandTests.test_subcommandAliasWithFlagsAndOptionsc                 C   sb   t  }|ddg | |d  | |jd | |jt | |jd  | |jd  dS )zG
        Flags and options of another subcommand are assigned.
        r   z--for-grailr}   rx   rz   N)	r|   r)   r   r/   r   rE   r   rw   r   r   r   r   r   )test_anotherSubcommandWithFlagsAndOptions#  s   z9SubCommandTests.test_anotherSubcommandWithFlagsAndOptionsc                 C   s@   t  }|dg | |d  | |j | t|d dS )z
        If no subcommand is specified and no default subcommand is assigned,
        a subcommand will not be implied.
        r   r}   r   N)r|   r)   r   rF   r   r   hasattrr   r   r   r   test_noSubcommand/  s
   z!SubCommandTests.test_noSubcommandc                 C   sh   t  }d|_|dg | |d  | |jd | |jt | 	|jd  | |jd d dS )	zK
        Flags and options in the default subcommand are assigned.
        r   r   r}   r~   rs   rt   rv   N)
r|   defaultSubCommandr)   r   r/   r   rE   r   rr   r   r   r   r   r   test_defaultSubcommand:  s   z&SubCommandTests.test_defaultSubcommandc                    s`   G dd dt j G  fdddt j}| }|dg | t|jd | |jj| dS )zw
        The parseOptions method from the Options object specified for the
        given subcommand is called.
        c                   @   rm   )zDSubCommandTests.test_subCommandParseOptionsHasParent.<locals>.SubOptc                 _   s&   | j | _tjj| g|R i | d S rD   )parent	sawParentr   Optionsr)   )r   akwr   r   r   r)   N  s   zQSubCommandTests.test_subCommandParseOptionsHasParent.<locals>.SubOpt.parseOptionsN)r   r    r!   r)   r   r   r   r   SubOptM  rq   r   c                          e Zd Zdd dfgZdS )zASubCommandTests.test_subCommandParseOptionsHasParent.<locals>.OptrM   r   rP   Nr   r    r!   r   r   r   r   r   OptR      
r   rM   r   N)r   r   r)   r   r   r   r/   r   )r   r   r   r   r   r   $test_subCommandParseOptionsHasParentG  s   z4SubCommandTests.test_subCommandParseOptionsHasParentc                    s   G dd dt j G  fdddt j}G  fdddt j}| }|dg | }|dg | t|jd	 | t|jd	 | |jj| | |jj| d
S )zh
        The .parent pointer is correct even when the same Options class is
        used twice.
        c                   @   s   e Zd ZdS )z:SubCommandTests.test_subCommandInTwoPlaces.<locals>.SubOptN)r   r    r!   r   r   r   r   r   b  s    r   c                       r   )z:SubCommandTests.test_subCommandInTwoPlaces.<locals>.OptFoorM   r   quuxNr   r   r   r   r   OptFooe  r   r   c                       r   )z:SubCommandTests.test_subCommandInTwoPlaces.<locals>.OptBarrP   br   Nr   r   r   r   r   OptBarj  r   r   rM   rP   r   N)r   r   r)   r   r   r   failUnlessIdenticalr   )r   r   r   oFoooBarr   r   r   test_subCommandInTwoPlaces\  s   z*SubCommandTests.test_subCommandInTwoPlacesN)r   r    r!   r6   r   r   r   r   r   r   r   r   r   r   r   r   r      s    r   c                   @   rc   )	HelpStringTestsz&
    Test generated help strings.
    c                 C   s   d  | _t | _dS )z;
        Instantiate a well-behaved Options class.
        r%   N)r&   r'   r   r(   r   r   r   r   r*   ~  s   zHelpStringTests.setUpc              
   C   sB   z| j   W dS  ty  } z| | W Y d}~dS d}~ww )z,
        __str__ shouldn't go boom.
        N)r(   __str__	Exceptionfail)r   rk   r   r   r   test_noGoBoom  s   zHelpStringTests.test_noGoBoomc                 C   sF   dd t | j D }| t|dk | |d ddk dS )zK
        Extra whitespace in flag and parameters docs is stripped.
        c                 S   s   g | ]}| d dkr|qS )r   r   )find).0sr   r   r   
<listcomp>  s    zJHelpStringTests.test_whitespaceStripFlagsAndParameters.<locals>.<listcomp>r   flagalliciousN)rj   r(   
splitlinesr   lenr   )r   linesr   r   r   &test_whitespaceStripFlagsAndParameters  s   z6HelpStringTests.test_whitespaceStripFlagsAndParametersN)r   r    r!   r6   r*   r   r   r   r   r   r   r   y  s
    	r   c                   @   s    e Zd ZdZdd Zdd ZdS )PortCoerceTestsz3
    Test the behavior of L{usage.portCoerce}.
    c                 C   s:   |  dtd |  dtd |  dtd dS )z4
        Test the answers with valid input.
        r   0i  3210i  65535N)r/   r   
portCoercer   r   r   r   test_validCoerce  s   z PortCoerceTests.test_validCoercec                 C   sD   |  ttjd |  ttjd |  ttjd |  ttjd dS )z"
        Test error path.
         z-21212189rM   N)rR   r`   r   r   r   r   r   r   test_errorCoerce  s   z PortCoerceTests.test_errorCoerceN)r   r    r!   r6   r   r   r   r   r   r   r     s    r   c                   @   s`   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S )ZshCompleterTestsz
    Test the behavior of the various L{twisted.usage.Completer} classes
    for producing output usable by zsh tab-completion system.
    c                 C   N   t  }|dt j}| |d t jddd}|dt j}| |d dS )zX
        Completer produces zsh shell-code that produces no completion matches.
        some-optionz:some-option:some actionTdescrrepeat*:some action:N)r   	Completer
_shellCode_ZSHr/   r   cgotr   r   r   test_completer     z ZshCompleterTests.test_completerc                 C   st   t  }|dt j}| |d t d}|dt j}| |d t jdddd}|dt j}| |d d	S )
zn
        CompleteFiles produces zsh shell-code that completes file names
        according to a glob.
        r   z:some-option (*):_files -g "*"z*.pyz$:some-option (*.py):_files -g "*.py"r   Tr   z%*:some action (*.py):_files -g "*.py"N)r   CompleteFilesr   r   r/   r   r   r   r   
test_files  s   
zZshCompleterTests.test_filesc                 C   r   )zV
        CompleteDirs produces zsh shell-code that completes directory names.
        r   z:some-option:_directoriesr   Tr   z*:some action:_directoriesN)r   CompleteDirsr   r   r/   r   r   r   r   	test_dirs  r   zZshCompleterTests.test_dirsc                 C   s~   t d}|dt j}| |d t g d}|dt j}| |d t jg dddd}|dt j}| |d	 d
S )zw
        CompleteList produces zsh shell-code that completes words from a fixed
        list of possibilities.
        ABCr   z:some-option:(A B C)123z:some-option:(1 2 3)r   Tr   z*:some action:(1 2 3)N)r   CompleteListr   r   r/   r   r   r   r   	test_list  s   
zZshCompleterTests.test_listc                 C   s   t d}|dt j}| |d t g d}|dt j}| |d t jg dddd}|dt j}d	}| || d
S )z
        CompleteMultiList produces zsh shell-code that completes multiple
        comma-separated words from a fixed list of possibilities.
        r   r   z-:some-option:_values -s , 'some-option' A B Cr   z-:some-option:_values -s , 'some-option' 1 2 3r   Tr   z.*:some action:_values -s , 'some action' 1 2 3N)r   CompleteMultiListr   r   r/   )r   r   r   expectedr   r   r   test_multiList  s   
z ZshCompleterTests.test_multiListc                 C   r   )zd
        CompleteUsernames produces zsh shell-code that completes system
        usernames.
        r   z:some-option:_usersr   Tr   z*:some action:_usersN)r   CompleteUsernamesr   r   r/   r   r   outr   r   r   test_usernames     z ZshCompleterTests.test_usernamesc                 C   r   )zc
        CompleteGroups produces zsh shell-code that completes system group
        names.
        r   z:group:_groupsr   Tr   z*:some action:_groupsN)r   CompleteGroupsr   r   r/   r   r   r   r   test_groups  r   zZshCompleterTests.test_groupsc                 C   r   )zU
        CompleteHostnames produces zsh shell-code that completes hostnames.
        r   z:some-option:_hostsr   Tr   z*:some action:_hostsN)r   CompleteHostnamesr   r   r/   r   r   r   r   test_hostnames   r   z ZshCompleterTests.test_hostnamesc                 C   sV   t  }|dt j}| |d t jddd}|dt j}| |d dS )z
        CompleteUserAtHost produces zsh shell-code that completes hostnames or
        a word of the form <username>@<hostname>.
        r   z:host | user@host:r   Tr   r   N)r   CompleteUserAtHostr   r   r   
startswithr   r   r   r   test_userAtHost,  s   z!ZshCompleterTests.test_userAtHostc                 C   r   )zv
        CompleteNetInterfaces produces zsh shell-code that completes system
        network interface names.
        r   z:some-option:_net_interfacesr   Tr   z*:some action:_net_interfacesN)r   CompleteNetInterfacesr   r   r/   r   r   r   r   test_netInterfaces9  r   z$ZshCompleterTests.test_netInterfacesN)r   r    r!   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @      e Zd ZdZdd ZdS )CompleterNotImplementedTestszs
    Using an unknown shell constant with the various Completer() classes
    should raise NotImplementedError
    c              
   C   sr   t jt jt jt jt jt jt jt jt j	t j
g
}|D ]}z| }W n ty,   |d}Y nw | t|jdd qdS )zM
        Using an unknown shellType should raise NotImplementedError
        Nbad_shell_type)r   r   r   r   r   r   r   r   r   r   r   BaseExceptionrR   NotImplementedErrorr   )r   classesclsactionr   r   r   test_unknownShellM  s*   

z.CompleterNotImplementedTests.test_unknownShellN)r   r    r!   r6   r   r   r   r   r   r   G  s    r   c                   @   s>   e Zd ZdZG dd dZdd Zdd Zdd	 Zd
d ZdS )FlagFunctionTestsz*
    Tests for L{usage.flagFunction}.
    c                   @   rc   )	zFlagFunctionTests.SomeClassz>
        Dummy class for L{usage.flagFunction} tests.
        c                 C      dS )z
            A one argument method to be tested by L{usage.flagFunction}.

            @param a: a useless argument to satisfy the function's signature.
            Nr   )r   r   r   r   r   oneArgr      z"FlagFunctionTests.SomeClass.oneArgc                 C   r   )zU
            A no argument method to be tested by L{usage.flagFunction}.
            Nr   r   r   r   r   noArgy  r   z!FlagFunctionTests.SomeClass.noArgc                 C   r   )aG  
            A multiple arguments method to be tested by L{usage.flagFunction}.

            @param a: a useless argument to satisfy the function's signature.
            @param b: a useless argument to satisfy the function's signature.
            @param c: a useless argument to satisfy the function's signature.
            Nr   )r   r   r   r   r   r   r   manyArgs~  r   z$FlagFunctionTests.SomeClass.manyArgsN)r   r    r!   r6   r   r   r   r   r   r   r   	SomeClassm  s
    r   c                 C      |  dt|  j dS )zs
        L{usage.flagFunction} returns C{False} if the method checked allows
        exactly one argument.
        FN)assertIsr   flagFunctionr   r   r   r   r   r   test_hasArg     zFlagFunctionTests.test_hasArgc                 C   r   )zq
        L{usage.flagFunction} returns C{True} if the method checked allows
        exactly no argument.
        TN)r   r   r  r   r   r   r   r   r   
test_noArg  r  zFlagFunctionTests.test_noArgc                 C   s,   |  tjtj|  j}| dt| dS )z
        L{usage.flagFunction} raises L{usage.UsageError} if the method checked
        allows more than one argument.
        z$Invalid Option function for manyArgsNrR   r   rS   r  r   r   r/   rj   r   excr   r   r   test_tooManyArguments  s   z'FlagFunctionTests.test_tooManyArgumentsc                 C   s.   |  tjtj|  jd}| dt| dS )z
        L{usage.flagFunction} uses the given method name in the error message
        raised when the method allows too many arguments.
        flubudufz$Invalid Option function for flubudufNr  r  r   r   r   ,test_tooManyArgumentsAndSpecificErrorMessage  s   z>FlagFunctionTests.test_tooManyArgumentsAndSpecificErrorMessageN)	r   r    r!   r6   r   r  r  r  r
  r   r   r   r   r   h  s    
r   c                   @   r   )OptionsInternalTestsz6
    Tests internal behavior of C{usage.Options}.
    c                 C   sN   G dd dt j}| }| tg dd|jd |jd |jd d dS )zs
        Options which are synonyms to another option are aliases towards the
        longest option name.
        c                   @   s   e Zd Zdd ZeZeZdS )z;OptionsInternalTests.test_optionsAliasesOrder.<locals>.Optsc                 S   r   )z~
                This is an option method with a very long name, that is going to
                be aliased.
                Nr   r   r   r   r   opt_very_very_long  r   zNOptionsInternalTests.test_optionsAliasesOrder.<locals>.Opts.opt_very_very_longN)r   r    r!   r  	opt_shortopt_sr   r   r   r   Opts  s    r  )r   shortvery-very-longr  r   r  N)r   r   r/   dictfromkeyssynonyms)r   r  r   r   r   r   test_optionsAliasesOrder  s   
z-OptionsInternalTests.test_optionsAliasesOrderN)r   r    r!   r6   r  r   r   r   r   r    s    r  N)r6   twisted.pythonr   twisted.trialr   r   r   TestCaser$   r7   rC   rU   rX   rd   rn   rr   rw   r|   r   r   r   r   r   r   r  r   r   r   r   <module>   s,   +;%
	
$ !B