
    ;e                     >   d Z ddlZddlZddlZddlZddlZddlmZ  G d dej                  Z	 G d dej
                  Zd Zd	 Zed
k    rNdej        v r ed           dS dej        v r e	                                             dS  ej                     dS dS )z@
Test script for the 'cmd' module
Original by Michael Schneider
    N)supportc                   N    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )samplecmdclassa  
    Instance the sampleclass:
    >>> mycmd = samplecmdclass()

    Test for the function parseline():
    >>> mycmd.parseline("")
    (None, None, '')
    >>> mycmd.parseline("?")
    ('help', '', 'help ')
    >>> mycmd.parseline("?help")
    ('help', 'help', 'help help')
    >>> mycmd.parseline("!")
    ('shell', '', 'shell ')
    >>> mycmd.parseline("!command")
    ('shell', 'command', 'shell command')
    >>> mycmd.parseline("func")
    ('func', '', 'func')
    >>> mycmd.parseline("func arg1")
    ('func', 'arg1', 'func arg1')


    Test for the function onecmd():
    >>> mycmd.onecmd("")
    >>> mycmd.onecmd("add 4 5")
    9
    >>> mycmd.onecmd("")
    9
    >>> mycmd.onecmd("test")
    *** Unknown syntax: test

    Test for the function emptyline():
    >>> mycmd.emptyline()
    *** Unknown syntax: test

    Test for the function default():
    >>> mycmd.default("default")
    *** Unknown syntax: default

    Test for the function completedefault():
    >>> mycmd.completedefault()
    This is the completedefault method
    >>> mycmd.completenames("a")
    ['add']

    Test for the function completenames():
    >>> mycmd.completenames("12")
    []
    >>> mycmd.completenames("help")
    ['help']

    Test for the function complete_help():
    >>> mycmd.complete_help("a")
    ['add']
    >>> mycmd.complete_help("he")
    ['help']
    >>> mycmd.complete_help("12")
    []
    >>> sorted(mycmd.complete_help(""))
    ['add', 'exit', 'help', 'life', 'meaning', 'shell']

    Test for the function do_help():
    >>> mycmd.do_help("testet")
    *** No help on testet
    >>> mycmd.do_help("add")
    help text for add
    >>> mycmd.onecmd("help add")
    help text for add
    >>> mycmd.onecmd("help meaning")  # doctest: +NORMALIZE_WHITESPACE
    Try and be nice to people, avoid eating fat, read a good book every
    now and then, get some walking in, and try to live together in peace
    and harmony with people of all creeds and nations.
    >>> mycmd.do_help("")
    <BLANKLINE>
    Documented commands (type help <topic>):
    ========================================
    add  help
    <BLANKLINE>
    Miscellaneous help topics:
    ==========================
    life  meaning
    <BLANKLINE>
    Undocumented commands:
    ======================
    exit  shell
    <BLANKLINE>

    Test for the function print_topics():
    >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10)
    header
    ======
    command1
    command2
    <BLANKLINE>

    Test for the function columnize():
    >>> mycmd.columnize([str(i) for i in range(20)])
    0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19
    >>> mycmd.columnize([str(i) for i in range(20)], 10)
    0  7   14
    1  8   15
    2  9   16
    3  10  17
    4  11  18
    5  12  19
    6  13

    This is an interactive test, put some commands in the cmdqueue attribute
    and let it execute
    This test includes the preloop(), postloop(), default(), emptyline(),
    parseline(), do_help() functions
    >>> mycmd.use_rawinput=0

    >>> mycmd.cmdqueue=["add", "add 4 5", "", "help", "help add", "exit"]
    >>> mycmd.cmdloop()  # doctest: +REPORT_NDIFF
    Hello from preloop
    *** invalid number of arguments
    9
    9
    <BLANKLINE>
    Documented commands (type help <topic>):
    ========================================
    add  help
    <BLANKLINE>
    Miscellaneous help topics:
    ==========================
    life  meaning
    <BLANKLINE>
    Undocumented commands:
    ======================
    exit  shell
    <BLANKLINE>
    help text for add
    Hello from postloop
    c                 $    t          d           d S )NzHello from preloopprintselfs    $/usr/lib/python3.11/test/test_cmd.pypreloopzsamplecmdclass.preloop   s    "#####    c                 $    t          d           d S )NzHello from postloopr   r	   s    r   postloopzsamplecmdclass.postloop   s    #$$$$$r   c                 $    t          d           d S )Nz"This is the completedefault methodr   )r
   ignoreds     r   completedefaultzsamplecmdclass.completedefault   s    233333r   c                 $    t          d           d S )Nzcomplete commandr   r	   s    r   complete_commandzsamplecmdclass.complete_command   s     !!!!!r   c                     d S N )r
   ss     r   do_shellzsamplecmdclass.do_shell   s    r   c                    |                                 }t          |          dk    rt          d           d S 	 d |D             }n # t          $ r t          d           Y d S w xY wt          |d         |d         z              d S )N   z*** invalid number of argumentsc                 ,    g | ]}t          |          S r   )int).0is     r   
