o
    GXhi                     @   s   d 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 dejZG dd dej	Z
ddd	Zd
d ZedkrSdejv rBed dS dejv rNe   dS e  dS dS )z@
Test script for the 'cmd' module
Original by Michael Schneider
    N)supportc                   @   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 )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', '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.do_help("")
    <BLANKLINE>
    Documented commands (type help <topic>):
    ========================================
    add  help
    <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()
    Hello from preloop
    help text for add
    *** invalid number of arguments
    9
    <BLANKLINE>
    Documented commands (type help <topic>):
    ========================================
    add  help
    <BLANKLINE>
    Undocumented commands:
    ======================
    exit  shell
    <BLANKLINE>
    help text for add
    Hello from postloop
    c                 C      t d d S )NzHello from preloopprintself r	   7/opt/python-3.10.19/usr/lib/python3.10/test/test_cmd.pypreloop      zsamplecmdclass.preloopc                 C   r   )NzHello from postloopr   r   r	   r	   r
   postloop   r   zsamplecmdclass.postloopc                 G   r   )Nz"This is the completedefault methodr   )r   Zignoredr	   r	   r
   completedefault   r   zsamplecmdclass.completedefaultc                 C   r   )Nzcomplete commandr   r   r	   r	   r
   complete_command   r   zsamplecmdclass.complete_commandc                 C   s   d S Nr	   )r   sr	   r	   r
   do_shell      zsamplecmdclass.do_shellc                 C   sh   |  }t|dkrtd d S z	dd |D }W n ty'   td Y d S w t|d |d   d S )N   z*** invalid number of argumentsc                 S   s   g | ]}t |qS r	   )int).0ir	   r	   r
   
<listcomp>   s    z)samplecmdclass.do_add.<locals>.<listcomp>z*** arguments should be numbersr      )splitlenr   
ValueError)r   r   lr	   r	   r
   do_add   s   zsamplecmdclass.do_addc                 C   r   )Nzhelp text for addr   r   r	   r	   r
   help_add   s   zsamplecmdclass.help_addc                 C      dS NTr	   )r   argr	   r	   r
   do_exit   r   zsamplecmdclass.do_exitN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r#   r	   r	   r	   r
   r      s    zr   c                   @   s>   e Zd ZG dd dejZG dd deZdd Zdd Zd	S )
TestAlternateInputc                   @   s   e Zd Zdd Zdd ZdS )zTestAlternateInput.simplecmdc                 C   s   t || jd d S )Nfiler   stdoutr   argsr	   r	   r
   do_print   s   z%TestAlternateInput.simplecmd.do_printc                 C   r    r!   r	   r-   r	   r	   r
   do_EOF   r   z#TestAlternateInput.simplecmd.do_EOFN)r$   r%   r&   r/   r0   r	   r	   r	   r
   	simplecmd   s    r1   c                   @   s   e Zd Zdd ZdS )zTestAlternateInput.simplecmd2c                 C   s   t d| jd dS )Nz*** Unknown syntax: EOFr)   Tr+   r-   r	   r	   r
   r0      s   z$TestAlternateInput.simplecmd2.do_EOFN)r$   r%   r&   r0   r	   r	   r	   r
   
simplecmd2   s    r2   c                 C   sB   t d}t  }| j||d}d|_|  | | d d S )Nprint test
print test2stdinr,   Fz(Cmd) test
(Cmd) test2
(Cmd) )ioStringIOr1   use_rawinputcmdloopassertMultiLineEqualgetvaluer   inputoutputcmdr	   r	   r
   test_file_with_missing_final_nl   s   

z2TestAlternateInput.test_file_with_missing_final_nlc                 C   sx   t d}t  }| j||d}d|_|  | | d t d}t  }||_||_|  | | d d S )Nr3   r4   Fz5(Cmd) test
(Cmd) test2
(Cmd) *** Unknown syntax: EOF
zprint 

z,(Cmd) 
(Cmd) 
(Cmd) *** Unknown syntax: EOF
)	r6   r7   r2   r8   r9   r:   r;   r5   r,   r<   r	   r	   r
   test_input_reset_at_EOF   s    



z*TestAlternateInput.test_input_reset_at_EOFN)	r$   r%   r&   r?   Cmdr1   r2   r@   rA   r	   r	   r	   r
   r(      s
    	r(   c                 C   s&   ddl m} t||  tt d S )Nr   )test_cmd)testrC   r   Zrun_doctestZrun_unittestr(   )verboserC   r	   r	   r
   	test_main   s   rF   c                 C   sP   t d}|jtjtjgddd}|d | }td |j	dd| d d S )	Ntracer   r   )Z
ignoredirsrG   countz4import importlib; importlib.reload(cmd); test_main()zWriting coverage results...T)Zshow_missingZsummarycoverdir)
r   import_moduleZTracesysbase_prefixbase_exec_prefixrunresultsr   Zwrite_results)rI   rG   Ztracerrr	   r	   r
   test_coverage   s   

rQ   __main__z-cz/tmp/cmd.coverz-ir   )r'   r?   rK   Zunittestr6   rD   r   rB   r   ZTestCaser(   rF   rQ   r$   argvr9   r	   r	   r	   r
   <module>   s$     
3	


