Ϫf[dZddlmZddlmZGddej ZGddejZGdd ej Z Gd d ejZ Gd d ej Z Gddej Z GddejZ GddejZGddej ZGddej ZGddej ZGddejZGddejZGddejZGd d!ejZGd"d#ejZGd$d%ejZGd&d'ejZy())zK Tests for L{twisted.python.usage}, a command line option parsing library. )usage)unittestcDeZdZgdgdgdgdgZgdddggZdZd Zy ) WellBehaved)longwdefaultzand a docstring)anothernz no docstring)longonlyNnoshort) shortlessNexceptzthis one got docstring)aflagfzO flagallicious docstringness for this here floutoc"d|jd<y)NPONY!myflagoptsselfs 9/usr/lib/python3/dist-packages/twisted/test/test_usage.py opt_myflagzWellBehaved.opt_myflag!s% (c(|d|jd<y)Nz WITH A PONY!myparamrrvalues r opt_myparamzWellBehaved.opt_myparam$s"' 6 )rN)__name__ __module__ __qualname__ optParametersoptFlagsrr"rrrr s43(%? M  # H&7rrc(eZdZdZdZdZdZdZy)ParseCorrectnessTestszZ Test L{usage.Options.parseOptions} for correct values under good conditions. cdj|_t|_|jj |jy)zL Instantiate and parseOptions a well-behaved Options class. A--long Alpha -n Beta --shortless Gamma -f --myflag --myparam TofuN)splitniceArgVrnice parseOptionsrs rsetUpzParseCorrectnessTests.setUp.s7 V %'  M  t}}-rcL|j|jjdd|j|jjdd|j|jjdd|j|jjddy ) z1 Parameters have correct values. rAlphar Betar r rGammaN assertEqualr/rrs rtest_checkParametersz*ParseCorrectnessTests.test_checkParameters;sx /9  2F;  3Y?  4g>rc|j|jjdd|j|jjddy)z, Flags have correct values. rrrNr6rs rtest_checkFlagsz%ParseCorrectnessTests.test_checkFlagsDs> 0!4 0!4rc|j|jjdd|j|jjddy)zB Custom flags and parameters have correct values. rrrzTofu WITH A PONY!Nr6rs rtest_checkCustomsz'ParseCorrectnessTests.test_checkCustomsKs? 17;  24GHrN)r#r$r%__doc__r1r8r;r=r(rrr*r*(s .?5Irr*cJeZdZddddegddddegdddd egd ddd eggZd ZeZy) TypedOptionsfoointNzFoo intfoofloatQ@z Foo floateggintzEgg int without defaulteggfloatzEgg float without defaultc||_y)zg This option has an underscore in its name to exercise the _ to - translation. N)underscoreValuer s ropt_under_scorezTypedOptions.opt_under_score[s %r)r#r$r%intfloatr&rIopt_ur(rrr@r@SsO 4i- T4e4 48#> T4!r1rVrYr]r`rdr(rrrNrNes'$ 7 B<<KrrNceZdZgdgZy)WrongTypedOptions)barwrongNNzBar with wrong coerceheN)r#r$r%r&r(rrrfrfs LMMrrfc4eZdZdZdZddddegddddeggZy)WeirdCallableOptionsctd)NOuch) RuntimeErrorr!s r_barzWeirdCallableOptions._bars 6""rctd)NYay) ValueErrorrns r_foozWeirdCallableOptions._foos rrgNzBar with strange callablefoowrongzFoo with strange callable)r#r$r%rorsr&r(rrrjrjs3#  T4!t}g}|j|y)zP The coerce functions are not called if no values are provided. N)rjr0rys rtest_notCalledInDefaultz'WrongTypedTests.test_notCalledInDefaults" # rc:t}dj}|jtj|j |}|j t|dt}dj}|jt|j |y)zI Errors raised by coerce functions are handled properly. z--foowrong blahz&Parameter type enforcement failed: Yayz--barwrong blahN) rjr-rbrrcr0r7strrm)rrzrUes rtest_weirdCallablez"WrongTypedTests.test_weirdCallablesy" # &&(   e.. F Q!IJ ! # &&( ,>rN)r#r$r%r>r{r}rr(rrrvrvs< ?rrvceZdZdZy) OutputTestsct}|jtj|jdg}|j t |dy)zF Error output case adjustment does not mangle options z-Zzoption -Z not recognizedN)rrbrrcr0r7r)roptrs rtest_uppercasingzOutputTests.test_uppercasingsBm   e..0@0@4& I Q!;? h/0 &67GrcXt}|jgd|j|d|j|jd|j |j t|j|j d|j|j ddy)zA Flags and options of a subcommand are assigned. )r--expect--torture-device=featherrrrrfeatherN rr0rr7rrRrrrrs r"test_subcommandWithFlagsAndOptionsz2SubCommandTests.test_subcommandWithFlagsAndOptions s   NO -./ }5 all,>?  X./ &67CrcXt}|jgd|j|d|j|jd|j |j t|j|j d|j|j ddy)zG Flags and options of a subcommand alias are assigned. )rrrrrrrrNrrs r'test_subcommandAliasWithFlagsAndOptionsz7SubCommandTests.test_subcommandAliasWithFlagsAndOptionss   JK -./ }5 all,>?  X./ &67CrcVt}|jddg|j|d|j|jd|j |j t|j|j d|j|j dy)zG Flags and options of another subcommand are assigned. rz --for-grailrrrN) rr0rr7rrRrrrrs r)test_anotherSubcommandWithFlagsAndOptionsz9SubCommandTests.test_anotherSubcommandWithFlagsAndOptions#s    ]34 -./ {3 all,<= k23  [12rct}|jdg|j|d|j|j|j t |dy)z If no subcommand is specified and no default subcommand is assigned, a subcommand will not be implied. rrrN)rr0rrSrrhasattrrs rtest_noSubcommandz!SubCommandTests.test_noSubcommand/sU   ,-. ,-. !,,' L12rcdt}d|_|jdg|j|d|j |j d|j |jt|j|jd|j |jddy) zK Flags and options in the default subcommand are assigned. rrrrrrrN) rdefaultSubCommandr0rr7rrRrrrrs rtest_defaultSubcommandz&SubCommandTests.test_defaultSubcommand:s  ' ,-. ,-. }5 all,>? h/0 &67Grc6GddtjGfddtj}|}|jdg|jt |j d|j |j j|y)zw The parseOptions method from the Options object specified for the given subcommand is called. ceZdZdZy)DSubCommandTests.test_subCommandParseOptionsHasParent..SubOptcl|j|_tjj|g|i|yrP)parent sawParentrOptionsr0)rakws rr0zQSubCommandTests.test_subCommandParseOptionsHasParent..SubOpt.parseOptionsNs)!% **4:!:r:rN)r#r$r%r0r(rrSubOptrMs ;rrceZdZddWdfgZy)ASubCommandTests.test_subCommandParseOptionsHasParent..Optr[rr_Nr#r$r%rrsrOptrRsVU+Krrr[rN)rrr0rrrr7r)rrrrs @r$test_subCommandParseOptionsHasParentz4SubCommandTests.test_subCommandParseOptionsHasParentGsk  ;U]] ;  %--  E w  k:; //3rc4GddtjGfddtj}Gfddtj}|}|jdg|}|jdg|jt |j d |jt |j d |j |j j||j |j j|y ) zh The .parent pointer is correct even when the same Options class is used twice. c eZdZy):SubCommandTests.test_subCommandInTwoPlaces..SubOptN)r#r$r%r(rrrrbs rrceZdZddWdfgZy):SubCommandTests.test_subCommandInTwoPlaces..OptFoor[rquuxNrrsrOptFooreVV,KrrceZdZddWdfgZy):SubCommandTests.test_subCommandInTwoPlaces..OptBarr_brNrrsrOptBarrjrrrr[r_rN)rrr0rrrfailUnlessIdenticalr)rrroFoooBarrs @rtest_subCommandInTwoPlacesz*SubCommandTests.test_subCommandInTwoPlaces\s  U]]  U]]   U]]  x 5'"x 5'" :; :;   !7!7>   !7!7>rN) r#r$r%r>rrrrrrrrr(rrrrs3 H D D 3 3 H4*?rrc"eZdZdZdZdZdZy)HelpStringTestsz& Test generated help strings. cLdj|_t|_y)z; Instantiate a well-behaved Options class. r,N)r-r.rr/rs rr1zHelpStringTests.setUp~s! V %'  M rc |jjy#t$r}|j|Yd}~yd}~wwxYw)z, __str__ shouldn't go boom. N)r/__str__ Exceptionfail)rrs r test_noGoBoomzHelpStringTests.test_noGoBooms4  II     IIaLL s A<Act|jjDcgc]}|jddk\s|}}|j t |dkD|j |djddk\ycc}w)zK Extra whitespace in flag and parameters docs is stripped. rr flagalliciousN)rr/ splitlinesfindrlen)rsliness r&test_whitespaceStripFlagsAndParametersz6HelpStringTests.test_whitespaceStripFlagsAndParametersso   N557Pq166'?a;OPP E Q' a o6!;<Qs B B N)r#r$r%r>r1rrr(rrrrys "=rrceZdZdZdZdZy)PortCoerceTestsz3 Test the behavior of L{usage.portCoerce}. c|jdtjd|jdtjd|jdtjdy)z4 Test the answers with valid input. r0i 3210i65535N)r7r portCoercers rtest_validCoercez PortCoerceTests.test_validCoercesU E,,S12 u//78  0 0 9:rc,|jttjd|jttjd|jttjd|jttjdy)z" Test error path. z-21212189r[N)rbrrrrrs rtest_errorCoercez PortCoerceTests.test_errorCoercesd *e&6&6; *e&6&6> *e&6&6A *e&6&6>rN)r#r$r%r>rrr(rrrrs;?rrcLeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z y ) ZshCompleterTestsz Test the behavior of the various L{twisted.usage.Completer} classes for producing output usable by zsh tab-completion system. c"tj}|jdtj}|j |dtjdd}|jdtj}|j |dy)zX Completer produces zsh shell-code that produces no completion matches. some-optionz :some-option: some actionTdescrrepeat*:some action:N)r Completer _shellCode_ZSHr7rcgots rtest_completerz ZshCompleterTests.test_completersi OO ll=%**5 o. OO- =ll=%**5 ./rctj}|jdtj}|j |dtjd}|jdtj}|j |dtjddd}|jdtj}|j |dy ) zn CompleteFiles produces zsh shell-code that completes file names according to a glob. rz:some-option (*):_files -g "*"z*.pyz$:some-option (*.py):_files -g "*.py"rTrz%*:some action (*.py):_files -g "*.py"N)r CompleteFilesrrr7rs r test_fileszZshCompleterTests.test_filess    !ll=%**5 >?    'll=%**5 DE   mD Ill=%**5 EFrc"tj}|jdtj}|j |dtjdd}|jdtj}|j |dy)zV CompleteDirs produces zsh shell-code that completes directory names. rz:some-option:_directoriesrTrz*:some action:_directoriesN)r CompleteDirsrrr7rs r test_dirszZshCompleterTests.test_dirssn    ll=%**5 9:   ]4 @ll=%**5 :;rctjd}|jdtj}|j |dtjgd}|jdtj}|j |dtjgddd}|jdtj}|j |d y ) zw CompleteList produces zsh shell-code that completes words from a fixed list of possibilities. ABCrz:some-option:(A B C)123z:some-option:(1 2 3)rTrz*:some action:(1 2 3)N)r CompleteListrrr7rs r test_listzZshCompleterTests.test_lists   u %ll=%**5 45    /ll=%**5 45   mD Qll=%**5 56rctjd}|jdtj}|j |dtjgd}|jdtj}|j |dtjgddd}|jdtj}d }|j ||y ) z CompleteMultiList produces zsh shell-code that completes multiple comma-separated words from a fixed list of possibilities. rrz-:some-option:_values -s , 'some-option' A B Cr z-:some-option:_values -s , 'some-option' 1 2 3rTrz.*:some action:_values -s , 'some action' 1 2 3N)rCompleteMultiListrrr7)rrrexpecteds rtest_multiListz ZshCompleterTests.test_multiLists  # #E *ll=%**5 MN  # #O 4ll=%**5 MN  # #O=QU Vll=%**5C h'rc"tj}|jdtj}|j |dtjdd}|jdtj}|j |dy)zd CompleteUsernames produces zsh shell-code that completes system usernames. rz:some-option:_usersrTrz*:some action:_usersN)rCompleteUsernamesrrr7rrouts rtest_usernamesz ZshCompleterTests.test_usernamessn  # # %ll=%**5 34  # #- Ell=%**5 45rc"tj}|jdtj}|j |dtjdd}|jdtj}|j |dy)zc CompleteGroups produces zsh shell-code that completes system group names. rz:group:_groupsrTrz*:some action:_groupsN)rCompleteGroupsrrr7rs r test_groupszZshCompleterTests.test_groupssn  "ll=%**5 ./  }T Bll=%**5 56rc"tj}|jdtj}|j |dtjdd}|jdtj}|j |dy)zU CompleteHostnames produces zsh shell-code that completes hostnames. rz:some-option:_hostsrTrz*:some action:_hostsN)rCompleteHostnamesrrr7rs rtest_hostnamesz ZshCompleterTests.test_hostnames sn  # # %ll=%**5 34  # #- Ell=%**5 45rcZtj}|jdtj}|j |j dtjdd}|jdtj}|j |j dy)z CompleteUserAtHost produces zsh shell-code that completes hostnames or a word of the form @. rz:host | user@host:rTrrN)rCompleteUserAtHostrrr startswithrs rtest_userAtHostz!ZshCompleterTests.test_userAtHost,sx  $ $ &ll=%**5 ';<=  $ $= Fll=%**5 '789rc"tj}|jdtj}|j |dtjdd}|jdtj}|j |dy)zv CompleteNetInterfaces produces zsh shell-code that completes system network interface names. rz:some-option:_net_interfacesrTrz*:some action:_net_interfacesN)rCompleteNetInterfacesrrr7rs rtest_netInterfacesz$ZshCompleterTests.test_netInterfaces9sn  ' ' )ll=%**5 <=  ' 'mD Ill=%**5 =>rN)r#r$r%r>rrrrrrrrr!r$r(rrrrs: 0G" <7"($ 6 7 6 : ?rrceZdZdZdZy)CompleterNotImplementedTestszs Using an unknown shell constant with the various Completer() classes should raise NotImplementedError c tjtjtjtjtj tj tjtjtjtjg }|D],} |}|jt|jdd.y#t$r |d}Y8wxYw)zM Using an unknown shellType should raise NotImplementedError Nbad_shell_type)rrrrr rrrrrr# BaseExceptionrbNotImplementedErrorr)rclassesclsactions rtest_unknownShellz.CompleterNotImplementedTests.test_unknownShellMs OO           # #  # #   # #  $ $  ' '   C #   #V%6%6>N   ! #T #sC  CCN)r#r$r%r>r.r(rrr&r&Gs  rr&c<eZdZdZGddZdZdZdZdZy) FlagFunctionTestsz* Tests for L{usage.flagFunction}. c"eZdZdZdZdZdZy)FlagFunctionTests.SomeClassz> Dummy class for L{usage.flagFunction} tests. cy)z A one argument method to be tested by L{usage.flagFunction}. @param a: a useless argument to satisfy the function's signature. Nr()rrs roneArgz"FlagFunctionTests.SomeClass.oneArgrrcy)zU A no argument method to be tested by L{usage.flagFunction}. Nr(rs rnoArgz!FlagFunctionTests.SomeClass.noArgyr5rcy)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()rrrrs rmanyArgsz$FlagFunctionTests.SomeClass.manyArgs~r5rN)r#r$r%r>r4r7r9r(rr SomeClassr2ms     rr:c~|jdtj|jjy)zs L{usage.flagFunction} returns C{False} if the method checked allows exactly one argument. FN)assertIsr flagFunctionr:r4rs r test_hasArgzFlagFunctionTests.test_hasArgs* eU//0@0G0GHIrc~|jdtj|jjy)zq L{usage.flagFunction} returns C{True} if the method checked allows exactly no argument. TN)r<rr=r:r7rs r test_noArgzFlagFunctionTests.test_noArgs* dE..t~~/?/E/EFGrc|jtjtj|j j }|j dt|y)z L{usage.flagFunction} raises L{usage.UsageError} if the method checked allows more than one argument. z$Invalid Option function for manyArgsNrbrrcr=r:r9r7rrexcs rtest_tooManyArgumentsz'FlagFunctionTests.test_tooManyArgumentssK    e00$..2B2K2K  ?SJrc|jtjtj|j j d}|j dt|y)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 flubudufNrBrCs r,test_tooManyArgumentsAndSpecificErrorMessagez>FlagFunctionTests.test_tooManyArgumentsAndSpecificErrorMessagesM    e00$..2B2K2KZ  ?SJrN) r#r$r%r>r:r>r@rErHr(rrr0r0hs)4JHKKrr0ceZdZdZdZy)OptionsInternalTestsz6 Tests internal behavior of C{usage.Options}. cGddtj}|}|jtj gdd|j d|j d|j ddy)zs Options which are synonyms to another option are aliases towards the longest option name. ceZdZdZeZeZy);OptionsInternalTests.test_optionsAliasesOrder..Optscy)z~ This is an option method with a very long name, that is going to be aliased. Nr(rs ropt_very_very_longzNOptionsInternalTests.test_optionsAliasesOrder..Opts.opt_very_very_longr5rN)r#r$r%rO opt_shortopt_sr(rrOptsrMs  +I&ErrR)rshortvery-very-longrTrrSN)rrr7dictfromkeyssynonyms)rrRrs rtest_optionsAliasesOrderz-OptionsInternalTests.test_optionsAliasesOrdersc  '5== 'v  MM:rXr(rrrJrJs  rrJN)r>twisted.pythonr twisted.trialrrrTestCaser*r@rNrfrjrvrrrrrrrrr&r0rJr(rrr\s\ !"7%--76(IH--(IV5==$8K""8KvN N 5== "?h''"?J=(##=u}} |?h''|?~!=h''!=H?h''?.P?))P?f8#4#4B?K))?KD 8,, r