<listcomp>z)samplecmdclass.do_add.<locals>.<listcomp>   s    ###AQ###r   z*** arguments should be numbersr      )splitlenr   
ValueError)r
   r   ls      r   do_addzsamplecmdclass.do_add   s    GGIIq66Q;;3444F	#####AA 	 	 	3444FF	 	ad1Q4is   A A$#A$c                 $    t          d           d S )Nzhelp text for addr   r	   s    r   help_addzsamplecmdclass.help_add   s    !"""r   c                 $    t          d           d S )NzTry and be nice to people, avoid eating fat, read a good book every now and then, get some walking in, and try to live together in peace and harmony with people of all creeds and nations.r   r	   s    r   help_meaningzsamplecmdclass.help_meaning   s      2 	3 	3 	3 	r   c                 $    t          d           d S )Nz&Always look on the bright side of lifer   r	   s    r   	help_lifezsamplecmdclass.help_life   s    6777r   c                     dS NTr   )r
   args     r   do_exitzsamplecmdclass.do_exit   s    tr   N)__name__
__module____qualname____doc__r   r   r   r   r   r&   r(   r*   r,   r0   r   r   r   r   r      s        E EN$ $ $% % %4 4 4" " "  
 
 
          r   r   c                   \    e Zd Z G d dej                  Z G d de          Zd Zd ZdS )TestAlternateInputc                       e Zd Zd Zd ZdS )TestAlternateInput.simplecmdc                 2    t          || j                   d S )Nfiler   stdoutr
   argss     r   do_printz%TestAlternateInput.simplecmd.do_print   s    $T[))))))r   c                     dS r.   r   r>   s     r   do_EOFz#TestAlternateInput.simplecmd.do_EOF   s    4r   N)r1   r2   r3   r@   rB   r   r   r   	simplecmdr8      s2        	* 	* 	*	 	 	 	 	r   rC   c                       e Zd Zd ZdS )TestAlternateInput.simplecmd2c                 2    t          d| j                   dS )Nz*** Unknown syntax: EOFr:   Tr<   r>   s     r   rB   z$TestAlternateInput.simplecmd2.do_EOF   s    +$+>>>>4r   N)r1   r2   r3   rB   r   r   r   
simplecmd2rE      s#        	 	 	 	 	r   rG   c                     t          j        d          }t          j                    }|                     ||          }d|_         |j                     |                     |                                d           d S )Nprint test
print test2stdinr=   Fz(Cmd) test
(Cmd) test2
(Cmd) )ioStringIOrC   use_rawinputcmdloopassertMultiLineEqualgetvaluer
   inputoutputcmds       r   test_file_with_missing_final_nlz2TestAlternateInput.test_file_with_missing_final_nl   sz    566nn5n88 !!&//"3"3	 	 	 	 	r   c                    t          j        d          }t          j                    }|                     ||          }d|_         |j                     |                     |                                d           t          j        d          }t          j                    }||_        ||_         |j                     |                     |                                d           d S )NrI   rJ   Fz5(Cmd) test
(Cmd) test2
(Cmd) *** Unknown syntax: EOF
zprint 

z,(Cmd) 
(Cmd) 
(Cmd) *** Unknown syntax: EOF
)	rL   rM   rG   rN   rO   rP   rQ   rK   r=   rR   s       r   test_input_reset_at_EOFz*TestAlternateInput.test_input_reset_at_EOF   s    566ooE&o99 !!&//"3"3/	1 	1 	1 L))	
!!&//"3"3/	1 	1 	1 	1 	1r   N)	r1   r2   r3   rU   CmdrC   rG   rV   rX   r   r   r   r6   r6      s            CG       Y   	 	 	1 1 1 1 1r   r6   c                 R    |                     t          j                               |S r   )addTestdoctestDocTestSuite)loadertestspatterns      r   
load_testsra      s"    	MM'&(()))Lr   c                 *   t          j        d          }|                    t          j        t          j        gdd          }|                    d           |                                }t          d           |	                    dd|            d S )	Ntracer   r!   )
ignoredirsrc   countz4import importlib; importlib.reload(cmd); test_main()zWriting coverage results...T)show_missingsummarycoverdir)
r   import_moduleTracesysbase_prefixbase_exec_prefixrunresultsr   write_results)rh   rc   tracerrs       r   test_coveragers      s    !'**E;;3?C4H"Jq  * *F
JJEFFFnnA	
'(((OOthOGGGGGr   __main__z-cz/tmp/cmd.coverz-i)r4   rU   rk   r\   unittestrL   testr   rY   r   TestCaser6   ra   rs   r1   argvrO   mainr   r   r   <module>rz      sK    


 



   				      s s s s sSW s s sl01 01 01 01 01* 01 01 01f  H H H zsx&'''''			  """"" r   